private function givenTheInvisibleActions(array $ids)
 {
     foreach ($ids as $id) {
         $this->actions[$id] = Mockster::of(Action::class);
         $this->registry->add($id, Mockster::mock($this->actions[$id]));
     }
 }
Ejemplo n.º 2
0
 protected function before()
 {
     $fields = new FieldRegistry();
     $this->inside = M::of(WebField::class);
     $fields->add(M::mock($this->inside));
     $this->field = new NullableField($fields);
     M::stub($this->inside->handles(Arg::any()))->will()->return_(true);
 }
Ejemplo n.º 3
0
 protected function before()
 {
     $fields = new FieldRegistry();
     $this->mockster = Mockster::of(WebField::class);
     Mockster::stub($this->mockster->handles(Argument::any()))->will()->return_(true);
     $this->detector = Mockster::of(MobileDetector::class);
     $fields->add(Mockster::mock($this->mockster));
     $this->field = new ArrayField($fields, Mockster::mock($this->detector));
 }
Ejemplo n.º 4
0
 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'];
     });
 }
Ejemplo n.º 5
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>');
 }
Ejemplo n.º 6
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');
 }
Ejemplo n.º 7
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(), '');
 }
Ejemplo n.º 8
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);
 }
Ejemplo n.º 9
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);
 }
Ejemplo n.º 10
0
 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);
 }
Ejemplo n.º 11
0
 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);
 }
Ejemplo n.º 12
0
 private function givenTheAction($id)
 {
     $this->actions->add($id, Mockster::mock(Mockster::of(Action::class)));
 }
Ejemplo n.º 13
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);
 }
Ejemplo n.º 14
0
 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)]);
 }
Ejemplo n.º 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>');
 }
Ejemplo n.º 16
0
 function nonStringParameter()
 {
     $action = Mockster::of(Action::class);
     $this->app->actions->add('foo', Mockster::mock($action));
     Mockster::stub($action->execute(Arg::any()))->will()->return_('foo');
     Mockster::stub($action->parameters())->will()->return_([new Parameter('date', new ClassType(\DateTime::class))]);
     $this->app->fields->add(new DateTimeField());
     $this->whenIExecute_With('foo', ['date' => 'today']);
     $this->thenTheCurrentActionShouldBe('http://example.com/base/foo?date=today');
 }
Ejemplo n.º 17
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);
 }
Ejemplo n.º 18
0
 /**
  * @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);
 }
Ejemplo n.º 19
0
 function testPrintHistoryOfMockedInterface()
 {
     /** @var Mockster $mockster */
     $mockster = Mockster::of(RecordStubUsageTest_Interface::class);
     $this->assert->contains((new HistoryPrinter())->printAll($mockster), 'History of [' . RecordStubUsageTest_Interface::class . ']');
 }
Ejemplo n.º 20
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));
 }
Ejemplo n.º 21
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());
    }
Ejemplo n.º 22
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())]);
 }
Ejemplo n.º 23
0
 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>');
 }