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(); }
public function givenNowIs($when) { $this->now = new \DateTimeImmutable($when); $settings = new Settings(); $settings->calendarId = 'myCalendarId'; $this->service = new ApplicationService(new Application($this->uid, $settings, Mockster::mock($this->calendar), $this->baseUrl, $this->now), $this->store); }
function requireHeadElementsOfInnerField() { M::stub($this->inside->headElements(Arg::any()))->will()->return_([new Element('link', ['src' => 'foo.bar']), new Element('link', ['src' => 'bar.bas'])]); $headElements = implode('', $this->field->headElements($this->param('foo'))); $this->assert->contains($headElements, (string) HeadElements::jquery()); $this->assert->contains($headElements, 'src="foo.bar"'); $this->assert->contains($headElements, 'src="bar.bas"'); }
function printLinksAsDropDown() { $this->actions->add('foo', Mockster::mock(Action::class)); $this->links->add(new IdentifierLink(\DateTime::class, 'foo', 'id')); $rendered = $this->renderer->render(new Identifier(\DateTime::class, 'foo-id')); $this->assert->contains($rendered, "DateTime\n<span class=\"caret\"></span>"); $this->assert->contains($rendered, '<ul class="dropdown-menu">' . '<li><a class="" href="foo?id=foo-id"></a></li>' . '</ul>'); }
function renderArrayWithItems() { Mockster::stub($this->mockster->render(Argument::any(), Argument::any()))->will()->forwardTo(function (Parameter $parameter, $value) { return '-- ' . $parameter->getName() . ': ' . $value . ' --'; }); $parameter = new Parameter('foo', new ArrayType(new StringType())); $rendered = $this->field->render($parameter, ['one', 'two']); $this->assert->contains($rendered, '<div class="array-item form-group input-group" style="width: 100%">' . "\n" . '<span class="sortable-handle input-group-addon"><span class="glyphicon glyphicon-sort"></span></span>' . "\n" . '-- foo[1]: one --'); $this->assert->contains($rendered, '<div class="array-item form-group input-group" style="width: 100%">' . "\n" . '<span class="sortable-handle input-group-addon"><span class="glyphicon glyphicon-sort"></span></span>' . "\n" . '-- foo[2]: two --'); }
function printLinks() { $this->renderers->add(new ElementRenderer()); $this->renderers->add(new PrimitiveRenderer()); $this->actions->add('foo', Mockster::mock(Action::class)); $this->actions->add('bar', Mockster::mock(Action::class)); $this->links->add(new ClassLink(\StdClass::class, 'foo')); $this->links->add(new ClassLink(\StdClass::class, 'bar')); $rendered = $this->tableRenderer->render(new ObjectTable([new \StdClass()], $this->types)); $this->assert->contains($rendered, '<div class="dropdown">' . "\n" . '<button class="btn btn-xs btn-primary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . "\n" . 'stdClass' . "\n" . '<span class="caret"></span>' . "\n" . '</button>' . "\n" . '<ul class="dropdown-menu">' . "\n" . '<li><a class="" href="foo"></a></li>' . "\n" . '<li><a class="" href="bar"></a></li>' . "\n" . '</ul>' . "\n" . '</div>'); }
function collectsHeadElementsOfItems() { $itemRenderer = Mockster::of(WebRenderer::class); $this->registry->add(Mockster::mock($itemRenderer)); Mockster::stub($itemRenderer->handles(Argument::any()))->will()->return_(true); Mockster::stub($itemRenderer->headElements('foo'))->will()->return_([new Element('foo')]); Mockster::stub($itemRenderer->headElements(Argument::object(\DateTime::class)))->will()->return_([new Element('bar')]); $elements = $this->renderer->headElements(['foo', new \DateTime()]); $this->assert->size($elements, 2); $this->assert((string) $elements[0], '<foo></foo>'); $this->assert((string) $elements[1], '<bar></bar>'); }
function restrictAccess() { $this->action->givenTheAction('foo'); $this->action->givenTheAction('bar'); $access = Mockster::of(AccessControl::class); Mockster::stub($access->isVisible('bar'))->will()->return_(true); WebApplication::init(function (WebApplication $application) use($access) { $application->restrictAccess(WebAccessControl::factory(Mockster::mock($access))); }, $this->web->factory); $this->whenIListTheActions(); $this->thenThereShouldBe_Actions(1); $this->thenAction_ShouldBe(1, 'bar'); }
function executeWithObject() { $this->file->givenTheFile_Containing('folder/file.php', '<?php class BarObjectAction { /** @var string */ public $one; /** @var string */ public $two; }'); $registry = M::of(ActionRegistry::class); $this->createGenerator(M::mock($registry))->fromFolder($this->file->fullPath('folder'), function ($object) { return $object->one . ':' . $object->two; }); /** @var GenericObjectAction $action */ $action = M::stub($registry->add('barObjectAction', A::any()))->has()->inCall(0)->argument('action'); $this->assert($action->execute(['one' => 'foo', 'two' => 'bar']), 'foo:bar'); $this->assert($action->description(), ''); }
private function givenAWebFieldRenderingWith($callback) { $field = Mockster::of(WebField::class); $this->fields->add(Mockster::mock($field)); Mockster::stub($field->handles(Arg::any()))->will()->return_(true); // Mockster::stub($field->inflate(Arg::any(), Arg::any()))->will()->forwardTo(function (Parameter $p, $s) { // return $p->getName() . '_' . $s . '(inflated)'; // }); Mockster::stub($field->render(Arg::any(), Arg::any()))->will()->forwardTo($callback); }
/** * @param Mockster|string|object $mockster * @param array $constructorArguments * @return object */ public static function uut($mockster, array $constructorArguments = []) { if (!$mockster instanceof Mockster) { $mockster = Mockster::of($mockster); } return $mockster->__uut($constructorArguments); }
private function givenIRegisteredTheAction($id) { $this->registry->add($id, Mockster::mock(Action::class)); }
private function whenIListAllActions() { $resource = new IndexResource(new Factory(), $this->app, Mockster::mock($this->cookies)); $resource->doGet($this->makeRequest()); }
private function givenAWebFieldRenderingWith($callback) { $field = Mockster::of(WebField::class); $this->fields->add(Mockster::mock($field)); Mockster::stub($field->handles(Arg::any()))->will()->return_(true); Mockster::stub($field->render(Arg::any(), Arg::any()))->will()->forwardTo($callback); }
function collectHeadElementsOfProperties() { $propertyRenderer = Mockster::of(WebRenderer::class); $this->renderers->add(Mockster::mock($propertyRenderer)); Mockster::stub($propertyRenderer->handles(Argument::any()))->will()->return_(true); Mockster::stub($propertyRenderer->headElements('uno'))->will()->return_([new Element('one')]); Mockster::stub($propertyRenderer->headElements('bar'))->will()->return_([new Element('foo')]); $elements = $this->renderer->headElements(json_decode('{"one":"uno", "foo":"bar"}')); $this->assert->size($elements, 2); $this->assert((string) $elements[0], '<one></one>'); $this->assert((string) $elements[1], '<foo></foo>'); }
public function given_IsModifying($id) { Mockster::stub($this->actions[$id]->isModifying())->will()->return_(true); }
function testChangeInjectionFilters() { $factory = Mockster::createFactory(function (MockProvider $provider) { $provider->setPropertyFilter(function (\ReflectionProperty $property) { return strpos($property->getDocComment(), '@inject'); }); }); /** @var InjectMocksTest_AnnotatedInjectableClass $mock */ $mock = (new Mockster(InjectMocksTest_AnnotatedInjectableClass::class, $factory))->__uut(); $this->assert->isInstanceOf($mock->foo, InjectMocksTest_FooClass::class); $this->assert->isNull($mock->bar); }
private function given_HasTheValue($parameterName, $value) { $parameter = $this->parameter($parameterName); Mockster::stub($this->reader->has($parameter))->will()->return_(true); Mockster::stub($this->reader->read($parameter))->will()->return_($value); }
private function whenIExecute_With($id, $params) { $fields = new FieldRegistry(); foreach ($this->fields as $field) { $fields->add(Mockster::mock($field)); } $reader = new FakeParameterReader($params); $executor = new Executor($this->action->registry, $fields, $reader, $this->access); $this->result = $executor->execute($id); }
function printLinks() { $this->renderers->add(new ElementRenderer()); $this->renderers->add(new PrimitiveRenderer()); $this->actions->add('foo', Mockster::mock(Action::class)); $this->actions->add('bar', Mockster::mock(Action::class)); $this->actions->add('baz', Mockster::mock(Action::class)); $this->links->add(new GenericLink('foo', function ($item) { return $item['id'] == 'foo item'; })); $this->links->add(new GenericLink('bar', function ($item) { return $item['id'] == 'bar item'; })); $this->links->add(new GenericLink('baz', function ($item) { return $item['id'] == 'bar item'; })); $rendered = $this->tableRenderer->render(new ArrayTable([['id' => 'foo item'], ['id' => 'bar item']])); $this->assert->contains($rendered, '<div class="dropdown">' . "\n" . '<button class="btn btn-xs btn-primary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . "\n" . 'Actions' . "\n" . '<span class="caret"></span>' . "\n" . '</button>' . "\n" . '<ul class="dropdown-menu">' . '<li><a class="" href="http://example.com/base/foo"></a></li>' . '</ul>' . "\n" . '</div>' . "\n" . '</td>' . "\n" . '<td>foo item</td>'); $this->assert->contains($rendered, '<div class="dropdown">' . "\n" . '<button class="btn btn-xs btn-primary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . "\n" . 'Actions' . "\n" . '<span class="caret"></span>' . "\n" . '</button>' . "\n" . '<ul class="dropdown-menu">' . "\n" . '<li><a class="" href="http://example.com/base/bar"></a></li>' . "\n" . '<li><a class="" href="http://example.com/base/baz"></a></li>' . "\n" . '</ul>' . "\n" . '</div>' . "\n" . '</td>' . "\n" . '<td>bar item</td>'); }
public function thenTheCalendarEventOfBrick_ShoudBeDeleted($description) { Mockster::stub($this->calendar->deleteEvent('myCalendarId', "Event-{$description}"))->shouldHave()->beenCalled(); }
/** * 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 = Mockster::of('FooClass'); $user = Mockster::of('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`. */ $foo->database->readUser(1)->will()->return_(Mockster::mock($user)); /* * 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. */ Mockster::uut($foo)->setUserName(1, 'Bart'); /* * Last, assert the expected behaviour. * * There should have been a call to `User::setName()` with the argument * `'Bart'` and a call on `Database::update()`. */ $this->assert($user->setName('Bart')->has()->beenCalled()); $this->assert($foo->database->update(Argument::any())->has()->beenCalled()); }
private function whenIExecuteThatActionWith($parameters) { $this->action->__call('executeWith', [Argument::any()])->will()->forwardTo(function ($object) { $this->instance = $object; }); $this->uut->execute($parameters); }
private function givenTheAction($id) { $this->actions->add($id, Mockster::mock(Mockster::of(Action::class))); }
private function givenARendererFor_RenderingWith($value, $callback) { $renderer = Mockster::of(Renderer::class); $this->renderers->add(Mockster::mock($renderer)); Mockster::stub($renderer->handles($value))->will()->return_(true); Mockster::stub($renderer->render(Argument::any()))->will()->forwardTo($callback); }
private function givenAWebFieldRequiringTheHeadElements($elements) { $field = Mockster::of(WebField::class); $this->fields->add(Mockster::mock($field)); Mockster::stub($field->handles(Arg::any()))->will()->return_(true); Mockster::stub($field->inflate(Arg::any(), Arg::any()))->will()->forwardTo(function ($s) { return $s . '(inflated)'; }); Mockster::stub($field->headElements(Arg::any()))->will()->forwardTo($elements); }
private function createMethodAction($class, $method) { $parser = Mockster::of(CommentParser::class); Mockster::stub($parser->parse(Argument::any()))->will()->forwardTo(function ($comment) { if (!$comment) { return null; } return $comment . ' (parsed)'; }); return new MethodAction(new $class(), $method, new TypeFactory(), Mockster::mock($parser)); }
function testPrintHistoryOfMockedInterface() { /** @var Mockster $mockster */ $mockster = Mockster::of(RecordStubUsageTest_Interface::class); $this->assert->contains((new HistoryPrinter())->printAll($mockster), 'History of [' . RecordStubUsageTest_Interface::class . ']'); }
private function givenANonModifyingAction($actionId) { $this->action = Mockster::of(Action::class); $this->app->actions->add($actionId, Mockster::mock($this->action)); Mockster::stub($this->action->execute(Arg::any()))->will()->return_('Value of ' . $actionId); Mockster::stub($this->action->isModifying())->will()->return_(false); Mockster::stub($this->action->caption())->will()->return_('My Foo'); Mockster::stub($this->action->parameters())->will()->return_([new Parameter('one', new StringType())]); }
public function thenBrick_ShouldBeCancelled($description) { $this->assert->contains($this->events, new BrickCancelled("Brick-{$description}")); Mockster::stub($this->calendar->deleteEvent('myCalendarId', "Event-{$description}"))->shouldHave()->beenCalled(); }