expect($foo->message())->toBe('Good Evening World!'); $foo2 = new Foo(); expect($foo2->message())->toBe('Good Bye World!'); $foo3 = new Foo(); expect($foo3->bar())->toBe('Hello Bar!'); }); it("stubs methods using closure", function () { Stub::on('Kahlan\\Spec\\Fixture\\Plugin\\Pointcut\\Foo')->methods(['message' => function () { return 'Good Evening World!'; }, 'bar' => function () { return 'Hello Bar!'; }]); $foo = new Foo(); expect($foo->message())->toBe('Good Evening World!'); $foo2 = new Foo(); expect($foo2->bar())->toBe('Hello Bar!'); }); it("throw an exception with invalid definition", function () { $closure = function () { $foo = new Foo(); Stub::on('Kahlan\\Spec\\Fixture\\Plugin\\Pointcut\\Foo')->methods(['bar' => 'Hello Bar!']); }; $message = "Stubbed method definition for `bar` must be a closure or an array of returned value(s)."; expect($closure)->toThrow(new InvalidArgumentException($message)); }); }); }); context("with a trait", function () { it("stubs a method", function () { Stub::on('Kahlan\\Spec\\Fixture\\Plugin\\Pointcut\\SubBar')->method('traitMethod')->andReturn('trait method stubbed !'); $subBar = new SubBar();
$interceptor = Interceptor::patch(compact('include', 'cachePath')); $interceptor->patchers()->add('pointcut', new PointcutPatcher()); $interceptor->patchers()->add('monkey', new MonkeyPatcher()); }); /** * Restore Interceptor class. */ afterAll(function () { Interceptor::load($this->previous); }); it("monkey patches a class", function () { $bar = Double::instance(); allow($bar)->toReceive('send')->andReturn('EOF'); allow('Kahlan\\Spec\\Fixture\\Plugin\\Pointcut\\Bar')->toBe($bar); $foo = new Foo(); expect($foo->bar())->toBe('EOF'); }); it("monkey patches a function", function () { $mon = new Mon(); allow('time')->toBe(function () { return 123; }); expect($mon->time())->toBe(123); }); it("throws an exception when trying to monkey patch an instance", function () { expect(function () { $foo = new Foo(); allow($foo)->toBe(Double::instance()); })->toThrow(new Exception("Error `toBe()` need to be applied on a fully-namespaced class or function name.")); }); it("throws an exception when trying to monkey patch an instance using a generic stub", function () {