function testDisableCheckingForEntireMock() { Mockster::stub($this->foo->returnsDateTime())->will()->return_(null); $this->foo->enableReturnTypeChecking(false); Mockster::stub($this->foo->returnsString())->will()->return_(null); $this->mock->returnsString(); $this->mock->returnsDateTime(); $this->pass(); }
function testDoNotStubStaticMethods() { try { $foo = new Mockster(StubMethodsTest_FooClass::class); /** @noinspection PhpUndefinedMethodInspection */ Mockster::stub($foo->staticMethod()); $this->fail("Should have thrown an exception"); } catch (\ReflectionException $e) { $this->assert->contains($e->getMessage(), 'static methods'); $this->assert->contains($e->getMessage(), 'FooClass::staticMethod'); } }
function testPropertyStubbingOverwritesArgumentsStubbing() { /** @var Mockster|InjectMocksTest_InjectableClass $injectable */ $injectable = new Mockster(InjectMocksTest_InjectableClass::class); /** @var Mockster|InjectMocksTest_FooClass $injected */ $injected = new Mockster(InjectMocksTest_FooClass::class); Mockster::stub($injected->foo())->will()->return_('argument'); Mockster::stub($injectable->bas->foo())->will()->return_('property'); /** @var InjectMocksTest_InjectableClass $mock */ $mock = $injectable->uut(['bas' => $injected->mock()]); $this->assert($mock->bas->foo(), 'property'); }
function testKeepVariadicMethod() { /** @var Mockster|CreateMocksTest_Methods $methods */ $methods = new Mockster(CreateMocksTest_Methods::class); /** @var CreateMocksTest_Methods $mock */ $mock = $methods->mock(); Mockster::stub($methods->variadic('one', 'two'))->will()->call(function ($args) { return json_encode($args); }); $this->assert->contains($mock->variadic('one', 'two'), '"0":"one","1":"two"'); }
function testPrintStubHistory() { Mockster::stub($this->foo->foo(Arg::any(), Arg::any()))->will()->return_('foo')->once()->then()->return_(['foo'])->once()->then()->return_(new \DateTime('2011-12-13 14:15:16 UTC'))->once()->then()->throw_(new \InvalidArgumentException("Oh no")); $this->mock->foo(4, 2); $this->mock->foo('One', 'Two'); $this->mock->foo('Three'); try { $this->mock->foo('Four', new RecordStubUsageTest_ToString()); } catch (\InvalidArgumentException $ignored) { } $this->assert((new HistoryPrinter())->printStub($this->foo->foo()), "No calls recorded for [" . RecordStubUsageTest_FooClass::class . "::foo()]"); $this->assert((new HistoryPrinter())->printStub($this->foo->foo(Arg::integer(), Arg::integer())), "History of [" . RecordStubUsageTest_FooClass::class . "::foo()]\n" . " foo(4, 2) -> 'foo'"); $this->assert((new HistoryPrinter())->printStub($this->foo->foo(Arg::string(), Arg::any())), "History of [" . RecordStubUsageTest_FooClass::class . "::foo()]\n" . " foo('One', 'Two') -> ['foo']\n" . " foo('Three', NULL) -> <DateTime>(2011-12-13T14:15:16+00:00)\n" . " foo('Four', <" . RecordStubUsageTest_ToString::class . ">('foo')) !! <InvalidArgumentException>('Oh no')"); }