|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Renderer; |
|
4
|
|
|
|
|
5
|
|
|
use PhpUnitGen\Configuration\ConfigurationInterface\ConfigInterface; |
|
6
|
|
|
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface; |
|
7
|
|
|
use PhpUnitGen\Renderer\RendererInterface\PhpFileRendererInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class PhpFileRenderer. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Paul Thébaud <[email protected]>. |
|
13
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
|
14
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
|
15
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
|
16
|
|
|
* @since Class available since Release 2.0.0. |
|
17
|
|
|
*/ |
|
18
|
|
|
class PhpFileRenderer extends AbstractPhpRenderer implements PhpFileRendererInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ClassHeaderRenderer $classHeaderRenderer The class header renderer. |
|
22
|
|
|
*/ |
|
23
|
|
|
private $classHeaderRenderer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ClassLikeRenderer $classLikeRenderer The interface renderer. |
|
27
|
|
|
*/ |
|
28
|
|
|
private $classLikeRenderer; |
|
29
|
|
|
|
|
30
|
|
|
private $functionRenderer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
* @param ClassHeaderRenderer $classHeaderRenderer The class header renderer. |
|
35
|
|
|
* @param ClassLikeRenderer $classLikeRenderer The interface renderer. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
ConfigInterface $config, |
|
39
|
|
|
ClassHeaderRenderer $classHeaderRenderer, |
|
40
|
|
|
ClassLikeRenderer $classLikeRenderer, |
|
41
|
|
|
FunctionRenderer $functionRenderer |
|
42
|
|
|
) { |
|
43
|
|
|
parent::__construct($config); |
|
44
|
|
|
|
|
45
|
|
|
$this->classHeaderRenderer = $classHeaderRenderer; |
|
46
|
|
|
$this->classLikeRenderer = $classLikeRenderer; |
|
47
|
|
|
$this->functionRenderer = $functionRenderer; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function invoke(PhpFileModelInterface $phpFile): string |
|
54
|
|
|
{ |
|
55
|
|
|
// Php tag |
|
56
|
|
|
$this->begin()->add('<?php')->add(); |
|
57
|
|
|
|
|
58
|
|
|
// Namespace |
|
59
|
|
|
$namespace = $phpFile->getNamespaceString(); |
|
60
|
|
|
$this->add(sprintf('namespace Test%s;', ($namespace === null? '' : '\\' . $namespace)))->add(); |
|
61
|
|
|
|
|
62
|
|
|
// Uses |
|
63
|
|
|
$this->multiple($this->getUseLines($phpFile))->add(); |
|
64
|
|
|
|
|
65
|
|
|
// Phpdoc |
|
66
|
|
|
$this->doc($this->getClassDoc($phpFile)); |
|
67
|
|
|
|
|
68
|
|
|
// Class |
|
69
|
|
|
$this->add(sprintf('class %s extends TestCase', $phpFile->getName())); |
|
70
|
|
|
|
|
71
|
|
|
// Content |
|
72
|
|
|
$this->add('{')->addContent($phpFile)->add('}'); |
|
73
|
|
|
|
|
74
|
|
|
return $this->get(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get php use statements. |
|
79
|
|
|
* |
|
80
|
|
|
* @param PhpFileModelInterface $phpFile The php file to use. |
|
81
|
|
|
* |
|
82
|
|
|
* @return array The use statements as an array. |
|
83
|
|
|
*/ |
|
84
|
|
|
private function getUseLines(PhpFileModelInterface $phpFile): array |
|
85
|
|
|
{ |
|
86
|
|
|
$lines = ['use PHPUnit\Framework\TestCase;']; |
|
87
|
|
|
foreach ($phpFile->getConcreteUses() as $use => $name) { |
|
88
|
|
|
// Get the last name part |
|
89
|
|
|
$nameArray = explode('\\', $use); |
|
90
|
|
|
$lastPart = end($nameArray); |
|
91
|
|
|
if ($lastPart === $name) { |
|
92
|
|
|
$lines[] = sprintf('use %s;', $use); |
|
93
|
|
|
} else { |
|
94
|
|
|
$lines[] = sprintf('use %s as %s;', $use, $name); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
return $lines; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get test class phpdoc statements. |
|
102
|
|
|
* |
|
103
|
|
|
* @param PhpFileModelInterface $phpFile The php file to use. |
|
104
|
|
|
* |
|
105
|
|
|
* @return array The phpdoc lines as an array. |
|
106
|
|
|
*/ |
|
107
|
|
|
private function getClassDoc(PhpFileModelInterface $phpFile): array |
|
108
|
|
|
{ |
|
109
|
|
|
$lines = [sprintf('Class %s.', $phpFile->getName()), '']; |
|
110
|
|
|
foreach ($this->config->getPhpDoc() as $annotation => $content) { |
|
111
|
|
|
$lines[] = sprintf('@%s %s', $annotation, $content); |
|
112
|
|
|
} |
|
113
|
|
|
$lines[] = ''; |
|
114
|
|
|
|
|
115
|
|
|
foreach ($phpFile->getInterfaces() as $elem) { |
|
116
|
|
|
$lines[] = sprintf('@covers \\%s', $phpFile->getFullNameFor($elem->getName())); |
|
117
|
|
|
} |
|
118
|
|
|
foreach ($phpFile->getTraits() as $elem) { |
|
119
|
|
|
$lines[] = sprintf('@covers \\%s', $phpFile->getFullNameFor($elem->getName())); |
|
120
|
|
|
} |
|
121
|
|
|
foreach ($phpFile->getClasses() as $elem) { |
|
122
|
|
|
$lines[] = sprintf('@covers \\%s', $phpFile->getFullNameFor($elem->getName())); |
|
123
|
|
|
} |
|
124
|
|
|
return $lines; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Add the class content. |
|
129
|
|
|
* |
|
130
|
|
|
* @param PhpFileModelInterface $phpFile The php file to use. |
|
131
|
|
|
* |
|
132
|
|
|
* @return AbstractPhpRenderer $this. |
|
133
|
|
|
*/ |
|
134
|
|
|
private function addContent(PhpFileModelInterface $phpFile): AbstractPhpRenderer |
|
135
|
|
|
{ |
|
136
|
|
|
$this->concat($this->classHeaderRenderer->invoke($phpFile)); |
|
137
|
|
|
|
|
138
|
|
|
foreach ($phpFile->getFunctions() as $function) { |
|
139
|
|
|
$this->concat($this->functionRenderer->invoke($function)); |
|
140
|
|
|
} |
|
141
|
|
|
foreach ($phpFile->getInterfaces() as $interface) { |
|
142
|
|
|
$this->concat($this->classLikeRenderer->invoke($interface)); |
|
143
|
|
|
} |
|
144
|
|
|
foreach ($phpFile->getTraits() as $trait) { |
|
145
|
|
|
$this->concat($this->classLikeRenderer->invoke($trait)); |
|
146
|
|
|
} |
|
147
|
|
|
foreach ($phpFile->getClasses() as $class) { |
|
148
|
|
|
$this->concat($this->classLikeRenderer->invoke($class)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$this->remove(); |
|
152
|
|
|
|
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|