mercredi 22 juin 2016

Mock function call in controller with silex and phpunit

I'm building a PHP application with Silex and PHPUnit for unit tests.

In my controller I've this following code :

 public  function indexAction(Application $app) {
    return $app['twig']->render('admin/pages/index.html.twig', array(
        'pages' => $this->pageDao->getAllPage()
    ));
}

In my unit test I want to check getAllPage was called.

I've wrote following unit test :

 public function testIndexAction()
{
    $client = $this->createClient();
    $stub   = $this->getMockBuilder('Lsi\Dao\PageDao')
        ->setMethods(array('getAllPage'))
        ->setConstructorArgs(array(DatabaseFactory::getDatabaseForTesting()))
        ->getMock();

    $crawler = $client->request('GET', '/private.pages/');

    $this->assertEquals($client->getResponse()->isOk(), true);

    $stub->expects($this->once())->method('getAllPage');

}

Controller route retuns content but I've an error on method expectation :

Expectation failed for method name is equal to when invoked 1 time(s). Method was expected to be called 1 times, actually called 0 times.

Thanks for your help.

Aucun commentaire:

Enregistrer un commentaire