Edit in GitHubLog an issue

Object manager helper

Block and model class constructors declare many dependencies. Adobe Commerce and Magento Open Source use constructor dependency injection. To unit test such classes, you must manually create mocks for all constructor parameters before you can instantiate the class objects. If the number of dependencies is ten or greater, this task is time-consuming.

Use the \Magento\Framework\TestFramework\Unit\Helper\ObjectManager helper class to simplify this task. Its methods automatically create mocks for all required dependencies. You can then instantiate a testing object by passing these mocks to a class constructor. You can still create your custom mocks, if needed.

Methods#

The ObjectManager public interface methods are:

getObject#

Creates mocks for all constructor dependencies and applies any specified custom mocks from $arguments array. Also, instantiates the required $className by using constructor with already existing mocks.

Syntax:

Copied to your clipboard
1public function getObject($className,
2 array $arguments = []);

Example:

Copied to your clipboard
1$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
2
3// default constructor arguments
4$scopePool = $objectManagerHelper->getObject('\Magento\App\Config\ScopePool');
5
6// custom constructor arguments
7$cacheMock = $this->getMock('\Magento\Cache\FrontendInterface');
8...
9$arguments = ['cache' => $cacheMock];
10$scopePool = $objectManagerHelper->getObject('\Magento\App\Config\ScopePool',
11 $arguments);

getCollectionMock#

Retrieves a collection instance with mocked getIterator method.

Syntax:

Copied to your clipboard
1public function getCollectionMock($className,
2 array $data);

The collection contains elements from the $data array.

Example:

Copied to your clipboard
1$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
2// Prepare mock for collection elements
3$option = $this->getMock(
4 'Magento\Bundle\Model\Option',
5 ['getSelections', '__wakeup', 'getData'],
6 [],
7 '',
8 false
9);
10$optionCollection =
11 $this->objectManagerHelper->getCollectionMock('Magento\Bundle\Model\Resource\Option\Collection',
12 [$options]);

getConstructArguments#

Lists dependency mocks for a specified class.

Syntax:

Copied to your clipboard
1public function getConstructArguments($className,
2 array $arguments = []);

In Adobe Commerce and Magento Open Source, several tests introduced mocks for abstract models and blocks.

Example:

Copied to your clipboard
1$attributeData = [
2 'store_label' => 'Test',
3 'attribute_code' => 'test',
4 'is_required' => 1,
5 'validate_rules' => [
6 'min_text_length' => 0,
7 'max_text_length' => 0,
8 'input_validation' => 0,
9 ]
10];
11$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
12$attributeClass = '\Magento\Eav\Model\Entity\Attribute\AbstractAttribute';
13$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
14// Retrieve mocked constructor arguments
15$arguments = $objectManagerHelper->getConstructArguments(
16 $attributeClass,
17 [
18 'data' => $attributeData,
19 ]
20);
21
22/** @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|\PHPUnit\Framework\MockObject\MockObject */
23$attribute = $this->getMockForAbstractClass($attributeClass,
24 $arguments);
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.