-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests-run
executable file
·116 lines (94 loc) · 3.3 KB
/
tests-run
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
#!/usr/bin/env php
<?php
namespace Loadsys\CakePHPShellScripts\TestsRun;
/**
* Prints usage information and exits.
*
* @param string $script Typically set to basename($argv[0]) when the method is called.
* @param int $exitCode The numeric exit code to return. 0 = success. >0 = failure.
* @return void
*/
function usage($script = null, $exitCode = 0) {
$script = $script ?: basename(__FILE__);
$usage = <<<EOD
${script}
Convenience wrapper around PHPUnit test runner.
Clears the Cake app's Caches and either executes the entire
test suite, or tests only the named file.
Usage:
bin/${script}
If any of the arguments are a path to a specific (existant)
file, the test for it will be called directly. This script
will automatically try to append ".php" to find a match.
If the named file is not a Test case itself, the script will
attempt to locate the counterpart test. For example, all of
the following inputs translate to the given TestCase:
src/Controller/UsersController
src/Controller/UsersController.php
tests/TestCase/Controller/UsersController
tests/TestCase/Controller/UsersController.php
--> tests/TestCase/Controller/UsersControllerTest.php
This is intended to allow this script to be paired with a
file watcher. When a source or test file changes, that
filename can be passed to this script to have the test case
automatically executed inside the vagrant environment.
EOD;
echo $usage;
exit($exitCode);
}
/**
* Locate the appropriate TestCase file for the provided $path.
*
* Applies a series of transformations to the provided path in an attempt
* to locate the TestCase file to execute for the $path. When a readable
* file is found, it is returned. Returns the original unmodified string
* on failure.
*
* Examples:
* src/Controller/UsersController
* src/Controller/UsersController.php
* tests/TestCase/Controller/UsersController
* tests/TestCase/Controller/UsersController.php
*
* --> tests/TestCase/Controller/UsersControllerTest.php
*
* @param string $path An (assumed) filesystem path for a source or test file to execute.
* @return string The (possibly transformed) path to the counterpart TestCase file.
* @link https://github.com/cakephp/cakephp/blob/2.6.9/lib/Cake/Console/Command/TestShell.php#L334
*/
function findTestCase($path) {
$p = $path;
if (substr($p, -4) !== '.php') {
$p .= '.php';
}
$p = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', realpath($p));
$p = preg_replace('|^src/|', 'tests/TestCase/', $p, -1, $replaceCount);
if ($replaceCount) {
$p = preg_replace('|/(.+).php$|', '/\1Test.php', $p);
}
if (is_readable($p)) {
return $p;
}
return $path;
}
/**
* main() ===================================================================
*/
if (isset($argv[1]) && $argv[1] == '-h') {
usage();
}
// Set up variables.
array_shift($argv); // Strip the script name off and throw it away.
$argc--; // Reindex argc so we can use it directly on remaining args.
if ($argc) {
$args = array_map('Loadsys\CakePHPShellScripts\TestsRun\findTestCase', $argv);
$args = array_map('escapeshellarg', $args);
$args = implode(' ', $args);
// $args = '"' . $args . '"';
$args = ' ' . $args;
} else {
$args = '';
}
$testCmd = "bin/vagrant-exec \"bin/phpunit{$args}\"";
//echo "## Running: `{$testCmd}`" . PHP_EOL;
system($testCmd);