$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.')); }); }); });
it("patches a core function with a closure", function () { $foo = new Foo(); Monkey::patch('time', function () { return 123; }); expect($foo->time())->toBe(123); }); it("patches a core class", function () { $foo = new Foo(); Monkey::patch('DateTime', 'kahlan\\spec\\suite\\plugin\\MyDateTime'); expect($foo->datetime()->getTimestamp())->toBe(245026800); }); it("patches a function", function () { $foo = new Foo(); Monkey::patch('kahlan\\spec\\fixture\\plugin\\monkey\\rand', 'kahlan\\spec\\suite\\plugin\\myrand'); expect($foo->rand(0, 100))->toBe(101); }); it("patches a class", function () { $foo = new Foo(); Monkey::patch('kahlan\\util\\Text', 'kahlan\\spec\\suite\\plugin\\MyString'); expect($foo->dump((object) 'hello'))->toBe('myhashvalue'); }); it("can unpatch a monkey patch", function () { $foo = new Foo(); Monkey::patch('kahlan\\spec\\fixture\\plugin\\monkey\\rand', 'kahlan\\spec\\suite\\plugin\\myrand'); expect($foo->rand(0, 100))->toBe(101); Monkey::reset('kahlan\\spec\\fixture\\plugin\\monkey\\rand'); expect($foo->rand(0, 100))->toBe(50); }); }); });