Completed
Push — master ( bb49a3...bb2037 )
by Adam
11:59
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 2
Bugs 0 Features 0
Metric Value
c 2
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
	 * Returns working directory. System will switch to this directory before running tests
22
	 * @return string
23
	 */
24 2
	public function getWorkingDir()
25
	{
26 2
		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 2
	public function setWorkingDir($workingDir)
35
	{
36 2
		$this->workingDir = $workingDir;
37 2
	}
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 2
	public function setTarget($target)
55
	{
56 2
		$this->target = $target;
57 2
	}
58
59
60
	/**
61
	 * @return array|NULL
62
	 */
63 2
	public function getOptions()
64
	{
65 2
		return $this->options;
66
	}
67
68
69
	/**
70
	 * Possible options:
71
	 * - executable (mandatory)
72
	 * - xdebugExtensionFile
73
	 * - configFile
74
	 * @param array|NULL $options
75
	 */
76 2
	public function setOptions(array $options = NULL)
77
	{
78 2
		$this->options = $options;
79 2
	}
80
81
82 2
	public function execute()
83
	{
84 2
		if (!isset($this->options['executable'])) {
85 1
			$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 1
		$cmd .= escapeshellarg($this->options['executable']) . ' ';
96 1
		$cmd .= escapeshellarg($this->target) . ' ';
97
98
		$optionalSwitches = [
99 1
			'configFile' => '--configuration',
100 1
		];
101 1 View Code Duplication
		foreach($optionalSwitches as $name => $switch) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102 1
			if(isset($this->options[$name])) {
103
				$cmd .= $switch . ' ';
104
				$cmd .= escapeshellarg($this->options[$name]) . ' ';
105
			}
106 1
		}
107
108 1
		$currdir = getcwd();
109 1
		$result = @chdir($this->workingDir);
110 1
		if (!$result) {
111
			$this->error("Cannot change working directory to '$this->workingDir'.");
112
		}
113
114 1
		$command = new Exec();
115 1
		$command->setCommand($cmd);
116 1
		$result = $command->execute();
117 1
		if ($result->getResult() !== 0) {
118
			$this->error(sprintf('Tests failed with code %d.', $result->getResult()));
119
		}
120
121 1
		$result = @chdir($currdir);
122 1
		if (!$result) {
123
			$this->error("Cannot change working directory back to '$currdir'.");
124
		}
125 1
	}
126
127
}