From 03c854b902eb82bbc7d651564e0ced38dbd3b7af Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Tue, 1 Jan 2019 21:11:29 +0100 Subject: [PATCH 1/6] Extract pure unit tests so that they can run fast - Move to separate directory (base for a separate test suite) - Inherit directly from PhpUnit - Configure test suite with dedicated XML file --- framework/core/{phpunit.xml => tests/phpunit.unit.xml} | 8 ++------ .../Api/ExceptionHandler/FloodingExceptionHandlerTest.php | 6 +++--- .../IlluminateValidationExceptionHandlerTest.php | 6 +++--- .../InvalidAccessTokenExceptionHandlerTest.php | 6 +++--- .../InvalidConfirmationTokenExceptionHandlerTest.php | 6 +++--- .../MethodNotAllowedExceptionHandlerTest.php | 6 +++--- .../ModelNotFoundExceptionHandlerTest.php | 6 +++--- .../PermissionDeniedExceptionHandlerTest.php | 6 +++--- .../RouteNotFoundExceptionHandlerTest.php | 6 +++--- .../TokenMismatchExceptionHandlerTest.php | 6 +++--- .../ExceptionHandler/ValidationExceptionHandlerTest.php | 6 +++--- .../Settings/DatabaseSettingsRepositoryTest.php | 6 +++--- .../Settings/MemoryCacheSettingsRepositoryTest.php | 6 +++--- 13 files changed, 38 insertions(+), 42 deletions(-) rename framework/core/{phpunit.xml => tests/phpunit.unit.xml} (68%) rename framework/core/tests/{ => unit}/Api/ExceptionHandler/FloodingExceptionHandlerTest.php (90%) rename framework/core/tests/{ => unit}/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php (94%) rename framework/core/tests/{ => unit}/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php (91%) rename framework/core/tests/{ => unit}/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php (91%) rename framework/core/tests/{ => unit}/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php (91%) rename framework/core/tests/{ => unit}/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php (90%) rename framework/core/tests/{ => unit}/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php (91%) rename framework/core/tests/{ => unit}/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php (90%) rename framework/core/tests/{ => unit}/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php (90%) rename framework/core/tests/{ => unit}/Api/ExceptionHandler/ValidationExceptionHandlerTest.php (93%) rename framework/core/tests/{ => unit}/Settings/DatabaseSettingsRepositoryTest.php (91%) rename framework/core/tests/{ => unit}/Settings/MemoryCacheSettingsRepositoryTest.php (94%) diff --git a/framework/core/phpunit.xml b/framework/core/tests/phpunit.unit.xml similarity index 68% rename from framework/core/phpunit.xml rename to framework/core/tests/phpunit.unit.xml index 9a6492957..aaa4c2d93 100644 --- a/framework/core/phpunit.xml +++ b/framework/core/tests/phpunit.unit.xml @@ -11,12 +11,8 @@ syntaxCheck="false"> - - ./tests/Install - - - ./tests - ./tests/Install + + ./unit diff --git a/framework/core/tests/Api/ExceptionHandler/FloodingExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/FloodingExceptionHandlerTest.php similarity index 90% rename from framework/core/tests/Api/ExceptionHandler/FloodingExceptionHandlerTest.php rename to framework/core/tests/unit/Api/ExceptionHandler/FloodingExceptionHandlerTest.php index 5b9b72398..e2043b493 100644 --- a/framework/core/tests/Api/ExceptionHandler/FloodingExceptionHandlerTest.php +++ b/framework/core/tests/unit/Api/ExceptionHandler/FloodingExceptionHandlerTest.php @@ -9,18 +9,18 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\ExceptionHandler; +namespace Flarum\Tests\unit\Api\ExceptionHandler; use Exception; use Flarum\Api\ExceptionHandler\FloodingExceptionHandler; use Flarum\Post\Exception\FloodingException; -use Flarum\Tests\Test\TestCase; +use PHPUnit\Framework\TestCase; class FloodingExceptionHandlerTest extends TestCase { private $handler; - public function init() + public function setUp() { $this->handler = new FloodingExceptionHandler; } diff --git a/framework/core/tests/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php similarity index 94% rename from framework/core/tests/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php rename to framework/core/tests/unit/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php index adbcc588d..ed3d799f6 100644 --- a/framework/core/tests/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php +++ b/framework/core/tests/unit/Api/ExceptionHandler/IlluminateValidationExceptionHandlerTest.php @@ -9,21 +9,21 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\ExceptionHandler; +namespace Flarum\Tests\unit\Api\ExceptionHandler; use Exception; use Flarum\Api\ExceptionHandler\IlluminateValidationExceptionHandler; -use Flarum\Tests\Test\TestCase; use Illuminate\Translation\ArrayLoader; use Illuminate\Translation\Translator; use Illuminate\Validation\Factory; use Illuminate\Validation\ValidationException; +use PHPUnit\Framework\TestCase; class IlluminateValidationExceptionHandlerTest extends TestCase { private $handler; - public function init() + public function setUp() { $this->handler = new IlluminateValidationExceptionHandler; } diff --git a/framework/core/tests/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php similarity index 91% rename from framework/core/tests/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php rename to framework/core/tests/unit/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php index bb270319f..3d7355eac 100644 --- a/framework/core/tests/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php +++ b/framework/core/tests/unit/Api/ExceptionHandler/InvalidAccessTokenExceptionHandlerTest.php @@ -9,18 +9,18 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\ExceptionHandler; +namespace Flarum\Tests\unit\Api\ExceptionHandler; use Exception; use Flarum\Api\Exception\InvalidAccessTokenException; use Flarum\Api\ExceptionHandler\InvalidAccessTokenExceptionHandler; -use Flarum\Tests\Test\TestCase; +use PHPUnit\Framework\TestCase; class InvalidAccessTokenExceptionHandlerTest extends TestCase { private $handler; - public function init() + public function setUp() { $this->handler = new InvalidAccessTokenExceptionHandler; } diff --git a/framework/core/tests/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php similarity index 91% rename from framework/core/tests/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php rename to framework/core/tests/unit/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php index f04b8edde..1f289fe73 100644 --- a/framework/core/tests/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php +++ b/framework/core/tests/unit/Api/ExceptionHandler/InvalidConfirmationTokenExceptionHandlerTest.php @@ -9,18 +9,18 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\ExceptionHandler; +namespace Flarum\Tests\unit\Api\ExceptionHandler; use Exception; use Flarum\Api\ExceptionHandler\InvalidConfirmationTokenExceptionHandler; -use Flarum\Tests\Test\TestCase; use Flarum\User\Exception\InvalidConfirmationTokenException; +use PHPUnit\Framework\TestCase; class InvalidConfirmationTokenExceptionHandlerTest extends TestCase { private $handler; - public function init() + public function setUp() { $this->handler = new InvalidConfirmationTokenExceptionHandler; } diff --git a/framework/core/tests/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php similarity index 91% rename from framework/core/tests/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php rename to framework/core/tests/unit/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php index 2da7da65d..23ce0761f 100644 --- a/framework/core/tests/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php +++ b/framework/core/tests/unit/Api/ExceptionHandler/MethodNotAllowedExceptionHandlerTest.php @@ -9,18 +9,18 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\ExceptionHandler; +namespace Flarum\Tests\unit\Api\ExceptionHandler; use Exception; use Flarum\Api\ExceptionHandler\MethodNotAllowedExceptionHandler; use Flarum\Http\Exception\MethodNotAllowedException; -use Flarum\Tests\Test\TestCase; +use PHPUnit\Framework\TestCase; class MethodNotAllowedExceptionHandlerTest extends TestCase { private $handler; - public function init() + public function setUp() { $this->handler = new MethodNotAllowedExceptionHandler(); } diff --git a/framework/core/tests/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php similarity index 90% rename from framework/core/tests/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php rename to framework/core/tests/unit/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php index 4e097d446..c1eb2efcc 100644 --- a/framework/core/tests/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php +++ b/framework/core/tests/unit/Api/ExceptionHandler/ModelNotFoundExceptionHandlerTest.php @@ -9,18 +9,18 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\ExceptionHandler; +namespace Flarum\Tests\unit\Api\ExceptionHandler; use Exception; use Flarum\Api\ExceptionHandler\ModelNotFoundExceptionHandler; -use Flarum\Tests\Test\TestCase; use Illuminate\Database\Eloquent\ModelNotFoundException; +use PHPUnit\Framework\TestCase; class ModelNotFoundExceptionHandlerTest extends TestCase { private $handler; - public function init() + public function setUp() { $this->handler = new ModelNotFoundExceptionHandler; } diff --git a/framework/core/tests/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php similarity index 91% rename from framework/core/tests/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php rename to framework/core/tests/unit/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php index ffdb0e9f4..dcfdb33f8 100644 --- a/framework/core/tests/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php +++ b/framework/core/tests/unit/Api/ExceptionHandler/PermissionDeniedExceptionHandlerTest.php @@ -9,18 +9,18 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\ExceptionHandler; +namespace Flarum\Tests\unit\Api\ExceptionHandler; use Exception; use Flarum\Api\ExceptionHandler\PermissionDeniedExceptionHandler; -use Flarum\Tests\Test\TestCase; use Flarum\User\Exception\PermissionDeniedException; +use PHPUnit\Framework\TestCase; class PermissionDeniedExceptionHandlerTest extends TestCase { private $handler; - public function init() + public function setUp() { $this->handler = new PermissionDeniedExceptionHandler; } diff --git a/framework/core/tests/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php similarity index 90% rename from framework/core/tests/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php rename to framework/core/tests/unit/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php index 61659c53e..51c82cb70 100644 --- a/framework/core/tests/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php +++ b/framework/core/tests/unit/Api/ExceptionHandler/RouteNotFoundExceptionHandlerTest.php @@ -9,18 +9,18 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\ExceptionHandler; +namespace Flarum\Tests\unit\Api\ExceptionHandler; use Exception; use Flarum\Api\ExceptionHandler\RouteNotFoundExceptionHandler; use Flarum\Http\Exception\RouteNotFoundException; -use Flarum\Tests\Test\TestCase; +use PHPUnit\Framework\TestCase; class RouteNotFoundExceptionHandlerTest extends TestCase { private $handler; - public function init() + public function setUp() { $this->handler = new RouteNotFoundExceptionHandler(); } diff --git a/framework/core/tests/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php similarity index 90% rename from framework/core/tests/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php rename to framework/core/tests/unit/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php index 352b156a0..eebb1bff8 100644 --- a/framework/core/tests/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php +++ b/framework/core/tests/unit/Api/ExceptionHandler/TokenMismatchExceptionHandlerTest.php @@ -9,18 +9,18 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\ExceptionHandler; +namespace Flarum\Tests\unit\Api\ExceptionHandler; use Exception; use Flarum\Api\ExceptionHandler\TokenMismatchExceptionHandler; use Flarum\Http\Exception\TokenMismatchException; -use Flarum\Tests\Test\TestCase; +use PHPUnit\Framework\TestCase; class TokenMismatchExceptionHandlerTest extends TestCase { private $handler; - public function init() + public function setUp() { $this->handler = new TokenMismatchExceptionHandler; } diff --git a/framework/core/tests/Api/ExceptionHandler/ValidationExceptionHandlerTest.php b/framework/core/tests/unit/Api/ExceptionHandler/ValidationExceptionHandlerTest.php similarity index 93% rename from framework/core/tests/Api/ExceptionHandler/ValidationExceptionHandlerTest.php rename to framework/core/tests/unit/Api/ExceptionHandler/ValidationExceptionHandlerTest.php index 1cd125467..a014a96e5 100644 --- a/framework/core/tests/Api/ExceptionHandler/ValidationExceptionHandlerTest.php +++ b/framework/core/tests/unit/Api/ExceptionHandler/ValidationExceptionHandlerTest.php @@ -9,18 +9,18 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\ExceptionHandler; +namespace Flarum\Tests\unit\Api\ExceptionHandler; use Exception; use Flarum\Api\ExceptionHandler\ValidationExceptionHandler; use Flarum\Foundation\ValidationException; -use Flarum\Tests\Test\TestCase; +use PHPUnit\Framework\TestCase; class ValidationExceptionHandlerTest extends TestCase { private $handler; - public function init() + public function setUp() { $this->handler = new ValidationExceptionHandler; } diff --git a/framework/core/tests/Settings/DatabaseSettingsRepositoryTest.php b/framework/core/tests/unit/Settings/DatabaseSettingsRepositoryTest.php similarity index 91% rename from framework/core/tests/Settings/DatabaseSettingsRepositoryTest.php rename to framework/core/tests/unit/Settings/DatabaseSettingsRepositoryTest.php index 134651487..c7188792d 100644 --- a/framework/core/tests/Settings/DatabaseSettingsRepositoryTest.php +++ b/framework/core/tests/unit/Settings/DatabaseSettingsRepositoryTest.php @@ -9,19 +9,19 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Settings; +namespace Flarum\Tests\unit\Settings; use Flarum\Settings\DatabaseSettingsRepository; -use Flarum\Tests\Test\TestCase; use Illuminate\Database\ConnectionInterface; use Mockery as m; +use PHPUnit\Framework\TestCase; class DatabaseSettingsRepositoryTest extends TestCase { private $connection; private $repository; - public function init() + public function setUp() { $this->connection = m::mock(ConnectionInterface::class); $this->repository = new DatabaseSettingsRepository($this->connection); diff --git a/framework/core/tests/Settings/MemoryCacheSettingsRepositoryTest.php b/framework/core/tests/unit/Settings/MemoryCacheSettingsRepositoryTest.php similarity index 94% rename from framework/core/tests/Settings/MemoryCacheSettingsRepositoryTest.php rename to framework/core/tests/unit/Settings/MemoryCacheSettingsRepositoryTest.php index 0e21815ca..aa2e35615 100644 --- a/framework/core/tests/Settings/MemoryCacheSettingsRepositoryTest.php +++ b/framework/core/tests/unit/Settings/MemoryCacheSettingsRepositoryTest.php @@ -9,19 +9,19 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Settings; +namespace Flarum\Tests\unit\Settings; use Flarum\Settings\MemoryCacheSettingsRepository; use Flarum\Settings\SettingsRepositoryInterface; -use Flarum\Tests\Test\TestCase; use Mockery as m; +use PHPUnit\Framework\TestCase; class MemoryCacheSettingsRepositoryTest extends TestCase { private $baseRepository; private $repository; - public function init() + public function setUp() { $this->baseRepository = m::mock(SettingsRepositoryInterface::class); $this->repository = new MemoryCacheSettingsRepository($this->baseRepository); From e835b537f12d6f16140ed937a8bb85f9644a1b7f Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Tue, 1 Jan 2019 22:17:09 +0100 Subject: [PATCH 2/6] Move integration tests to separate directory Again, we do all of this to prepare for creating "real" test suites for each type of tests. --- framework/core/.gitignore | 2 +- .../tests/{Test/Concerns => integration}/CreatesForum.php | 8 ++++---- .../{Test/Concerns => integration}/MakesApiRequests.php | 2 +- .../{Test/Concerns => integration}/ManagesContent.php | 2 +- .../Concerns => integration}/RetrievesAuthorizedUsers.php | 2 +- framework/core/tests/{Test => integration}/TestCase.php | 6 +++--- .../api}/Auth/AuthenticateWithApiKeyTest.php | 6 +++--- .../api}/Controller/ApiControllerTestCase.php | 6 +++--- .../api}/Controller/CreateDiscussionControllerTest.php | 2 +- .../api}/Controller/CreateGroupControllerTest.php | 2 +- .../api}/Controller/CreatePostControllerTest.php | 2 +- .../api}/Controller/CreateTokenControllerTest.php | 2 +- .../api}/Controller/CreateUserControllerTest.php | 2 +- .../api}/Controller/DeleteDiscussionControllerTest.php | 2 +- .../api}/Controller/ListDiscussionsControllerTest.php | 2 +- .../api}/Controller/ListGroupsControllerTest.php | 2 +- .../api}/Controller/ListNotificationsControllerTest.php | 2 +- .../api}/Controller/ListUsersControllerTest.php | 2 +- .../api}/Controller/ShowDiscussionControllerTest.php | 4 ++-- .../api}/Controller/UpdateUserControllerTest.php | 2 +- .../installer}/DefaultInstallationTest.php | 0 .../tests/{ => integration}/tmp/public/assets/.gitkeep | 0 .../core/tests/{ => integration}/tmp/storage/.gitkeep | 0 .../{ => integration}/tmp/storage/formatter/.gitkeep | 0 .../tests/{ => integration}/tmp/storage/sessions/.gitkeep | 0 .../{ => integration}/tmp/vendor/composer/installed.json | 0 26 files changed, 30 insertions(+), 30 deletions(-) rename framework/core/tests/{Test/Concerns => integration}/CreatesForum.php (94%) rename framework/core/tests/{Test/Concerns => integration}/MakesApiRequests.php (94%) rename framework/core/tests/{Test/Concerns => integration}/ManagesContent.php (96%) rename framework/core/tests/{Test/Concerns => integration}/RetrievesAuthorizedUsers.php (95%) rename framework/core/tests/{Test => integration}/TestCase.php (84%) rename framework/core/tests/{Api => integration/api}/Auth/AuthenticateWithApiKeyTest.php (96%) rename framework/core/tests/{Api => integration/api}/Controller/ApiControllerTestCase.php (88%) rename framework/core/tests/{Api => integration/api}/Controller/CreateDiscussionControllerTest.php (97%) rename framework/core/tests/{Api => integration/api}/Controller/CreateGroupControllerTest.php (97%) rename framework/core/tests/{Api => integration/api}/Controller/CreatePostControllerTest.php (95%) rename framework/core/tests/{Api => integration/api}/Controller/CreateTokenControllerTest.php (95%) rename framework/core/tests/{Api => integration/api}/Controller/CreateUserControllerTest.php (97%) rename framework/core/tests/{Api => integration/api}/Controller/DeleteDiscussionControllerTest.php (94%) rename framework/core/tests/{Api => integration/api}/Controller/ListDiscussionsControllerTest.php (95%) rename framework/core/tests/{Api => integration/api}/Controller/ListGroupsControllerTest.php (93%) rename framework/core/tests/{Api => integration/api}/Controller/ListNotificationsControllerTest.php (94%) rename framework/core/tests/{Api => integration/api}/Controller/ListUsersControllerTest.php (94%) rename framework/core/tests/{Api => integration/api}/Controller/ShowDiscussionControllerTest.php (95%) rename framework/core/tests/{Api => integration/api}/Controller/UpdateUserControllerTest.php (96%) rename framework/core/tests/{Install => integration/installer}/DefaultInstallationTest.php (100%) rename framework/core/tests/{ => integration}/tmp/public/assets/.gitkeep (100%) rename framework/core/tests/{ => integration}/tmp/storage/.gitkeep (100%) rename framework/core/tests/{ => integration}/tmp/storage/formatter/.gitkeep (100%) rename framework/core/tests/{ => integration}/tmp/storage/sessions/.gitkeep (100%) rename framework/core/tests/{ => integration}/tmp/vendor/composer/installed.json (100%) diff --git a/framework/core/.gitignore b/framework/core/.gitignore index fb4aab981..56b9aaf7d 100644 --- a/framework/core/.gitignore +++ b/framework/core/.gitignore @@ -4,6 +4,6 @@ composer.phar node_modules .DS_Store Thumbs.db -/tests/tmp +/tests/integration/tmp .vagrant .idea/* diff --git a/framework/core/tests/Test/Concerns/CreatesForum.php b/framework/core/tests/integration/CreatesForum.php similarity index 94% rename from framework/core/tests/Test/Concerns/CreatesForum.php rename to framework/core/tests/integration/CreatesForum.php index 643afe5b1..9d2838057 100644 --- a/framework/core/tests/Test/Concerns/CreatesForum.php +++ b/framework/core/tests/integration/CreatesForum.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Test\Concerns; +namespace Flarum\Tests\integration; use Flarum\Database\DatabaseMigrationRepository; use Flarum\Database\Migrator; @@ -57,9 +57,9 @@ trait CreatesForum protected function createsSite() { $paths = [ - 'base' => __DIR__.'/../../tmp', - 'public' => __DIR__.'/../../tmp/public', - 'storage' => __DIR__.'/../../tmp/storage', + 'base' => __DIR__.'/tmp', + 'public' => __DIR__.'/tmp/public', + 'storage' => __DIR__.'/tmp/storage', ]; if ($this->isInstalled) { diff --git a/framework/core/tests/Test/Concerns/MakesApiRequests.php b/framework/core/tests/integration/MakesApiRequests.php similarity index 94% rename from framework/core/tests/Test/Concerns/MakesApiRequests.php rename to framework/core/tests/integration/MakesApiRequests.php index c7f165d97..7bbfc2ee3 100644 --- a/framework/core/tests/Test/Concerns/MakesApiRequests.php +++ b/framework/core/tests/integration/MakesApiRequests.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Test\Concerns; +namespace Flarum\Tests\integration; use Flarum\Api\Client; use Flarum\User\Guest; diff --git a/framework/core/tests/Test/Concerns/ManagesContent.php b/framework/core/tests/integration/ManagesContent.php similarity index 96% rename from framework/core/tests/Test/Concerns/ManagesContent.php rename to framework/core/tests/integration/ManagesContent.php index 80437c7fc..2be3562ae 100644 --- a/framework/core/tests/Test/Concerns/ManagesContent.php +++ b/framework/core/tests/integration/ManagesContent.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Test\Concerns; +namespace Flarum\Tests\integration; use Flarum\Post\CommentPost; use Flarum\Post\Event\Posted; diff --git a/framework/core/tests/Test/Concerns/RetrievesAuthorizedUsers.php b/framework/core/tests/integration/RetrievesAuthorizedUsers.php similarity index 95% rename from framework/core/tests/Test/Concerns/RetrievesAuthorizedUsers.php rename to framework/core/tests/integration/RetrievesAuthorizedUsers.php index 07f33b693..31b685a9a 100644 --- a/framework/core/tests/Test/Concerns/RetrievesAuthorizedUsers.php +++ b/framework/core/tests/integration/RetrievesAuthorizedUsers.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Test\Concerns; +namespace Flarum\Tests\integration; use Flarum\User\User; diff --git a/framework/core/tests/Test/TestCase.php b/framework/core/tests/integration/TestCase.php similarity index 84% rename from framework/core/tests/Test/TestCase.php rename to framework/core/tests/integration/TestCase.php index 21dbfff7b..b313f533e 100644 --- a/framework/core/tests/Test/TestCase.php +++ b/framework/core/tests/integration/TestCase.php @@ -9,14 +9,14 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Test; +namespace Flarum\Tests\integration; use PHPUnit\Framework\TestCase as Test; abstract class TestCase extends Test { - use Concerns\CreatesForum, - Concerns\MakesApiRequests; + use CreatesForum, + MakesApiRequests; public function setUp() { diff --git a/framework/core/tests/Api/Auth/AuthenticateWithApiKeyTest.php b/framework/core/tests/integration/api/Auth/AuthenticateWithApiKeyTest.php similarity index 96% rename from framework/core/tests/Api/Auth/AuthenticateWithApiKeyTest.php rename to framework/core/tests/integration/api/Auth/AuthenticateWithApiKeyTest.php index 0d2f179a5..a2463b58c 100644 --- a/framework/core/tests/Api/Auth/AuthenticateWithApiKeyTest.php +++ b/framework/core/tests/integration/api/Auth/AuthenticateWithApiKeyTest.php @@ -9,13 +9,13 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Auth; +namespace Flarum\Tests\integration\api\Auth; use Carbon\Carbon; use Flarum\Api\ApiKey; use Flarum\Api\Controller\CreateGroupController; -use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers; -use Flarum\Tests\Test\TestCase; +use Flarum\Tests\integration\RetrievesAuthorizedUsers; +use Flarum\Tests\integration\TestCase; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; diff --git a/framework/core/tests/Api/Controller/ApiControllerTestCase.php b/framework/core/tests/integration/api/Controller/ApiControllerTestCase.php similarity index 88% rename from framework/core/tests/Api/Controller/ApiControllerTestCase.php rename to framework/core/tests/integration/api/Controller/ApiControllerTestCase.php index 6e3028d1a..d00446db1 100644 --- a/framework/core/tests/Api/Controller/ApiControllerTestCase.php +++ b/framework/core/tests/integration/api/Controller/ApiControllerTestCase.php @@ -9,10 +9,10 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; -use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers; -use Flarum\Tests\Test\TestCase; +use Flarum\Tests\integration\RetrievesAuthorizedUsers; +use Flarum\Tests\integration\TestCase; use Flarum\User\User; use Illuminate\Support\Arr; use Psr\Http\Message\ResponseInterface; diff --git a/framework/core/tests/Api/Controller/CreateDiscussionControllerTest.php b/framework/core/tests/integration/api/Controller/CreateDiscussionControllerTest.php similarity index 97% rename from framework/core/tests/Api/Controller/CreateDiscussionControllerTest.php rename to framework/core/tests/integration/api/Controller/CreateDiscussionControllerTest.php index 9fdfb4a41..af658a801 100644 --- a/framework/core/tests/Api/Controller/CreateDiscussionControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreateDiscussionControllerTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\CreateDiscussionController; use Flarum\Discussion\Discussion; diff --git a/framework/core/tests/Api/Controller/CreateGroupControllerTest.php b/framework/core/tests/integration/api/Controller/CreateGroupControllerTest.php similarity index 97% rename from framework/core/tests/Api/Controller/CreateGroupControllerTest.php rename to framework/core/tests/integration/api/Controller/CreateGroupControllerTest.php index 36425435a..402950480 100644 --- a/framework/core/tests/Api/Controller/CreateGroupControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreateGroupControllerTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\CreateGroupController; use Flarum\Group\Group; diff --git a/framework/core/tests/Api/Controller/CreatePostControllerTest.php b/framework/core/tests/integration/api/Controller/CreatePostControllerTest.php similarity index 95% rename from framework/core/tests/Api/Controller/CreatePostControllerTest.php rename to framework/core/tests/integration/api/Controller/CreatePostControllerTest.php index 523b7e75b..541d8a1ce 100644 --- a/framework/core/tests/Api/Controller/CreatePostControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreatePostControllerTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\CreatePostController; use Flarum\Discussion\Discussion; diff --git a/framework/core/tests/Api/Controller/CreateTokenControllerTest.php b/framework/core/tests/integration/api/Controller/CreateTokenControllerTest.php similarity index 95% rename from framework/core/tests/Api/Controller/CreateTokenControllerTest.php rename to framework/core/tests/integration/api/Controller/CreateTokenControllerTest.php index 24c0cf7dd..eb6e6beb7 100644 --- a/framework/core/tests/Api/Controller/CreateTokenControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreateTokenControllerTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\CreateTokenController; use Flarum\Http\AccessToken; diff --git a/framework/core/tests/Api/Controller/CreateUserControllerTest.php b/framework/core/tests/integration/api/Controller/CreateUserControllerTest.php similarity index 97% rename from framework/core/tests/Api/Controller/CreateUserControllerTest.php rename to framework/core/tests/integration/api/Controller/CreateUserControllerTest.php index d7ca4a83c..14feb6055 100644 --- a/framework/core/tests/Api/Controller/CreateUserControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreateUserControllerTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\CreateUserController; use Flarum\Settings\SettingsRepositoryInterface; diff --git a/framework/core/tests/Api/Controller/DeleteDiscussionControllerTest.php b/framework/core/tests/integration/api/Controller/DeleteDiscussionControllerTest.php similarity index 94% rename from framework/core/tests/Api/Controller/DeleteDiscussionControllerTest.php rename to framework/core/tests/integration/api/Controller/DeleteDiscussionControllerTest.php index bd174d73e..9d7f87f93 100644 --- a/framework/core/tests/Api/Controller/DeleteDiscussionControllerTest.php +++ b/framework/core/tests/integration/api/Controller/DeleteDiscussionControllerTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\DeleteDiscussionController; use Flarum\Discussion\Discussion; diff --git a/framework/core/tests/Api/Controller/ListDiscussionsControllerTest.php b/framework/core/tests/integration/api/Controller/ListDiscussionsControllerTest.php similarity index 95% rename from framework/core/tests/Api/Controller/ListDiscussionsControllerTest.php rename to framework/core/tests/integration/api/Controller/ListDiscussionsControllerTest.php index c7d2abe93..3471f3444 100644 --- a/framework/core/tests/Api/Controller/ListDiscussionsControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ListDiscussionsControllerTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\ListDiscussionsController; use Flarum\Discussion\Discussion; diff --git a/framework/core/tests/Api/Controller/ListGroupsControllerTest.php b/framework/core/tests/integration/api/Controller/ListGroupsControllerTest.php similarity index 93% rename from framework/core/tests/Api/Controller/ListGroupsControllerTest.php rename to framework/core/tests/integration/api/Controller/ListGroupsControllerTest.php index 34d945a90..cd0cdc6a4 100644 --- a/framework/core/tests/Api/Controller/ListGroupsControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ListGroupsControllerTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\ListGroupsController; use Flarum\Group\Group; diff --git a/framework/core/tests/Api/Controller/ListNotificationsControllerTest.php b/framework/core/tests/integration/api/Controller/ListNotificationsControllerTest.php similarity index 94% rename from framework/core/tests/Api/Controller/ListNotificationsControllerTest.php rename to framework/core/tests/integration/api/Controller/ListNotificationsControllerTest.php index f269b8da8..5f64d7b54 100644 --- a/framework/core/tests/Api/Controller/ListNotificationsControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ListNotificationsControllerTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\ListNotificationsController; diff --git a/framework/core/tests/Api/Controller/ListUsersControllerTest.php b/framework/core/tests/integration/api/Controller/ListUsersControllerTest.php similarity index 94% rename from framework/core/tests/Api/Controller/ListUsersControllerTest.php rename to framework/core/tests/integration/api/Controller/ListUsersControllerTest.php index 34350db87..c6e6e0551 100644 --- a/framework/core/tests/Api/Controller/ListUsersControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ListUsersControllerTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\ListUsersController; diff --git a/framework/core/tests/Api/Controller/ShowDiscussionControllerTest.php b/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php similarity index 95% rename from framework/core/tests/Api/Controller/ShowDiscussionControllerTest.php rename to framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php index 5c4b8ae28..6931e7d9b 100644 --- a/framework/core/tests/Api/Controller/ShowDiscussionControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php @@ -9,11 +9,11 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\ShowDiscussionController; use Flarum\Discussion\Discussion; -use Flarum\Tests\Test\Concerns\ManagesContent; +use Flarum\Tests\integration\ManagesContent; class ShowDiscussionControllerTest extends ApiControllerTestCase { diff --git a/framework/core/tests/Api/Controller/UpdateUserControllerTest.php b/framework/core/tests/integration/api/Controller/UpdateUserControllerTest.php similarity index 96% rename from framework/core/tests/Api/Controller/UpdateUserControllerTest.php rename to framework/core/tests/integration/api/Controller/UpdateUserControllerTest.php index 2ea9ecb3e..a42562b62 100644 --- a/framework/core/tests/Api/Controller/UpdateUserControllerTest.php +++ b/framework/core/tests/integration/api/Controller/UpdateUserControllerTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Flarum\Tests\Api\Controller; +namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\UpdateUserController; diff --git a/framework/core/tests/Install/DefaultInstallationTest.php b/framework/core/tests/integration/installer/DefaultInstallationTest.php similarity index 100% rename from framework/core/tests/Install/DefaultInstallationTest.php rename to framework/core/tests/integration/installer/DefaultInstallationTest.php diff --git a/framework/core/tests/tmp/public/assets/.gitkeep b/framework/core/tests/integration/tmp/public/assets/.gitkeep similarity index 100% rename from framework/core/tests/tmp/public/assets/.gitkeep rename to framework/core/tests/integration/tmp/public/assets/.gitkeep diff --git a/framework/core/tests/tmp/storage/.gitkeep b/framework/core/tests/integration/tmp/storage/.gitkeep similarity index 100% rename from framework/core/tests/tmp/storage/.gitkeep rename to framework/core/tests/integration/tmp/storage/.gitkeep diff --git a/framework/core/tests/tmp/storage/formatter/.gitkeep b/framework/core/tests/integration/tmp/storage/formatter/.gitkeep similarity index 100% rename from framework/core/tests/tmp/storage/formatter/.gitkeep rename to framework/core/tests/integration/tmp/storage/formatter/.gitkeep diff --git a/framework/core/tests/tmp/storage/sessions/.gitkeep b/framework/core/tests/integration/tmp/storage/sessions/.gitkeep similarity index 100% rename from framework/core/tests/tmp/storage/sessions/.gitkeep rename to framework/core/tests/integration/tmp/storage/sessions/.gitkeep diff --git a/framework/core/tests/tmp/vendor/composer/installed.json b/framework/core/tests/integration/tmp/vendor/composer/installed.json similarity index 100% rename from framework/core/tests/tmp/vendor/composer/installed.json rename to framework/core/tests/integration/tmp/vendor/composer/installed.json From 31266d3029887fa2b1e8b21c0d2d0cfc344597a9 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 30 Jan 2019 21:15:27 +0100 Subject: [PATCH 3/6] Make integration tests independent This creates a dedicated test suite for integration tests. All of them can be run independently, and there is no order dependency - previously, all integration tests needed the installer test to run first, and they would fail if installation failed. Now, the developer will have to set up a Flarum database to be used by these tests. A setup script to make this simple will be added in the next commit. Small tradeoff: the installer is NOT tested in our test suite anymore, only implicitly through the setup script. If we decide that this is a problem, we can still set up separate, dedicated installer tests which should probably test the web installer. --- .../core/src/Foundation/InstalledApp.php | 5 + .../core/src/Foundation/InstalledSite.php | 2 +- .../core/tests/integration/CreatesForum.php | 141 ------------------ .../tests/integration/MakesApiRequests.php | 30 ---- .../core/tests/integration/ManagesContent.php | 45 ------ .../integration/RetrievesAuthorizedUsers.php | 65 ++++++-- framework/core/tests/integration/TestCase.php | 68 +++++++-- .../api/Auth/AuthenticateWithApiKeyTest.php | 27 +++- .../api/Controller/ApiControllerTestCase.php | 12 +- .../CreateDiscussionControllerTest.php | 35 +++-- .../Controller/CreateGroupControllerTest.php | 31 ++-- .../Controller/CreatePostControllerTest.php | 36 +++-- .../Controller/CreateTokenControllerTest.php | 21 ++- .../Controller/CreateUserControllerTest.php | 25 +++- .../DeleteDiscussionControllerTest.php | 29 +++- .../ListDiscussionsControllerTest.php | 31 +++- .../ListNotificationsControllerTest.php | 14 +- .../Controller/ListUsersControllerTest.php | 20 ++- .../ShowDiscussionControllerTest.php | 55 ++++--- .../Controller/UpdateUserControllerTest.php | 45 ++++-- .../installer/DefaultInstallationTest.php | 75 ---------- framework/core/tests/phpunit.integration.xml | 23 +++ 22 files changed, 416 insertions(+), 419 deletions(-) delete mode 100644 framework/core/tests/integration/CreatesForum.php delete mode 100644 framework/core/tests/integration/MakesApiRequests.php delete mode 100644 framework/core/tests/integration/ManagesContent.php delete mode 100644 framework/core/tests/integration/installer/DefaultInstallationTest.php create mode 100644 framework/core/tests/phpunit.integration.xml diff --git a/framework/core/src/Foundation/InstalledApp.php b/framework/core/src/Foundation/InstalledApp.php index 94d8d570f..39478d96e 100644 --- a/framework/core/src/Foundation/InstalledApp.php +++ b/framework/core/src/Foundation/InstalledApp.php @@ -42,6 +42,11 @@ class InstalledApp implements AppInterface $this->config = $config; } + public function getContainer() + { + return $this->container; + } + /** * @return \Psr\Http\Server\RequestHandlerInterface */ diff --git a/framework/core/src/Foundation/InstalledSite.php b/framework/core/src/Foundation/InstalledSite.php index bf1de46c5..88abddbd6 100644 --- a/framework/core/src/Foundation/InstalledSite.php +++ b/framework/core/src/Foundation/InstalledSite.php @@ -72,7 +72,7 @@ class InstalledSite implements SiteInterface /** * Create and boot a Flarum application instance. * - * @return AppInterface + * @return InstalledApp */ public function bootApp(): AppInterface { diff --git a/framework/core/tests/integration/CreatesForum.php b/framework/core/tests/integration/CreatesForum.php deleted file mode 100644 index 9d2838057..000000000 --- a/framework/core/tests/integration/CreatesForum.php +++ /dev/null @@ -1,141 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\integration; - -use Flarum\Database\DatabaseMigrationRepository; -use Flarum\Database\Migrator; -use Flarum\Foundation\Application; -use Flarum\Foundation\InstalledSite; -use Flarum\Foundation\SiteInterface; -use Flarum\Foundation\UninstalledSite; -use Flarum\Http\Server; -use Flarum\Install\Console\DataProviderInterface; -use Flarum\Install\DatabaseConfig; -use Illuminate\Database\ConnectionInterface; -use Illuminate\Database\Connectors\MySqlConnector; -use Illuminate\Database\MySqlConnection; -use Illuminate\Filesystem\Filesystem; - -trait CreatesForum -{ - /** - * @var Server - */ - protected $http; - - /** - * @var SiteInterface - */ - protected $site; - - /** - * @var Application - */ - protected $app; - - /** - * @var DataProviderInterface - */ - protected $configuration; - - /** - * Make the test set up Flarum as an installed app. - * - * @var bool - */ - protected $isInstalled = true; - - protected function createsSite() - { - $paths = [ - 'base' => __DIR__.'/tmp', - 'public' => __DIR__.'/tmp/public', - 'storage' => __DIR__.'/tmp/storage', - ]; - - if ($this->isInstalled) { - $this->site = new InstalledSite($paths, $this->getFlarumConfig()); - } else { - $this->site = new UninstalledSite($paths); - } - } - - protected function createsHttpForum() - { - $this->app = $this->site->bootApp(); - } - - protected function getDatabaseConfiguration(): DatabaseConfig - { - return new DatabaseConfig( - 'mysql', - env('DB_HOST', 'localhost'), - 3306, - env('DB_DATABASE', 'flarum'), - env('DB_USERNAME', 'root'), - env('DB_PASSWORD', ''), - env('DB_PREFIX', '') - ); - } - - protected function refreshApplication() - { - $this->seedsDatabase(); - - $this->createsSite(); - - $this->createsHttpForum(); - } - - protected function teardownApplication() - { - /** @var ConnectionInterface $connection */ - $connection = app(ConnectionInterface::class); - $connection->rollBack(); - } - - protected function getFlarumConfig() - { - return [ - 'debug' => true, - 'database' => $this->getDatabaseConfiguration()->toArray(), - 'url' => 'http://flarum.local', - 'paths' => [ - 'api' => 'api', - 'admin' => 'admin', - ], - ]; - } - - protected function seedsDatabase() - { - if (! $this->isInstalled) { - return; - } - - $dbConfig = $this->getDatabaseConfiguration()->toArray(); - - $pdo = (new MySqlConnector)->connect($dbConfig); - $db = new MySqlConnection($pdo, $dbConfig['database'], $dbConfig['prefix'], $dbConfig); - - $repository = new DatabaseMigrationRepository($db, 'migrations'); - $migrator = new Migrator($repository, $db, new Filesystem); - - if (! $migrator->getRepository()->repositoryExists()) { - $migrator->getRepository()->createRepository(); - } - - $migrator->run(__DIR__.'/../../../migrations'); - - $db->beginTransaction(); - } -} diff --git a/framework/core/tests/integration/MakesApiRequests.php b/framework/core/tests/integration/MakesApiRequests.php deleted file mode 100644 index 7bbfc2ee3..000000000 --- a/framework/core/tests/integration/MakesApiRequests.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\integration; - -use Flarum\Api\Client; -use Flarum\User\Guest; -use Flarum\User\User; -use Psr\Http\Message\ResponseInterface; - -trait MakesApiRequests -{ - public function call(string $controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface - { - /** @var Client $api */ - $api = app(Client::class); - - $api->setErrorHandler(null); - - return $api->send($controller, $actor ?? new Guest, $queryParams, $body); - } -} diff --git a/framework/core/tests/integration/ManagesContent.php b/framework/core/tests/integration/ManagesContent.php deleted file mode 100644 index 2be3562ae..000000000 --- a/framework/core/tests/integration/ManagesContent.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\integration; - -use Flarum\Post\CommentPost; -use Flarum\Post\Event\Posted; - -trait ManagesContent -{ - use RetrievesAuthorizedUsers; - - protected function addPostByNormalUser(): CommentPost - { - $actor = $this->getNormalUser(); - - $post = CommentPost::reply( - $this->discussion->id, - 'a normal reply - too-obscure', - $actor->id, - '127.0.0.1' - ); - - $post->save(); - - if (! $this->discussion->firstPost) { - $this->discussion->setFirstPost($post); - $this->discussion->setLastPost($post); - - $this->discussion->save(); - - event(new Posted($post, $actor)); - } - - return $post; - } -} diff --git a/framework/core/tests/integration/RetrievesAuthorizedUsers.php b/framework/core/tests/integration/RetrievesAuthorizedUsers.php index 31b685a9a..135ef7bec 100644 --- a/framework/core/tests/integration/RetrievesAuthorizedUsers.php +++ b/framework/core/tests/integration/RetrievesAuthorizedUsers.php @@ -11,27 +11,60 @@ namespace Flarum\Tests\integration; -use Flarum\User\User; - trait RetrievesAuthorizedUsers { - protected $userAttributes = [ - 'username' => 'normal', - 'password' => 'too-obscure', - 'email' => 'normal@machine.local' - ]; - - public function getAdminUser(): User + protected function adminGroup(): array { - return User::find(1); + return [ + 'id' => 1, + 'name_singular' => 'Admin', + 'name_plural' => 'Admins', + 'color' => '#B72A2A', + 'icon' => 'fas fa-wrench', + ]; } - public function getNormalUser(): User + protected function guestGroup(): array { - return User::unguarded(function () { - return User::firstOrCreate([ - 'username' => $this->userAttributes['username'] - ], $this->userAttributes); - }); + return [ + 'id' => 2, + 'name_singular' => 'Guest', + 'name_plural' => 'Guests', + 'color' => null, + 'icon' => null, + ]; + } + + protected function memberGroup(): array + { + return [ + 'id' => 3, + 'name_singular' => 'Member', + 'name_plural' => 'Members', + 'color' => null, + 'icon' => null, + ]; + } + + protected function adminUser(): array + { + return [ + 'id' => 1, + 'username' => 'admin', + 'password' => '$2y$10$HMOAe.XaQjOimA778VmFue1OCt7tj5j0wk5vfoL/CMSJq2BQlfBV2', // BCrypt hash for "password" + 'email' => 'admin@machine.local', + 'is_email_confirmed' => 1, + ]; + } + + protected function normalUser(): array + { + return [ + 'id' => 2, + 'username' => 'normal', + 'password' => '$2y$10$LO59tiT7uggl6Oe23o/O6.utnF6ipngYjvMvaxo1TciKqBttDNKim', // BCrypt hash for "too-obscure" + 'email' => 'normal@machine.local', + 'is_email_confirmed' => 1, + ]; } } diff --git a/framework/core/tests/integration/TestCase.php b/framework/core/tests/integration/TestCase.php index b313f533e..9d199564e 100644 --- a/framework/core/tests/integration/TestCase.php +++ b/framework/core/tests/integration/TestCase.php @@ -11,21 +11,71 @@ namespace Flarum\Tests\integration; -use PHPUnit\Framework\TestCase as Test; +use Flarum\Foundation\InstalledSite; +use Illuminate\Database\ConnectionInterface; -abstract class TestCase extends Test +abstract class TestCase extends \PHPUnit\Framework\TestCase { - use CreatesForum, - MakesApiRequests; - public function setUp() { - $this->refreshApplication(); - $this->init(); + parent::setUp(); + + // Boot the Flarum app + $this->app(); } - protected function init() + protected $app; + + /** + * @return \Flarum\Foundation\InstalledApp + */ + protected function app() { - // .. allows implementation by children without the need to call the parent. + if (! is_null($this->app)) { + return $this->app; + } + + $site = new InstalledSite( + [ + 'base' => __DIR__.'/tmp', + 'public' => __DIR__.'/tmp/public', + 'storage' => __DIR__.'/tmp/storage', + ], + include __DIR__.'/tmp/config.php' + ); + + return $this->app = $site->bootApp(); + } + + protected $database; + + protected function database(): ConnectionInterface + { + if (is_null($this->database)) { + $this->database = $this->app()->getContainer()->make( + ConnectionInterface::class + ); + } + + return $this->database; + } + + protected function prepareDatabase(array $tableData) + { + // We temporarily disable foreign key checks to simplify this process. + $this->database()->getSchemaBuilder()->disableForeignKeyConstraints(); + + // First, truncate all referenced tables so that they are empty. + foreach (array_keys($tableData) as $table) { + $this->database()->table($table)->truncate(); + } + + // Then, insert all rows required for this test case. + foreach ($tableData as $table => $rows) { + $this->database()->table($table)->insert($rows); + } + + // And finally, turn on foreign key checks again. + $this->database()->getSchemaBuilder()->enableForeignKeyConstraints(); } } diff --git a/framework/core/tests/integration/api/Auth/AuthenticateWithApiKeyTest.php b/framework/core/tests/integration/api/Auth/AuthenticateWithApiKeyTest.php index a2463b58c..9614ba888 100644 --- a/framework/core/tests/integration/api/Auth/AuthenticateWithApiKeyTest.php +++ b/framework/core/tests/integration/api/Auth/AuthenticateWithApiKeyTest.php @@ -13,9 +13,12 @@ namespace Flarum\Tests\integration\api\Auth; use Carbon\Carbon; use Flarum\Api\ApiKey; +use Flarum\Api\Client; use Flarum\Api\Controller\CreateGroupController; use Flarum\Tests\integration\RetrievesAuthorizedUsers; use Flarum\Tests\integration\TestCase; +use Flarum\User\Guest; +use Flarum\User\User; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; @@ -28,6 +31,18 @@ class AuthenticateWithApiKeyTest extends TestCase { use RetrievesAuthorizedUsers; + public function setUp() + { + parent::setUp(); + + $this->prepareDatabase([ + 'users' => [ + $this->adminUser(), + $this->normalUser(), + ], + ]); + } + protected function key(int $user_id = null): ApiKey { return ApiKey::unguarded(function () use ($user_id) { @@ -45,9 +60,11 @@ class AuthenticateWithApiKeyTest extends TestCase */ public function cannot_authorize_without_key() { - $this->call( - CreateGroupController::class - ); + /** @var Client $api */ + $api = $this->app()->getContainer()->make(Client::class); + $api->setErrorHandler(null); + + $api->send(CreateGroupController::class, new Guest); } /** @@ -79,7 +96,7 @@ class AuthenticateWithApiKeyTest extends TestCase */ public function personal_api_token_cannot_authenticate_as_anyone() { - $user = $this->getNormalUser(); + $user = User::find(2); $key = $this->key($user->id); @@ -105,7 +122,7 @@ class AuthenticateWithApiKeyTest extends TestCase */ public function personal_api_token_authenticates_user() { - $user = $this->getNormalUser(); + $user = User::find(2); $key = $this->key($user->id); diff --git a/framework/core/tests/integration/api/Controller/ApiControllerTestCase.php b/framework/core/tests/integration/api/Controller/ApiControllerTestCase.php index d00446db1..4f0b1296f 100644 --- a/framework/core/tests/integration/api/Controller/ApiControllerTestCase.php +++ b/framework/core/tests/integration/api/Controller/ApiControllerTestCase.php @@ -11,8 +11,10 @@ namespace Flarum\Tests\integration\api\Controller; +use Flarum\Api\Client; use Flarum\Tests\integration\RetrievesAuthorizedUsers; use Flarum\Tests\integration\TestCase; +use Flarum\User\Guest; use Flarum\User\User; use Illuminate\Support\Arr; use Psr\Http\Message\ResponseInterface; @@ -46,9 +48,13 @@ abstract class ApiControllerTestCase extends TestCase ); } - protected function tearDown() + public function call(string $controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface { - $this->actor = null; - parent::tearDown(); + /** @var Client $api */ + $api = $this->app()->getContainer()->make(Client::class); + + $api->setErrorHandler(null); + + return $api->send($controller, $actor ?? new Guest, $queryParams, $body); } } diff --git a/framework/core/tests/integration/api/Controller/CreateDiscussionControllerTest.php b/framework/core/tests/integration/api/Controller/CreateDiscussionControllerTest.php index af658a801..23580f85b 100644 --- a/framework/core/tests/integration/api/Controller/CreateDiscussionControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreateDiscussionControllerTest.php @@ -13,7 +13,7 @@ namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\CreateDiscussionController; use Flarum\Discussion\Discussion; -use Flarum\Post\Post; +use Flarum\User\User; use Illuminate\Support\Arr; class CreateDiscussionControllerTest extends ApiControllerTestCase @@ -25,12 +25,31 @@ class CreateDiscussionControllerTest extends ApiControllerTestCase 'content' => 'predetermined content for automated testing - too-obscure' ]; + public function setUp() + { + parent::setUp(); + + $this->prepareDatabase([ + 'discussions' => [], + 'posts' => [], + 'users' => [ + $this->adminUser(), + ], + 'groups' => [ + $this->adminGroup(), + ], + 'group_user' => [ + ['user_id' => 1, 'group_id' => 1], + ], + ]); + } + /** * @test */ public function can_create_discussion() { - $this->actor = $this->getAdminUser(); + $this->actor = User::find(1); $response = $this->callWith($this->data); @@ -51,7 +70,7 @@ class CreateDiscussionControllerTest extends ApiControllerTestCase */ public function cannot_create_discussion_without_content() { - $this->actor = $this->getAdminUser(); + $this->actor = User::find(1); $data = Arr::except($this->data, 'content'); @@ -65,18 +84,10 @@ class CreateDiscussionControllerTest extends ApiControllerTestCase */ public function cannot_create_discussion_without_title() { - $this->actor = $this->getAdminUser(); + $this->actor = User::find(1); $data = Arr::except($this->data, 'title'); $this->callWith($data); } - - public function tearDown() - { - Discussion::where('title', $this->data['title'])->delete(); - // Prevent floodgate from kicking in. - Post::where('user_id', $this->getAdminUser()->id)->delete(); - parent::tearDown(); - } } diff --git a/framework/core/tests/integration/api/Controller/CreateGroupControllerTest.php b/framework/core/tests/integration/api/Controller/CreateGroupControllerTest.php index 402950480..8a52f63f7 100644 --- a/framework/core/tests/integration/api/Controller/CreateGroupControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreateGroupControllerTest.php @@ -13,6 +13,7 @@ namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\CreateGroupController; use Flarum\Group\Group; +use Flarum\User\User; use Illuminate\Support\Str; class CreateGroupControllerTest extends ApiControllerTestCase @@ -26,6 +27,24 @@ class CreateGroupControllerTest extends ApiControllerTestCase 'color' => null ]; + public function setUp() + { + parent::setUp(); + + $this->prepareDatabase([ + 'users' => [ + $this->adminUser(), + $this->normalUser(), + ], + 'groups' => [ + $this->adminGroup(), + ], + 'group_user' => [ + ['user_id' => 1, 'group_id' => 1], + ], + ]); + } + /** * @test * @expectedException \Illuminate\Validation\ValidationException @@ -33,7 +52,7 @@ class CreateGroupControllerTest extends ApiControllerTestCase */ public function admin_cannot_create_group_without_data() { - $this->actor = $this->getAdminUser(); + $this->actor = User::find(1); $this->callWith(); } @@ -43,7 +62,7 @@ class CreateGroupControllerTest extends ApiControllerTestCase */ public function admin_can_create_group() { - $this->actor = $this->getAdminUser(); + $this->actor = User::find(1); $response = $this->callWith($this->data); @@ -65,14 +84,8 @@ class CreateGroupControllerTest extends ApiControllerTestCase */ public function unauthorized_user_cannot_create_group() { - $this->actor = $this->getNormalUser(); + $this->actor = User::find(2); $this->callWith($this->data); } - - public function tearDown() - { - Group::where('icon', $this->data['icon'])->delete(); - parent::tearDown(); - } } diff --git a/framework/core/tests/integration/api/Controller/CreatePostControllerTest.php b/framework/core/tests/integration/api/Controller/CreatePostControllerTest.php index 541d8a1ce..286c7afec 100644 --- a/framework/core/tests/integration/api/Controller/CreatePostControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreatePostControllerTest.php @@ -11,8 +11,9 @@ namespace Flarum\Tests\integration\api\Controller; +use Carbon\Carbon; use Flarum\Api\Controller\CreatePostController; -use Flarum\Discussion\Discussion; +use Flarum\User\User; use Illuminate\Support\Arr; class CreatePostControllerTest extends ApiControllerTestCase @@ -23,17 +24,28 @@ class CreatePostControllerTest extends ApiControllerTestCase 'content' => 'reply with predetermined content for automated testing - too-obscure' ]; - /** - * @var Discussion - */ - protected $discussion; - - protected function init() + public function setUp() { - $this->actor = $this->getNormalUser(); - $this->discussion = Discussion::start(__CLASS__, $this->actor); + parent::setUp(); - $this->discussion->save(); + $this->prepareDatabase([ + 'discussions' => [ + ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2], + ], + 'posts' => [], + 'users' => [ + $this->normalUser(), + ], + 'groups' => [ + $this->memberGroup(), + ], + 'group_user' => [ + ['user_id' => 2, 'group_id' => 3], + ], + 'group_permission' => [ + ['permission' => 'viewDiscussions', 'group_id' => 3], + ] + ]); } /** @@ -41,9 +53,11 @@ class CreatePostControllerTest extends ApiControllerTestCase */ public function can_create_reply() { + $this->actor = User::find(2); + $body = []; Arr::set($body, 'data.attributes', $this->data); - Arr::set($body, 'data.relationships.discussion.data.id', $this->discussion->id); + Arr::set($body, 'data.relationships.discussion.data.id', 1); $response = $this->callWith($body); diff --git a/framework/core/tests/integration/api/Controller/CreateTokenControllerTest.php b/framework/core/tests/integration/api/Controller/CreateTokenControllerTest.php index eb6e6beb7..0d86d63c1 100644 --- a/framework/core/tests/integration/api/Controller/CreateTokenControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreateTokenControllerTest.php @@ -18,24 +18,33 @@ class CreateTokenControllerTest extends ApiControllerTestCase { protected $controller = CreateTokenController::class; + public function setUp() + { + parent::setUp(); + + $this->prepareDatabase([ + 'users' => [ + $this->normalUser(), + ], + ]); + } + /** * @test */ public function user_generates_token() { - $user = $this->getNormalUser(); - $response = $this->call($this->controller, null, [], [ - 'identification' => $user->username, - 'password' => $this->userAttributes['password'] + 'identification' => 'normal', + 'password' => 'too-obscure' ]); $data = json_decode($response->getBody()->getContents(), true); - $this->assertEquals($user->id, $data['userId']); + $this->assertEquals(2, $data['userId']); $token = $data['token']; - $this->assertEquals($user->id, AccessToken::findOrFail($token)->user_id); + $this->assertEquals(2, AccessToken::findOrFail($token)->user_id); } } diff --git a/framework/core/tests/integration/api/Controller/CreateUserControllerTest.php b/framework/core/tests/integration/api/Controller/CreateUserControllerTest.php index 14feb6055..ee9979aa8 100644 --- a/framework/core/tests/integration/api/Controller/CreateUserControllerTest.php +++ b/framework/core/tests/integration/api/Controller/CreateUserControllerTest.php @@ -26,6 +26,23 @@ class CreateUserControllerTest extends ApiControllerTestCase 'email' => 'test@machine.local' ]; + public function setUp() + { + parent::setUp(); + + $this->prepareDatabase([ + 'users' => [ + $this->adminUser(), + ], + 'groups' => [ + $this->adminGroup(), + ], + 'group_user' => [ + ['user_id' => 1, 'group_id' => 1], + ], + ]); + } + /** * @test * @expectedException \Illuminate\Validation\ValidationException @@ -60,7 +77,7 @@ class CreateUserControllerTest extends ApiControllerTestCase */ public function admins_can_create_activated_users() { - $this->actor = $this->getAdminUser(); + $this->actor = User::find(1); $response = $this->callWith(array_merge($this->data, [ 'isEmailConfirmed' => 1 @@ -90,10 +107,4 @@ class CreateUserControllerTest extends ApiControllerTestCase $settings->set('allow_sign_up', true); } } - - public function tearDown() - { - User::where('username', $this->data['username'])->delete(); - parent::tearDown(); - } } diff --git a/framework/core/tests/integration/api/Controller/DeleteDiscussionControllerTest.php b/framework/core/tests/integration/api/Controller/DeleteDiscussionControllerTest.php index 9d7f87f93..cceaeb1e9 100644 --- a/framework/core/tests/integration/api/Controller/DeleteDiscussionControllerTest.php +++ b/framework/core/tests/integration/api/Controller/DeleteDiscussionControllerTest.php @@ -11,19 +11,34 @@ namespace Flarum\Tests\integration\api\Controller; +use Carbon\Carbon; use Flarum\Api\Controller\DeleteDiscussionController; -use Flarum\Discussion\Discussion; +use Flarum\User\User; class DeleteDiscussionControllerTest extends ApiControllerTestCase { protected $controller = DeleteDiscussionController::class; - protected $discussion; - protected function init() + public function setUp() { - $this->discussion = Discussion::start(__CLASS__, $this->getNormalUser()); + parent::setUp(); - $this->discussion->save(); + $this->prepareDatabase([ + 'discussions' => [ + ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2], + ], + 'posts' => [], + 'users' => [ + $this->adminUser(), + $this->normalUser(), + ], + 'groups' => [ + $this->adminGroup(), + ], + 'group_user' => [ + ['user_id' => 1, 'group_id' => 1], + ], + ]); } /** @@ -31,9 +46,9 @@ class DeleteDiscussionControllerTest extends ApiControllerTestCase */ public function admin_can_delete() { - $this->actor = $this->getAdminUser(); + $this->actor = User::find(1); - $response = $this->callWith([], ['id' => $this->discussion->id]); + $response = $this->callWith([], ['id' => 1]); $this->assertEquals(204, $response->getStatusCode()); } diff --git a/framework/core/tests/integration/api/Controller/ListDiscussionsControllerTest.php b/framework/core/tests/integration/api/Controller/ListDiscussionsControllerTest.php index 3471f3444..24f476038 100644 --- a/framework/core/tests/integration/api/Controller/ListDiscussionsControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ListDiscussionsControllerTest.php @@ -11,13 +11,38 @@ namespace Flarum\Tests\integration\api\Controller; +use Carbon\Carbon; use Flarum\Api\Controller\ListDiscussionsController; -use Flarum\Discussion\Discussion; +use Flarum\User\User; class ListDiscussionsControllerTest extends ApiControllerTestCase { protected $controller = ListDiscussionsController::class; + public function setUp() + { + parent::setUp(); + + $this->prepareDatabase([ + 'discussions' => [ + ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 1, 'comment_count' => 1], + ], + 'posts' => [ + ['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

foo bar

'], + ], + 'users' => [ + $this->normalUser(), + ], + 'groups' => [ + $this->memberGroup(), + $this->guestGroup(), + ], + 'group_permission' => [ + ['permission' => 'viewDiscussions', 'group_id' => 2], + ] + ]); + } + /** * @test */ @@ -28,7 +53,7 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($response->getBody()->getContents(), true); - $this->assertEquals(Discussion::count(), count($data['data'])); + $this->assertEquals(1, count($data['data'])); } /** @@ -36,7 +61,7 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase */ public function can_search_for_author() { - $user = $this->getNormalUser(); + $user = User::find(2); $response = $this->callWith([], [ 'filter' => [ diff --git a/framework/core/tests/integration/api/Controller/ListNotificationsControllerTest.php b/framework/core/tests/integration/api/Controller/ListNotificationsControllerTest.php index 5f64d7b54..f72fd9e5c 100644 --- a/framework/core/tests/integration/api/Controller/ListNotificationsControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ListNotificationsControllerTest.php @@ -12,11 +12,23 @@ namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\ListNotificationsController; +use Flarum\User\User; class ListNotificationsControllerTest extends ApiControllerTestCase { protected $controller = ListNotificationsController::class; + public function setUp() + { + parent::setUp(); + + $this->prepareDatabase([ + 'users' => [ + $this->normalUser(), + ], + ]); + } + /** * @test * @expectedException \Flarum\User\Exception\PermissionDeniedException @@ -31,7 +43,7 @@ class ListNotificationsControllerTest extends ApiControllerTestCase */ public function show_index_for_user() { - $this->actor = $this->getNormalUser(); + $this->actor = User::find(2); $response = $this->callWith(); diff --git a/framework/core/tests/integration/api/Controller/ListUsersControllerTest.php b/framework/core/tests/integration/api/Controller/ListUsersControllerTest.php index c6e6e0551..30a961849 100644 --- a/framework/core/tests/integration/api/Controller/ListUsersControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ListUsersControllerTest.php @@ -12,11 +12,29 @@ namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\ListUsersController; +use Flarum\User\User; class ListUsersControllerTest extends ApiControllerTestCase { protected $controller = ListUsersController::class; + public function setUp() + { + parent::setUp(); + + $this->prepareDatabase([ + 'users' => [ + $this->adminUser(), + ], + 'groups' => [ + $this->adminGroup(), + ], + 'group_user' => [ + ['user_id' => 1, 'group_id' => 1], + ], + ]); + } + /** * @test * @expectedException \Flarum\User\Exception\PermissionDeniedException @@ -31,7 +49,7 @@ class ListUsersControllerTest extends ApiControllerTestCase */ public function shows_index_for_admin() { - $this->actor = $this->getAdminUser(); + $this->actor = User::find(1); $response = $this->callWith(); diff --git a/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php b/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php index 6931e7d9b..ab1fe702a 100644 --- a/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php +++ b/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php @@ -11,14 +11,13 @@ namespace Flarum\Tests\integration\api\Controller; +use Carbon\Carbon; use Flarum\Api\Controller\ShowDiscussionController; use Flarum\Discussion\Discussion; -use Flarum\Tests\integration\ManagesContent; +use Flarum\User\User; class ShowDiscussionControllerTest extends ApiControllerTestCase { - use ManagesContent; - protected $controller = ShowDiscussionController::class; /** @@ -26,9 +25,34 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase */ protected $discussion; - protected function init() + public function setUp() { - $this->discussion = Discussion::start(__CLASS__, $this->getNormalUser()); + parent::setUp(); + + $this->prepareDatabase([ + 'discussions' => [ + ['id' => 1, 'title' => 'Empty discussion', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => null, 'comment_count' => 0, 'is_private' => 0], + ['id' => 2, 'title' => 'Discussion with post', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 1, 'comment_count' => 1, 'is_private' => 0], + ['id' => 3, 'title' => 'Private discussion', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => null, 'comment_count' => 0, 'is_private' => 1], + ], + 'posts' => [ + ['id' => 1, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

a normal reply - too-obscure

'], + ], + 'users' => [ + $this->normalUser(), + ], + 'groups' => [ + $this->guestGroup(), + $this->memberGroup(), + ], + 'group_user' => [ + ['user_id' => 2, 'group_id' => 3], + ], + 'group_permission' => [ + ['permission' => 'viewDiscussions', 'group_id' => 2], + ['permission' => 'viewDiscussions', 'group_id' => 3], + ] + ]); } /** @@ -36,11 +60,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase */ public function author_can_see_discussion() { - $this->discussion->save(); + $this->actor = User::find(2); - $this->actor = $this->getNormalUser(); - - $response = $this->callWith([], ['id' => $this->discussion->id]); + $response = $this->callWith([], ['id' => 1]); $this->assertEquals(200, $response->getStatusCode()); } @@ -51,9 +73,7 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase */ public function guest_cannot_see_empty_discussion() { - $this->discussion->save(); - - $response = $this->callWith([], ['id' => $this->discussion->id]); + $response = $this->callWith([], ['id' => 1]); $this->assertEquals(200, $response->getStatusCode()); } @@ -63,11 +83,7 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase */ public function guest_can_see_discussion() { - $this->discussion->save(); - - $this->addPostByNormalUser(); - - $response = $this->callWith([], ['id' => $this->discussion->id]); + $response = $this->callWith([], ['id' => 2]); $this->assertEquals(200, $response->getStatusCode()); } @@ -78,9 +94,6 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase */ public function guests_cannot_see_private_discussion() { - $this->discussion->is_private = true; - $this->discussion->save(); - - $this->callWith([], ['id' => $this->discussion->id]); + $this->callWith([], ['id' => 3]); } } diff --git a/framework/core/tests/integration/api/Controller/UpdateUserControllerTest.php b/framework/core/tests/integration/api/Controller/UpdateUserControllerTest.php index a42562b62..bceb4895f 100644 --- a/framework/core/tests/integration/api/Controller/UpdateUserControllerTest.php +++ b/framework/core/tests/integration/api/Controller/UpdateUserControllerTest.php @@ -12,6 +12,7 @@ namespace Flarum\Tests\integration\api\Controller; use Flarum\Api\Controller\UpdateUserController; +use Flarum\User\User; class UpdateUserControllerTest extends ApiControllerTestCase { @@ -21,24 +22,41 @@ class UpdateUserControllerTest extends ApiControllerTestCase 'email' => 'newemail@machine.local', ]; - protected $userAttributes = [ - 'username' => 'timtom', - 'password' => 'too-obscure', - 'email' => 'timtom@machine.local', - 'is_email_confirmed' => true, - ]; + public function setUp() + { + parent::setUp(); + + $this->prepareDatabase([ + 'users' => [ + $this->adminUser(), + $this->normalUser(), + ], + 'groups' => [ + $this->adminGroup(), + $this->memberGroup(), + ], + 'group_user' => [ + ['user_id' => 1, 'group_id' => 1], + ['user_id' => 2, 'group_id' => 3], + ], + 'group_permission' => [ + ['permission' => 'viewUserList', 'group_id' => 3], + ] + ]); + } /** * @test */ public function users_can_see_their_private_information() { - $this->actor = $this->getNormalUser(); - $response = $this->callWith([], ['id' => $this->actor->id]); + $this->actor = User::find(2); + + $response = $this->callWith([], ['id' => 2]); // Test for successful response and that the email is included in the response $this->assertEquals(200, $response->getStatusCode()); - $this->assertContains('timtom@machine.local', (string) $response->getBody()); + $this->assertContains('normal@machine.local', (string) $response->getBody()); } /** @@ -46,17 +64,12 @@ class UpdateUserControllerTest extends ApiControllerTestCase */ public function users_can_not_see_other_users_private_information() { - $this->actor = $this->getNormalUser(); + $this->actor = User::find(2); $response = $this->callWith([], ['id' => 1]); // Make sure sensitive information is not made public $this->assertEquals(200, $response->getStatusCode()); - $this->assertNotContains('admin@example.com', (string) $response->getBody()); - } - - public function tearDown() - { - parent::tearDown(); + $this->assertNotContains('admin@machine.local', (string) $response->getBody()); } } diff --git a/framework/core/tests/integration/installer/DefaultInstallationTest.php b/framework/core/tests/integration/installer/DefaultInstallationTest.php deleted file mode 100644 index 27eac7a42..000000000 --- a/framework/core/tests/integration/installer/DefaultInstallationTest.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Tests\Install; - -use Flarum\Install\AdminUser; -use Flarum\Install\Installation; -use Flarum\Tests\Test\TestCase; -use Illuminate\Database\Connectors\ConnectionFactory; - -class DefaultInstallationTest extends TestCase -{ - protected $isInstalled = false; - - /** - * @test - */ - public function allows_forum_installation() - { - if (file_exists(base_path('config.php'))) { - unlink(base_path('config.php')); - } - - /** @var Installation $installation */ - $installation = app(Installation::class); - - $installation - ->debugMode(true) - ->baseUrl('http://flarum.local') - ->databaseConfig($this->getDatabaseConfiguration()) - ->adminUser($this->getAdmin()) - ->settings($this->getSettings()) - ->build()->run(); - - $this->assertFileExists(base_path('config.php')); - - $this->assertEquals( - $this->getDatabase()->table('users')->find(1)->username, - 'admin' - ); - } - - private function getDatabase() - { - $factory = new ConnectionFactory(app()); - - return $factory->make($this->getDatabaseConfiguration()->toArray()); - } - - private function getAdmin(): AdminUser - { - return new AdminUser( - 'admin', - 'password', - 'admin@example.com' - ); - } - - private function getSettings() - { - return [ - 'forum_title' => 'Development Forum', - 'mail_driver' => 'log', - 'welcome_title' => 'Welcome to Development Forum', - ]; - } -} diff --git a/framework/core/tests/phpunit.integration.xml b/framework/core/tests/phpunit.integration.xml new file mode 100644 index 000000000..2ebc8b719 --- /dev/null +++ b/framework/core/tests/phpunit.integration.xml @@ -0,0 +1,23 @@ + + + + + + + ./integration + + + + + ./src/ + + + From 7870bf9149cb22dde9c61ecaffd9445d54cad967 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 30 Jan 2019 21:16:25 +0100 Subject: [PATCH 4/6] Setup Composer commands for testing and setup --- framework/core/composer.json | 9 ++++ framework/core/tests/integration/setup.php | 53 ++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 framework/core/tests/integration/setup.php diff --git a/framework/core/composer.json b/framework/core/composer.json index a7ddf99df..15b2d449a 100644 --- a/framework/core/composer.json +++ b/framework/core/composer.json @@ -86,5 +86,14 @@ "branch-alias": { "dev-master": "0.1.x-dev" } + }, + "scripts": { + "test": [ + "@test:unit", + "@test:integration" + ], + "test:unit": "phpunit -c tests/phpunit.unit.xml", + "test:integration": "phpunit -c tests/phpunit.integration.xml", + "test:setup": "@php tests/integration/setup.php" } } diff --git a/framework/core/tests/integration/setup.php b/framework/core/tests/integration/setup.php new file mode 100644 index 000000000..e207e29d0 --- /dev/null +++ b/framework/core/tests/integration/setup.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Flarum\Install\AdminUser; +use Flarum\Install\DatabaseConfig; +use Flarum\Install\Installation; + +require __DIR__.'/../../vendor/autoload.php'; + +/* + * Setup installation configuration + */ + +$installation = new Installation( + __DIR__.'/tmp', + __DIR__.'/tmp/public', + __DIR__.'/tmp/storage' +); + +$pipeline = $installation + ->configPath('config.php') + ->debugMode(true) + ->baseUrl('http://localhost') + ->databaseConfig(new DatabaseConfig( + 'mysql', + env('DB_HOST', 'localhost'), + intval(env('DB_PORT', 3306)), + env('DB_DATABASE', 'flarum_test'), + env('DB_USERNAME', 'root'), + env('DB_PASSWORD', ''), + env('DB_PREFIX', '') + )) + ->adminUser(new AdminUser( + 'admin', + 'secret', + 'admin@flarum.email' + )) + ->settings(['mail_driver' => 'log']) + ->build(); + +/* + * Run the actual configuration + */ + +$pipeline->run(); From 11a2af43f3c70062d7ac8d26253310a9aae7df32 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Thu, 31 Jan 2019 21:23:27 +0100 Subject: [PATCH 5/6] travis: Configure setup for integration tests --- framework/core/.travis.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/framework/core/.travis.yml b/framework/core/.travis.yml index d4a4effca..97a55c9fa 100644 --- a/framework/core/.travis.yml +++ b/framework/core/.travis.yml @@ -7,16 +7,14 @@ cache: install: - composer install - - mysql -e 'CREATE DATABASE flarum;' + - mysql -e 'CREATE DATABASE flarum_test;' before_script: - echo 'error_reporting = E_ALL' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini + - composer test:setup script: - - vendor/bin/phpunit --coverage-clover=coverage.xml - -after_success: - - bash <(curl -s https://codecov.io/bash) + - composer test jobs: include: From b0d948dc32fe1eeedb4deba273d61e4cfdd56fd8 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Thu, 31 Jan 2019 21:30:16 +0100 Subject: [PATCH 6/6] Add helpful (?) output to test setup script --- framework/core/tests/integration/setup.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/framework/core/tests/integration/setup.php b/framework/core/tests/integration/setup.php index e207e29d0..590538f2b 100644 --- a/framework/core/tests/integration/setup.php +++ b/framework/core/tests/integration/setup.php @@ -15,6 +15,25 @@ use Flarum\Install\Installation; require __DIR__.'/../../vendor/autoload.php'; +$host = env('DB_HOST', 'localhost'); +$port = intval(env('DB_PORT', 3306)); +$name = env('DB_DATABASE', 'flarum_test'); +$user = env('DB_USERNAME', 'root'); +$pass = env('DB_PASSWORD', ''); +$pref = env('DB_PREFIX', ''); + +echo "Connecting to database $name at $host:$port.\n"; +echo "Logging in as $user with password '$pass'.\n"; +echo "Table prefix: '$pref'\n"; + +echo "\n\nCancel now if that's not what you want...\n"; +echo "Use the following environment variables for configuration:\n"; +echo "DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD, DB_PREFIX\n"; + +sleep(5); + +echo "\nOff we go...\n"; + /* * Setup installation configuration */ @@ -51,3 +70,5 @@ $pipeline = $installation */ $pipeline->run(); + +echo "Installation complete\n";