|
9 | 9 |
|
10 | 10 | use Magento\Framework\App\ObjectManager;
|
11 | 11 | use Magento\Framework\ObjectManagerInterface;
|
12 |
| -use Magento\Framework\TestFramework\Unit\Model\ObjectManager as ObjectManagerMock; |
13 | 12 |
|
14 | 13 | /**
|
15 | 14 | * The event listener which instantiates ObjectManager before test run
|
16 | 15 | */
|
17 | 16 | class ReplaceObjectManager extends \PHPUnit_Framework_BaseTestListener
|
18 | 17 | {
|
19 |
| - /** |
20 |
| - * @var ObjectManagerInterface |
21 |
| - */ |
22 |
| - private $objectManager; |
23 |
| - |
24 | 18 | /**
|
25 | 19 | * Replaces ObjectManager before run for each test
|
26 | 20 | *
|
27 |
| - * Replace existing instance of the Application's ObjectManager with an instance, |
28 |
| - * defined in the Unit Test framework |
| 21 | + * Replace existing instance of the Application's ObjectManager with the mock. |
29 | 22 | *
|
30 | 23 | * This avoids the issue with a not initialized ObjectManager
|
31 |
| - * and allows to customize its behaviour with expected for unit testing |
| 24 | + * and makes working with ObjectManager predictable as it always contains clear mock for each test |
32 | 25 | *
|
33 | 26 | * @param \PHPUnit_Framework_Test $test
|
34 | 27 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
35 | 28 | */
|
36 | 29 | public function startTest(\PHPUnit_Framework_Test $test)
|
37 | 30 | {
|
38 |
| - if (!$this->objectManager) { |
39 |
| - $this->objectManager = new ObjectManagerMock(); |
| 31 | + if ($test instanceof \PHPUnit_Framework_TestCase) { |
| 32 | + $objectManagerMock = $test->getMockBuilder(ObjectManagerInterface::class) |
| 33 | + ->getMockForAbstractClass(); |
| 34 | + ObjectManager::setInstance($objectManagerMock); |
40 | 35 | }
|
41 |
| - ObjectManager::setInstance($this->objectManager); |
42 | 36 | }
|
43 | 37 | }
|
0 commit comments