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?

Fabian Schmengler
66.2k25 gold badges191 silver badges422 bronze badges
asked Jun 29, 2015 at 6:03

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).

answered Dec 10, 2015 at 0:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.