}); it("throws an exception when using an undefined matcher name for a specific class", function () { $closure = function () { Matcher::get('toHelloWorld', 'stdClass'); }; expect($closure)->toThrow(new Exception("Unexisting matcher attached to `'toHelloWorld'` for `stdClass`.")); }); }); describe("::unregister()", function () { it("unregisters a matcher", function () { Matcher::register('toBeOrNotToBe', Double::classname(['extends' => 'Kahlan\\Matcher\\ToBe'])); expect(Matcher::exists('toBeOrNotToBe'))->toBe(true); Matcher::unregister('toBeOrNotToBe'); expect(Matcher::exists('toBeOrNotToBe'))->toBe(false); }); it("unregisters all matchers", function () { expect(Matcher::get())->toBeGreaterThan(1); Matcher::unregister(true); Matcher::register('toHaveLength', 'Kahlan\\Matcher\\ToHaveLength'); expect(Matcher::get())->toHaveLength(1); }); }); describe("::reset()", function () { it("unregisters all matchers", function () { expect(Matcher::get())->toBeGreaterThan(1); Matcher::reset(); Matcher::register('toHaveLength', 'Kahlan\\Matcher\\ToHaveLength'); expect(Matcher::get())->toHaveLength(1); }); }); });
<?php namespace Kahlan\Spec\Suite; use Exception; use RuntimeException; use stdClass; use DateTime; use Kahlan\Specification; use Kahlan\Matcher; use Kahlan\Expectation; use Kahlan\Plugin\Stub; describe("Expectation", function () { beforeEach(function () { $this->matchers = Matcher::get(); }); afterEach(function () { Matcher::reset(); foreach ($this->matchers as $name => $value) { foreach ($value as $for => $class) { Matcher::register($name, $class, $for); } } }); describe("->__call()", function () { it("throws an exception when using an undefined matcher name", function () { $closure = function () { $result = Expectation::expect(true)->toHelloWorld(true); }; expect($closure)->toThrow(new Exception("Unexisting matcher attached to `'toHelloWorld'`.")); });
/** * Returns a compatible matcher class name according to a passed actual value. * * @param string $name The name of the matcher. * @param string $actual The actual value. * @return string A matcher class name. */ public function _matcher($matcherName, $actual) { if (!Matcher::exists($matcherName, true)) { throw new Exception("Unexisting matcher attached to `'{$matcherName}'`."); } $matcher = null; foreach (Matcher::get($matcherName, true) as $target => $value) { if (!$target) { $matcher = $value; continue; } if ($actual instanceof $target) { $matcher = $value; } } if (!$matcher) { throw new Exception("Unexisting matcher attached to `'{$matcherName}'` for `{$target}`."); } return $matcher; }