Completed
Push — master ( 34a972...bd351a )
by Adam
04:37
created

PhpUnit::execute()   C

Complexity

Conditions 8
Paths 96

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 15.2923

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 39
ccs 17
cts 33
cp 0.5152
rs 5.3846
cc 8
eloc 25
nc 96
nop 0
crap 15.2923
1
<?php
2
3
4
namespace Genesis\Commands;
5
6
7
/**
8
 * @author Adam Bisek <[email protected]>
9
 */
10
class PhpUnit extends Command
11
{
12
13
	private $workingDir;
14
15
	private $target;
16
17
	private $options;
18
19
20
	/**
21
	 * Returns working directory. System will switch to this directory before running tests
22
	 * @return string
23
	 */
24 1
	public function getWorkingDir()
25
	{
26 1
		return $this->workingDir;
27
	}
28
29
30
	/**
31
	 * Sets working directory. System will switch to this directory before running tests
32
	 * @param mixed $workingDir
33
	 */
34 1
	public function setWorkingDir($workingDir)
35
	{
36 1
		$this->workingDir = $workingDir;
37 1
	}
38
39
40
	/**
41
	 * Returns target to be executed. Dir in working directory, dot (current dir), TestFile, etc
42
	 * @return mixed
43
	 */
44 1
	public function getTarget()
45 1
	{
46
		return $this->target;
47
	}
48
49
50
	/**
51
	 * Sets target to be executed. Dir in working directory, dot (current dir), TestFile, etc
52
	 * @param mixed $target
53
	 */
54 1
	public function setTarget($target)
55
	{
56 1
		$this->target = $target;
57 1
	}
58
59
60
	/**
61
	 * @return array|NULL
62
	 */
63 1
	public function getOptions()
64
	{
65 1
		return $this->options;
66
	}
67
68
69
	/**
70
	 * Possible options:
71
	 * - executable (mandatory)
72
	 * - xdebugExtensionFile
73
	 * - configFile
74
	 * @param array|NULL $options
75
	 */
76 1
	public function setOptions(array $options = NULL)
77
	{
78 1
		$this->options = $options;
79 1
	}
80
81
82 1
	public function execute()
83
	{
84 1
		if (!isset($this->options['executable'])) {
85
			$this->error('PHPUnit executable not defined.');
86
		}
87
88 1
		$cmd = 'php ';
89 1
		if (isset($this->options['xdebugExtensionFile'])) {
90
			if (!is_file($this->options['xdebugExtensionFile'])) { // PHP is quite when extension file does not exists
91
				$this->error("Xdebug extension file '{$this->options['xdebugExtensionFile']}' does not exists.");
92
			}
93
			$cmd .= '-d zend_extension=' . escapeshellarg($this->options['xdebugExtensionFile']) . ' ';
94
		}
95
96 1
		$cmd .= escapeshellarg($this->options['executable']) . ' ';
97 1
		if (isset($this->options['configFile'])) {
98
			$cmd .= '--configuration ';
99
			$cmd .= escapeshellarg($this->options['configFile']) . ' ';
100
		}
101 1
		$cmd .= escapeshellarg($this->target) . ' ';
102
103 1
		$currdir = getcwd();
104 1
		$result = @chdir($this->workingDir);
105 1
		if (!$result) {
106
			$this->error("Cannot change working directory to '$this->workingDir'.");
107
		}
108
109 1
		$command = new Exec();
110 1
		$command->setCommand($cmd);
111 1
		$result = $command->execute();
112 1
		if ($result->getResult() !== 0) {
113
			$this->error(sprintf('Tests failed with code %d.', $result->getResult()));
114
		}
115
116 1
		$result = @chdir($currdir);
117 1
		if (!$result) {
118
			$this->error("Cannot change working directory back to '$currdir'.");
119
		}
120 1
	}
121
122
}