1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace hanneskod\readmetester; |
6
|
|
|
|
7
|
|
|
use hanneskod\readmetester\Expectation\ExpectationFactory; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Create examples from definitions created by Parser |
11
|
|
|
*/ |
12
|
|
|
class ExampleFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ExpectationFactory |
16
|
|
|
*/ |
17
|
|
|
private $expectationFactory; |
18
|
|
|
|
19
|
|
|
public function __construct(ExpectationFactory $expectationFactory) |
20
|
|
|
{ |
21
|
|
|
$this->expectationFactory = $expectationFactory; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create examples from definitions |
26
|
|
|
* |
27
|
|
|
* @param Definition[] $defenitions Example definitions as created by Parser |
28
|
|
|
* @return Example[] |
29
|
|
|
*/ |
30
|
|
|
public function createExamples(array $defenitions): array |
31
|
|
|
{ |
32
|
|
|
$examples = []; |
33
|
|
|
$context = null; |
34
|
|
|
|
35
|
|
|
foreach ($defenitions as $index => $def) { |
36
|
|
|
if ($def->isAnnotatedWith('ignore')) { |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$name = (string)($index + 1); |
41
|
|
|
|
42
|
|
|
if ($def->isAnnotatedWith('example')) { |
43
|
|
|
$name = $def->getAnnotation('example')->getArgument(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (isset($examples[$name])) { |
47
|
|
|
throw new \RuntimeException("Example '$name' already exists in definition ".($index + 1)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($context) { |
51
|
|
|
$def->getCodeBlock()->prepend($context); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($def->isAnnotatedWith('extends')) { |
55
|
|
|
$extends = $def->getAnnotation('extends')->getArgument(); |
56
|
|
|
if (!isset($examples[$extends])) { |
57
|
|
|
throw new \RuntimeException( |
58
|
|
|
"Example '$extends' does not exist and can not be extended in definition ".($index + 1) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$def->getCodeBlock()->prepend($examples[$extends]->getCodeBlock()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$expectations = $this->createExpectations($def); |
66
|
|
|
|
67
|
|
|
if (empty($expectations)) { |
68
|
|
|
$expectations[] = $this->expectationFactory->createExpectation(new Annotation('expectNothing')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$examples[$name] = new Example($name, $def->getCodeBlock(), $expectations); |
72
|
|
|
|
73
|
|
|
if ($def->isAnnotatedWith('exampleContext')) { |
74
|
|
|
$context = $def->getCodeBlock(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $examples; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create expectation from definition data |
84
|
|
|
* |
85
|
|
|
* @return Expectation\ExpectationInterface[] |
86
|
|
|
*/ |
87
|
|
|
private function createExpectations(Definition $def): array |
88
|
|
|
{ |
89
|
|
|
$expectations = []; |
90
|
|
|
|
91
|
|
|
foreach ($def->getAnnotations() as $annotation) { |
92
|
|
|
$expectations[] = $this->expectationFactory->createExpectation($annotation); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return array_filter($expectations); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|