Object Manager helper

Block and model class constructors declare many dependencies. The Magento system uses 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.

Do not use the ObjectManager helper class for classes with a small number of dependencies.

ObjectManager 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:

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

Example:

1
2
3
4
5
6
7
8
9
10
11
$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);

// default constructor arguments
$scopePool = $objectManagerHelper->getObject('\Magento\App\Config\ScopePool');

// custom constructor arguments
$cacheMock = $this->getMock('\Magento\Cache\FrontendInterface');
...
$arguments = array('cache' => $cacheMock);
$scopePool = $objectManagerHelper->getObject('\Magento\App\Config\ScopePool',
     $arguments);

getCollectionMock

Retrieves a collection instance with mocked getIterator method.

Syntax:

1
2
public function getCollectionMock($className,
     array $data);

The collection contains elements from the $data array.

Example:

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

getConstructArguments

Lists dependency mocks for a specified class.

Syntax:

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

In the Magento system, several tests introduced mocks for abstract models and blocks.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$attributeData = array(
    'store_label' => 'Test',
    'attribute_code' => 'test',
    'is_required' => 1,
    'validate_rules' => array(
        'min_text_length' => 0,
        'max_text_length' => 0,
        'input_validation' => 0,
    )
);
$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
$attributeClass = '\Magento\Eav\Model\Entity\Attribute\AbstractAttribute';
$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
// Retrieve mocked constructor arguments
$arguments = $objectManagerHelper->getConstructArguments(
    $attributeClass,
    array(
        'data' => $attributeData,
    )
);

/** @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|\PHPUnit\Framework\MockObject\MockObject */
$attribute = $this->getMockForAbstractClass($attributeClass,
    $arguments);