44
55namespace Codeception \Module ;
66
7- use Codeception \Configuration ;
7+ use Codeception \Configuration as CodeceptConfig ;
88use Codeception \Exception \ModuleConfigException ;
99use Codeception \Lib \Connector \Laravel as LaravelConnector ;
1010use Codeception \Lib \Framework ;
2121use Codeception \Module \Laravel \InteractsWithSession ;
2222use Codeception \Module \Laravel \InteractsWithViews ;
2323use Codeception \Module \Laravel \MakesHttpRequests ;
24+ use Codeception \Module \Laravel \ServicesTrait ;
2425use Codeception \Subscriber \ErrorHandler ;
2526use Codeception \TestInterface ;
2627use Codeception \Util \ReflectionHelper ;
27- use Exception ;
2828use Illuminate \Database \Connection ;
2929use Illuminate \Database \DatabaseManager ;
30- use Illuminate \Database \Eloquent \Factory ;
3130use Illuminate \Foundation \Application ;
3231use Illuminate \Routing \Route ;
3332use ReflectionException ;
33+ use Symfony \Component \Routing \CompiledRoute as SymfonyCompiledRoute ;
34+ use Throwable ;
3435
3536/**
3637 *
@@ -134,18 +135,24 @@ class Laravel extends Framework implements ActiveRecord, PartedModule
134135 use InteractsWithSession;
135136 use InteractsWithViews;
136137 use MakesHttpRequests;
138+ use ServicesTrait;
137139
138140 /**
139141 * @var Application
140142 */
141143 public $ app ;
142144
145+ /**
146+ * @var LaravelConnector
147+ */
148+ public $ client ;
149+
143150 /**
144151 * @var array
145152 */
146153 public $ config = [];
147154
148- public function __construct (ModuleContainer $ container , ?array $ config = null )
155+ public function __construct (ModuleContainer $ moduleContainer , ?array $ config = null )
149156 {
150157 $ this ->config = array_merge (
151158 [
@@ -167,15 +174,18 @@ public function __construct(ModuleContainer $container, ?array $config = null)
167174 (array )$ config
168175 );
169176
170- $ projectDir = explode ($ this ->config ['packages ' ], Configuration ::projectDir ())[0 ];
177+ $ projectDir = explode ($ this ->config ['packages ' ], CodeceptConfig ::projectDir ())[0 ];
171178 $ projectDir .= $ this ->config ['root ' ];
172179
173180 $ this ->config ['project_dir ' ] = $ projectDir ;
174181 $ this ->config ['bootstrap_file ' ] = $ projectDir . $ this ->config ['bootstrap ' ];
175182
176- parent ::__construct ($ container );
183+ parent ::__construct ($ moduleContainer );
177184 }
178185
186+ /**
187+ * @return string[]
188+ */
179189 public function _parts (): array
180190 {
181191 return ['orm ' ];
@@ -194,8 +204,7 @@ public function _initialize()
194204 /**
195205 * Before hook.
196206 *
197- * @param TestInterface $test
198- * @throws Exception
207+ * @throws Throwable
199208 */
200209 public function _before (TestInterface $ test )
201210 {
@@ -207,7 +216,7 @@ public function _before(TestInterface $test)
207216 }
208217
209218 if ($ this ->applicationUsesDatabase () && $ this ->config ['cleanup ' ]) {
210- $ this ->app [ ' db ' ] ->beginTransaction ();
219+ $ this ->getDb () ->beginTransaction ();
211220 $ this ->debugSection ('Database ' , 'Transaction started ' );
212221 }
213222
@@ -219,13 +228,12 @@ public function _before(TestInterface $test)
219228 /**
220229 * After hook.
221230 *
222- * @param TestInterface $test
223- * @throws Exception
231+ * @throws Throwable
224232 */
225233 public function _after (TestInterface $ test )
226234 {
227235 if ($ this ->applicationUsesDatabase ()) {
228- $ db = $ this ->app [ ' db ' ] ;
236+ $ db = $ this ->getDb () ;
229237
230238 if ($ db instanceof DatabaseManager) {
231239 if ($ this ->config ['cleanup ' ]) {
@@ -245,33 +253,31 @@ public function _after(TestInterface $test)
245253
246254 // Remove references to Faker in factories to prevent memory leak
247255 unset($ this ->app [\Faker \Generator::class]);
248- unset($ this ->app [Factory::class]);
256+ unset($ this ->app [\ Illuminate \ Database \ Eloquent \ Factory::class]);
249257 }
250258 }
251259
252260 /**
253261 * Does the application use the database?
254- *
255- * @return bool
256262 */
257263 private function applicationUsesDatabase (): bool
258264 {
259- return ! empty ($ this ->app [ ' config ' ] ['database.default ' ]);
265+ return ! empty ($ this ->getConfig () ['database.default ' ]);
260266 }
261267
262268 /**
263269 * Make sure the Laravel bootstrap file exists.
264270 *
265271 * @throws ModuleConfigException
266272 */
267- protected function checkBootstrapFileExists (): void
273+ private function checkBootstrapFileExists (): void
268274 {
269275 $ bootstrapFile = $ this ->config ['bootstrap_file ' ];
270276
271277 if (!file_exists ($ bootstrapFile )) {
272278 throw new ModuleConfigException (
273279 $ this ,
274- "Laravel bootstrap file not found in $ bootstrapFile. \n"
280+ "Laravel bootstrap file not found in { $ bootstrapFile} . \n"
275281 . "Please provide a valid path by using the 'bootstrap' config param. "
276282 );
277283 }
@@ -280,7 +286,7 @@ protected function checkBootstrapFileExists(): void
280286 /**
281287 * Register Laravel autoloaders.
282288 */
283- protected function registerAutoloaders (): void
289+ private function registerAutoloaders (): void
284290 {
285291 require $ this ->config ['project_dir ' ] . $ this ->config ['vendor_dir ' ] . DIRECTORY_SEPARATOR . 'autoload.php ' ;
286292 }
@@ -289,24 +295,25 @@ protected function registerAutoloaders(): void
289295 * Revert back to the Codeception error handler,
290296 * because Laravel registers it's own error handler.
291297 */
292- protected function revertErrorHandler (): void
298+ private function revertErrorHandler (): void
293299 {
294- $ handler = new ErrorHandler ();
295- set_error_handler ([$ handler , 'errorHandler ' ]);
300+ $ errorHandler = new ErrorHandler ();
301+ set_error_handler ([$ errorHandler , 'errorHandler ' ]);
296302 }
297303
298304 /**
299305 * Returns a list of recognized domain names.
300306 * This elements of this list are regular expressions.
301307 *
302- * @return array
303308 * @throws ReflectionException
309+ * @return string[]
304310 */
305311 protected function getInternalDomains (): array
306312 {
307313 $ internalDomains = [$ this ->getApplicationDomainRegex ()];
308314
309- foreach ($ this ->app ['routes ' ] as $ route ) {
315+ /** @var Route $route */
316+ foreach ($ this ->getRoutes () as $ route ) {
310317 if (!is_null ($ route ->domain ())) {
311318 $ internalDomains [] = $ this ->getDomainRegex ($ route );
312319 }
@@ -330,13 +337,12 @@ private function getApplicationDomainRegex(): string
330337 /**
331338 * Get the regex for matching the domain part of this route.
332339 *
333- * @param Route $route
334- * @return string
335340 * @throws ReflectionException
336341 */
337- private function getDomainRegex (Route $ route )
342+ private function getDomainRegex (Route $ route ): string
338343 {
339344 ReflectionHelper::invokePrivateMethod ($ route , 'compileRoute ' );
345+ /** @var SymfonyCompiledRoute $compiledRoute */
340346 $ compiledRoute = ReflectionHelper::readPrivateProperty ($ route , 'compiled ' );
341347
342348 return $ compiledRoute ->getHostRegex ();
0 commit comments