From e124b41977504067f6e1b6b3bf2e39c9b1d31324 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 8 Oct 2019 13:57:50 +0200 Subject: [PATCH] Tests: TestCase: Initialize $this->app on every run --- tests/Unit/Controllers/AuthControllerTest.php | 2 +- tests/Unit/HasDatabase.php | 8 +++----- .../Http/SessionHandlers/DatabaseHandlerTest.php | 3 ++- tests/Unit/Models/EventConfigTest.php | 6 ++++-- tests/Unit/Models/LogEntryTest.php | 3 ++- tests/Unit/Models/User/HasUserModelTest.php | 3 ++- tests/Unit/Models/User/UserTest.php | 3 ++- tests/Unit/TestCase.php | 12 ++++++++++++ 8 files changed, 28 insertions(+), 12 deletions(-) diff --git a/tests/Unit/Controllers/AuthControllerTest.php b/tests/Unit/Controllers/AuthControllerTest.php index 6c237264..50ce014d 100644 --- a/tests/Unit/Controllers/AuthControllerTest.php +++ b/tests/Unit/Controllers/AuthControllerTest.php @@ -12,9 +12,9 @@ use Engelsystem\Http\Validation\Validator; use Engelsystem\Models\User\Settings; use Engelsystem\Models\User\User; use Engelsystem\Test\Unit\HasDatabase; +use Engelsystem\Test\Unit\TestCase; use Illuminate\Support\Collection; use PHPUnit\Framework\MockObject\MockObject; -use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; diff --git a/tests/Unit/HasDatabase.php b/tests/Unit/HasDatabase.php index 7a58bb2b..dbaa253e 100644 --- a/tests/Unit/HasDatabase.php +++ b/tests/Unit/HasDatabase.php @@ -2,7 +2,6 @@ namespace Engelsystem\Test\Unit; -use Engelsystem\Application; use Engelsystem\Database\Database; use Engelsystem\Database\Migration\Migrate; use Engelsystem\Database\Migration\MigrationServiceProvider; @@ -27,12 +26,11 @@ trait HasDatabase $connection->getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->database = new Database($connection); - $app = new Application(); - $app->instance(Database::class, $this->database); - $app->register(MigrationServiceProvider::class); + $this->app->instance(Database::class, $this->database); + $this->app->register(MigrationServiceProvider::class); /** @var Migrate $migration */ - $migration = $app->get('db.migration'); + $migration = $this->app->get('db.migration'); $migration->initMigration(); $this->database diff --git a/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php b/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php index 14f23c00..0325ccfe 100644 --- a/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php +++ b/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php @@ -4,7 +4,7 @@ namespace Engelsystem\Test\Unit\Http\SessionHandlers; use Engelsystem\Http\SessionHandlers\DatabaseHandler; use Engelsystem\Test\Unit\HasDatabase; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\TestCase; class DatabaseHandlerTest extends TestCase { @@ -90,6 +90,7 @@ class DatabaseHandlerTest extends TestCase */ protected function setUp(): void { + parent::setUp(); $this->initDatabase(); } } diff --git a/tests/Unit/Models/EventConfigTest.php b/tests/Unit/Models/EventConfigTest.php index e2ab5d10..18d27007 100644 --- a/tests/Unit/Models/EventConfigTest.php +++ b/tests/Unit/Models/EventConfigTest.php @@ -5,7 +5,7 @@ namespace Engelsystem\Test\Unit\Models; use Carbon\Carbon; use Engelsystem\Models\EventConfig; use Engelsystem\Test\Unit\HasDatabase; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\TestCase; class EventConfigTest extends TestCase { @@ -102,7 +102,8 @@ class EventConfigTest extends TestCase */ protected function getEventConfig() { - return new class extends EventConfig { + return new class extends EventConfig + { /** * @param string $value * @param string $type @@ -122,6 +123,7 @@ class EventConfigTest extends TestCase */ protected function setUp(): void { + parent::setUp(); $this->initDatabase(); } } diff --git a/tests/Unit/Models/LogEntryTest.php b/tests/Unit/Models/LogEntryTest.php index 0a0efa3c..4b772cd0 100644 --- a/tests/Unit/Models/LogEntryTest.php +++ b/tests/Unit/Models/LogEntryTest.php @@ -4,7 +4,7 @@ namespace Engelsystem\Test\Unit\Models; use Engelsystem\Models\LogEntry; use Engelsystem\Test\Unit\HasDatabase; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\TestCase; use Psr\Log\LogLevel; class LogEntryTest extends TestCase @@ -38,6 +38,7 @@ class LogEntryTest extends TestCase */ protected function setUp(): void { + parent::setUp(); $this->initDatabase(); } } diff --git a/tests/Unit/Models/User/HasUserModelTest.php b/tests/Unit/Models/User/HasUserModelTest.php index 58c01e1e..4f6da9ad 100644 --- a/tests/Unit/Models/User/HasUserModelTest.php +++ b/tests/Unit/Models/User/HasUserModelTest.php @@ -5,8 +5,8 @@ namespace Engelsystem\Test\Unit\Models; use Engelsystem\Models\User\HasUserModel; use Engelsystem\Test\Unit\HasDatabase; use Engelsystem\Test\Unit\Models\User\Stub\HasUserModelImplementation; +use Engelsystem\Test\Unit\TestCase; use Illuminate\Database\Eloquent\Relations\BelongsTo; -use PHPUnit\Framework\TestCase; class HasUserModelTest extends TestCase { @@ -28,6 +28,7 @@ class HasUserModelTest extends TestCase */ protected function setUp(): void { + parent::setUp(); $this->initDatabase(); } } diff --git a/tests/Unit/Models/User/UserTest.php b/tests/Unit/Models/User/UserTest.php index 0e17d137..3e793832 100644 --- a/tests/Unit/Models/User/UserTest.php +++ b/tests/Unit/Models/User/UserTest.php @@ -10,7 +10,7 @@ use Engelsystem\Models\User\Settings; use Engelsystem\Models\User\State; use Engelsystem\Models\User\User; use Engelsystem\Test\Unit\HasDatabase; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\TestCase; class UserTest extends TestCase { @@ -95,6 +95,7 @@ class UserTest extends TestCase */ protected function setUp(): void { + parent::setUp(); $this->initDatabase(); } } diff --git a/tests/Unit/TestCase.php b/tests/Unit/TestCase.php index e1a704d9..dba8c989 100644 --- a/tests/Unit/TestCase.php +++ b/tests/Unit/TestCase.php @@ -2,12 +2,16 @@ namespace Engelsystem\Test\Unit; +use Engelsystem\Application; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\Rule\InvocationOrder; use PHPUnit\Framework\TestCase as PHPUnitTestCase; abstract class TestCase extends PHPUnitTestCase { + /** @var Application */ + protected $app; + /** * @param MockObject $object * @param string $method @@ -34,4 +38,12 @@ abstract class TestCase extends PHPUnitTestCase $invocation->willReturn($return); } } + + /** + * Called before each test run + */ + protected function setUp(): void + { + $this->app = new Application(__DIR__ . '/../../'); + } }