expect($mon->time())->toBe(456); }); it("expects stubbed method to be stubbed as expected using closures", function () { $mon = new Mon(); allow('time')->toBe(function () { return 123; }, function () { return 456; }); expect($mon->time())->toBe(123); expect($mon->time())->toBe(456); }); it("expects stubbed method to be stubbed only when the with constraint is respected", function () { $mon = new Mon(); allow('Kahlan\\Spec\\Fixture\\Plugin\\Monkey\\rand')->toBeCalled()->with(10, 20)->andReturn(40); expect($mon->rand(0, 10))->toBe(5); expect($mon->rand(10, 20))->toBe(40); }); it('makes built-in PHP function to work', function () { allow('file_get_contents')->toBeOK(); $mon = new Mon(); expect($mon->loadFile())->toBe(null); }); it("throws an exception when trying to call `toReceive()`", function () { expect(function () { allow('time')->toReceive('something')->andReturn(123, 456); })->toThrow(new Exception("Error `toReceive()` are only available on classes/instances not functions.")); }); }); it("throws an exception when trying to call `andReturn()` right away", function () { expect(function () {
$patch->toBe(new DateTime('@123'), new DateTime('@456')); expect($mon->datetime()->getTimestamp())->toBe(123); expect($mon->datetime()->getTimestamp())->toBe(456); }); it("patches a function", function () { $mon = new Mon(); Monkey::patch('Kahlan\\Spec\\Fixture\\Plugin\\Monkey\\rand', 'Kahlan\\Spec\\Suite\\Plugin\\myrand'); expect($mon->rand(0, 100))->toBe(101); }); it("patches a class", function () { $mon = new Mon(); Monkey::patch('Kahlan\\Util\\Text', 'Kahlan\\Spec\\Mock\\Plugin\\Monkey\\MyString'); expect($mon->dump((object) 'hello'))->toBe('myhashvalue'); }); it("can unpatch a monkey patch", function () { $mon = new Mon(); Monkey::patch('Kahlan\\Spec\\Fixture\\Plugin\\Monkey\\rand', 'Kahlan\\Spec\\Suite\\Plugin\\myrand'); expect($mon->rand(0, 100))->toBe(101); Monkey::reset('Kahlan\\Spec\\Fixture\\Plugin\\Monkey\\rand'); expect($mon->rand(0, 100))->toBe(50); }); it("throws an exception with trying to patch an unsupported functions or core langage statements", function () { $closure = function () { Monkey::patch('func_get_args', function () { return []; }); }; expect($closure)->toThrow(new Exception('Monkey patching `func_get_args()` is not supported by Kahlan.')); }); }); });
$mon->time(); }); it("expects called function to be called in a defined order", function () { $mon = new Mon(); expect('time')->toBeCalled()->ordered; expect('Kahlan\\Spec\\Fixture\\Plugin\\Monkey\\rand')->toBeCalled()->with(5, 10)->ordered; expect('Kahlan\\Spec\\Fixture\\Plugin\\Monkey\\rand')->toBeCalled()->with(10, 20)->ordered; $mon->time(); $mon->rand(5, 10); $mon->rand(10, 20); }); it("expects called function called in a different order to be uncalled", function () { $mon = new Mon(); expect('time')->toBeCalled()->ordered; expect('Kahlan\\Spec\\Fixture\\Plugin\\Monkey\\rand')->not->toBeCalled()->with(5, 10)->ordered; $mon->rand(5, 10); $mon->time(); }); }); }); }); describe("->description()", function () { it("returns the description message for not received call", function () { $mon = new Mon(); $matcher = new ToBeCalled('time'); $matcher->resolve(['instance' => $matcher, 'data' => ['actual' => 'time', 'logs' => []]]); $actual = $matcher->description(); expect($actual['description'])->toBe('be called.'); expect($actual['data'])->toBe(['actual' => 'time()', 'actual called times' => 0, 'expected to be called' => 'time()']); }); it("returns the description message for not received call the specified number of times", function () {