|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\PHPUnit; |
|
4
|
|
|
|
|
5
|
|
|
use Magento\Framework\App\ResourceConnection; |
|
6
|
|
|
use Magento\Framework\DB\Adapter\AdapterInterface; |
|
7
|
|
|
use N98\Magento\Application; |
|
8
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class TestCase |
|
13
|
|
|
* |
|
14
|
|
|
* @codeCoverageIgnore |
|
15
|
|
|
* @package N98\Magento\Command\PHPUnit |
|
16
|
|
|
*/ |
|
17
|
|
|
class TestCase extends \PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Application |
|
21
|
|
|
*/ |
|
22
|
|
|
private $application = null; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string|null |
|
26
|
|
|
*/ |
|
27
|
|
|
private $root; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $varname name of the environment variable containing the test-root |
|
31
|
|
|
* @param string $basename name of the stopfile containing the test-root |
|
32
|
|
|
* |
|
33
|
|
|
* @return string|null |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function getTestMagentoRootFromEnvironment($varname, $basename) |
|
36
|
|
|
{ |
|
37
|
|
|
$root = getenv($varname); |
|
38
|
|
|
if (empty($root) && strlen($basename)) { |
|
39
|
|
|
$stopfile = getcwd() . '/' . $basename; |
|
40
|
|
|
if (is_readable($stopfile) && $buffer = rtrim(file_get_contents($stopfile))) { |
|
41
|
|
|
$root = $buffer; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
if (empty($root)) { |
|
45
|
|
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
# directory test |
|
49
|
|
|
if (!is_dir($root)) { |
|
50
|
|
|
throw new RuntimeException( |
|
51
|
|
|
sprintf("%s path '%s' is not a directory", $varname, $root) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
# resolve root to realpath to be independent to current working directory |
|
56
|
|
|
$rootRealpath = realpath($root); |
|
57
|
|
|
if (false === $rootRealpath) { |
|
58
|
|
|
throw new RuntimeException( |
|
59
|
|
|
sprintf("Failed to resolve %s path '%s' with realpath()", $varname, $root) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $rootRealpath; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* getter for the magento root directory of the test-suite |
|
68
|
|
|
* |
|
69
|
|
|
* @see ApplicationTest::testExecute |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
|
|
|
|
|
72
|
|
|
*/ |
|
73
|
|
|
public function getTestMagentoRoot() |
|
74
|
|
|
{ |
|
75
|
|
|
if ($this->root) { |
|
|
|
|
|
|
76
|
|
|
return $this->root; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$varname = 'N98_MAGERUN2_TEST_MAGENTO_ROOT'; |
|
80
|
|
|
$basename = '.n98-magerun2'; |
|
81
|
|
|
|
|
82
|
|
|
$root = self::getTestMagentoRootFromEnvironment($varname, $basename); |
|
83
|
|
|
|
|
84
|
|
|
if (null === $root) { |
|
85
|
|
|
$this->markTestSkipped( |
|
86
|
|
|
"Please specify environment variable $varname with path to your test magento installation!" |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->root = $root; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return Application|PHPUnit_Framework_MockObject_MockObject |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getApplication() |
|
97
|
|
|
{ |
|
98
|
|
|
if ($this->application === null) { |
|
99
|
|
|
$root = $this->getTestMagentoRoot(); |
|
100
|
|
|
|
|
101
|
|
|
/** @var Application|PHPUnit_Framework_MockObject_MockObject $application */ |
|
102
|
|
|
$application = $this->getMock('N98\Magento\Application', array('getMagentoRootFolder')); |
|
103
|
|
|
$loader = require __DIR__ . '/../../../../../vendor/autoload.php'; |
|
104
|
|
|
$application->setAutoloader($loader); |
|
|
|
|
|
|
105
|
|
|
$application->expects($this->any())->method('getMagentoRootFolder')->will($this->returnValue($root)); |
|
|
|
|
|
|
106
|
|
|
$application->init(); |
|
|
|
|
|
|
107
|
|
|
$application->initMagento(); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
$this->application = $application; |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $this->application; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return AdapterInterface |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getDatabaseConnection() |
|
119
|
|
|
{ |
|
120
|
|
|
$resource = $this->getApplication()->getObjectManager()->get(ResourceConnection::class); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
return $resource->getConnection('write'); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.