-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutoPHPUnit.php
157 lines (129 loc) · 2.54 KB
/
AutoPHPUnit.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
<?php
/**
* Automaticaly run the PHPUnit tests everytime a file is saved
*
*
* Make it recursive
* Clear the screen
* Get the path
*/
class AutoPHPUnit
{
/**
* @var string
*/
private $pathToTest;
/**
* @var string
*/
private $autoPHPUnitPath;
/**
* @var string
*/
private $configFile = '';
/**
* Class constructor
*/
public function __construct()
{
$this->readParams();
$this->autoPHPUnitPath = realpath(dirname(__FILE__));
}
/**
* Run the program
*
* @return void
*/
public function run() {
$this->runUnitTests();
$this->watchFiles();
}
/**
* Wait for a change in the file system
*
* @return void
*/
protected function watchFiles() {
require $this->autoPHPUnitPath . '/RecursiveFolderWatcher.php';
$o = new RecursiveFolderWatcher($this->pathToTest, array($this, 'runUnitTests'));
$o->watch();
}
/**
* Callback function to run the unit tests
*
* @return void
*/
public function runUnitTests() {
$this->clearConsole();
$configOption = $this->getConfigParamForPHPUnit();
//--testdox
$command = 'phpunit ' . $configOption . ' ' . $this->pathToTest;
error_log($command);
echo `$command`;
}
/**
* Clear the console
*
* @return void
*/
protected function clearConsole() {
passthru('clear');
}
/**
* Read the command line params
*
* @return void
*/
private function readParams() {
$options = getopt('c::p::', array('config::', 'path::'));
$this->readConfigFile($options);
$this->readPathToTest($options);
}
/**
* Read the config file from the options
*
* @return void
*/
private function readConfigFile(array $options) {
$configFile = '';
if (array_key_exists('c', $options)) {
$configFile = $options['c'];
} else if (array_key_exists('config', $options)) {
$configFile = $options['config'];
}
$this->configFile = $configFile;
}
/**
* Read the path to test
*
* @return void
*/
private function readPathToTest(array $options) {
$path = '';
if (array_key_exists('p', $options)) {
$path = $options['p'];
} else if (array_key_exists('path', $options)) {
$path = $options['path'];
} else {
$path = getcwd();
}
$this->pathToTest = $path;
}
/**
* Return the config params for the phpunit command
*
* @return string
*/
private function getConfigParamForPHPUnit() {
$path = $this->configFile;
if (0 === strlen($path)) {
$path = $this->pathToTest . '/phpunit.xml';
}
if (file_exists($path)) {
return '-c ' . $path;
}
return '';
}
}
$runner = new AutoPHPUnit();
$runner->run();