Skip to content

Commit 800093d

Browse files
authored
Move Service getters to their related file (#32)
1 parent d3b75a6 commit 800093d

File tree

9 files changed

+161
-158
lines changed

9 files changed

+161
-158
lines changed

src/Codeception/Lib/Connector/Laravel.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
use Closure;
88
use Codeception\Lib\Connector\Laravel\ExceptionHandlerDecorator as LaravelExceptionHandlerDecorator;
99
use Codeception\Lib\Connector\Laravel6\ExceptionHandlerDecorator as Laravel6ExceptionHandlerDecorator;
10-
use Codeception\Module\Laravel\ServicesTrait;
1110
use Codeception\Stub;
1211
use Exception;
12+
use Illuminate\Contracts\Config\Repository as Config;
1313
use Illuminate\Contracts\Debug\ExceptionHandler;
1414
use Illuminate\Contracts\Events\Dispatcher;
15+
use Illuminate\Contracts\Events\Dispatcher as Events;
1516
use Illuminate\Contracts\Foundation\Application as AppContract;
17+
use Illuminate\Contracts\Http\Kernel as HttpKernel;
18+
use Illuminate\Database\ConnectionResolverInterface as Db;
1619
use Illuminate\Database\Eloquent\Model;
1720
use Illuminate\Foundation\Application;
1821
use Illuminate\Foundation\Bootstrap\RegisterProviders;
@@ -24,8 +27,6 @@
2427

2528
class Laravel extends Client
2629
{
27-
use ServicesTrait;
28-
2930
/**
3031
* @var array
3132
*/
@@ -447,4 +448,44 @@ public function haveInstance(string $abstract, object $instance): void
447448
{
448449
$this->instances[$abstract] = $instance;
449450
}
451+
452+
/**
453+
* @return \Illuminate\Config\Repository
454+
*/
455+
public function getConfig(): ?Config
456+
{
457+
return $this->app['config'] ?? null;
458+
}
459+
460+
/**
461+
* @return \Illuminate\Database\DatabaseManager
462+
*/
463+
public function getDb(): ?Db
464+
{
465+
return $this->app['db'] ?? null;
466+
}
467+
468+
/**
469+
* @return \Illuminate\Events\Dispatcher
470+
*/
471+
public function getEvents(): ?Events
472+
{
473+
return $this->app['events'] ?? null;
474+
}
475+
476+
/**
477+
* @return \Illuminate\Foundation\Exceptions\Handler
478+
*/
479+
public function getExceptionHandler(): ?ExceptionHandler
480+
{
481+
return $this->app[ExceptionHandler::class] ?? null;
482+
}
483+
484+
/**
485+
* @return \Illuminate\Foundation\Http\Kernel
486+
*/
487+
public function getHttpKernel(): ?HttpKernel
488+
{
489+
return $this->app[HttpKernel::class] ?? null;
490+
}
450491
}

src/Codeception/Module/Laravel.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
use Codeception\Module\Laravel\InteractsWithSession;
2222
use Codeception\Module\Laravel\InteractsWithViews;
2323
use Codeception\Module\Laravel\MakesHttpRequests;
24-
use Codeception\Module\Laravel\ServicesTrait;
2524
use Codeception\Subscriber\ErrorHandler;
2625
use Codeception\TestInterface;
2726
use Codeception\Util\ReflectionHelper;
27+
use Illuminate\Contracts\Config\Repository as Config;
2828
use Illuminate\Database\Connection;
2929
use Illuminate\Database\DatabaseManager;
3030
use Illuminate\Foundation\Application;
@@ -136,7 +136,6 @@ class Laravel extends Framework implements ActiveRecord, PartedModule
136136
use InteractsWithSession;
137137
use InteractsWithViews;
138138
use MakesHttpRequests;
139-
use ServicesTrait;
140139

141140
/**
142141
* @var Application
@@ -279,6 +278,14 @@ protected function getInternalDomains(): array
279278
return array_unique($internalDomains);
280279
}
281280

281+
/**
282+
* @return \Illuminate\Config\Repository
283+
*/
284+
protected function getConfig(): ?Config
285+
{
286+
return $this->app['config'] ?? null;
287+
}
288+
282289
/**
283290
* Does the application use the database?
284291
*/

src/Codeception/Module/Laravel/InteractsWithAuthentication.php

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,24 @@
66

77
use Illuminate\Auth\GuardHelpers;
88
use Illuminate\Contracts\Auth\Authenticatable;
9+
use Illuminate\Contracts\Auth\Factory as Auth;
910

1011
trait InteractsWithAuthentication
1112
{
13+
/**
14+
* Set the given user object to the current or specified Guard.
15+
*/
16+
public function amActingAs(Authenticatable $user, string $guardName = null): void
17+
{
18+
if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) {
19+
$user->wasRecentlyCreated = false;
20+
}
21+
22+
$this->getAuth()->guard($guardName)->setUser($user);
23+
24+
$this->getAuth()->shouldUse($guardName);
25+
}
26+
1227
/**
1328
* Set the currently logged in user for the application.
1429
* Unlike 'amActingAs', this method does update the session, fire the login events
@@ -41,20 +56,6 @@ public function amLoggedAs($user, string $guardName = null): void
4156
);
4257
}
4358

44-
/**
45-
* Set the given user object to the current or specified Guard.
46-
*/
47-
public function amActingAs(Authenticatable $user, string $guardName = null): void
48-
{
49-
if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) {
50-
$user->wasRecentlyCreated = false;
51-
}
52-
53-
$this->getAuth()->guard($guardName)->setUser($user);
54-
55-
$this->getAuth()->shouldUse($guardName);
56-
}
57-
5859
/**
5960
* Assert that the user is authenticated as the given user.
6061
*/
@@ -104,19 +105,19 @@ public function dontSeeAuthentication(string $guardName = null): void
104105
}
105106

