This repository was archived by the owner on May 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathecomdev-phpunit.php
372 lines (323 loc) · 12.7 KB
/
ecomdev-phpunit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <[email protected]>
*/
require_once realpath(dirname($_SERVER["SCRIPT_FILENAME"])) . DIRECTORY_SEPARATOR . 'abstract.php';
// Only this workaround fixes Magento core issue in 1.8 :(
$abstractShell = new ReflectionClass('Mage_Shell_Abstract');
define('PHPUNIT_MAGE_PATH', dirname(dirname($abstractShell->getFileName())));
$appFile = PHPUNIT_MAGE_PATH . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'Mage.php';
require_once $appFile;
/**
* Shell script for autoinstalling of required files for phpunit extension
*
*
*/
class EcomDev_PHPUnit_Install extends Mage_Shell_Abstract
{
const FILE_LOCAL_XML = 'app/etc/local.xml.phpunit';
const FILE_PHPUNIT_XML = 'phpunit.xml.dist';
const FILE_CONFIG_XML = 'app/code/community/EcomDev/PHPUnit/etc/config.xml';
const OLD_FILE_MATCH = '/\\<file\\>UnitTests.php\\<\\/file\\>/';
/**
* Config xml value map
*
* @var array[]
*/
protected $_valuesMap = array(
'db-host' => array(
'type' => 'string',
'nullable' => true,
'path' => '//global/resources/default_setup/connection/host'
),
'db-user' => array(
'type' => 'string',
'nullable' => true,
'path' => '//global/resources/default_setup/connection/username'
),
'db-pwd' => array(
'type' => 'string',
'nullable' => true,
'path' => '//global/resources/default_setup/connection/password'
),
'db-name' => array(
'type' => 'string',
'path' => '//global/resources/default_setup/connection/dbname'
),
'base-url' => array(
'type' => 'string',
'path' => array(
'//default/web/secure/base_url',
'//default/web/unsecure/base_url'
)
),
'same-db' => array(
'type' => 'boolean',
'path' => '//phpunit/allow_same_db'
),
'url-rewrite' => array(
'type' => 'boolean',
'path' => '//default/web/seo/use_rewrites'
)
);
/**
* This script doesn't need initialization of Magento
*
* @var bool
*/
protected $_includeMage = false;
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f ecomdev-phpunit.php -- -a <action> <options>
-h --help Shows usage
--action -a <action> Perform one of the defined actions below
Defined actions:
install Copies required extension files if they were not created before
-r --rewrite Overrides phpunit.xml.dist file, even if it exists
cache Clears magento and phpunit cache directories
phpunit-config Copies phpunit.xml.dist file from extension if it doesn't exist
-r --rewrite Overrides phpunit.xml.dist file, even if it exists
magento-config Updates settings in local.xml.phpunit file to change settings
--db-host <string> Changes test DB host
--db-name <string> Changes test DB name
--db-user <string> Changes test DB username
--db-pwd <string> Changes test DB password
--same-db <bool> Changes same db usage flag for unit tests
--url-rewrite <bool> Changes use of url rewrites for unit tests
--base-url <string> Changes base url for controller tests
show-version Shows current version of the module
change-status Changes status of EcomDev_PHPUnitTest module, that contains built in supplied tests
--enable Used to determine the status of it. If not specified, it will be disabled
fix-autoloader Patches Varien_Autoload class to suppress include warning, that breaks class_exists().
USAGE;
}
/**
* Runs scripts itself
*/
public function run()
{
if (!$this->getArg('action') && !$this->getArg('a')) {
die($this->usageHelp());
}
$this->_args['module'] = dirname(dirname(__FILE__));
$this->_args['project'] = PHPUNIT_MAGE_PATH;
$action = $this->getArg('action') ?: $this->getArg('a');
switch ($action) {
case 'install':
// Installation is silent
$this->_copyLocalXml();
$this->_copyPHPUnitXml();
$this->_cleanCache();
break;
case 'cache':
$this->_cleanCache();
echo "Cache was cleared\n";
break;
case 'phpunit-config':
$this->_copyPHPUnitXml();
echo "phpunit.xml.dist file was copied/updated\n";
break;
case 'magento-config':
$this->_updateLocalXml();
break;
case 'change-status':
$this->_changeBuiltInTestStatus($this->getArg('enable'));
$this->_cleanCache();
echo "EcomDev_PHPUnitTest module status was changed\n";
break;
case 'fix-autoloader':
$this->_fixAutoloader();
break;
case 'show-version':
$version = $this->_retrieveVersion();
$this->_cleanCache();
echo "EcomDev_PHPUnit module version is {$version} \n";
break;
default:
/* Force show help */
$this->_args['h'] = true;
$this->_showHelp();
break;
}
}
/**
* Copies local XML file from phpunit extension folder
*
*/
protected function _copyLocalXml()
{
if (!file_exists($this->getArg('project') . DIRECTORY_SEPARATOR . self::FILE_LOCAL_XML)) {
copy($this->getArg('module') . DIRECTORY_SEPARATOR . self::FILE_LOCAL_XML,
$this->getArg('project') . DIRECTORY_SEPARATOR . self::FILE_LOCAL_XML);
}
}
/**
* Checks existence of phpunit.xml.dist file, if file is outdated,
* it just replaces the content of it.
*
*/
protected function _copyPHPUnitXml()
{
if ($this->getArg('rewrite') || $this->getArg('r')
|| !file_exists($this->getArg('project') . DIRECTORY_SEPARATOR . self::FILE_PHPUNIT_XML)
|| preg_match(self::OLD_FILE_MATCH,
file_get_contents($this->getArg('project') . DIRECTORY_SEPARATOR . self::FILE_PHPUNIT_XML))) {
copy($this->getArg('module') . DIRECTORY_SEPARATOR . self::FILE_PHPUNIT_XML,
$this->getArg('project') . DIRECTORY_SEPARATOR . self::FILE_PHPUNIT_XML);
}
}
/**
* Clears cache of the magento project
*
*
*/
protected function _cleanCache()
{
if (is_dir($this->getArg('project') . '/var/cache')) {
shell_exec('rm -rf ' . $this->getArg('project') . '/var/cache');
}
if (is_dir($this->getArg('project') . '/var/phpunit.cache')) {
shell_exec('rm -rf ' . $this->getArg('project') . '/var/phpunit.cache');
}
}
/**
* Changes extension suite internal tests status
*
* @param bool $status
*/
protected function _changeBuiltInTestStatus($status)
{
if (!file_exists($this->getArg('project') . '/app/etc/modules/EcomDev_PHPUnitTest.xml')) {
die('Cannot find EcomDev_PHPUnitTest.xml file in app/etc/modules directory');
}
$disableFile = $this->getArg('project') . '/app/etc/modules/ZDisable_EcomDev_PHPUnitTest.xml';
if ($status && file_exists($disableFile)) {
unlink($disableFile);
} elseif (!$status && !file_exists($disableFile)) {
$fileContent = new Varien_Simplexml_Element('<config />');
$fileContent->addChild('modules')
->addChild('EcomDev_PHPUnitTest')
->addChild('active', 'false');
$fileContent->asNiceXml($disableFile);
}
}
/**
* Updates local.xml.phpunit values
*
*/
protected function _updateLocalXml()
{
$localXml = $this->getArg('project') . DIRECTORY_SEPARATOR . self::FILE_LOCAL_XML;
if (!file_exists($localXml)) {
die('Cannot find local.xml.phpunit file in app/etc directory');
}
/* @var $localXmlConfig Varien_Simplexml_Element */
$localXmlConfig = simplexml_load_file($localXml, 'Varien_Simplexml_Element');
foreach ($this->_args as $name => $value) {
if (isset($this->_valuesMap[$name])) {
$info = $this->_valuesMap[$name];
if (empty($info['nullable']) && $value === true) {
die("--$name value should be specified\n".$this->usageHelp());
}
if (!is_array($info['path'])) {
$info['path'] = array($info['path']);
}
foreach ($info['path'] as $path) {
/** @var $currentElement Varien_Simplexml_Element */
$currentElement = current($localXmlConfig->xpath($path));
if (!$currentElement) {
if ($value === true) {
continue;
}
$parents = explode('/', ltrim($path, '/'));
// Remove last item, since it is our element
$currentElementName = array_pop($parents);
$parentElement = $localXmlConfig;
foreach ($parents as $parent) {
if (!isset($parentElement->$parent)) {
$parentElement->$parent = null;
}
$parentElement = $parentElement->$parent;
}
} else {
$parentElement = $currentElement->getParent();
$currentElementName = $currentElement->getName();
}
if ($currentElement) {
unset($parentElement->$currentElementName);
}
if ($value !== true) {
if ($info['type'] === 'boolean') {
$value = $value ? '1' : '0';
}
$parentElement->$currentElementName = $value;
printf("Changed value to %s for %s node\n", $value, $path);
} else {
printf("Removed %s node\n", $path);
}
}
}
}
if (isset($currentElement)) {
$localXmlConfig->asNiceXml($localXml);
printf("Saved updated configuration at %s\n", $localXml);
}
}
/**
* Fixes Varien_Autoloader problem on phpunit test cases
*
*
*/
protected function _fixAutoloader()
{
$autoloaderFile = $this->getArg('project') . DIRECTORY_SEPARATOR . 'lib/Varien/Autoload.php';
file_put_contents(
$autoloaderFile,
str_replace(
'return include $classFile;',
'return @include $classFile;',
file_get_contents($autoloaderFile)
)
);
echo "Varien_Autoloader was patched\n";
}
/**
* Retrieve current module version from the config.xml file
*
* @return string
*/
protected function _retrieveVersion()
{
if (!file_exists($this->getArg('project') . DS . self::FILE_CONFIG_XML)) {
die('Cannot find module config file!');
}
$configFilePath = $this->getArg('project') . DS . self::FILE_CONFIG_XML;
/** @var $moduleConfigXml Varien_Simplexml_Element */
$moduleConfigXml = simplexml_load_file($configFilePath, 'Varien_Simplexml_Element');
if (!isset($moduleConfigXml->modules->EcomDev_PHPUnit->version)) {
die('Cannot retrieve module version!');
}
$version = $moduleConfigXml->modules->EcomDev_PHPUnit->version;
return $version;
}
}
$shell = new EcomDev_PHPUnit_Install();
$shell->run();