diff --git a/tests/Api/Controller/CreateUserControllerTest.php b/tests/Api/Controller/CreateUserControllerTest.php index 285b2a53a..dd51dc0da 100644 --- a/tests/Api/Controller/CreateUserControllerTest.php +++ b/tests/Api/Controller/CreateUserControllerTest.php @@ -84,7 +84,7 @@ class CreateUserControllerTest extends ApiControllerTestCase public function disabling_sign_up_prevents_user_creation() { /** @var SettingsRepositoryInterface $settings */ - $settings = $this->app->make(SettingsRepositoryInterface::class); + $settings = app(SettingsRepositoryInterface::class); $settings->set('allow_sign_up', false); try { diff --git a/tests/Install/DefaultInstallationCommandTest.php b/tests/Install/DefaultInstallationCommandTest.php index 7a8014fc0..0d2fee829 100644 --- a/tests/Install/DefaultInstallationCommandTest.php +++ b/tests/Install/DefaultInstallationCommandTest.php @@ -12,9 +12,8 @@ namespace Flarum\Tests\Install; use Flarum\Install\Console\InstallCommand; -use Flarum\Install\InstallServiceProvider; use Flarum\Tests\Test\TestCase; -use Flarum\User\User; +use Illuminate\Database\Connectors\ConnectionFactory; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\StreamOutput; @@ -27,13 +26,12 @@ class DefaultInstallationCommandTest extends TestCase */ public function allows_forum_installation() { - if (file_exists($this->app->basePath().DIRECTORY_SEPARATOR.'config.php')) { - unlink($this->app->basePath().DIRECTORY_SEPARATOR.'config.php'); + if (file_exists(base_path('config.php'))) { + unlink(base_path('config.php')); } - $this->app->register(InstallServiceProvider::class); /** @var InstallCommand $command */ - $command = $this->app->make(InstallCommand::class); + $command = app(InstallCommand::class); $command->setDataSource($this->configuration); $body = fopen('php://temp', 'wb+'); @@ -42,10 +40,20 @@ class DefaultInstallationCommandTest extends TestCase $command->run($input, $output); - $this->assertFileExists($this->app->basePath().DIRECTORY_SEPARATOR.'config.php'); + $this->assertFileExists(base_path('config.php')); $admin = $this->configuration->getAdminUser(); - $this->assertEquals(User::find(1)->username, $admin['username']); + $this->assertEquals( + $this->getDatabase()->table('users')->find(1)->username, + $admin['username'] + ); + } + + private function getDatabase() + { + $factory = new ConnectionFactory(app()); + + return $factory->make($this->configuration->getDatabaseConfiguration()); } } diff --git a/tests/Test/Concerns/CreatesForum.php b/tests/Test/Concerns/CreatesForum.php index ac4029731..9476dcfe6 100644 --- a/tests/Test/Concerns/CreatesForum.php +++ b/tests/Test/Concerns/CreatesForum.php @@ -11,14 +11,17 @@ namespace Flarum\Tests\Test\Concerns; +use Flarum\Database\DatabaseMigrationRepository; use Flarum\Database\Migrator; use Flarum\Foundation\Application; -use Flarum\Foundation\Site; +use Flarum\Foundation\InstalledSite; +use Flarum\Foundation\SiteInterface; +use Flarum\Foundation\UninstalledSite; use Flarum\Http\Server; use Flarum\Install\Console\DataProviderInterface; use Flarum\Install\Console\DefaultsDataProvider; use Illuminate\Database\ConnectionInterface; -use Illuminate\Database\Schema\Builder; +use Illuminate\Database\Connectors\ConnectionFactory; trait CreatesForum { @@ -28,7 +31,7 @@ trait CreatesForum protected $http; /** - * @var Site + * @var SiteInterface */ protected $site; @@ -51,41 +54,51 @@ trait CreatesForum protected function createsSite() { - $this->site = (new Site) - ->setBasePath(__DIR__.'/../../tmp') - ->setPublicPath(__DIR__.'/../../tmp/public'); + if ($this->isInstalled) { + $this->site = new InstalledSite( + __DIR__.'/../../tmp', + __DIR__.'/../../tmp/public', + $this->getFlarumConfig() + ); + } else { + $this->site = new UninstalledSite( + __DIR__.'/../../tmp', + __DIR__.'/../../tmp/public' + ); + } } protected function createsHttpForum() { - $this->http = Server::fromSite( - $this->site - ); + $this->app = $this->site->bootApp(); - $this->app = $this->http->app; + $this->http = new Server( + $this->app->getRequestHandler() + ); } - protected function refreshApplication() + protected function collectsConfiguration() { - $this->createsSite(); + $this->configuration = new DefaultsDataProvider(); - $data = new DefaultsDataProvider(); + $this->configuration->setDebugMode(); + $this->configuration->setSetting('mail_driver', 'log'); - $data->setDebugMode(); - $data->setSetting('mail_driver', 'log'); - - $database = $data->getDatabaseConfiguration(); + $database = $this->configuration->getDatabaseConfiguration(); $database['host'] = env('DB_HOST', $database['host']); $database['database'] = env('DB_DATABASE', $database['database']); $database['username'] = env('DB_USERNAME', $database['username']); $database['password'] = env('DB_PASSWORD', $database['password']); - $data->setDatabaseConfiguration($database); + $this->configuration->setDatabaseConfiguration($database); + } - $this->configuration = $data; + protected function refreshApplication() + { + $this->collectsConfiguration(); - $this->setsApplicationConfiguration($data); + $this->seedsDatabase(); - $this->seedsApplication(); + $this->createsSite(); $this->createsHttpForum(); } @@ -93,59 +106,56 @@ trait CreatesForum protected function teardownApplication() { /** @var ConnectionInterface $connection */ - $connection = $this->app->make(ConnectionInterface::class); + $connection = app(ConnectionInterface::class); $connection->rollBack(); } - protected function setsApplicationConfiguration(DataProviderInterface $data) + protected function getFlarumConfig() { - if ($this->isInstalled) { - $dbConfig = $data->getDatabaseConfiguration(); - $this->site->setConfig( - $config = [ - 'debug' => $data->isDebugMode(), - 'database' => [ - 'driver' => $dbConfig['driver'], - 'host' => $dbConfig['host'], - 'database' => $dbConfig['database'], - 'username' => $dbConfig['username'], - 'password' => $dbConfig['password'], - 'charset' => 'utf8mb4', - 'collation' => 'utf8mb4_unicode_ci', - 'prefix' => $dbConfig['prefix'], - 'port' => $dbConfig['port'], - 'strict' => false - ], - 'url' => $data->getBaseUrl(), - 'paths' => [ - 'api' => 'api', - 'admin' => 'admin', - ], - ] - ); - } + $dbConfig = $this->configuration->getDatabaseConfiguration(); + + return [ + 'debug' => $this->configuration->isDebugMode(), + 'database' => [ + 'driver' => $dbConfig['driver'], + 'host' => $dbConfig['host'], + 'database' => $dbConfig['database'], + 'username' => $dbConfig['username'], + 'password' => $dbConfig['password'], + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'prefix' => $dbConfig['prefix'], + 'port' => $dbConfig['port'], + 'strict' => false + ], + 'url' => $this->configuration->getBaseUrl(), + 'paths' => [ + 'api' => 'api', + 'admin' => 'admin', + ], + ]; } - protected function seedsApplication() + protected function seedsDatabase() { - if ($this->isInstalled) { - $app = app(\Illuminate\Contracts\Foundation\Application::class); - - $app->bind(Builder::class, function ($container) { - return $container->make(ConnectionInterface::class)->getSchemaBuilder(); - }); - - /** @var Migrator $migrator */ - $migrator = $app->make(Migrator::class); - if (! $migrator->getRepository()->repositoryExists()) { - $migrator->getRepository()->createRepository(); - } - - $migrator->run(__DIR__.'/../../../migrations'); - - /** @var ConnectionInterface $connection */ - $connection = $app->make(\Illuminate\Database\ConnectionInterface::class); - $connection->beginTransaction(); + if (! $this->isInstalled) { + return; } + + $app = app(\Illuminate\Contracts\Foundation\Application::class); + + $factory = new ConnectionFactory($app); + $db = $factory->make($this->configuration->getDatabaseConfiguration()); + + $repository = new DatabaseMigrationRepository($db, 'migrations'); + $migrator = new Migrator($repository, $db, app('files')); + + if (! $migrator->getRepository()->repositoryExists()) { + $migrator->getRepository()->createRepository(); + } + + $migrator->run(__DIR__.'/../../../migrations'); + + $db->beginTransaction(); } } diff --git a/tests/Test/Concerns/MakesApiRequests.php b/tests/Test/Concerns/MakesApiRequests.php index d963e9af0..3e039ad49 100644 --- a/tests/Test/Concerns/MakesApiRequests.php +++ b/tests/Test/Concerns/MakesApiRequests.php @@ -11,24 +11,17 @@ namespace Flarum\Tests\Test\Concerns; -use Flarum\Api\ApiServiceProvider; use Flarum\Api\Client; use Flarum\User\Guest; -use Flarum\User\SessionServiceProvider; use Flarum\User\User; -use Flarum\User\UserServiceProvider; use Psr\Http\Message\ResponseInterface; trait MakesApiRequests { public function call(string $controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface { - $this->app->register(SessionServiceProvider::class); - $this->app->register(UserServiceProvider::class); - $this->app->register(ApiServiceProvider::class); - $this->app->make('flarum.api.middleware'); /** @var Client $api */ - $api = $this->app->make(Client::class); + $api = app(Client::class); return $api->send($controller, $actor ?? new Guest, $queryParams, $body); }