Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions src/Codeception/Lib/Connector/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use Closure;
use Codeception\Lib\Connector\Laravel\ExceptionHandlerDecorator as LaravelExceptionHandlerDecorator;
use Codeception\Lib\Connector\Laravel6\ExceptionHandlerDecorator as Laravel6ExceptionHandlerDecorator;
use Codeception\Module\Laravel\ServicesTrait;
use Codeception\Stub;
use Exception;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as Events;
use Illuminate\Contracts\Foundation\Application as AppContract;
use Illuminate\Contracts\Http\Kernel as HttpKernel;
use Illuminate\Database\ConnectionResolverInterface as Db;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Bootstrap\RegisterProviders;
Expand All @@ -24,8 +27,6 @@

class Laravel extends Client
{
use ServicesTrait;

/**
* @var array
*/
Expand Down Expand Up @@ -447,4 +448,44 @@ public function haveInstance(string $abstract, object $instance): void
{
$this->instances[$abstract] = $instance;
}

/**
* @return \Illuminate\Config\Repository
*/
public function getConfig(): ?Config
{
return $this->app['config'] ?? null;
}

/**
* @return \Illuminate\Database\DatabaseManager
*/
public function getDb(): ?Db
{
return $this->app['db'] ?? null;
}

/**
* @return \Illuminate\Events\Dispatcher
*/
public function getEvents(): ?Events
{
return $this->app['events'] ?? null;
}

/**
* @return \Illuminate\Foundation\Exceptions\Handler
*/
public function getExceptionHandler(): ?ExceptionHandler
{
return $this->app[ExceptionHandler::class] ?? null;
}

/**
* @return \Illuminate\Foundation\Http\Kernel
*/
public function getHttpKernel(): ?HttpKernel
{
return $this->app[HttpKernel::class] ?? null;
}
}
11 changes: 9 additions & 2 deletions src/Codeception/Module/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
use Codeception\Module\Laravel\InteractsWithSession;
use Codeception\Module\Laravel\InteractsWithViews;
use Codeception\Module\Laravel\MakesHttpRequests;
use Codeception\Module\Laravel\ServicesTrait;
use Codeception\Subscriber\ErrorHandler;
use Codeception\TestInterface;
use Codeception\Util\ReflectionHelper;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager;
use Illuminate\Foundation\Application;
Expand Down Expand Up @@ -136,7 +136,6 @@ class Laravel extends Framework implements ActiveRecord, PartedModule
use InteractsWithSession;
use InteractsWithViews;
use MakesHttpRequests;
use ServicesTrait;

/**
* @var Application
Expand Down Expand Up @@ -279,6 +278,14 @@ protected function getInternalDomains(): array
return array_unique($internalDomains);
}

/**
* @return \Illuminate\Config\Repository
*/
protected function getConfig(): ?Config
{
return $this->app['config'] ?? null;
}

/**
* Does the application use the database?
*/
Expand Down
49 changes: 29 additions & 20 deletions src/Codeception/Module/Laravel/InteractsWithAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,24 @@

use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Factory as Auth;

trait InteractsWithAuthentication
{
/**
* Set the given user object to the current or specified Guard.
*/
public function amActingAs(Authenticatable $user, string $guardName = null): void
{
if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) {
$user->wasRecentlyCreated = false;
}

$this->getAuth()->guard($guardName)->setUser($user);

$this->getAuth()->shouldUse($guardName);
}

/**
* Set the currently logged in user for the application.
* Unlike 'amActingAs', this method does update the session, fire the login events
Expand Down Expand Up @@ -41,20 +56,6 @@ public function amLoggedAs($user, string $guardName = null): void
);
}

/**
* Set the given user object to the current or specified Guard.
*/
public function amActingAs(Authenticatable $user, string $guardName = null): void
{
if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) {
$user->wasRecentlyCreated = false;
}

$this->getAuth()->guard($guardName)->setUser($user);

$this->getAuth()->shouldUse($guardName);
}

/**
* Assert that the user is authenticated as the given user.
*/
Expand Down Expand Up @@ -104,19 +105,19 @@ public function dontSeeAuthentication(string $guardName = null): void
}

/**
* Checks that a user is authenticated.
* Logout user.
*/
public function seeAuthentication(string $guardName = null): void
public function logout(): void
{
$this->assertTrue($this->isAuthenticated($guardName), 'The user is not authenticated');
$this->getAuth()->logout();
}

