Fix tests after sites refactoring

This commit is contained in:
Franz Liedke 2018-08-15 00:47:00 +02:00
parent fb5740926a
commit 034000ea0b
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
4 changed files with 95 additions and 84 deletions

View File

@ -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 {

View File

@ -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());
}
}

View File

@ -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,17 +106,16 @@ 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(),
$dbConfig = $this->configuration->getDatabaseConfiguration();
return [
'debug' => $this->configuration->isDebugMode(),
'database' => [
'driver' => $dbConfig['driver'],
'host' => $dbConfig['host'],
@ -116,36 +128,34 @@ trait CreatesForum
'port' => $dbConfig['port'],
'strict' => false
],
'url' => $data->getBaseUrl(),
'url' => $this->configuration->getBaseUrl(),
'paths' => [
'api' => 'api',
'admin' => 'admin',
],
]
);
}
];
}
protected function seedsApplication()
protected function seedsDatabase()
{
if ($this->isInstalled) {
if (! $this->isInstalled) {
return;
}
$app = app(\Illuminate\Contracts\Foundation\Application::class);
$app->bind(Builder::class, function ($container) {
return $container->make(ConnectionInterface::class)->getSchemaBuilder();
});
$factory = new ConnectionFactory($app);
$db = $factory->make($this->configuration->getDatabaseConfiguration());
$repository = new DatabaseMigrationRepository($db, 'migrations');
$migrator = new Migrator($repository, $db, app('files'));
/** @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();
}
$db->beginTransaction();
}
}

View File

@ -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);
}