Completed
Push — master ( cc7f5d...e5b923 )
by Adam
03:50
created

PhpUnit::setTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
	 * @return mixed
22
	 */
23 1
	public function getWorkingDir()
24
	{
25 1
		return $this->workingDir;
26
	}
27
28
29
	/**
30
	 * @param mixed $workingDir
31
	 */
32 1
	public function setWorkingDir($workingDir)
33
	{
34 1
		$this->workingDir = $workingDir;
35 1
	}
36
37
38
	/**
39
	 * @return mixed
40
	 */
41 1
	public function getTarget()
42
	{
43 1
		return $this->target;
44
	}
45
46
47
	/**
48
	 * @param mixed $target
49
	 */
50 1
	public function setTarget($target)
51
	{
52 1
		$this->target = $target;
53 1
	}
54
55
56
	/**
57
	 * @return array|NULL
58
	 */
59 1
	public function getOptions()
60
	{
61 1
		return $this->options;
62
	}
63
64
65
	/**
66
	 * @param array|NULL $options
67
	 */
68 1
	public function setOptions(array $options = NULL)
69
	{
70 1
		$this->options = $options;
71 1
	}
72
73
74 1
	public function execute()
75
	{
76 1
		if (!isset($this->options['executable'])) {
77
			$this->error('PHPUnit executable not defined.');
78
		}
79
80 1
		$cmd = escapeshellarg($this->options['executable']) . ' ';
81 1
		if (isset($this->options['xdebugExtensionFile'])) {
82
			if(!is_file($this->options['xdebugExtensionFile'])){ // PHP is quite when extension file does not exists
83
				$this->error("Xdebug extension file '{$this->options['xdebugExtensionFile']}' does not exists.");
84
			}
85
			$cmd .= '-d zend_extension=' . escapeshellarg($this->options['xdebugExtensionFile']) . ' ';
86
		}
87 1
		if (isset($this->options['configFile'])) {
88
			$cmd .= '--configuration ';
89
			$cmd .= escapeshellarg($this->options['configFile']) . ' ';
90
		}
91 1
		$cmd .= escapeshellarg($this->target) . ' ';
92
93 1
		$currdir = getcwd();
94 1
		$result = @chdir($this->workingDir);
95 1
		if(!$result){
96
			$this->error("Cannot change working directory to '$this->workingDir'.");
97
		}
98
99 1
		$command = new Exec();
100 1
		$command->setCommand($cmd);
101 1
		$result = $command->execute();
102 1
		if ($result->getResult() !== 0) {
103
			$this->error(sprintf('Tests failed with code %d.', $result->getResult()));
104
		}
105
106 1
		$result = @chdir($currdir);
107 1
		if(!$result){
108
			$this->error("Cannot change working directory back to '$currdir'.");
109
		}
110 1
	}
111
112
}