1
+ <?php
2
+
3
+ namespace Vigneshc91 \LaravelTestGenerator ;
4
+
5
+ class Formatter
6
+ {
7
+ protected $ cases ;
8
+
9
+ protected $ file ;
10
+
11
+ protected $ namespace ;
12
+
13
+ protected $ destinationFilePath ;
14
+
15
+ protected $ directory ;
16
+
17
+ protected $ sync ;
18
+
19
+ /**
20
+ * Initiate the options
21
+ *
22
+ * @param string $directory
23
+ * @param boolean $sync
24
+ */
25
+ public function __construct ($ directory , $ sync )
26
+ {
27
+ $ this ->directory = $ directory ;
28
+ $ this ->sync = $ sync ;
29
+ $ this ->file = __DIR__ .'/Test/UserTest.php ' ;
30
+ $ this ->namespace = 'namespace Tests\Feature ' . ($ this ->directory ? '\\' . $ this ->directory : '' ) . '; ' ;
31
+ $ this ->destinationFilePath = base_path ('tests/Feature/ ' . $ this ->directory );
32
+ $ this ->cases = [];
33
+ }
34
+
35
+ /**
36
+ * Format the test case in the controller
37
+ *
38
+ * @param array $case
39
+ * @param string $url
40
+ * @param string $method
41
+ * @param string $controllerName
42
+ * @param string $actionName
43
+ * @return void
44
+ */
45
+ public function format ($ case , $ url , $ method , $ controllerName , $ actionName , $ auth )
46
+ {
47
+ $ this ->cases [$ controllerName ]['action ' ] = $ actionName ;
48
+ $ this ->cases [$ controllerName ]['url ' ] = $ url ;
49
+ $ this ->cases [$ controllerName ]['method ' ] = $ method ;
50
+ $ this ->cases [$ controllerName ]['params ' ] = $ case ;
51
+ $ this ->cases [$ controllerName ]['auth ' ] = $ auth ;
52
+ if (empty ($ this ->cases [$ controllerName ]['function ' ])) {
53
+ $ this ->cases [$ controllerName ]['function ' ] = [];
54
+ }
55
+ $ this ->formatFunction ($ controllerName );
56
+ }
57
+
58
+ /**
59
+ * Generate the files for all the test cases
60
+ *
61
+ * @return void
62
+ */
63
+ public function generate ()
64
+ {
65
+ $ this ->createDirectory ();
66
+ $ this ->formatFile ();
67
+ }
68
+
69
+ /**
70
+ * Set the function for success and failure case
71
+ *
72
+ * @return void
73
+ */
74
+ protected function formatFunction ($ controllerName )
75
+ {
76
+ $ functionName = '' ;
77
+ $ i = 0 ;
78
+ $ controller = $ this ->cases [$ controllerName ];
79
+
80
+ foreach ($ controller ['params ' ] as $ index => $ item ) {
81
+ # Add function documentation
82
+ $ function = "\t" . '/** ' . PHP_EOL . "\t" . ' * ' . $ controller ['action ' ] . PHP_EOL . "\t" . ' * ' . PHP_EOL ;
83
+
84
+ # Check @depends to be added or not
85
+ if ($ this ->sync ) {
86
+ if ($ i > 0 ) {
87
+ $ function .= "\t" . ' * @depends ' . $ functionName . PHP_EOL ;
88
+ } else {
89
+ if (count ($ controller ['function ' ]) > 0 ) {
90
+ $ function .= "\t" . ' * @depends ' . end ($ controller ['function ' ])['name ' ] . PHP_EOL ;
91
+ }
92
+ }
93
+ }
94
+
95
+ $ function .= "\t" . ' * @return void ' . PHP_EOL . "\t" . ' */ ' . PHP_EOL ;
96
+ $ functionName = $ this ->getFunctionName ($ index , $ controller ['action ' ]);
97
+
98
+ # Function name and declaration
99
+ $ function .= "\t" . 'public function ' . $ functionName . '() ' ;
100
+
101
+ # Function definition
102
+ $ body = "\t\t" .'$response = $this->json( \'' . strtoupper ($ controller ['method ' ]) . '\', \'' . $ controller ['url ' ] . '\', [ ' ;
103
+
104
+ # Request parameters
105
+ $ params = $ this ->getParams ($ item );
106
+ $ body .= $ params ? PHP_EOL . $ params . PHP_EOL . "\t\t" . '] ' : '] ' ;
107
+
108
+ $ body .= $ controller ['auth ' ] ? ", [ \n\t\t\t'Authorization' => 'Bearer ' \n\t\t] " : '' ;
109
+
110
+ $ body .= '); ' ;
111
+ # Assert response
112
+ $ body .= PHP_EOL . PHP_EOL . "\t\t" . '$response->assertStatus( ' . ($ index == 'failure ' ? '400 ' : '200 ' ) . '); ' . PHP_EOL ;
113
+
114
+ # Add the function to the global array
115
+ $ this ->cases [$ controllerName ]['function ' ][] = [
116
+ 'name ' => $ functionName ,
117
+ 'code ' => $ function . PHP_EOL . "\t" . '{ ' . PHP_EOL . $ body . PHP_EOL . "\t" . '} ' . PHP_EOL
118
+ ];
119
+
120
+ $ i ++;
121
+ }
122
+
123
+ }
124
+
125
+ /**
126
+ * Format the test cases for the writing to the file
127
+ *
128
+ * @return void
129
+ */
130
+ protected function formatFile ()
131
+ {
132
+ foreach ($ this ->cases as $ key => $ value ) {
133
+ $ lines = file ($ this ->file , FILE_IGNORE_NEW_LINES );
134
+ $ lines [2 ] = $ this ->namespace ;
135
+ $ lines [8 ] = $ this ->getClassName ($ key , $ lines [8 ]);
136
+ $ functions = implode (PHP_EOL , array_pluck ($ value ['function ' ], 'code ' ));
137
+ $ content = array_merge (array_slice ($ lines , 0 , 10 ) , [$ functions ] , array_slice ($ lines , 11 ));
138
+
139
+ $ this ->writeToFile ($ key . 'Test ' , $ content );
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Write the string into the file
145
+ *
146
+ * @param string $controllerName
147
+ * @param string $rule
148
+ * @return void
149
+ */
150
+ protected function writeToFile ($ controllerName , $ content )
151
+ {
152
+ $ fileName = $ this ->destinationFilePath . '/ ' . $ controllerName . '.php ' ;
153
+ $ file = fopen ($ fileName , 'w ' );
154
+ foreach ($ content as $ index => $ value ) {
155
+ fwrite ($ file , $ value .PHP_EOL );
156
+ }
157
+ fclose ($ file );
158
+
159
+ echo "\033[32m " . basename ($ fileName ). ' Created Successfully ' . PHP_EOL ;
160
+ }
161
+
162
+ /**
163
+ * Get the class name from the controller name
164
+ *
165
+ * @param string $controllerName
166
+ * @param string $line
167
+ * @return string
168
+ */
169
+ protected function getClassName ($ controllerName , $ line )
170
+ {
171
+ return str_replace ('UserTest ' , $ controllerName . 'Test ' , $ line );
172
+ }
173
+
174
+ /**
175
+ * Get the request parameters string array format for printing in the file
176
+ *
177
+ * @param array $param
178
+ * @return string
179
+ */
180
+ protected function getParams ($ param )
181
+ {
182
+ if (empty ($ param )) {
183
+ return '' ;
184
+ }
185
+ $ param = json_encode ($ param );
186
+ $ param = str_replace (['{ ' , '} ' ], '' , $ param );
187
+ $ param = "\t\t\t" .$ param ;
188
+ $ param = str_replace ('": ' , '" => ' , $ param );
189
+ $ param = str_replace (', ' , ", \n\t\t\t" , $ param );
190
+ return $ param ;
191
+ }
192
+
193
+ /**
194
+ * Get the name of the test case function
195
+ *
196
+ * @param string $index
197
+ * @param string $action
198
+ * @return string
199
+ */
200
+ protected function getFunctionName ($ index , $ action )
201
+ {
202
+ $ name = 'test ' . $ action ;
203
+ return $ index == 'failure ' ? $ name . 'WithError ' : $ name ;
204
+ }
205
+
206
+ /**
207
+ * Create a new directory if not exist
208
+ *
209
+ * @return void
210
+ */
211
+ protected function createDirectory ()
212
+ {
213
+ $ dirName = $ this ->destinationFilePath ;
214
+ if (!is_dir ($ dirName )) {
215
+ mkdir ($ dirName , 0755 , true );
216
+ }
217
+ }
218
+ }
0 commit comments