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 . $this->getDuskTestName() . '.php')) { |
|
|
|
|
35
|
|
|
throw new TestFileExists(sprintf("The file %s already exists", $this->getDuskTestName() . '.php')); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
} |
38
|
21 |
|
} |
39
|
|
|
|
40
|
21 |
|
protected function convertDuskClassAndMethodsArrayToText($dusk_class_and_methods) |
41
|
|
|
{ |
42
|
|
|
|
43
|
21 |
|
$this->getAndSetHeaderArea(); |
44
|
|
|
|
45
|
21 |
|
foreach ($dusk_class_and_methods as $dusk_class_and_method_index => $dusk_class_and_method) { |
46
|
|
|
/** |
47
|
|
|
* Check if set |
48
|
|
|
*/ |
49
|
21 |
|
$this->addParentContent($dusk_class_and_method['parent']); |
50
|
|
|
|
51
|
21 |
|
$this->addSteps($dusk_class_and_method['steps']); |
52
|
14 |
|
} |
53
|
|
|
|
54
|
21 |
|
$this->getAndSetFooterArea(); |
55
|
21 |
|
} |
56
|
|
|
|
57
|
21 |
View Code Duplication |
protected function addParentContent($parent_function) |
|
|
|
|
58
|
|
|
{ |
59
|
21 |
|
$parent_base = __DIR__ . '/../../stubs/parent.txt'; |
60
|
21 |
|
$base = $this->getFilesystem()->get($parent_base); |
|
|
|
|
61
|
|
|
|
62
|
21 |
|
$base = str_replace("[PARENT_METHOD]", $parent_function, $base); |
63
|
|
|
|
64
|
21 |
|
$this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $base; |
65
|
21 |
|
} |
66
|
|
|
|
67
|
21 |
|
protected function addSteps(array $steps) |
68
|
|
|
{ |
69
|
21 |
|
$references = ""; |
70
|
21 |
|
$path = __DIR__ . '/../../stubs/step.txt'; |
71
|
21 |
|
$step_template = $this->getFilesystem()->get($path); |
|
|
|
|
72
|
|
|
|
73
|
21 |
|
foreach ($steps as $step) { |
74
|
21 |
|
$references = $references . $step['reference'] . ";\n "; |
75
|
21 |
|
$this->addNewStepToTest($step, $step_template); |
76
|
14 |
|
} |
77
|
|
|
|
78
|
21 |
|
$this->dusk_class_and_methods_string = str_replace("[STEPS_AREA]", $references, $this->dusk_class_and_methods_string); |
79
|
21 |
|
} |
80
|
|
|
|
81
|
21 |
|
protected function addNewStepToTest($step, $step_template) |
82
|
|
|
{ |
83
|
21 |
|
if (substr_count($this->dusk_class_and_methods_string, sprintf("protected function %s()", $step['method_name'])) < 1) { |
84
|
21 |
|
$content = str_replace("[STEP]", $step['method_name'], $step_template); |
85
|
21 |
|
$this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content; |
86
|
14 |
|
} |
87
|
21 |
|
} |
88
|
|
|
|
89
|
21 |
View Code Duplication |
protected function getAndSetHeaderArea() |
|
|
|
|
90
|
|
|
{ |
91
|
21 |
|
$path = __DIR__ . '/../../stubs/header.txt'; |
92
|
21 |
|
$content = $this->getFilesystem()->get($path); |
|
|
|
|
93
|
|
|
|
94
|
21 |
|
$content = str_replace("[TEST_NAME]", $this->write_class_name, $content); |
95
|
21 |
|
$this->dusk_class_and_methods_string = $content; |
96
|
21 |
|
} |
97
|
|
|
|
98
|
21 |
|
protected function getAndSetFooterArea() |
99
|
|
|
{ |
100
|
21 |
|
$path = __DIR__ . '/../../stubs/footer.txt'; |
101
|
21 |
|
$content = $this->getFilesystem()->get($path); |
|
|
|
|
102
|
|
|
|
103
|
21 |
|
$this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content; |
104
|
21 |
|
} |
105
|
|
|
|
106
|
3 |
|
public function getDuskClassAndMethodsString() |
107
|
|
|
{ |
108
|
3 |
|
return $this->dusk_class_and_methods_string; |
109
|
|
|
} |
110
|
|
|
|
111
|
21 |
|
protected function saveToFile() |
112
|
|
|
{ |
113
|
|
|
|
114
|
21 |
|
$this->getFilesystem()->put( |
|
|
|
|
115
|
21 |
|
sprintf("%s/%s.php", $this->write_destination_folder_path, $this->write_class_name), |
116
|
21 |
|
$this->dusk_class_and_methods_string |
117
|
14 |
|
); |
118
|
21 |
|
} |
119
|
|
|
} |
120
|
|
|
|
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.