예제 #1
0
 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();
 }
예제 #2
0
파일: DomainDriver.php 프로젝트: rtens/ucdi
 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);
 }
예제 #3
0
 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"');
 }
예제 #4
0
 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>');
 }
예제 #5
0
 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 --');
 }
예제 #6
0
 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>');
 }
예제 #7
0
 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>');
 }
예제 #8
0
 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');
 }
예제 #9
0
 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(), '');
 }
예제 #10
0
 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);
 }
예제 #11
0
파일: Mockster.php 프로젝트: rtens/mockster
 /**
  * @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);
 }
예제 #12
0
 private function givenIRegisteredTheAction($id)
 {
     $this->registry->add($id, Mockster::mock(Action::class));
 }
예제 #13
0
 private function whenIListAllActions()
 {
     $resource = new IndexResource(new Factory(), $this->app, Mockster::mock($this->cookies));
     $resource->doGet($this->makeRequest());
 }
예제 #14
0
 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);
 }
예제 #15
0
 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>');
 }
예제 #16
0
 public function given_IsModifying($id)
 {
     Mockster::stub($this->actions[$id]->isModifying())->will()->return_(true);
 }
예제 #17
0
 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);
 }
예제 #19
0
 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);
 }
예제 #20
0
 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>');
 }
예제 #21
0
 public function thenTheCalendarEventOfBrick_ShoudBeDeleted($description)
 {
     Mockster::stub($this->calendar->deleteEvent('myCalendarId', "Event-{$description}"))->shouldHave()->beenCalled();
 }
예제 #22
0
    /**
     * 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());
    }
예제 #23
0
 private function whenIExecuteThatActionWith($parameters)
 {
     $this->action->__call('executeWith', [Argument::any()])->will()->forwardTo(function ($object) {
         $this->instance = $object;
     });
     $this->uut->execute($parameters);
 }
예제 #24
0
 private function givenTheAction($id)
 {
     $this->actions->add($id, Mockster::mock(Mockster::of(Action::class)));
 }
예제 #25
0
 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);
 }
예제 #26
0
 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);
 }
예제 #27
0
 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));
 }
예제 #28
0
 function testPrintHistoryOfMockedInterface()
 {
     /** @var Mockster $mockster */
     $mockster = Mockster::of(RecordStubUsageTest_Interface::class);
     $this->assert->contains((new HistoryPrinter())->printAll($mockster), 'History of [' . RecordStubUsageTest_Interface::class . ']');
 }
예제 #29
0
 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())]);
 }
예제 #30
0
 public function thenBrick_ShouldBeCancelled($description)
 {
     $this->assert->contains($this->events, new BrickCancelled("Brick-{$description}"));
     Mockster::stub($this->calendar->deleteEvent('myCalendarId', "Event-{$description}"))->shouldHave()->beenCalled();
 }