1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* this file is part of magerun |
4
|
|
|
* |
5
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace N98\Magento; |
9
|
|
|
|
10
|
|
|
use PHPUnit_Framework_MockObject_Generator; |
11
|
|
|
use PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount; |
12
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
13
|
|
|
use PHPUnit_Framework_MockObject_Stub_Return; |
14
|
|
|
use PHPUnit_Framework_SkippedTestError; |
15
|
|
|
use RuntimeException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Magento test-application, the one used in unit and integration testing. |
19
|
|
|
* |
20
|
|
|
* @package N98\Magento |
21
|
|
|
*/ |
22
|
|
|
class TestApplication |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Application |
26
|
|
|
*/ |
27
|
|
|
private $application; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string|null |
31
|
|
|
*/ |
32
|
|
|
private $root; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $varname; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $basename; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $varname name of the environment variable containing the test-root |
46
|
|
|
* @param string $basename name of the stopfile containing the test-root |
47
|
|
|
* |
48
|
|
|
* @return string|null |
49
|
|
|
*/ |
50
|
|
|
public static function getTestMagentoRootFromEnvironment($varname, $basename) |
51
|
|
|
{ |
52
|
|
|
$root = getenv($varname); |
53
|
|
|
if (empty($root) && strlen($basename)) { |
54
|
|
|
$stopfile = getcwd() . '/' . $basename; |
55
|
|
|
if (is_readable($stopfile) && $buffer = rtrim(file_get_contents($stopfile))) { |
56
|
|
|
$root = $buffer; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
if (empty($root)) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
# directory test |
64
|
|
|
if (!is_dir($root)) { |
65
|
|
|
throw new RuntimeException( |
66
|
|
|
sprintf("%s path '%s' is not a directory", $varname, $root) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
# resolve root to realpath to be independent to current working directory |
71
|
|
|
$rootRealpath = realpath($root); |
72
|
|
|
if (false === $rootRealpath) { |
73
|
|
|
throw new RuntimeException( |
74
|
|
|
sprintf("Failed to resolve %s path '%s' with realpath()", $varname, $root) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $rootRealpath; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public static function getConfig() |
85
|
|
|
{ |
86
|
|
|
$testApplication = new TestApplication(); |
87
|
|
|
$config = $testApplication->getApplication()->getConfig(); |
|
|
|
|
88
|
|
|
|
89
|
|
|
return $config; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* TestApplication constructor. |
94
|
|
|
* |
95
|
|
|
* @param string $varname [optional] name of the environment variable containing the path to magento-root |
|
|
|
|
96
|
|
|
*/ |
97
|
|
|
public function __construct($varname = null, $basename = null) |
98
|
|
|
{ |
99
|
|
|
if (null === $varname) { |
100
|
|
|
$varname = 'N98_MAGERUN2_TEST_MAGENTO_ROOT'; |
101
|
|
|
} |
102
|
|
|
if (null === $basename) { |
103
|
|
|
$basename = '.n98-magerun2'; |
104
|
|
|
} |
105
|
|
|
$this->varname = $varname; |
106
|
|
|
$this->basename = $basename; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* getter for the magento root directory of the test-suite |
111
|
|
|
* |
112
|
|
|
* @see ApplicationTest::testExecute |
113
|
|
|
* |
114
|
|
|
* @return string |
|
|
|
|
115
|
|
|
*/ |
116
|
|
|
public function getTestMagentoRoot() |
117
|
|
|
{ |
118
|
|
|
if ($this->root) { |
|
|
|
|
119
|
|
|
return $this->root; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$varname = $this->varname; |
123
|
|
|
|
124
|
|
|
$root = self::getTestMagentoRootFromEnvironment($varname, $this->basename); |
125
|
|
|
|
126
|
|
|
if (null === $root) { |
127
|
|
|
$this->markTestSkipped( |
128
|
|
|
"Please specify environment variable $varname with path to your test magento installation!" |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->root = $root; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return Application|PHPUnit_Framework_MockObject_MockObject |
137
|
|
|
*/ |
138
|
|
|
public function getApplication() |
139
|
|
|
{ |
140
|
|
|
if ($this->application === null) { |
141
|
|
|
$root = $this->getTestMagentoRoot(); |
142
|
|
|
|
143
|
|
|
$mockObjectGenerator = new PHPUnit_Framework_MockObject_Generator; |
144
|
|
|
|
145
|
|
|
/** @var Application|PHPUnit_Framework_MockObject_MockObject $application */ |
146
|
|
|
$application = $mockObjectGenerator->getMock('N98\Magento\Application', array('getMagentoRootFolder')); |
147
|
|
|
|
148
|
|
|
// Get the composer bootstraph |
149
|
|
|
if (defined('PHPUNIT_COMPOSER_INSTALL')) { |
150
|
|
|
$loader = require PHPUNIT_COMPOSER_INSTALL; |
151
|
|
|
} elseif (file_exists(__DIR__ . '/../../../../../autoload.php')) { |
152
|
|
|
// Installed via composer, already in vendor |
153
|
|
|
$loader = require __DIR__ . '/../../../../../autoload.php'; |
154
|
|
|
} else { |
155
|
|
|
// Check if testing root package without PHPUnit |
156
|
|
|
$loader = require __DIR__ . '/../../../vendor/autoload.php'; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$application->setAutoloader($loader); |
|
|
|
|
160
|
|
|
$application->expects($this->any())->method('getMagentoRootFolder')->will($this->returnValue($root)); |
|
|
|
|
161
|
|
|
$application->init(); |
|
|
|
|
162
|
|
|
$application->initMagento(); |
|
|
|
|
163
|
|
|
|
164
|
|
|
$this->application = $application; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->application; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/* |
171
|
|
|
* PHPUnit TestCase methods |
172
|
|
|
*/ |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns a matcher that matches when the method it is evaluated for |
176
|
|
|
* is executed zero or more times. |
177
|
|
|
* |
178
|
|
|
* @return PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount |
179
|
|
|
* @since Method available since Release 3.0.0 |
180
|
|
|
*/ |
181
|
|
|
public static function any() |
182
|
|
|
{ |
183
|
|
|
return new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* |
188
|
|
|
* |
189
|
|
|
* @param mixed $value |
190
|
|
|
* @return PHPUnit_Framework_MockObject_Stub_Return |
191
|
|
|
* @since Method available since Release 3.0.0 |
192
|
|
|
*/ |
193
|
|
|
public static function returnValue($value) |
194
|
|
|
{ |
195
|
|
|
return new PHPUnit_Framework_MockObject_Stub_Return($value); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Mark the test as skipped. |
200
|
|
|
* |
201
|
|
|
* @param string $message |
202
|
|
|
* @throws PHPUnit_Framework_SkippedTestError |
203
|
|
|
* @since Method available since Release 3.0.0 |
204
|
|
|
*/ |
205
|
|
|
public static function markTestSkipped($message = '') |
206
|
|
|
{ |
207
|
|
|
throw new PHPUnit_Framework_SkippedTestError($message); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: