1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GD\Helpers; |
4
|
|
|
|
5
|
|
|
use GD\Exceptions\TestFileExists; |
6
|
|
|
|
7
|
|
|
trait WritePHPUnitFile |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
protected $dusk_class_and_methods_string = ""; |
11
|
|
|
protected $write_destination_folder_path = ""; |
12
|
|
|
protected $write_class_name = ""; |
13
|
|
|
|
14
|
21 |
|
public function writeUnitTest($path, $name, $dusk_class_and_methods) |
15
|
|
|
{ |
16
|
|
|
|
17
|
21 |
|
$this->write_class_name = $name; |
18
|
|
|
|
19
|
21 |
|
$this->write_destination_folder_path = $path; |
20
|
|
|
|
21
|
21 |
|
$this->checkUnitFolder(); |
22
|
|
|
|
23
|
21 |
|
$this->convertDuskClassAndMethodsArrayToText($dusk_class_and_methods); |
24
|
|
|
|
25
|
21 |
|
$this->saveToFile(); |
26
|
21 |
|
} |
27
|
|
|
|
28
|
21 |
|
protected function checkUnitFolder() |
29
|
|
|
{ |
30
|
|
|
|
31
|
21 |
|
if (!$this->getFilesystem()->exists($this->write_destination_folder_path)) { |
|
|
|
|
32
|
|
|
$this->getFilesystem()->makeDirectory($this->write_destination_folder_path, 0777, true); |
|
|
|
|
33
|
|
|
} else { |
34
|
21 |
|
if ($this->getFilesystem()->exists($this->write_destination_folder_path . |
|
|
|
|
35
|
21 |
|
$this->getDuskTestName() . '.php')) { |
|
|
|
|
36
|
|
|
throw new TestFileExists(sprintf("The file %s already exists", $this->getDuskTestName() . '.php')); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
} |
39
|
21 |
|
} |
40
|
|
|
|
41
|
21 |
|
protected function convertDuskClassAndMethodsArrayToText($dusk_class_and_methods) |
42
|
|
|
{ |
43
|
|
|
|
44
|
21 |
|
$this->getAndSetHeaderArea(); |
45
|
|
|
|
46
|
21 |
|
foreach ($dusk_class_and_methods as $dusk_class_and_method_index => $dusk_class_and_method) { |
47
|
|
|
/** |
48
|
|
|
* Check if set |
49
|
|
|
*/ |
50
|
21 |
|
$this->addParentContent($dusk_class_and_method['parent']); |
51
|
|
|
|
52
|
21 |
|
$this->addSteps($dusk_class_and_method['steps']); |
53
|
14 |
|
} |
54
|
|
|
|
55
|
21 |
|
$this->getAndSetFooterArea(); |
56
|
21 |
|
} |
57
|
|
|
|
58
|
21 |
View Code Duplication |
protected function addParentContent($parent_function) |
|
|
|
|
59
|
|
|
{ |
60
|
21 |
|
$parent_base = __DIR__ . '/../../stubs/parent.txt'; |
61
|
21 |
|
$base = $this->getFilesystem()->get($parent_base); |
|
|
|
|
62
|
|
|
|
63
|
21 |
|
$base = str_replace("[PARENT_METHOD]", $parent_function, $base); |
64
|
|
|
|
65
|
21 |
|
$this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $base; |
66
|
21 |
|
} |
67
|
|
|
|
68
|
21 |
|
protected function addSteps(array $steps) |
69
|
|
|
{ |
70
|
21 |
|
$references = ""; |
71
|
21 |
|
$path = __DIR__ . '/../../stubs/step.txt'; |
72
|
21 |
|
$step_template = $this->getFilesystem()->get($path); |
|
|
|
|
73
|
|
|
|
74
|
21 |
|
foreach ($steps as $step) { |
75
|
21 |
|
$references = $references . $step['reference'] . ";\n "; |
76
|
21 |
|
$this->addNewStepToTest($step, $step_template); |
77
|
14 |
|
} |
78
|
|
|
|
79
|
21 |
|
$this->dusk_class_and_methods_string = |
80
|
21 |
|
str_replace("[STEPS_AREA]", $references, $this->dusk_class_and_methods_string); |
81
|
21 |
|
} |
82
|
|
|
|
83
|
21 |
|
protected function addNewStepToTest($step, $step_template) |
84
|
|
|
{ |
85
|
21 |
|
$method = sprintf("protected function %s()", $step['method_name']); |
86
|
21 |
|
$found = substr_count($this->dusk_class_and_methods_string, $method); |
87
|
21 |
|
if ($found < 1) { |
88
|
21 |
|
$content = str_replace("[STEP]", $step['method_name'], $step_template); |
89
|
21 |
|
$this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content; |
90
|
14 |
|
} |
91
|
21 |
|
} |
92
|
|
|
|
93
|
21 |
View Code Duplication |
protected function getAndSetHeaderArea() |
|
|
|
|
94
|
|
|
{ |
95
|
21 |
|
$path = __DIR__ . '/../../stubs/header.txt'; |
96
|
21 |
|
$content = $this->getFilesystem()->get($path); |
|
|
|
|
97
|
|
|
|
98
|
21 |
|
$content = str_replace("[TEST_NAME]", $this->write_class_name, $content); |
99
|
21 |
|
$this->dusk_class_and_methods_string = $content; |
100
|
21 |
|
} |
101
|
|
|
|
102
|
21 |
|
protected function getAndSetFooterArea() |
103
|
|
|
{ |
104
|
21 |
|
$path = __DIR__ . '/../../stubs/footer.txt'; |
105
|
21 |
|
$content = $this->getFilesystem()->get($path); |
|
|
|
|
106
|
|
|
|
107
|
21 |
|
$this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content; |
108
|
21 |
|
} |
109
|
|
|
|
110
|
3 |
|
public function getDuskClassAndMethodsString() |
111
|
|
|
{ |
112
|
3 |
|
return $this->dusk_class_and_methods_string; |
113
|
|
|
} |
114
|
|
|
|
115
|
21 |
|
protected function saveToFile() |
116
|
|
|
{ |
117
|
|
|
|
118
|
21 |
|
$this->getFilesystem()->put( |
|
|
|
|
119
|
21 |
|
sprintf("%s/%s.php", $this->write_destination_folder_path, $this->write_class_name), |
120
|
21 |
|
$this->dusk_class_and_methods_string |
121
|
14 |
|
); |
122
|
21 |
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.