Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
PHPUnit, dependencies and dependency injection
  • I'm playing around with PHPUnit. I've managed to get a test to run, but I'm having some trouble testing a specific method.

    I have static method \Model\Category::corporate(\Model\Client $client) which obviously takes a \Model\Client object as a parameter (is this an implicit or explicit dependency?) I want to make sure that \Model\Category::corporate() returns an array. This is my TestCase:

    <?php

    namespace Test\Model;

    /**
     * @group App
     * @group Model
     */
    class Category extends \TestCase
    {
        public function testCorporateReturnsArray()
        {
            $client = $this->getMock('\\Model\\Client');
            $result = \Model\Category::corporate($client);
            $this->assertEquals(true, is_array($result));
    }

    When I run `oil test --group=App`, I get the following output:

    Tests Running...This may take a few moments.
    PHPUnit 3.7.28-1-g598a467 by Sebastian Bergmann.

    Configuration read from /httpd/www/example.com/fuel/app/phpunit.xml

    E

    Time: 258 ms, Memory: 15.25Mb

    There was 1 error:

    1) Test\Model\Category::testCorporateReturnsArray
    Fuel\Core\PhpErrorException: Invalid argument supplied for foreach()

    /httpd/www/example.com/fuel/core/bootstrap.php:98
    /httpd/www/example.com/fuel/packages/orm/classes/model.php:784
    /httpd/www/example.com/fuel/app/tests/model/category.php:15

    FAILURES!
    Tests: 1, Assertions: 0, Errors: 1.

    I looked through the call stack and found that the root source of the error is the call to "getMock('\\Model\\Client')". It also appears that the error is being thrown within the ORM package, when attempting to loop over internal properties. Beyond that, I'm clueless.

    From what I understand, manually creating a \Model\Client object to pass to the method I am testing is a bad way of doing things. Is that correct?

    I'm *supposed* to be making implicit dependencies explicit by using dependency injection (I think).

    Should I explicitly create a \Model\Client object to pass to the method I'm testing? Or how else would I go about testing my \Model\Category::corporate() method?
  • You've hit one of the reasons for redesigning the Fuel architecture for v2. The current architecture doesn't lend itself to being tested.

    I don't know what your \Model\Category::corporate() does, but it clearly expects a fully functional ORM model, and not a mockup. getMock() is meant to create pseudo objects that have dummy methods on which you can define behaviour so you can test them.

    An ORM object is a complex object, has lots of static methods and data, and also constructs new objects internally. All a big no-no if you want proper testing.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion