From 3314a84b4ede0eab6411231b69e6d2539cf0aaa4 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 24 Oct 2018 23:16:29 +0200 Subject: [PATCH] Fix the test setup and installer tests We are still testing the installation logic, but not testing the actual CLI task. I would love to do that, but IMO we first need to find a way to do this fully from the outside, by invoking and talking to the installer through the shell. Because acceptance tests are easier to do when fully decoupled from the application. (After all, they are intended to save us from breaking things when changing code; and we cannot prove that when we change the tests at the same time.) It might be easier to start with acceptance tests for the web installer, though. --- .../DefaultInstallationCommandTest.php | 59 ------------ .../tests/Install/DefaultInstallationTest.php | 91 +++++++++++++++++++ .../core/tests/Test/Concerns/CreatesForum.php | 60 +++++------- 3 files changed, 114 insertions(+), 96 deletions(-) delete mode 100644 framework/core/tests/Install/DefaultInstallationCommandTest.php create mode 100644 framework/core/tests/Install/DefaultInstallationTest.php diff --git a/framework/core/tests/Install/DefaultInstallationCommandTest.php b/framework/core/tests/Install/DefaultInstallationCommandTest.php deleted file mode 100644 index 0d2fee829..000000000 --- a/framework/core/tests/Install/DefaultInstallationCommandTest.php +++ /dev/null @@ -1,59 +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\Console\InstallCommand; -use Flarum\Tests\Test\TestCase; -use Illuminate\Database\Connectors\ConnectionFactory; -use Symfony\Component\Console\Input\StringInput; -use Symfony\Component\Console\Output\StreamOutput; - -class DefaultInstallationCommandTest extends TestCase -{ - protected $isInstalled = false; - - /** - * @test - */ - public function allows_forum_installation() - { - if (file_exists(base_path('config.php'))) { - unlink(base_path('config.php')); - } - - /** @var InstallCommand $command */ - $command = app(InstallCommand::class); - $command->setDataSource($this->configuration); - - $body = fopen('php://temp', 'wb+'); - $input = new StringInput(''); - $output = new StreamOutput($body); - - $command->run($input, $output); - - $this->assertFileExists(base_path('config.php')); - - $admin = $this->configuration->getAdminUser(); - - $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/framework/core/tests/Install/DefaultInstallationTest.php b/framework/core/tests/Install/DefaultInstallationTest.php new file mode 100644 index 000000000..b7507ea7b --- /dev/null +++ b/framework/core/tests/Install/DefaultInstallationTest.php @@ -0,0 +1,91 @@ + + * + * 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\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')); + + $admin = $this->getAdmin(); + + $this->assertEquals( + $this->getDatabase()->table('users')->find(1)->username, + $admin['username'] + ); + } + + private function getDatabase() + { + $factory = new ConnectionFactory(app()); + + return $factory->make($this->getDatabaseConfiguration()); + } + + private function getAdmin() + { + return [ + 'username' => 'admin', + 'password' => 'password', + 'password_confirmation' => 'password', + 'email' => 'admin@example.com', + ]; + } + + private function getSettings() + { + return [ + 'allow_post_editing' => 'reply', + 'allow_renaming' => '10', + 'allow_sign_up' => '1', + 'custom_less' => '', + 'default_locale' => 'en', + 'default_route' => '/all', + 'extensions_enabled' => '[]', + 'forum_title' => 'Development Forum', + 'forum_description' => '', + 'mail_driver' => 'log', + 'mail_from' => 'noreply@flarum.dev', + 'theme_colored_header' => '0', + 'theme_dark_mode' => '0', + 'theme_primary_color' => '#4D698E', + 'theme_secondary_color' => '#4D698E', + 'welcome_message' => 'This is beta software and you should not use it in production.', + 'welcome_title' => 'Welcome to Development Forum', + ]; + } +} diff --git a/framework/core/tests/Test/Concerns/CreatesForum.php b/framework/core/tests/Test/Concerns/CreatesForum.php index a16b2269d..9046aac0c 100644 --- a/framework/core/tests/Test/Concerns/CreatesForum.php +++ b/framework/core/tests/Test/Concerns/CreatesForum.php @@ -19,9 +19,10 @@ 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\Connectors\ConnectionFactory; +use Illuminate\Database\Connectors\MySqlConnector; +use Illuminate\Database\MySqlConnection; +use Illuminate\Filesystem\Filesystem; trait CreatesForum { @@ -72,26 +73,24 @@ trait CreatesForum $this->app = $this->site->bootApp(); } - protected function collectsConfiguration() + protected function getDatabaseConfiguration() { - $this->configuration = new DefaultsDataProvider(); - - $this->configuration->setDebugMode(); - $this->configuration->setSetting('mail_driver', 'log'); - - $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']); - $database['prefix'] = env('DB_PREFIX', $database['prefix']); - $this->configuration->setDatabaseConfiguration($database); + return [ + 'driver' => 'mysql', + 'host' => env('DB_HOST', 'localhost'), + 'database' => env('DB_DATABASE', 'flarum'), + 'username' => env('DB_USERNAME', 'root'), + 'password' => env('DB_PASSWORD', ''), + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'prefix' => env('DB_PREFIX', ''), + 'port' => '3306', + 'strict' => false, + ]; } protected function refreshApplication() { - $this->collectsConfiguration(); - $this->seedsDatabase(); $this->createsSite(); @@ -108,23 +107,10 @@ trait CreatesForum protected function getFlarumConfig() { - $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(), + 'debug' => true, + 'database' => $this->getDatabaseConfiguration(), + 'url' => 'http://flarum.local', 'paths' => [ 'api' => 'api', 'admin' => 'admin', @@ -138,13 +124,13 @@ trait CreatesForum return; } - $app = app(\Illuminate\Contracts\Foundation\Application::class); + $dbConfig = $this->getDatabaseConfiguration(); - $factory = new ConnectionFactory($app); - $db = $factory->make($this->configuration->getDatabaseConfiguration()); + $pdo = (new MySqlConnector)->connect($dbConfig); + $db = new MySqlConnection($pdo, $dbConfig['database'], $dbConfig['prefix'], $dbConfig); $repository = new DatabaseMigrationRepository($db, 'migrations'); - $migrator = new Migrator($repository, $db, app('files')); + $migrator = new Migrator($repository, $db, new Filesystem); if (! $migrator->getRepository()->repositoryExists()) { $migrator->getRepository()->createRepository();