TheWebs
TheWebs

Reputation: 12933

Stubbing a method in php

I have an issue where I don't know how to say, when x is called, it expects y to be called at least once and I don't care what y does, so long as its called.

So if we look at this example:

public static function createAction($params) {
    $postParams = $params->request()->post();

    // Controller logic here ...

    $params->redirect('/signin');
  }

What I want to do in my test, which is below, is say - I expect redirect() with a param of '/singin' to be called 1 time.

My Test looks like:

<?php

use \GP\Models\User;
use Slim\Environment;
use \GP\Controllers\UserController;

class UserControllerTest extends PHPUnit_Framework_TestCase {

    protected $em;

    protected $app;

    public function setUp() {
        $this->em = getEntityManager(true);
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);

        $mdFactory = $this->em->getMetadataFactory();
        $tool->dropSchema($mdFactory->getallMetadata());
        $tool->createSchema($mdFactory->getallMetadata());
        parent::setUp();

        $_SESSION = array();
        $this->app = new \Slim\Slim(array('mode' => 'testing'));
    }

    public function tearDown() {
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);

        $mdFactory = $this->em->getMetadataFactory();
        $tool->dropSchema($mdFactory->getallMetadata());
        parent::tearDown();
    }

    public function testUserCreate() {
        Environment::mock(array(
            'REQUEST_METHOD' => 'POST',
            'slim.input'     => 'firstname=user&lastname=somethingelse&username=admin&[email protected]&password=1234567890&repassword=1234567890'
        ));

        UserController::createAction($this->app);

        $user = $this->em->getRepository('\GP\Models\User')
                     ->findBy(array('user_name' => 'admin'));

        $this->assertNotEmpty($user);
    }
}

And when its run I get:

$ phpunit
PHPUnit 4.6.6 by Sebastian Bergmann and contributors.

Configuration read from /var/www/html/Grab-Project/phpunit.xml

....E

Time: 13.45 seconds, Memory: 21.25Mb

There was 1 error:

1) UserControllerTest::testUserCreate
Slim\Exception\Stop: 

/var/www/html/Grab-Project/vendor/slim/slim/Slim/Slim.php:1022
/var/www/html/Grab-Project/vendor/slim/slim/Slim/Slim.php:1042
/var/www/html/Grab-Project/vendor/slim/slim/Slim/Slim.php:1105
/var/www/html/Grab-Project/app/Controllers/UserController.php:73
/var/www/html/Grab-Project/tests/app/controllers/UserControllerTest.php:40

FAILURES!
Tests: 5, Assertions: 8, Errors: 1.

This issue happens when it tries to call redirect. Its because I haven't specified in my test where slim should redirect, instead I want to stub that method and say, I expect to find this user in the database, like I am and I expect this method with this param to be called once.

I how to do this in rails but reading the documentation is confusing me ...

Upvotes: 0

Views: 108

Answers (1)

melvin
melvin

Reputation: 1125

If $params->redirect('/signin'); is the line it breaks i think you should be able to solve this with.

In the setup:

$this->app = $this->getMock(
  '\Slim\Slim', 
  array('redirect'), 
  array(array('mode' => 'testing'))
);

array(array( is on purpose as it is an array of constructor arguments and your constuctor looks like it wants an array as first param.

This you can then add to your test function.

$this->app->expects($this->once())->method('redirect');

Upvotes: 1

Related Questions