Skip to content

Commit a893a9f

Browse files
committed
MAGETWO-69240: Unit Tests have different behavior between run for a bunch and a single test
1 parent e513ab5 commit a893a9f

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

dev/tests/unit/phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
</exclude>
3939
</whitelist>
4040
</filter>
41+
<listeners>
42+
<listener class="Magento\Framework\TestFramework\Unit\Listener\ReplaceObjectManager"/>
43+
</listeners>
4144
<logging>
4245
<!--coverage_html_placeholder
4346
<log type="coverage-html" target="{{coverage_dir}}/test-reports/coverage" charset="UTF-8" yui="true" highlight="true"/>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\TestFramework\Unit\Listener;
8+
9+
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\TestFramework\Unit\Model\ObjectManager as ObjectManagerMock;
13+
14+
/**
15+
* The event listener which instantiates ObjectManager before test run
16+
*/
17+
class ReplaceObjectManager extends \PHPUnit_Framework_BaseTestListener
18+
{
19+
/**
20+
* @var ObjectManagerInterface
21+
*/
22+
private $objectManager;
23+
24+
/**
25+
* Replaces ObjectManager before run for each test
26+
*
27+
* Replace existing instance of the Application's ObjectManager with an instance,
28+
* defined in the Unit Test framework
29+
*
30+
* This avoids the issue with a not initialized ObjectManager
31+
* and allows to customize its behaviour with expected for unit testing
32+
*
33+
* @param \PHPUnit_Framework_Test $test
34+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
35+
*/
36+
public function startTest(\PHPUnit_Framework_Test $test)
37+
{
38+
if (!$this->objectManager) {
39+
$this->objectManager = new ObjectManagerMock();
40+
}
41+
ObjectManager::setInstance($this->objectManager);
42+
}
43+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\TestFramework\Unit\Model;
8+
9+
/**
10+
* ObjectManager which throws an exception when its methods are called
11+
*/
12+
class ObjectManager implements \Magento\Framework\ObjectManagerInterface
13+
{
14+
/**
15+
* Throws an exception for any call of this method.
16+
*
17+
* {@inheritdoc}
18+
*
19+
* @throws \RuntimeException
20+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
21+
*/
22+
public function create($type, array $arguments = [])
23+
{
24+
$this->throwNotMockedObjectManagerException();
25+
}
26+
27+
/**
28+
* Throws an exception for any call of this method.
29+
*
30+
* {@inheritdoc}
31+
*
32+
* @throws \RuntimeException
33+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
34+
*/
35+
public function get($type)
36+
{
37+
$this->throwNotMockedObjectManagerException();
38+
}
39+
40+
/**
41+
* Throws an exception for any call of this method.
42+
*
43+
* {@inheritdoc}
44+
*
45+
* @throws \RuntimeException
46+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
47+
*/
48+
public function configure(array $configuration)
49+
{
50+
$this->throwNotMockedObjectManagerException();
51+
}
52+
53+
/**
54+
* Throws an exception when ObjectManager has been called.
55+
*
56+
* This exception notifies that a developer should mock the ObjectManager or avoid its usage.
57+
*
58+
* @return void
59+
* @throws \RuntimeException
60+
*/
61+
private function throwNotMockedObjectManagerException()
62+
{
63+
throw new \RuntimeException('ObjectManager shouldn\'t be used in unit tests without mocking');
64+
}
65+
}

0 commit comments

Comments
 (0)