describe("Kahlan", function () { /** * Save current & reinitialize the Interceptor class. */ before(function () { $this->previous = Interceptor::instance(); Interceptor::unpatch(); }); /** * Restore Interceptor class. */ after(function () { Interceptor::load($this->previous); }); beforeEach(function () { $this->specs = new Kahlan(['autoloader' => Interceptor::composer()[0], 'suite' => new Suite(['matcher' => new Matcher()])]); }); describe("->loadConfig()", function () { it("sets passed arguments to specs", function () { $args = ['--src=src', '--spec=spec/Fixture/Kahlan/Spec', '--pattern=*MySpec.php', '--reporter=verbose', '--coverage=3', '--config=spec/Fixture/Kahlan/kahlan-config.php', '--ff=5', '--cc', '--no-colors', '--no-header', '--include=*', '--exclude=Kahlan\\', '--persistent=false', '--autoclear=Kahlan\\Plugin\\Monkey', '--autoclear=Kahlan\\Plugin\\Call', '--autoclear=Kahlan\\Plugin\\Stub', '--autoclear=Kahlan\\Plugin\\Quit']; $this->specs->loadConfig($args); expect($this->specs->args()->get())->toBe(['src' => ['src'], 'spec' => ['spec/Fixture/Kahlan/Spec'], 'reporter' => ["verbose"], 'pattern' => "*MySpec.php", 'config' => "spec/Fixture/Kahlan/kahlan-config.php", 'ff' => 5, 'cc' => true, 'no-colors' => true, 'no-header' => true, 'include' => ['*'], 'exclude' => ['Kahlan\\'], 'persistent' => false, 'autoclear' => ['Kahlan\\Plugin\\Monkey', 'Kahlan\\Plugin\\Call', 'Kahlan\\Plugin\\Stub', 'Kahlan\\Plugin\\Quit'], 'coverage' => '3']); }); it("loads the config file", function () { $this->specs->loadConfig(['--spec=spec/Fixture/Kahlan/Spec/PassTest.php', '--config=spec/Fixture/Kahlan/kahlan-config.php', '--pattern=*Test.php', '--reporter=none']); $this->specs->run(); expect($this->specs->suite()->loaded)->toBe(true); Interceptor::unpatch(); }); it("echoes version if --version if provided", function () { $version = Kahlan::VERSION;
$interceptor = Interceptor::patch(['cachePath' => $this->cachePath]); $new = new Interceptor(['originalLoader' => $interceptor->originalLoader(), 'cachePath' => $this->cachePath]); Interceptor::load($new); expect(Interceptor::instance())->toBe($new); expect(Interceptor::instance())->not->toBe($interceptor); }); }); describe("::instance()", function () { it("returns the interceptor autoloader", function () { $interceptor = Interceptor::patch(['cachePath' => $this->cachePath]); expect($interceptor)->toBeAnInstanceOf("Kahlan\\Jit\\Interceptor"); }); }); describe("::composer()", function () { it("returns the composer autoloader", function () { $composer = Interceptor::composer()[0]; expect($composer)->toBeAnInstanceOf("Composer\\Autoload\\ClassLoader"); }); }); describe("->__construct()", function () { it("clear caches if `'clearCache'` is `true`", function () { touch($this->cachePath . DS . 'CachedFile.php'); $this->interceptor = Interceptor::patch(['cachePath' => $this->cachePath, 'clearCache' => true]); expect(file_exists($this->cachePath . DS . 'CachedFile.php'))->toBe(false); }); it("initializes watched files if passed to the constructor", function () { $this->temp = Dir::tempnam(null, 'cache'); touch($this->temp . DS . 'watched1.php'); touch($this->temp . DS . 'watched2.php'); $watched = [$this->temp . DS . 'watched1.php', $this->temp . DS . 'watched2.php']; $this->interceptor = Interceptor::patch(['cachePath' => $this->cachePath, 'watch' => $watched]);
Filters::apply(Parrot2::class, 'tell', function ($next, $message) { return $next("HeHe! {$message}"); }); $parrot = new Parrot2(); expect($parrot->tell('Hello'))->toBe('HeHe! Hello'); $patchers = $interceptor->patchers(); expect($patchers->exists('filter'))->toBe(true); Filters::unpatch(); expect($patchers->exists('filter'))->toBe(false); Interceptor::load($previous); }); it("enables/disables the filter JIT patching by using composer compatible autoloader", function () { $previous = Interceptor::instance(); Interceptor::unpatch(); expect(class_exists(Parrot3::class, false))->toBe(false); $interceptor = Filters::patch([Parrot3::class], ['loader' => Interceptor::composer()]); Filters::apply(Parrot3::class, 'tell', function ($next, $message) { return $next("HeHe! {$message}"); }); $parrot = new Parrot3(); expect($parrot->tell('Hello'))->toBe('HeHe! Hello'); $patchers = $interceptor->patchers(); expect($patchers->exists('filter'))->toBe(true); Filters::unpatch(); expect($patchers->exists('filter'))->toBe(false); Interceptor::load($previous); }); it("throws an exception when no autoloader are available", function () { $loaders = spl_autoload_functions(); foreach ($loaders as $loader) { spl_autoload_unregister($loader);