1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace GD\Helpers; |
5
|
|
|
|
6
|
|
|
use GD\Exceptions\TestFileExists; |
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
8
|
|
|
|
9
|
|
|
abstract class WriteFileBase |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected $write_destination_folder_path = ""; |
13
|
|
|
protected $dusk_class_and_methods_string = ""; |
14
|
|
|
protected $write_class_name = ""; |
15
|
|
|
protected $spacing = ""; |
16
|
|
|
protected $existing_test_content; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Filesystem |
20
|
|
|
*/ |
21
|
|
|
protected $filesystem; |
22
|
|
|
protected $base; |
23
|
|
|
protected $step_template; |
24
|
|
|
protected $content; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function getWriteClassName() |
30
|
|
|
{ |
31
|
|
|
return $this->write_class_name; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return Filesystem |
37
|
|
|
*/ |
38
|
36 |
|
public function getFilesystem() |
39
|
|
|
{ |
40
|
36 |
|
if (!$this->filesystem) { |
41
|
36 |
|
$this->setFilesystem(); |
42
|
24 |
|
} |
43
|
36 |
|
return $this->filesystem; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param null $filesystem |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
36 |
|
public function setFilesystem($filesystem = null) |
51
|
|
|
{ |
52
|
36 |
|
if (!$filesystem) { |
53
|
36 |
|
$filesystem = new Filesystem(); |
54
|
24 |
|
} |
55
|
|
|
|
56
|
36 |
|
$this->filesystem = $filesystem; |
57
|
36 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
24 |
|
protected function convertDuskClassAndMethodsArrayToText($dusk_class_and_methods) |
61
|
|
|
{ |
62
|
|
|
|
63
|
24 |
|
$this->getAndSetHeaderArea(); |
64
|
|
|
|
65
|
24 |
|
foreach ($dusk_class_and_methods as $dusk_class_and_method_index => $dusk_class_and_method) { |
66
|
|
|
/** |
67
|
|
|
* Check if set |
68
|
|
|
*/ |
69
|
24 |
|
$this->addParentContent($dusk_class_and_method['parent']); |
70
|
|
|
|
71
|
24 |
|
$this->addSteps($dusk_class_and_method['steps']); |
72
|
16 |
|
} |
73
|
|
|
|
74
|
24 |
|
$this->getAndSetFooterArea(); |
75
|
24 |
|
} |
76
|
|
|
|
77
|
6 |
|
public function getDuskClassAndMethodsString() |
78
|
|
|
{ |
79
|
6 |
|
return $this->dusk_class_and_methods_string; |
80
|
|
|
} |
81
|
|
|
|
82
|
24 |
|
protected function getAndSetHeaderArea() |
83
|
|
|
{ |
84
|
24 |
|
$content = str_replace("[TEST_NAME]", $this->write_class_name, $this->content); |
85
|
24 |
|
$this->dusk_class_and_methods_string = $content; |
86
|
24 |
|
} |
87
|
|
|
|
88
|
|
|
abstract protected function getAndSetFooterArea(); |
89
|
|
|
|
90
|
33 |
|
protected function addSteps(array $steps) |
91
|
|
|
{ |
92
|
33 |
|
$references = ""; |
93
|
|
|
|
94
|
33 |
|
foreach ($steps as $index => $step) { |
95
|
33 |
|
$references = $references . $this->spacing . $step['reference'] . $this->notLastLine($steps, $index); |
96
|
33 |
|
$this->addNewStepToTest($step, $this->step_template); |
97
|
22 |
|
} |
98
|
|
|
|
99
|
33 |
|
$this->dusk_class_and_methods_string = |
100
|
33 |
|
str_replace("[STEPS_AREA]", $references, $this->dusk_class_and_methods_string); |
101
|
33 |
|
} |
102
|
|
|
|
103
|
33 |
|
protected function notLastLine(array $steps, $index) |
104
|
|
|
{ |
105
|
33 |
|
if (($index + 1) < count($steps)) { |
106
|
33 |
|
return ";\n "; |
107
|
|
|
} |
108
|
|
|
|
109
|
33 |
|
return ";"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
33 |
|
protected function addParentContent($parent_function) |
114
|
|
|
{ |
115
|
33 |
|
$base = str_replace("[PARENT_METHOD]", $parent_function, $this->base); |
116
|
|
|
|
117
|
33 |
|
$this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $base; |
118
|
33 |
|
} |
119
|
|
|
|
120
|
24 |
|
protected function addNewStepToTest($step, $step_template) |
121
|
|
|
{ |
122
|
24 |
|
$method = sprintf("protected function %s()", $step['method_name']); |
123
|
24 |
|
$found = substr_count($this->dusk_class_and_methods_string, $method); |
124
|
|
|
|
125
|
24 |
View Code Duplication |
if ($found < 1) { |
|
|
|
|
126
|
24 |
|
$content = str_replace("[STEP]", $step['method_name'], $step_template); |
127
|
24 |
|
$this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content; |
128
|
16 |
|
} |
129
|
24 |
|
} |
130
|
|
|
|
131
|
24 |
|
protected function checkDestinationTestFolder() |
132
|
|
|
{ |
133
|
|
|
|
134
|
24 |
|
if (!$this->getFilesystem()->exists($this->write_destination_folder_path)) { |
135
|
|
|
$this->getFilesystem()->makeDirectory($this->write_destination_folder_path, 0777, true); |
136
|
|
|
} else { |
137
|
24 |
|
if ($this->getFilesystem()->exists($this->write_destination_folder_path . |
138
|
24 |
|
$this->write_class_name . '.php')) { |
139
|
|
|
throw new TestFileExists(sprintf("The file %s already exists", $this->write_class_name . '.php')); |
140
|
|
|
} |
141
|
|
|
} |
142
|
24 |
|
} |
143
|
|
|
|
144
|
33 |
|
protected function saveToFile() |
145
|
|
|
{ |
146
|
33 |
|
$this->getFilesystem()->put( |
147
|
33 |
|
sprintf("%s/%s.php", $this->write_destination_folder_path, $this->write_class_name), |
148
|
33 |
|
$this->dusk_class_and_methods_string |
149
|
22 |
|
); |
150
|
33 |
|
} |
151
|
|
|
|
152
|
9 |
|
public function getExistingTestContent() |
153
|
|
|
{ |
154
|
|
|
|
155
|
9 |
|
if (!$this->existing_test_content) { |
156
|
|
|
$this->setExistingTestContent(); |
157
|
|
|
} |
158
|
|
|
|
159
|
9 |
|
return $this->existing_test_content; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param mixed $existing_test_content |
164
|
|
|
*/ |
165
|
9 |
|
public function setExistingTestContent($existing_test_content = null) |
166
|
|
|
{ |
167
|
9 |
|
if (!$existing_test_content) { |
168
|
|
|
$file_to_append = $this->write_destination_folder_path . '/' . $this->write_class_name . '.php'; |
169
|
|
|
$content = $this->getFilesystem()->get($file_to_append); |
170
|
|
|
$this->existing_test_content = $content; |
171
|
|
|
} |
172
|
|
|
|
173
|
9 |
|
$this->existing_test_content = $existing_test_content; |
174
|
9 |
|
} |
175
|
|
|
} |
176
|
|
|
|
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.