Magento 2 Unit Tests – Unit Test with Mock Data Not Working

magento2phpunitunit tests

I'm trying to write unit tests for my code, but for some reason I can't get it to work. To simplify it, I have the following:

namespace Custom\Shipping\Model\Config\Source;

use Custom\Foundation\Model\Config\Source\AbstractSource;
use Custom\Shipping\Helper\Config;


/**
 * Class ProductTypes
 * @package Custom\Shipping\Model\Config\Source
 */
class ProductTypes extends AbstractSource
{
    /**
     * @var Config
     */
    protected $configHelper;

    /**
     * ProductTypes constructor.
     * @param Config $configHelper
     */
    public function __construct(
        Config $configHelper
    )
    {
        $this->configHelper = $configHelper;
    }

    /**
     * @return array
     */
    public function toArray()
    {
        $returnValues = [];

        $productTypes = $this->configHelper->getProductTypes();
        foreach ($productTypes as $key => $value) {
            $returnValues[$key] = $key;
        }

        return $returnValues;
    }
}

Now, I want to mock the result of $this->configHelper->getProductTypes(), so I set up my unit test like this:

namespace Custom\Shipping\Test\Unit\Model\Config\Source;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

/**
 * Class ProductTypesTest
 */
class ProductTypesTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var \Custom\Shipping\Model\Config\Source\ProductTypes
     */
    protected $sourceModel;

    /**
     * @var \Custom\Shipping\Helper\Config|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $configHelperMock;

    /**
     * Set up before every test
     */
    protected function setUp()
    {
        // Mock our config helper for this test:
        $this->configHelperMock = $this->getMockBuilder('Custom\Shipping\Helper\Config')
            ->disableOriginalConstructor()
            ->getMock();

        // Create our class to test:
        $this->sourceModel = (new ObjectManager($this))
            ->getObject(
                'Custom\Shipping\Model\Config\Source\ProductTypes',
                [
                    'Config' => $this->configHelperMock
                ]
            );
    }

    /**
     * @param $productTypes
     * @param $expectedResult
     * @dataProvider toArrayDataProvider
     */
    public function testToArray($productTypes, $expectedResult)
    {
        $this->configHelperMock
            ->method('getProductTypes')
            ->willReturn($productTypes);

        $this->sourceModel->toArray();

        // $this->assertEquals($expectedResult, $this->sourceModel->toArray());
    }

    /**
     * @return array
     */
    public function toArrayDataProvider()
    {
        return [
            [['Foo' => 2.95, 'Bar' => 4.95], ['Foo' => 'Foo', 'Bar' => 'Bar']]
        ];
    }
}

Now, as you can see in testToArray() a tell my mock object to return the data from the data provider when getProductTypes() is called.

However … the return value is always NULL.

The strange part is, that:

  • When I do $this->configHelperMock->getProductTypes() in my test, it returns the correct result.
  • When I check with get_class() in my actual class I get the mocked object.
  • When I set $this->configHelper->method('getProductTypes')->willReturn('foo') in my actual class it returns foo not NULL (as expected).
  • When I move my constructing / creating of my mock object from setUp to my actual test it still returns NULL.
  • Even if I put everything in the test and don't use setUp() it still … returns … NULL:

This also doesn't work:

public function testToArray($productTypes, $expectedResult)
{
    // Mock our config helper for this test:
    $this->configHelperMock = $this->getMockBuilder('Custom\Shipping\Helper\Config')
        ->disableOriginalConstructor()
        ->getMock();

    $this->configHelperMock
        ->method('getProductTypes')
        ->willReturn($productTypes);

    // Create our class to test:
    $this->sourceModel = (new ObjectManager($this))
        ->getObject(
            'Custom\Shipping\Model\Config\Source\ProductTypes',
            [
                'Config' => $this->configHelperMock
            ]
        );

    $this->sourceModel->toArray();

    // $this->assertEquals($expectedResult, $this->sourceModel->toArray());
}

So why can't I get it to work? I've used a similar approach on another (helper) class and that works just fine! This has been bugging me for a couple of hours now!

Any help is greatly appreciated.

EDIT: I included the source of Config. No magic getters ….

namespace Custom\Shipping\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

/**
 * Class Config
 * @package Custom\Shipping\Helper
 */
class Config extends AbstractHelper
{
    /**
     * @return array
     */
    public function getProductTypes()
    {
        $configValue = $this->scopeConfig->getValue('carriers/custom/product_types');

        $returnValue = [];

        $lines = explode("\n", $configValue);
        foreach ($lines as $line) {
            $line = trim($line);
            if (!empty($line)) {
                $arr = explode('::', $line);
                if (count($arr) === 2) {
                    $returnValue[trim($arr[0])] = (float) $arr[1];
                }
            }
        }

        return $returnValue;
    }
}

Best Answer

Given the information you posted, I suspect that the class \Custom\Shipping\Helper\Config implements (or inherits) public function __call() from a parent class and that the method getProductTypes() is not a real method but rather a "magic getter".

Magic methods have to be explicitly specified on generated test doubles, otherwise they can not be stubbed for specific return values or otherwise configured.

Try to create the stub as follows:

$this->configHelperMock = $this->getMockBuilder(\Custom\Shipping\Helper\Config::class)
    ->setMethods(['getProductTypes'])
    ->disableOriginalConstructor()
    ->getMock();

This will only stub that one method on the generated test double class.
If you want the test double to stub all methods, not just the one magic getter, while still specifying additional "magic" methods, you have to specify all methods, for example like so:

$methodsToStub = array_merge(
    get_class_methods(\Custom\Shipping\Helper\Config::class), 
    ['getProductTypes']
);
$this->configHelperMock = $this->getMockBuilder(\Custom\Shipping\Helper\Config::class)
    ->setMethods($methodsToStub)
    ->disableOriginalConstructor()
    ->getMock();

That is one of the reasons to favor real methods over magic methods.

I'm not 100% sure this is the reason why you are experiencing this issue since you didn't post the source of \Custom\Shipping\Helper\Config, but please let me know if this helps.

I can't stop myself from adding the following suggestions, too:

  • Use the PHP 5.5 ::class constant instead of string class names, that way your IDE (hopefully PHPStorm) can help you find typos.
    E.g. $this->getMockBuilder(\Custom\Shipping\Helper\Config::class)
    Whats more, you can also use class imports for readability, so in your case just $this->getMockBuilder(Config::class)

  • In Unit tests, don't use the ObjectManager at all. Just instantiate your class with new, since all dependencies are test doubles anyway.
    Less magic makes for easier debugging. Especially like in your case where there is only a single dependency.

    $this->sourceModel = new \Custom\Shipping\Model\Config\Source\ProductTypes(
        $this->configHelperMock
    );
    
  • Use more descriptive names for the test methods. It pays off a few months later when the under test has already been forgotten and suddenly the test breaks after an upgrade or similar change. E.g. testToArrayReturnsIdenticalKeysAndValues()