/** * The Constructor. * * @param array $config Possible options values are: * - `'driver'` _object_: the driver instance which will log the coverage data. * - `'path'` _array_ : the path(s) which contain the code source files. * - `'base'` _string_: the base path of the repo (default: `getcwd`). * - `'prefix'` _string_: some prefix to remove to get the real file path. */ public function __construct($config = []) { $defaults = ['driver' => null, 'path' => [], 'include' => '*.php', 'exclude' => [], 'type' => 'file', 'skipDots' => true, 'leavesOnly' => false, 'followSymlinks' => true, 'recursive' => true, 'base' => str_replace(DIRECTORY_SEPARATOR, '/', getcwd())]; $config += $defaults; if (Interceptor::instance()) { $config += ['prefix' => rtrim(Interceptor::instance()->cachePath(), DS)]; } else { $config += ['prefix' => '']; } $this->_driver = $config['driver']; $this->_paths = (array) $config['path']; $this->_base = $config['base']; $this->_prefix = $config['prefix']; $files = Dir::scan($this->_paths, $config); foreach ($files as $file) { $this->_coverage[realpath($file)] = []; } }
/** * Save current & reinitialize the Interceptor class. */ before(function () { $this->previous = Interceptor::instance(); Interceptor::unpatch(); $cachePath = rtrim(sys_get_temp_dir(), DS) . DS . 'kahlan'; $include = ['kahlan\\spec\\']; $interceptor = Interceptor::patch(compact('include', 'cachePath')); $interceptor->patchers()->add('pointcut', new Pointcut()); }); /** * Restore Interceptor class. */ after(function () { Interceptor::load($this->previous); }); describe("::on()", function () { context("with an instance", function () { it("stubs a method", function () { $foo = new Foo(); Stub::on($foo)->method('message')->andReturn('Good Bye!'); expect($foo->message())->toBe('Good Bye!'); }); it("stubs only on the stubbed instance", function () { $foo = new Foo(); Stub::on($foo)->method('message')->andReturn('Good Bye!'); expect($foo->message())->toBe('Good Bye!'); $foo2 = new Foo(); expect($foo2->message())->toBe('Hello World!'); });
/** * The default `'patcher'` filter. */ protected function _patchers() { return Filter::on($this, 'patchers', [], function ($chain) { if (!($interceptor = Interceptor::instance())) { return; } $patchers = $interceptor->patchers(); $patchers->add('substitute', new DummyClass(['namespaces' => ['spec\\']])); $patchers->add('pointcut', new Pointcut()); $patchers->add('monkey', new Monkey()); $patchers->add('rebase', new Rebase()); $patchers->add('quit', new Quit()); }); }
<?php use Doctrine\Common\Annotations\AnnotationRegistry; use filter\Filter; use jit\Interceptor; Filter::register('doctrine.exclude.annotations', function ($chain) { $extra = ['Doctrine\\Common\\Annotations\\Annotation']; $exclude = $this->args()->get('exclude'); $exclude = is_array($exclude) ? $exclude + $extra : $extra; $this->args()->set('exclude', $exclude); return $chain->next(); }); Filter::register('doctrine.annotations.autoloader', function ($chain) { AnnotationRegistry::registerLoader(Interceptor::instance()->loader()); return $chain->next(); }); Filter::apply($this, 'interceptor', 'doctrine.exclude.annotations'); Filter::apply($this, 'run', 'doctrine.annotations.autoloader');
Filter::apply($this->specs, 'load', 'spec.load'); Filter::register('spec.reporters', function ($chain) use(&$order) { $order[] = 'reporters'; }); Filter::apply($this->specs, 'reporters', 'spec.reporters'); Filter::register('spec.matchers', function ($chain) use(&$order) { $order[] = 'matchers'; }); Filter::apply($this->specs, 'matchers', 'spec.matchers'); Filter::register('spec.run', function ($chain) use(&$order) { $order[] = 'run'; }); Filter::apply($this->specs, 'run', 'spec.run'); Filter::register('spec.reporting', function ($chain) use(&$order) { $order[] = 'reporting'; }); Filter::apply($this->specs, 'reporting', 'spec.reporting'); Filter::register('spec.stop', function ($chain) use(&$order) { $order[] = 'stop'; }); Filter::apply($this->specs, 'stop', 'spec.stop'); Filter::register('spec.quit', function ($chain) use(&$order) { $order[] = 'quit'; }); Filter::apply($this->specs, 'quit', 'spec.quit'); $this->specs->run(); expect($order)->toBe(['bootstrap', 'interceptor', 'namespaces', 'patchers', 'load', 'reporters', 'matchers', 'run', 'reporting', 'stop', 'quit']); Interceptor::unpatch(); }); }); });