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 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 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 --'); }
public function before() { $this->calendar = Mockster::of(Calendar::class); $this->store = new EventStore(); $this->uid = new FakeUidGenerator(); $this->baseUrl = Url::fromString('http://example.com/ucdi'); $this->givenNowIs('now'); Mockster::stub($this->calendar->insertEvent(Arg::any(), Arg::any(), Arg::any(), Arg::any(), Arg::any()))->will()->call(function ($args) { return 'Event-' . $args['summary']; }); }
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(), ''); }
public function thenBrick_ShouldBeCancelled($description) { $this->assert->contains($this->events, new BrickCancelled("Brick-{$description}")); Mockster::stub($this->calendar->deleteEvent('myCalendarId', "Event-{$description}"))->shouldHave()->beenCalled(); }
function collectsHeadElementsFromCellRenderers() { $renderer = Mockster::of(WebRenderer::class); Mockster::stub($renderer->handles(Argument::any()))->will()->return_(true); Mockster::stub($renderer->headElements(Argument::any()))->will()->return_([new Element('foo'), new Element('bar')]); $this->renderers->add(Mockster::mock($renderer)); $elements = $this->tableRenderer->headElements(new ArrayTable([['uno' => 'one']])); $this->assert->size($elements, 2); $this->assert((string) $elements[0], '<foo></foo>'); $this->assert((string) $elements[1], '<bar></bar>'); }
public function thenTheCalendarEventOfBrick_ShoudBeDeleted($description) { Mockster::stub($this->calendar->deleteEvent('myCalendarId', "Event-{$description}"))->shouldHave()->beenCalled(); }
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'); } }
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); }
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 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_HasTheParameter_OfATypeMatching($id, $name, $matching, $required = false) { $type = Mockster::of(Type::class); Mockster::stub($type->is($matching))->will()->return_(true); $this->params[$id][] = new Parameter($name, Mockster::mock($type), $required); }
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); }
private function whenICreateAnObjectActionFrom($className) { $this->action = Mockster::of(ObjectAction::class); $parser = Mockster::of(CommentParser::class); Mockster::stub($parser->parse(Argument::any()))->will()->forwardTo(function ($comment) { return $comment . ' (parsed)'; }); $this->uut = Mockster::uut($this->action, [$className, new TypeFactory(), Mockster::mock($parser)]); }
function testSuccessfullyAssertThrownException() { Mockster::stub($this->foo->foo())->will()->throw_(new \InvalidArgumentException('Oh oh')); try { $this->mock->foo(); } catch (\Exception $ignored) { } $this->try->tryTo(function () { Mockster::stub($this->foo->foo())->shouldHave()->inCall(0)->thrown(new \InvalidArgumentException('Oh oh')); }); $this->try->thenNoExceptionShouldBeThrown(); }
public function thenAnAppointment_For_Starting_Ending_WithTheDescription_ShouldBeInsertedInMyCalendar($summary, $brickId, $start, $end, $description) { Mockster::stub($this->calendar->insertEvent('myCalendarId', $summary, new \DateTimeImmutable($start), new \DateTimeImmutable($end), $description))->shouldHave()->beenCalled(); $this->assert->contains($this->events, new CalendarEventInserted($brickId, 'myCalendarId', 'Event-' . $summary)); }
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); }
public function given_IsModifying($id) { Mockster::stub($this->actions[$id]->isModifying())->will()->return_(true); }
private function givenThereAreNoSavedCrumbs() { Mockster::stub($this->cookies->hasKey(ExecuteResource::BREADCRUMB_COOKIE))->will()->return_(false); }
private function givenAFieldHandling_InflatingWith($type, $callback) { $field = Mockster::of(Field::class); $this->fields[] = $field; Mockster::stub($field->handles(Argument::any()))->will()->forwardTo(function (Parameter $p) use($type) { $pType = $p->getType(); return $type == null || $pType instanceof UnknownType && $pType->getHint() == $type; }); Mockster::stub($field->inflate(Argument::any(), Argument::any()))->will()->forwardTo($callback); }
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 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); }
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 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')"); }
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())]); }
function testKeepVariadicMethod() { if (!method_exists(\ReflectionMethod::class, 'isVariadic')) { $this->markIncomplete('Variadic methods requires PHP 5.6'); } eval(' class CreateMocksTest_VariadicMethod { public function variadic(...$a) { return $a; } }'); /** @var Mockster|object $methods */ $methods = new Mockster('CreateMocksTest_VariadicMethod'); /** @var object $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"'); }