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