$actual = Interceptor::instance(); expect($success)->toBe(true); expect($actual)->toBe(null); }); it("returns `false` if there's no patched autoloader", function () { Interceptor::patch(['cachePath' => $this->cachePath]); Interceptor::unpatch(); $success = Interceptor::unpatch(); expect($success)->toBe(false); }); }); describe("::load()", function () { it("auto unpatch when loading an interceptor autoloader", function () { $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"); });
/** * Save current & reinitialize the Interceptor class. */ beforeAll(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('quit', new QuitPatcher()); }); /** * Restore Interceptor class. */ afterAll(function () { Interceptor::load($this->previous); }); describe("::enable()", function () { it("enables quit statements", function () { Quit::disable(); expect(Quit::enabled())->toBe(false); Quit::enable(); expect(Quit::enabled())->toBe(true); }); }); describe("::disable()", function () { it("disables quit statements", function () { Quit::enable(); expect(Quit::enabled())->toBe(true); Quit::disable(); expect(Quit::enabled())->toBe(false);
}); 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); } try { Filters::patch(); $success = true; } catch (Exception $e) { $success = $e->getMessage(); } foreach ($loaders as $loader) { spl_autoload_register($loader); }