Example #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();
 }
Example #2
0
 public function printAll(Mockster $mockster)
 {
     $class = (new \ReflectionClass($mockster->mock()))->getParentClass();
     $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);
 }
Example #3
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')");
 }
Example #4
0
 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');
     }
 }
Example #5
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);
 }
Example #6
0
 function testKeepVariadicMethod()
 {
     /** @var Mockster|CreateMocksTest_Methods $methods */
     $methods = new Mockster(CreateMocksTest_Methods::class);
     /** @var CreateMocksTest_Methods $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"');
 }
Example #7
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 = new Mockster('FooClass');
        $user = new Mockster('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`.
         */
        $userMock = $user->mock();
        $foo->database->readUser(1)->will()->return_($userMock);
        /*
         * 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.
         */
        $foo->uut()->setUserName(1, 'Bart');
        /*
         * Last, assert the expected behaviour.
         *
         * There should have been one call to `User::setName()` with the argument
         * `'Bart'` and one call on `Database::update()` with the `User` mock instance.
         */
        $this->assert($user->setName('Bart')->has()->beenCalled());
        $this->assert($foo->database->update($userMock)->has()->beenCalled());
    }