106107
/**
107-
* Checks that a user is authenticated.
108+
* Logout user.
108109
*/
109-
public function seeAuthentication(string $guardName = null): void
110+
public function logout(): void
110111
{
111-
$this->assertTrue($this->isAuthenticated($guardName), 'The user is not authenticated');
112+
$this->getAuth()->logout();
112113
}
113114

114115
/**
115-
* Logout user.
116+
* Checks that a user is authenticated.
116117
*/
117-
public function logout(): void
118+
public function seeAuthentication(string $guardName = null): void
118119
{
119-
$this->getAuth()->logout();
120+
$this->assertTrue($this->isAuthenticated($guardName), 'The user is not authenticated');
120121
}
121122

122123
/**
@@ -140,4 +141,12 @@ protected function isAuthenticated(?string $guardName): bool
140141
{
141142
return $this->getAuth()->guard($guardName)->check();
142143
}
144+
145+
/**
146+
* @return \Illuminate\Auth\AuthManager|\Illuminate\Contracts\Auth\StatefulGuard
147+
*/
148+
protected function getAuth(): ?Auth
149+
{
150+
return $this->app['auth'] ?? null;
151+
}
143152
}

src/Codeception/Module/Laravel/InteractsWithConsole.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Codeception\Module\Laravel;
66

7+
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
78
use Symfony\Component\Console\Output\OutputInterface;
89

910
trait InteractsWithConsole
@@ -32,4 +33,12 @@ public function callArtisan(string $command, array $parameters = [], OutputInter
3233

3334
$console->call($command, $parameters, $output);
3435
}
36+
37+
/**
38+
* @return \Illuminate\Foundation\Console\Kernel
39+
*/
40+
protected function getConsoleKernel(): ?ConsoleKernel
41+
{
42+
return $this->app[ConsoleKernel::class] ?? null;
43+
}
3544
}

src/Codeception/Module/Laravel/InteractsWithEloquent.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Codeception\Module\Laravel;
66

