Skip to content

Commit

Permalink
Update codebase to PHP 7.4 (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
TavoNiievez authored Nov 21, 2021
1 parent 91e812b commit 71f92ce
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 90 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
php: [7.3, 7.4, 8.0]
php: [7.4, 8.0]
laravel: [6, 8]

steps:
Expand Down Expand Up @@ -74,6 +74,4 @@ jobs:
working-directory: framework-tests

- name: Run test suite
run: |
php vendor/bin/codecept build -c framework-tests
php vendor/bin/codecept run Functional -c framework-tests
run: php vendor/bin/codecept run Functional -c framework-tests
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
],
"minimum-stability": "RC",
"require": {
"php": "^7.3 | ^8.0",
"php": "^7.4 | ^8.0",
"ext-json": "*",
"codeception/lib-innerbrowser": "^1.3",
"codeception/codeception": "^4.0"
},
"require-dev": {
"codeception/module-asserts": "^1.3",
"codeception/module-rest": "^1.2",
"laravel/framework": "^6.0 | ^7.0 | ^8.0",
"vlucas/phpdotenv": "^3.6 | ^4.1 | ^5.2"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A Codeception module for Laravel framework.
## Requirements

* `Laravel 6` or higher.
* `PHP 7.3` or higher.
* `PHP 7.4` or higher.

## Installation

Expand Down
80 changes: 25 additions & 55 deletions src/Codeception/Lib/Connector/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Codeception\Lib\Connector\Laravel\ExceptionHandlerDecorator as LaravelExceptionHandlerDecorator;
use Codeception\Lib\Connector\Laravel6\ExceptionHandlerDecorator as Laravel6ExceptionHandlerDecorator;
use Codeception\Module\Laravel as LaravelModule;
use Codeception\Stub;
use Exception;
use Illuminate\Contracts\Config\Repository as Config;
Expand All @@ -27,75 +28,42 @@

class Laravel extends Client
{
/**
* @var array
*/
private $bindings = [];
private array $bindings = [];

/**
* @var array
*/
private $contextualBindings = [];
private array $contextualBindings = [];

/**
* @var object[]
*/
private $instances = [];
private array $instances = [];

/**
* @var callable[]
*/
private $applicationHandlers = [];
private array $applicationHandlers = [];

/**
* @var Application
*/
private $app;
private ?AppContract $app = null;

/**
* @var \Codeception\Module\Laravel
*/
private $module;
private LaravelModule $module;

/**
* @var bool
*/
private $firstRequest = true;
private bool $firstRequest = true;

/**
* @var array
*/
private $triggeredEvents = [];
private array $triggeredEvents = [];

/**
* @var bool
*/
private $exceptionHandlingDisabled;
private bool $exceptionHandlingDisabled;

/**
* @var bool
*/
private $middlewareDisabled;
private bool $middlewareDisabled;

/**
* @var bool
*/
private $eventsDisabled;
private bool $eventsDisabled;

/**
* @var bool
*/
private $modelEventsDisabled;
private bool $modelEventsDisabled;

/**
* @var object
*/
private $oldDb;
private ?object $oldDb = null;

/**
* Constructor.
*
* @param \Codeception\Module\Laravel $module
* @param LaravelModule $module
* @throws Exception
*/
public function __construct($module)
Expand All @@ -113,6 +81,7 @@ public function __construct($module)
if (array_key_exists('url', $this->module->config)) {
$components = parse_url($this->module->config['url']);
}

$host = $components['host'] ?? 'localhost';

parent::__construct($this->app, ['HTTP_HOST' => $host]);
Expand All @@ -132,6 +101,7 @@ protected function doRequest($request): Response
if (!$this->firstRequest) {
$this->initialize($request);
}

$this->firstRequest = false;

$this->applyBindings();
Expand All @@ -157,27 +127,27 @@ private function initialize(SymfonyRequest $request = null): void
$this->oldDb = $db;
}

$this->app = $this->kernel = $this->loadApplication();
$this->app = $this->loadApplication();
$this->kernel = $this->app;

// Set the request instance for the application,
if (is_null($request)) {
$appConfig = require $this->module->config['project_dir'] . 'config/app.php';
$request = SymfonyRequest::create($appConfig['url']);
}

$this->app->instance('request', Request::createFromBase($request));

// Reset the old database after all the service providers are registered.
if ($this->oldDb) {
$this->getEvents()->listen('bootstrapped: ' . RegisterProviders::class, function () {
$this->app->singleton('db', function () {
return $this->oldDb;
});
$this->getEvents()->listen('bootstrapped: ' . RegisterProviders::class, function (): void {
$this->app->singleton('db', fn(): object => $this->oldDb);
});
}

$this->getHttpKernel()->bootstrap();

$listener = function ($event) {
$listener = function ($event): void {
$this->triggeredEvents[] = $this->normalizeEvent($event);
};

Expand Down Expand Up @@ -230,7 +200,7 @@ private function mockEventDispatcher(): void
// Even if events are disabled we still want to record the triggered events.
// But by mocking the event dispatcher the wildcard listener registered in the initialize method is removed.
// So to record the triggered events we have to catch the calls to the fire method of the event dispatcher mock.
$callback = function ($event) {
$callback = function ($event): array {
$this->triggeredEvents[] = $this->normalizeEvent($event);

return [];
Expand All @@ -253,7 +223,7 @@ private function normalizeEvent($event): string
$event = get_class($event);
}

if (preg_match('/^bootstrapp(ing|ed): /', $event)) {
if (preg_match('#^bootstrapp(ing|ed): #', $event)) {
return $event;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,13 @@

class ExceptionHandlerDecorator implements ExceptionHandlerContract
{
/**
* @var ExceptionHandlerContract
*/
private $laravelExceptionHandler;
private ExceptionHandlerContract $laravelExceptionHandler;

/**
* @var bool
*/
private $exceptionHandlingDisabled = true;
private bool $exceptionHandlingDisabled = true;

public function __construct(object $laravelExceptionHandler)
public function __construct(ExceptionHandlerContract $exceptionHandler)
{
$this->laravelExceptionHandler = $laravelExceptionHandler;
$this->laravelExceptionHandler = $exceptionHandler;
}

public function exceptionHandlingDisabled(bool $exceptionHandlingDisabled): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,13 @@

class ExceptionHandlerDecorator implements ExceptionHandlerContract
{
/**
* @var ExceptionHandlerContract
*/
private $laravelExceptionHandler;
private ExceptionHandlerContract $laravelExceptionHandler;

/**
* @var bool
*/
private $exceptionHandlingDisabled = true;
private bool $exceptionHandlingDisabled = true;

public function __construct(object $laravelExceptionHandler)
public function __construct(ExceptionHandlerContract $exceptionHandler)
{
$this->laravelExceptionHandler = $laravelExceptionHandler;
$this->laravelExceptionHandler = $exceptionHandler;
}

public function exceptionHandlingDisabled(bool $exceptionHandlingDisabled): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait InteractsWithAuthentication
*/
public function amActingAs(Authenticatable $user, string $guardName = null): void
{
if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) {
if (property_exists($user, 'wasRecentlyCreated') && $user->wasRecentlyCreated) {
$user->wasRecentlyCreated = false;
}

Expand Down Expand Up @@ -57,7 +57,7 @@ public function amLoggedAs($user, string $guardName = null): void
$guard = $this->getAuth()->guard($guardName);
$this->assertTrue(
$guard->attempt($user)
, 'Failed to login with credentials ' . json_encode($user)
, 'Failed to login with credentials ' . json_encode($user, JSON_THROW_ON_ERROR)
);
}

Expand Down
13 changes: 7 additions & 6 deletions src/Codeception/Module/Laravel/InteractsWithEloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function dontSeeRecord($table, $attributes = []): void

if (class_exists($table)) {
if ($foundMatchingRecord = (bool)$this->findModel($table, $attributes)) {
$this->fail("Unexpectedly found matching {$table} with " . json_encode($attributes));
$this->fail("Unexpectedly found matching {$table} with " . json_encode($attributes, JSON_THROW_ON_ERROR));
}
} elseif ($foundMatchingRecord = (bool)$this->findRecord($table, $attributes)) {
$this->fail("Unexpectedly found matching record in table '{$table}'");
Expand Down Expand Up @@ -89,7 +89,7 @@ public function grabRecord($table, $attributes = [])
{
if (class_exists($table)) {
if (!$model = $this->findModel($table, $attributes)) {
$this->fail("Could not find {$table} with " . json_encode($attributes));
$this->fail("Could not find {$table} with " . json_encode($attributes, JSON_THROW_ON_ERROR));
}

return $model;
Expand Down Expand Up @@ -192,7 +192,7 @@ public function haveRecord($table, $attributes = [])
$table = $this->getDb()->table($table);
return $table->insertGetId($attributes);
} catch (Throwable $t) {
$this->fail("Could not insert record into table '$table':\n\n" . $t->getMessage());
$this->fail("Could not insert record into table '{$table}':\n\n" . $t->getMessage());
}
}

Expand Down Expand Up @@ -275,14 +275,14 @@ public function seeNumRecords(int $expectedNum, string $table, array $attributes
$this->assertSame(
$expectedNum,
$currentNum,
"The number of found {$table} ({$currentNum}) does not match expected number {$expectedNum} with " . json_encode($attributes)
"The number of found {$table} ({$currentNum}) does not match expected number {$expectedNum} with " . json_encode($attributes, JSON_THROW_ON_ERROR)
);
} else {
$currentNum = $this->countRecords($table, $attributes);
$this->assertSame(
$expectedNum,
$currentNum,
"The number of found records in table {$table} ({$currentNum}) does not match expected number $expectedNum with " . json_encode($attributes)
"The number of found records in table {$table} ({$currentNum}) does not match expected number $expectedNum with " . json_encode($attributes, JSON_THROW_ON_ERROR)
);
}
}
Expand Down Expand Up @@ -310,7 +310,7 @@ public function seeRecord($table, $attributes = []): void

if (class_exists($table)) {
if (!$foundMatchingRecord = (bool)$this->findModel($table, $attributes)) {
$this->fail("Could not find {$table} with " . json_encode($attributes));
$this->fail("Could not find {$table} with " . json_encode($attributes, JSON_THROW_ON_ERROR));
}
} elseif (!$foundMatchingRecord = (bool)$this->findRecord($table, $attributes)) {
$this->fail("Could not find matching record in table '{$table}'");
Expand Down Expand Up @@ -373,6 +373,7 @@ private function buildQuery(string $table, array $attributes = [])
$query->where($key, $value);
}
}

return $query;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Codeception/Module/Laravel/InteractsWithRouting.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function seeCurrentActionIs(string $action): void
'\\'
);

if ($currentAction != $action) {
if ($currentAction !== $action) {
$this->fail("Current action is '{$currentAction}'");
}
}
Expand Down

0 comments on commit 71f92ce

Please sign in to comment.