Completed
Push — master ( 79443c...8117c1 )
by Adam
04:00
created

PhpUnit   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 64.58%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 14
c 5
b 1
f 1
lcom 1
cbo 3
dl 0
loc 111
ccs 31
cts 48
cp 0.6458
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getWorkingDir() 0 4 1
A setWorkingDir() 0 4 1
A getTarget() 0 4 1
A setTarget() 0 4 1
A getOptions() 0 4 1
A setOptions() 0 4 1
C execute() 0 37 8
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 = escapeshellarg($this->options['executable']) . ' ';
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 1
		if (isset($this->options['configFile'])) {
96
			$cmd .= '--configuration ';
97
			$cmd .= escapeshellarg($this->options['configFile']) . ' ';
98
		}
99 1
		$cmd .= escapeshellarg($this->target) . ' ';
100
101 1
		$currdir = getcwd();
102 1
		$result = @chdir($this->workingDir);
103 1
		if (!$result) {
104
			$this->error("Cannot change working directory to '$this->workingDir'.");
105
		}
106
107 1
		$command = new Exec();
108 1
		$command->setCommand($cmd);
109 1
		$result = $command->execute();
110 1
		if ($result->getResult() !== 0) {
111
			$this->error(sprintf('Tests failed with code %d.', $result->getResult()));
112
		}
113
114 1
		$result = @chdir($currdir);
115 1
		if (!$result) {
116
			$this->error("Cannot change working directory back to '$currdir'.");
117
		}
118 1
	}
119
120
}