mirror of
https://github.com/flarum/framework.git
synced 2025-06-07 00:44:34 +08:00
Fix tests after sites refactoring
This commit is contained in:
@ -84,7 +84,7 @@ class CreateUserControllerTest extends ApiControllerTestCase
|
|||||||
public function disabling_sign_up_prevents_user_creation()
|
public function disabling_sign_up_prevents_user_creation()
|
||||||
{
|
{
|
||||||
/** @var SettingsRepositoryInterface $settings */
|
/** @var SettingsRepositoryInterface $settings */
|
||||||
$settings = $this->app->make(SettingsRepositoryInterface::class);
|
$settings = app(SettingsRepositoryInterface::class);
|
||||||
$settings->set('allow_sign_up', false);
|
$settings->set('allow_sign_up', false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -12,9 +12,8 @@
|
|||||||
namespace Flarum\Tests\Install;
|
namespace Flarum\Tests\Install;
|
||||||
|
|
||||||
use Flarum\Install\Console\InstallCommand;
|
use Flarum\Install\Console\InstallCommand;
|
||||||
use Flarum\Install\InstallServiceProvider;
|
|
||||||
use Flarum\Tests\Test\TestCase;
|
use Flarum\Tests\Test\TestCase;
|
||||||
use Flarum\User\User;
|
use Illuminate\Database\Connectors\ConnectionFactory;
|
||||||
use Symfony\Component\Console\Input\StringInput;
|
use Symfony\Component\Console\Input\StringInput;
|
||||||
use Symfony\Component\Console\Output\StreamOutput;
|
use Symfony\Component\Console\Output\StreamOutput;
|
||||||
|
|
||||||
@ -27,13 +26,12 @@ class DefaultInstallationCommandTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function allows_forum_installation()
|
public function allows_forum_installation()
|
||||||
{
|
{
|
||||||
if (file_exists($this->app->basePath().DIRECTORY_SEPARATOR.'config.php')) {
|
if (file_exists(base_path('config.php'))) {
|
||||||
unlink($this->app->basePath().DIRECTORY_SEPARATOR.'config.php');
|
unlink(base_path('config.php'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->app->register(InstallServiceProvider::class);
|
|
||||||
/** @var InstallCommand $command */
|
/** @var InstallCommand $command */
|
||||||
$command = $this->app->make(InstallCommand::class);
|
$command = app(InstallCommand::class);
|
||||||
$command->setDataSource($this->configuration);
|
$command->setDataSource($this->configuration);
|
||||||
|
|
||||||
$body = fopen('php://temp', 'wb+');
|
$body = fopen('php://temp', 'wb+');
|
||||||
@ -42,10 +40,20 @@ class DefaultInstallationCommandTest extends TestCase
|
|||||||
|
|
||||||
$command->run($input, $output);
|
$command->run($input, $output);
|
||||||
|
|
||||||
$this->assertFileExists($this->app->basePath().DIRECTORY_SEPARATOR.'config.php');
|
$this->assertFileExists(base_path('config.php'));
|
||||||
|
|
||||||
$admin = $this->configuration->getAdminUser();
|
$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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,14 +11,17 @@
|
|||||||
|
|
||||||
namespace Flarum\Tests\Test\Concerns;
|
namespace Flarum\Tests\Test\Concerns;
|
||||||
|
|
||||||
|
use Flarum\Database\DatabaseMigrationRepository;
|
||||||
use Flarum\Database\Migrator;
|
use Flarum\Database\Migrator;
|
||||||
use Flarum\Foundation\Application;
|
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\Http\Server;
|
||||||
use Flarum\Install\Console\DataProviderInterface;
|
use Flarum\Install\Console\DataProviderInterface;
|
||||||
use Flarum\Install\Console\DefaultsDataProvider;
|
use Flarum\Install\Console\DefaultsDataProvider;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
use Illuminate\Database\Schema\Builder;
|
use Illuminate\Database\Connectors\ConnectionFactory;
|
||||||
|
|
||||||
trait CreatesForum
|
trait CreatesForum
|
||||||
{
|
{
|
||||||
@ -28,7 +31,7 @@ trait CreatesForum
|
|||||||
protected $http;
|
protected $http;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Site
|
* @var SiteInterface
|
||||||
*/
|
*/
|
||||||
protected $site;
|
protected $site;
|
||||||
|
|
||||||
@ -51,41 +54,51 @@ trait CreatesForum
|
|||||||
|
|
||||||
protected function createsSite()
|
protected function createsSite()
|
||||||
{
|
{
|
||||||
$this->site = (new Site)
|
if ($this->isInstalled) {
|
||||||
->setBasePath(__DIR__.'/../../tmp')
|
$this->site = new InstalledSite(
|
||||||
->setPublicPath(__DIR__.'/../../tmp/public');
|
__DIR__.'/../../tmp',
|
||||||
|
__DIR__.'/../../tmp/public',
|
||||||
|
$this->getFlarumConfig()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$this->site = new UninstalledSite(
|
||||||
|
__DIR__.'/../../tmp',
|
||||||
|
__DIR__.'/../../tmp/public'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function createsHttpForum()
|
protected function createsHttpForum()
|
||||||
{
|
{
|
||||||
$this->http = Server::fromSite(
|
$this->app = $this->site->bootApp();
|
||||||
$this->site
|
|
||||||
);
|
|
||||||
|
|
||||||
$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();
|
$database = $this->configuration->getDatabaseConfiguration();
|
||||||
$data->setSetting('mail_driver', 'log');
|
|
||||||
|
|
||||||
$database = $data->getDatabaseConfiguration();
|
|
||||||
$database['host'] = env('DB_HOST', $database['host']);
|
$database['host'] = env('DB_HOST', $database['host']);
|
||||||
$database['database'] = env('DB_DATABASE', $database['database']);
|
$database['database'] = env('DB_DATABASE', $database['database']);
|
||||||
$database['username'] = env('DB_USERNAME', $database['username']);
|
$database['username'] = env('DB_USERNAME', $database['username']);
|
||||||
$database['password'] = env('DB_PASSWORD', $database['password']);
|
$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();
|
$this->createsHttpForum();
|
||||||
}
|
}
|
||||||
@ -93,59 +106,56 @@ trait CreatesForum
|
|||||||
protected function teardownApplication()
|
protected function teardownApplication()
|
||||||
{
|
{
|
||||||
/** @var ConnectionInterface $connection */
|
/** @var ConnectionInterface $connection */
|
||||||
$connection = $this->app->make(ConnectionInterface::class);
|
$connection = app(ConnectionInterface::class);
|
||||||
$connection->rollBack();
|
$connection->rollBack();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setsApplicationConfiguration(DataProviderInterface $data)
|
protected function getFlarumConfig()
|
||||||
{
|
{
|
||||||
if ($this->isInstalled) {
|
$dbConfig = $this->configuration->getDatabaseConfiguration();
|
||||||
$dbConfig = $data->getDatabaseConfiguration();
|
|
||||||
$this->site->setConfig(
|
return [
|
||||||
$config = [
|
'debug' => $this->configuration->isDebugMode(),
|
||||||
'debug' => $data->isDebugMode(),
|
'database' => [
|
||||||
'database' => [
|
'driver' => $dbConfig['driver'],
|
||||||
'driver' => $dbConfig['driver'],
|
'host' => $dbConfig['host'],
|
||||||
'host' => $dbConfig['host'],
|
'database' => $dbConfig['database'],
|
||||||
'database' => $dbConfig['database'],
|
'username' => $dbConfig['username'],
|
||||||
'username' => $dbConfig['username'],
|
'password' => $dbConfig['password'],
|
||||||
'password' => $dbConfig['password'],
|
'charset' => 'utf8mb4',
|
||||||
'charset' => 'utf8mb4',
|
'collation' => 'utf8mb4_unicode_ci',
|
||||||
'collation' => 'utf8mb4_unicode_ci',
|
'prefix' => $dbConfig['prefix'],
|
||||||
'prefix' => $dbConfig['prefix'],
|
'port' => $dbConfig['port'],
|
||||||
'port' => $dbConfig['port'],
|
'strict' => false
|
||||||
'strict' => false
|
],
|
||||||
],
|
'url' => $this->configuration->getBaseUrl(),
|
||||||
'url' => $data->getBaseUrl(),
|
'paths' => [
|
||||||
'paths' => [
|
'api' => 'api',
|
||||||
'api' => 'api',
|
'admin' => 'admin',
|
||||||
'admin' => 'admin',
|
],
|
||||||
],
|
];
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function seedsApplication()
|
protected function seedsDatabase()
|
||||||
{
|
{
|
||||||
if ($this->isInstalled) {
|
if (! $this->isInstalled) {
|
||||||
$app = app(\Illuminate\Contracts\Foundation\Application::class);
|
return;
|
||||||
|
|
||||||
$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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,24 +11,17 @@
|
|||||||
|
|
||||||
namespace Flarum\Tests\Test\Concerns;
|
namespace Flarum\Tests\Test\Concerns;
|
||||||
|
|
||||||
use Flarum\Api\ApiServiceProvider;
|
|
||||||
use Flarum\Api\Client;
|
use Flarum\Api\Client;
|
||||||
use Flarum\User\Guest;
|
use Flarum\User\Guest;
|
||||||
use Flarum\User\SessionServiceProvider;
|
|
||||||
use Flarum\User\User;
|
use Flarum\User\User;
|
||||||
use Flarum\User\UserServiceProvider;
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
trait MakesApiRequests
|
trait MakesApiRequests
|
||||||
{
|
{
|
||||||
public function call(string $controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface
|
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 */
|
/** @var Client $api */
|
||||||
$api = $this->app->make(Client::class);
|
$api = app(Client::class);
|
||||||
|
|
||||||
return $api->send($controller, $actor ?? new Guest, $queryParams, $body);
|
return $api->send($controller, $actor ?? new Guest, $queryParams, $body);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user