Ejemplo n.º 1
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"');
 }
Ejemplo n.º 2
0
 function testCallback()
 {
     $callback = function (ExactArgument $argument) {
         return $argument->value() == 'foo';
     };
     $this->assert(Argument::callback($callback)->accepts(new ExactArgument('foo')));
     $this->assert->not(Argument::callback($callback)->accepts(new ExactArgument('bar')));
 }
Ejemplo n.º 3
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 --');
 }
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 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.º 7
0
 public function printAll(Mockster $mockster)
 {
     $classProperty = (new \ReflectionClass($mockster))->getProperty('class');
     $classProperty->setAccessible(true);
     $class = new \ReflectionClass($classProperty->getValue($mockster));
     $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);
 }
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 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.º 10
0
 /**
  * @param $method
  * @param $arguments
  * @return array|Argument[]
  */
 private function normalize($method, $arguments)
 {
     $normalized = [];
     $reflection = new \ReflectionMethod($this->class, $method);
     foreach ($reflection->getParameters() as $i => $parameter) {
         if (array_key_exists($i, $arguments)) {
             $argument = $arguments[$i];
             if ($argument instanceof Argument) {
                 $normalized[] = $argument;
             } else {
                 $normalized[] = Argument::exact($argument);
             }
         } else {
             if ($parameter->isDefaultValueAvailable()) {
                 $normalized[] = Argument::exact($parameter->getDefaultValue());
             }
         }
     }
     return $normalized;
 }
Ejemplo n.º 11
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.º 12
0
 public function given_ExecutesWith($id, $callback)
 {
     Mockster::stub($this->actions[$id]->execute(Argument::any()))->will()->forwardTo($callback);
 }
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 whenIExecuteThatActionWith($parameters)
 {
     $this->action->__call('executeWith', [Argument::any()])->will()->forwardTo(function ($object) {
         $this->instance = $object;
     });
     $this->uut->execute($parameters);
 }
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
 private function thenNoCrumbsShouldBeSaved()
 {
     /** @noinspection PhpVoidFunctionResultUsedInspection */
     $this->assert->not(Mockster::stub($this->cookies->create(Arg::any(), ExecuteResource::BREADCRUMB_COOKIE))->has()->beenCalled());
 }
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
 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')");
 }
Ejemplo n.º 19
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.º 20
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.º 21
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>');
 }
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 testCanGetMoreGeneral()
 {
     Mockster::stub($this->foo->bar('one'))->will()->return_('bar');
     Mockster::stub($this->foo->bar(Argument::any()))->will()->return_('foo');
     $this->assert($this->mock->bar('one'), 'bar');
     $this->assert($this->mock->bar('two'), 'foo');
 }