예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     if (is_array($this->value)) {
         $keys = array_keys($this->value);
     } else {
         $keys = array_keys(get_object_vars($this->value));
     }
     if ($this->configurationOnly) {
         essence()->setMatcherConfiguration("Essence\\Matchers\\ContainMatcher", $keys);
         return true;
     }
     list($elements) = $this->arguments;
     if (!is_array($elements)) {
         $elements = [$elements];
     }
     foreach ($elements as $key) {
         if (!in_array($key, $keys, true)) {
             $this->setMessage("key %s does not exist in %s", [$key, $this->value]);
             return false;
         }
     }
     $this->setMessage("key %s exists in %s", [$key, $this->value]);
     return true;
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     $actualLength = null;
     if (is_string($this->value)) {
         // @suggestion multibyte?
         $actualLength = strlen($this->value);
     }
     if (is_array($this->value)) {
         $actualLength = count($this->value);
     }
     if (is_object($this->value)) {
         $actualLength = count(get_object_vars($this->value));
     }
     // The configuration mode.
     if ($this->configurationOnly) {
         essence()->setMatcherConfiguration(__CLASS__, ["length" => $actualLength]);
         return true;
     }
     list($length) = $this->arguments;
     if ($length !== $actualLength) {
         $this->setMessage("%s (expected length) is not equal to %s (actual length)", [$length, $actualLength]);
         return false;
     }
     $this->setMessage("%s and %s have identical length", [$length, $actualLength]);
     return true;
 }
예제 #3
0
 /**
  * @test
  */
 public function it_works_as_expected()
 {
     $extension = \Mockery::mock(get_class($this->subject) . "[assertTrue]");
     $extension->shouldReceive("assertTrue")->twice()->with(true)->andReturn(null);
     essence()->setBuilder($this->mockAssertionBuilder());
     essence()->setBuilder($this->mockAssertionBuilder());
     $extension->tearDown();
 }
예제 #4
0
 /**
  * @test
  */
 public function it_works_as_expected()
 {
     $matcher = new ContainMatcher([1, 2, 3], [true]);
     $this->assertFalse($matcher->run());
     $this->assertNotNull($matcher->getMessage());
     $this->assertTrue((new ContainMatcher("foobar", ["foo"]))->run());
     essence()->setMatcherConfiguration($this->subject, ["foo"]);
     $this->assertTrue((new ContainMatcher([], ["foo"]))->run());
 }
예제 #5
0
 /**
  * @test
  */
 public function it_works_as_expected()
 {
     $matcher = new KeysMatcher(["foo" => 123, "bar" => 321], [["foo", "baz"]]);
     $this->assertFalse($matcher->run());
     $this->assertNotNull($matcher->getMessage());
     $this->assertTrue((new KeysMatcher((object) ["foo" => "bar"], ["foo"]))->run());
     (new KeysMatcher(["foo" => 123], [], true))->run();
     $this->assertEquals(essence()->getConfiguration("matcher_settings"), ["Essence\\Matchers\\ContainMatcher" => ["foo"]]);
 }
예제 #6
0
 /**
  * @test
  */
 public function it_works_as_expected()
 {
     $matcher = new AboveMatcher(18, [20]);
     $this->assertFalse($matcher->run());
     $this->assertNotNull($matcher->getMessage());
     $this->assertEquals($matcher->getValue(), 18);
     $this->assertEquals($matcher->getArguments(), [20]);
     essence()->setMatcherConfiguration("Essence\\Matchers\\LengthMatcher", ["length" => 15]);
     $this->assertTrue((new AboveMatcher(10, [13]))->run());
 }
예제 #7
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     if (count($this->arguments) > 0) {
         $this->number = (int) end($this->arguments);
     } else {
         $this->incorrectUsage("Desired condition was not specified.");
     }
     if ($config = essence()->getMatcherConfiguration("Essence\\Matchers\\LengthMatcher")) {
         $this->value = $config["length"];
     }
 }
예제 #8
0
 /**
  * @return void
  */
 public function tearDown()
 {
     $assertions = essence()->validateAll();
     for ($index = 0; $index < $assertions; $index += 1) {
         // The fastest one (I believe).
         $this->assertTrue(true);
     }
     // If Mockery is utilised.
     if (class_exists("Mockery")) {
         \Mockery::close();
     }
 }
예제 #9
0
 /**
  * @test
  */
 public function it_works_as_expected()
 {
     // Strings.
     $matcher = new LengthMatcher("foobar", [5]);
     $this->assertFalse($matcher->run());
     $this->assertNotNull($matcher->getMessage());
     // Arrays.
     $this->assertFalse((new LengthMatcher([1, 2, 3], [10]))->run());
     // Objects (keys).
     $object = (object) ["foo" => 123, "bar" => 321];
     $this->assertTrue((new LengthMatcher($object, [2]))->run());
     // Run in configuration mode.
     (new LengthMatcher([1, 2, 3], [], true))->run();
     $this->assertInternalType("array", $config = essence()->getMatcherConfiguration("Essence\\Matchers\\LengthMatcher"));
     $this->assertEquals(3, $config["length"]);
 }
예제 #10
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     $matcher = get_class($this);
     if ($this->configurationOnly and !in_array("configuration", $this->modes)) {
         $this->incorrectUsage("You can't run {$matcher} in the configuration mode.");
         // @codeCoverageIgnoreStart
     }
     // @codeCoverageIgnoreEnd
     if (!in_array(gettype($this->value), $this->valueType)) {
         $this->incorrectUsage("You are trying to run {$matcher} with invalid data.");
         // @codeCoverageIgnoreStart
     }
     // @codeCoverageIgnoreEnd
     if (!is_null($configuration = essence()->getMatcherConfiguration($matcher))) {
         $this->value = $configuration;
     }
 }
예제 #11
0
 /**
  * @test
  */
 public function it_provides_wrappers_for_better_readability()
 {
     $this->assertInstanceOf("Essence\\Essence", expect(123));
     $this->assertSame(expect($essence = essence(123)), $essence);
 }
예제 #12
0
 /**
  * Just a wrapper function for better readability.
  */
 function expect($value)
 {
     return $value instanceof Essence\Essence ? $value : essence($value);
 }