/**
* Logout user.
* Checks that a user is authenticated.
*/
public function logout(): void
public function seeAuthentication(string $guardName = null): void
{
$this->getAuth()->logout();
$this->assertTrue($this->isAuthenticated($guardName), 'The user is not authenticated');
}

/**
Expand All @@ -140,4 +141,12 @@ protected function isAuthenticated(?string $guardName): bool
{
return $this->getAuth()->guard($guardName)->check();
}

/**
* @return \Illuminate\Auth\AuthManager|\Illuminate\Contracts\Auth\StatefulGuard
*/
protected function getAuth(): ?Auth
{
return $this->app['auth'] ?? null;
}
}
9 changes: 9 additions & 0 deletions src/Codeception/Module/Laravel/InteractsWithConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Codeception\Module\Laravel;

use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
use Symfony\Component\Console\Output\OutputInterface;

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

$console->call($command, $parameters, $output);
}

/**
* @return \Illuminate\Foundation\Console\Kernel
*/
protected function getConsoleKernel(): ?ConsoleKernel
{
return $this->app[ConsoleKernel::class] ?? null;
}
}
9 changes: 9 additions & 0 deletions src/Codeception/Module/Laravel/InteractsWithEloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Codeception\Module\Laravel;

use Illuminate\Database\ConnectionResolverInterface as Db;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Factories\Factory as EloquentFactory;
Expand Down Expand Up @@ -390,4 +391,12 @@ private function getQueryBuilderFromTable(string $table): Builder
{
return $this->getDb()->table($table);
}

/**
* @return \Illuminate\Database\DatabaseManager
*/
protected function getDb(): ?Db
{
return $this->app['db'] ?? null;
}
}
35 changes: 35 additions & 0 deletions src/Codeception/Module/Laravel/InteractsWithRouting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Codeception\Module\Laravel;

use Illuminate\Contracts\Routing\Registrar as Router;
use Illuminate\Contracts\Routing\UrlGenerator as Url;
use Illuminate\Routing\Route;
use ReflectionClass;
use ReflectionException;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

trait InteractsWithRouting
{
Expand Down Expand Up @@ -158,4 +161,36 @@ protected function normalizeActionToFullNamespacedAction(string $action): string

return trim($action, '\\');
}

/**
* @return \Illuminate\Routing\UrlGenerator
*/
protected function getUrlGenerator(): ?Url
{
return $this->app['url'] ?? null;
}

/**
* @return \Illuminate\Http\Request
*/
protected function getRequestObject(): ?SymfonyRequest
{
return $this->app['request'] ?? null;
}

/**
* @return \Illuminate\Routing\Router
*/
protected function getRouter(): ?Router
{
return $this->app['router'] ?? null;
}

/**
* @return \Illuminate\Routing\RouteCollectionInterface|\Illuminate\Routing\RouteCollection
*/
protected function getRoutes()
{
return $this->app['routes'] ?? null;
}
}
26 changes: 17 additions & 9 deletions src/Codeception/Module/Laravel/InteractsWithSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

trait InteractsWithSession
{
/**
* Flush all of the current session data.
*/
public function flushSession(): void
{
$this->startSession();
$this->getSession()->flush();
}

/**
* Set the session to the given array.
*/
Expand Down Expand Up @@ -68,15 +77,6 @@ public function seeSessionHasValues(array $bindings): void
}
}

/**
* Flush all of the current session data.
*/
public function flushSession(): void
{
$this->startSession();
$this->getSession()->flush();
}

/**
* Start the session for the application.
*/
Expand All @@ -86,4 +86,12 @@ protected function startSession(): void
$this->getSession()->start();
}
}

/**
* @return \Illuminate\Contracts\Session\Session|\Illuminate\Session\SessionManager
*/
protected function getSession()
{
return $this->app['session'] ?? null;
}
}
9 changes: 9 additions & 0 deletions src/Codeception/Module/Laravel/InteractsWithViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Codeception\Module\Laravel;

use Illuminate\Contracts\View\Factory as View;
use Illuminate\Support\ViewErrorBag;

trait InteractsWithViews
Expand Down Expand Up @@ -113,4 +114,12 @@ protected function getViewErrorBag(): ViewErrorBag
{
return $this->getView()->shared('errors');
}

/**
* @return \Illuminate\View\Factory
*/
protected function getView(): ?View
{
return $this->app['view'] ?? null;
}
}
Loading