3

When I was writing unit tests for one magento collection I faced with the following strange thing:

Magento allows create mock for collection with it's internal phpunit method:

\Magento\Framework\TestFramework\Unit\Helper\ObjectManager

public function getCollectionMock($className, array $data)
{
    if (!is_subclass_of($className, '\Magento\Framework\Data\Collection')) {
        throw new \InvalidArgumentException(
            $className . ' does not instance of \Magento\Framework\Data\Collection'
        );
    }
    $mock = $this->_testObject->getMock($className, [], [], '', false, false);
    $iterator = new \ArrayIterator($data);
    $mock->expects(
        $this->_testObject->any()
    )->method(
        'getIterator'
    )->will(
        $this->_testObject->returnValue($iterator)
    );
    return $mock;
}

The problem appear when I'm trying to create Mock of \Magento\Framework\Data\Collection.

As you can see magento checks if described class is subclass of \Magento\Framework\Data\Collection. Not instance of!

Magento returns the described collection here:

$product->getMediaGalleryImages();

The question is: is it a bug or a feature?

1 Answer 1

2

You can always submit a pull request to fix the grammar here if nothing else! $className . ' does not instance of \Magento\Framework\Data\Collection'.

I cannot think of any reason why it is seeing if subclass (child) vs instance of (current class or child). I mean you cannot use instanceof because it requires an instance of the class (is_subclass_of() above is working on the class name in a string, which instanceof does not support).

Most likely when written they did not have a need for class or descendant. You could try for a pull request of checking for is_subclass_of() or equality (same class name).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.