1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ShouldPHP |
4
|
|
|
* |
5
|
|
|
* @author Gabriel Jacinto <[email protected]> |
6
|
|
|
* @status dev |
7
|
|
|
* @link https://github.com/GabrielJMJ/ShouldPHP |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Gabrieljmj\Should\Runner; |
12
|
|
|
|
13
|
|
|
use Gabrieljmj\Should\Runner\RunnerInterface; |
14
|
|
|
use Gabrieljmj\Should\Runner\Rule\RuleInterface; |
15
|
|
|
use Gabrieljmj\Should\Ambient\AmbientInterface; |
16
|
|
|
use Gabrieljmj\Should\Report\Report; |
17
|
|
|
|
18
|
|
|
abstract class AbstractRunner implements RunnerInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \Gabrieljmj\Should\Report\Report |
22
|
|
|
*/ |
23
|
|
|
protected $report; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $rules = []; |
29
|
|
|
|
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->report = new Report('test'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param \Gabrieljmj\Should\AmbientInterface $ambient |
37
|
|
|
*/ |
38
|
|
|
protected function runTest(AmbientInterface $ambient) |
39
|
|
|
{ |
40
|
|
|
$ambient->run(); |
41
|
|
|
$cReport = $ambient->getReport(); |
42
|
|
|
|
43
|
|
|
if ($this->report === null) { |
44
|
|
|
$this->report = new Report('test'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->combineReport($cReport); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function pushRule(RuleInterface $rule) |
51
|
|
|
{ |
52
|
|
|
if ($this->acceptRule($rule)) { |
53
|
|
|
if (!in_array($rule, $this->rules)) { |
54
|
|
|
$this->rules[] = $rule; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the test report |
61
|
|
|
* |
62
|
|
|
* @return \Gabrieljmj\Should\Report\Report |
63
|
|
|
*/ |
64
|
|
|
public function getReport() |
65
|
|
|
{ |
66
|
|
|
return $this->report; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function combineReport(Report $report) |
70
|
|
|
{ |
71
|
|
|
$assertList = $report->getAssertList(); |
72
|
|
|
|
73
|
|
|
foreach ($assertList as $testType => $value) { |
74
|
|
View Code Duplication |
if (isset($assertList[$testType]['fail'])) { |
|
|
|
|
75
|
|
|
foreach ($assertList[$testType]['fail'] as $element => $fails) { |
76
|
|
|
foreach ($fails as $fail) { |
77
|
|
|
$this->report->addAssert($fail); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
if (isset($assertList[$testType]['success'])) { |
|
|
|
|
83
|
|
|
foreach ($assertList[$testType]['success'] as $element => $successes) { |
84
|
|
|
foreach ($successes as $success) { |
85
|
|
|
$this->report->addAssert($success); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
abstract protected function acceptRule(RuleInterface $rule); |
93
|
|
|
} |
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.