A
A
Artem2017-03-27 14:21:15
PHP
Artem, 2017-03-27 14:21:15

How to test a class that uses a Silex object?

Hello.
Faced the problem of writing unit tests for classes that use the Silex framework object. I can't figure out how to write a mock for it.
I have a class:

class ProjectHelper
{
    private $app;

    function __construct ( Application $app )
    {
        $this -> app = $app;
    }

    public function GetProjectList ()
    {
        ...
        $row = $this -> app['db'] -> fetchAll ( $query );
        ...
    }
}

I want to test the GetProjectList method, so I need to put a mock on app['db'] -> fetchAll(), but how exactly do I do it?
This one doesn't want to work:
$appMock = $this -> getMockBuilder ( 'Silex\Application' )
            -> disableOriginalConstructor ()
            -> getMock ();

        $appMock -> expects ( $this -> once () )
            -> method ( 'fetchAll' )
            -> will ( $this -> returnValue ( 1 ) );

PHPUnit throws an error:
Trying to configure method "fetchAll" which cannot be configured because it does not exist, has not been specified, is final, or is static

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question