1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SmartWeb\ModuleTesting\Generator; |
5
|
|
|
|
6
|
|
|
use Codeception\Configuration; |
7
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
8
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use SmartWeb\ModuleTesting\BaseUnitTest; |
11
|
|
|
use SmartWeb\ModuleTesting\Codeception\SetupInterface; |
12
|
|
|
use SmartWeb\ModuleTesting\Codeception\Template\TemplateFactory; |
13
|
|
|
use const DIRECTORY_SEPARATOR; |
14
|
|
|
use function array_merge; |
15
|
|
|
use function class_basename; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class TestGenerator |
20
|
|
|
* |
21
|
|
|
* @package SmartWeb\Testing\Generator |
22
|
|
|
*/ |
23
|
|
|
class TestGenerator |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Filesystem |
28
|
|
|
*/ |
29
|
|
|
protected $disk; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var SetupInterface |
33
|
|
|
*/ |
34
|
|
|
protected $setup; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $suite; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $name; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $settings; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $templatePath; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* TestGenerator constructor. |
58
|
|
|
* |
59
|
|
|
* @param SetupInterface $setup |
60
|
|
|
* @param string $suite |
61
|
|
|
* @param string $name |
62
|
|
|
* @param array $settings |
63
|
|
|
* |
64
|
|
|
* @throws \Codeception\Exception\ConfigurationException |
65
|
|
|
* @throws \Exception |
66
|
|
|
*/ |
67
|
|
|
public function __construct(SetupInterface $setup, string $suite, string $name, array $settings = []) |
68
|
|
|
{ |
69
|
|
|
$this->setup = $setup; |
70
|
|
|
$this->disk = $setup->getDisk(); |
71
|
|
|
$this->suite = $suite; |
72
|
|
|
$this->name = $this->prepareName($name); |
73
|
|
|
$this->settings = $this->prepareSettings($settings); |
74
|
|
|
$this->templatePath = 'test.' . Str::lower($this->suite); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $settings |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
* @throws \Codeception\Exception\ConfigurationException |
82
|
|
|
* @throws \Exception |
83
|
|
|
*/ |
84
|
|
|
protected function prepareSettings(array $settings) : array |
85
|
|
|
{ |
86
|
|
|
|
87
|
|
|
$suiteSettings = $this->getSuiteSettings($this->suite); |
88
|
|
|
$moduleSettings = $this->getSettingsForModule(); |
89
|
|
|
|
90
|
|
|
$baseSettings = [ |
91
|
|
|
'namespace' => $suiteSettings['namespace'], |
92
|
|
|
'baseClass' => BaseUnitTest::class, |
93
|
|
|
'baseClassShort' => 'BaseUnitTest', |
94
|
|
|
'name' => class_basename($this->name), |
95
|
|
|
'group' => 'exampleGroup', |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$actor = ($suiteSettings['actor'] ?? $moduleSettings['actor']) ?? 'Tester'; |
99
|
|
|
$baseSettings['actorClass'] = $this->suite . '\\' . $actor; |
100
|
|
|
|
101
|
|
|
return array_merge($baseSettings, $settings); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $suite |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
* @throws \Exception |
109
|
|
|
*/ |
110
|
|
|
protected function getSuiteSettings(string $suite) : array |
111
|
|
|
{ |
112
|
|
|
return Configuration::suiteSettings($suite, $this->getSettingsForModule()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array |
117
|
|
|
* @throws \Codeception\Exception\ConfigurationException |
118
|
|
|
*/ |
119
|
|
|
protected function getSettingsForModule() |
120
|
|
|
{ |
121
|
|
|
return Configuration::config($this->setup->getWorkDir()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @throws FileNotFoundException |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function generate() |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$factory = new TemplateFactory(); |
130
|
|
|
$fileContents = $factory->getTemplate($this->templatePath)->produceWith($this->settings); |
131
|
|
|
|
132
|
|
|
$path = $this->suite . DIRECTORY_SEPARATOR . $this->name . 'Test.php'; |
133
|
|
|
|
134
|
|
|
$this->createFile($path, $fileContents); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $name |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
protected function prepareName(string $name) : string |
143
|
|
|
{ |
144
|
|
|
return Str::endsWith($name, 'Test') |
145
|
|
|
? Str::replaceLast('Test', '', $name) |
146
|
|
|
: $name; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $path |
151
|
|
|
* @param $contents |
152
|
|
|
* |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
View Code Duplication |
protected function createFile(string $path, $contents) : bool |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$path = $this->getPath($path); |
158
|
|
|
|
159
|
|
|
if (!$this->disk->exists($path)) { |
160
|
|
|
return $this->disk->put($path, $contents); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $path |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
protected function getPath(string $path) : string |
172
|
|
|
{ |
173
|
|
|
return $this->setup->getWorkDir() . DIRECTORY_SEPARATOR . $this->setup->getTestsDir() . DIRECTORY_SEPARATOR . $path; |
174
|
|
|
} |
175
|
|
|
} |
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.