7+
use Illuminate\Database\ConnectionResolverInterface as Db;
78
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
89
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
910
use Illuminate\Database\Eloquent\Factories\Factory as EloquentFactory;
@@ -390,4 +391,12 @@ private function getQueryBuilderFromTable(string $table): Builder
390391
{
391392
return $this->getDb()->table($table);
392393
}
394+
395+
/**
396+
* @return \Illuminate\Database\DatabaseManager
397+
*/
398+
protected function getDb(): ?Db
399+
{
400+
return $this->app['db'] ?? null;
401+
}
393402
}

src/Codeception/Module/Laravel/InteractsWithRouting.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace Codeception\Module\Laravel;
66

7+
use Illuminate\Contracts\Routing\Registrar as Router;
8+
use Illuminate\Contracts\Routing\UrlGenerator as Url;
79
use Illuminate\Routing\Route;
810
use ReflectionClass;
911
use ReflectionException;
12+
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
1013

1114
trait InteractsWithRouting
1215
{
@@ -158,4 +161,36 @@ protected function normalizeActionToFullNamespacedAction(string $action): string
158161

159162
return trim($action, '\\');
160163
}
164+
165+
/**
166+
* @return \Illuminate\Routing\UrlGenerator
167+
*/
168+
protected function getUrlGenerator(): ?Url
169+
{
170+
return $this->app['url'] ?? null;
171+
}
172+
173+
/**
174+
* @return \Illuminate\Http\Request
175+
*/
176+
protected function getRequestObject(): ?SymfonyRequest
177+
{
178+
return $this->app['request'] ?? null;
179+
}
180+
181+
/**
182+
* @return \Illuminate\Routing\Router
183+
*/
184+
protected function getRouter(): ?Router
185+
{
186+
return $this->app['router'] ?? null;
187+
}
188+
189+
/**
190+
* @return \Illuminate\Routing\RouteCollectionInterface|\Illuminate\Routing\RouteCollection
191+
*/
192+
protected function getRoutes()
193+
{
194+
return $this->app['routes'] ?? null;
195+
}
161196
}

src/Codeception/Module/Laravel/InteractsWithSession.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
trait InteractsWithSession
88
{
9+
/**
10+
* Flush all of the current session data.
11+
*/
12+
public function flushSession(): void
13+
{
14+
$this->startSession();
15+
$this->getSession()->flush();
16+
}
17+
918
/**
1019
* Set the session to the given array.
1120
*/
@@ -68,15 +77,6 @@ public function seeSessionHasValues(array $bindings): void
6877
}
6978
}
7079

71-
/**
72-
* Flush all of the current session data.
73-
*/
74-
public function flushSession(): void
75-
{
76-
$this->startSession();
77-
$this->getSession()->flush();
78-
}
79-
8080
/**
8181
* Start the session for the application.
8282
*/
@@ -86,4 +86,12 @@ protected function startSession(): void
8686
$this->getSession()->start();
8787
}
8888
}
89+
90+
/**
91+
* @return \Illuminate\Contracts\Session\Session|\Illuminate\Session\SessionManager
92+
*/
93+
protected function getSession()
94+
{
95+
return $this->app['session'] ?? null;
96+
}
8997
}

src/Codeception/Module/Laravel/InteractsWithViews.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Codeception\Module\Laravel;
66

7+
use Illuminate\Contracts\View\Factory as View;
78
use Illuminate\Support\ViewErrorBag;
89

910
trait InteractsWithViews
@@ -113,4 +114,12 @@ protected function getViewErrorBag(): ViewErrorBag
113114
{
114115
return $this->getView()->shared('errors');
115116
}
117+
118+
/**
119+
* @return \Illuminate\View\Factory
120+
*/
121+
protected function getView(): ?View
122+
{
123+
return $this->app['view'] ?? null;
124+
}
116125
}

0 commit comments

Comments
 (0)