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 () { allow('time')->andReturn(123); })->toThrow(new Exception("You must to call `toReceive()/toBeCalled()` before defining a return value.")); }); 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.')); }); }); });
$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 () { $mon = new Mon(); $matcher = new ToBeCalled('time'); $matcher->times(2); $matcher->resolve(['instance' => $matcher, 'data' => ['actual' => 'time', 'logs' => []]]); $actual = $matcher->description(); expect($actual['description'])->toBe('be called the expected times.'); expect($actual['data'])->toBe(['actual' => 'time()', 'actual called times' => 0, 'expected to be called' => 'time()', 'expected called times' => 2]); }); it("returns the description message for wrong passed arguments", function () { $mon = new Mon(); $matcher = new ToBeCalled('time'); $matcher->with('Hello World!'); $mon->time(); $matcher->resolve(['instance' => $matcher, 'data' => ['actual' => 'time', 'logs' => []]]); $actual = $matcher->description(); expect($actual['description'])->toBe('be called with expected parameters.'); expect($actual['data'])->toBe(['actual' => 'time()', 'actual called times' => 1, 'actual called parameters list' => [[]], 'expected to be called' => 'time()', 'expected parameters' => ['Hello World!']]); }); }); describe("->ordered()", function () { it("throw an exception when trying to play with core instance", function () { expect(function () { $matcher = new ToBeCalled('a'); $matcher->order; })->toThrow(new Exception("Unsupported attribute `order` only `ordered` is available."));