Example #1
0
 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');
 }
Example #2
0
 public function before()
 {
     $this->foo = new Mockster(CheckReturnTypeTest_FooClass::class);
     $this->mock = $this->foo->mock();
     $this->uut = $this->foo->uut();
 }
Example #3
0
 function testPassConstructorArgumentsByName()
 {
     $this->mock = $this->foo->uut(['one' => 'uno', 'two' => 'dos']);
     $this->assert($this->mock->constructorArguments, ['uno', 'dos']);
 }
Example #4
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());
    }