/** * {@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; }
/** * {@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; }
/** * @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(); }
/** * @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()); }
/** * @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"]]); }
/** * @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()); }
/** * {@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"]; } }
/** * @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(); } }
/** * @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"]); }
/** * {@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; } }
/** * @test */ public function it_provides_wrappers_for_better_readability() { $this->assertInstanceOf("Essence\\Essence", expect(123)); $this->assertSame(expect($essence = essence(123)), $essence); }
/** * Just a wrapper function for better readability. */ function expect($value) { return $value instanceof Essence\Essence ? $value : essence($value); }