/** * Patches the string. * * @param string $namespace The namespace. * @param string $ref The fully namespaced class/function reference string. * @param boolean $isFunc Boolean indicating if $ref is a function reference. * @return string A fully namespaced reference. */ public static function patched($namespace, $ref, $isFunc = true, &$substitute = null) { $name = $ref; if ($namespace) { if (!$isFunc || function_exists("{$namespace}\\{$ref}")) { $name = "{$namespace}\\{$ref}"; } } $method = isset(static::$_registered[$name]) ? static::$_registered[$name] : null; $fake = $method ? $method->substitute() : null; if (!$isFunc) { if (is_object($fake)) { $substitute = $fake; } return $fake ?: $name; } if (!Suite::registered($name) && !$method) { return $name; } return function () use($name, $method) { $args = func_get_args(); if (Suite::registered($name)) { Calls::log(null, compact('name', 'args')); } if ($method && $method->matchArgs($args)) { return $method($args); } return call_user_func_array($name, $args); }; }
/** * Constructor * * @param string|object $actual A fully-namespaced class name or an object instance. * @param string $expected The expected method method name to be called. */ public function __construct($actual) { $actual = ltrim($actual, '\\'); $this->_actual = $actual; Suite::register(Suite::hash($actual)); $this->_message = new Message(['name' => $actual]); $this->_backtrace = Debugger::backtrace(); }
/** * Point cut called before method execution. * * @return boolean If `true` is returned, the normal execution of the method is aborted. */ public static function before($method, $self, &$params) { if (!Suite::registered()) { return false; } list($class, $name) = explode('::', $method); $lsb = is_object($self) ? get_class($self) : $self; if (!Suite::registered($lsb) && !Suite::registered($class)) { return false; } if ($name === '__call' || $name === '__callStatic') { $name = array_shift($params); $params = array_shift($params); } return static::_stubbedMethod($lsb, $self, $class, $name, $params); }
/** * Clears the registered references. * * @param string $reference An instance or a fully namespaced class name or `null` to clear all. */ public static function reset($reference = null) { if ($reference === null) { static::$_registered = []; Suite::reset(); return; } unset(static::$_registered[Suite::hash($reference)]); }
/** * Clears the registered references & logs. */ public static function reset() { static::$_logs = []; static::$_index = 0; Suite::reset(); }
function skipIf($condition) { $current = Specification::current() ?: Suite::current(); return $current->skipIf($condition); }
}); describe("::register()", function () { it("return `false` if the hash is not registered", function () { $instance = new stdClass(); $hash = Suite::hash($instance); expect(Suite::registered($hash))->toBe(false); }); }); describe("::reset()", function () { it("clears registered hashes", function () { $instance = new stdClass(); $hash = Suite::hash($instance); Suite::register($hash); expect(Suite::registered($hash))->toBe(true); Suite::reset(); expect(Suite::registered($hash))->toBe(false); }); }); describe("->status()", function () { it("returns `0` if a specs suite passes", function () { $describe = $this->suite->describe("", function () { $this->it("passes", function () { $this->expect(true)->toBe(true); }); }); $this->suite->run(); expect($this->suite->status())->toBe(0); }); it("returns `-1` if a specs suite fails", function () { $describe = $this->suite->describe("", function () { $this->it("fails", function () {
<?php namespace Kahlan\Spec\Suite; use stdClass; use Exception; use Kahlan\Suite; use Kahlan\Specification; use Kahlan\Matcher; use Kahlan\Plugin\Double; describe("Specification", function () { beforeAll(function () { Suite::$PHP = 5; }); afterAll(function () { Suite::$PHP = PHP_MAJOR_VERSION; }); beforeEach(function () { $this->spec = new Specification(['closure' => function () { }]); }); describe("->passed()", function () { it("returns the closure return value", function () { $this->spec = new Specification(['closure' => function () { return 'hello world'; }]); $return = null; $this->spec->passed($return); expect($return)->toBe('hello world'); }); it("fails when an expectation is not verified", function () {
/** * Retrieve the symfony application container for the current Symfony application. * @return ContainerInterface */ function container() { return Suite::current()->symfony->getContainer(); }
expect($expectation->description())->toBe('receive the expected method.'); expect($expectation->messages())->toBe(['it runs a spec']); }); it("logs the not attribute", function () { $this->spec = new Specification(['closure' => function () { $this->expect(true)->not->toBe(false); }]); expect($this->spec->passed())->toBe(true); $passes = $this->spec->summary()->logs('passed'); expect($passes)->toHaveLength(1); $pass = reset($passes); $expectation = $pass->children()[0]; expect($expectation->not())->toBe(true); }); it("logs deferred matcher backtrace", function () { $root = new Suite(); $root->backtraceFocus(['*Spec.php', '*.spec.php']); $this->spec = new Specification(['parent' => $root, 'closure' => function () { $this->expect(Double::instance())->not->toReceive('helloWorld'); }]); expect($this->spec->passed())->toBe(true); $passes = $this->spec->summary()->logs('passed'); expect($passes)->toHaveLength(1); $pass = reset($passes); $expectation = $pass->children()[0]; $file = $expectation->file(); expect($file)->toMatch('~Specification.spec.php$~'); }); it("logs the not attribute with a deferred matcher", function () { $this->spec = new Specification(['closure' => function () { $stub = Double::instance();
/** * Watch a message. * * @param string|object $actual A fully-namespaced class name or an object instance. * @param string $method The expected method method name to be called. * @param object A message instance. */ protected function _watch($message) { $reference = $message->reference(); if (!$reference) { Suite::register($message->name()); return $message; } if (is_object($reference)) { Suite::register(get_class($reference)); } Suite::register(Suite::hash($reference)); return $message; }
/** * Gets the browser session. * * @param string $session The session name. * @return Session */ function browser($session = null) { return Suite::current()->mink->getSession($session); }