Skip to content

Commit 0a65a10

Browse files
committed
Updated module class logic
1 parent 5812ef2 commit 0a65a10

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

src/Codeception/Module/Laravel.php

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Codeception\Module;
66

7-
use Codeception\Configuration;
7+
use Codeception\Configuration as CodeceptConfig;
88
use Codeception\Exception\ModuleConfigException;
99
use Codeception\Lib\Connector\Laravel as LaravelConnector;
1010
use Codeception\Lib\Framework;
@@ -21,16 +21,17 @@
2121
use Codeception\Module\Laravel\InteractsWithSession;
2222
use Codeception\Module\Laravel\InteractsWithViews;
2323
use Codeception\Module\Laravel\MakesHttpRequests;
24+
use Codeception\Module\Laravel\ServicesTrait;
2425
use Codeception\Subscriber\ErrorHandler;
2526
use Codeception\TestInterface;
2627
use Codeception\Util\ReflectionHelper;
27-
use Exception;
2828
use Illuminate\Database\Connection;
2929
use Illuminate\Database\DatabaseManager;
30-
use Illuminate\Database\Eloquent\Factory;
3130
use Illuminate\Foundation\Application;
3231
use Illuminate\Routing\Route;
3332
use 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

Comments
 (0)