public function printAll(Mockster $mockster) { $class = (new \ReflectionClass($mockster->mock()))->getParentClass(); $all = []; foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { $arguments = array_map(function () { return Argument::any(); }, $method->getParameters()); $calls = $mockster->__call($method->getName(), $arguments)->has()->calls(); if ($calls) { $all[] = $this->printCalls($method->getName(), $calls); } } return "History of [{$class->getName()}]\n " . implode("\n ", $all); }
function testProtectedMethods() { $foo = new Mockster(StubMethodsTest_FooClass::class); /** @noinspection PhpUndefinedMethodInspection */ Mockster::stub($foo->protectedMethod())->will()->return_('bar'); /** @noinspection PhpUndefinedMethodInspection */ Mockster::stub($foo->proxyMethod())->dontStub(); /** @var StubMethodsTest_FooClass $mock */ $mock = $foo->mock(); $this->assert($mock->proxyMethod(), "bar"); }
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"'); }
public function before() { $this->foo = new Mockster(RecordStubUsageTest_FooClass::class); $this->mock = $this->foo->mock(); }
public function before() { $this->foo = new Mockster(CheckReturnTypeTest_FooClass::class); $this->mock = $this->foo->mock(); $this->uut = $this->foo->uut(); }
/** * A typical test with *mockster* might look like this. */ public function testQuickStart() { /** * <a href="javascript:" onclick="$('#quickStartDefinitions').toggle();"> * Show class definitions for this example * </a><div id="quickStartDefinitions" style="display: none;"> */ eval(' class FooClass { /** @var MyDatabase <- */ protected $database; public function setUserName($id, $name) { $user = $this->database->readUser($id); $user->setName($name); $this->database->update($user); } } class MyDatabase { public function readUser($id) { // [...] } public function update($object) { // [...] } } class MyUser { public function setName($name) { // [...] } }'); // </div> /* * First create `Mockster` instances of the classes we're gonna mock. */ $foo = new Mockster('FooClass'); $user = new Mockster('MyUser'); /* * Then configure the behaviour of the dependencies of our *Unit Under Test*. * * The `Database` should return a mock of the `User` class when called with the argument `1`. */ $userMock = $user->mock(); $foo->database->readUser(1)->will()->return_($userMock); /* * Now execute the code to be tested. * * The `uut()` method will create an instance of the `FooClass` with * all it's dependencies replaced by mocks and none of it's methods stubbed. */ $foo->uut()->setUserName(1, 'Bart'); /* * Last, assert the expected behaviour. * * There should have been one call to `User::setName()` with the argument * `'Bart'` and one call on `Database::update()` with the `User` mock instance. */ $this->assert($user->setName('Bart')->has()->beenCalled()); $this->assert($foo->database->update($userMock)->has()->beenCalled()); }