mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 19:04:03 +00:00
Add DatabaseCreationTest
This commit is contained in:
parent
c71e639767
commit
f993308b16
8 changed files with 128 additions and 50 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
||||||
|
.env
|
||||||
composer.lock
|
composer.lock
|
||||||
vendor/
|
vendor/
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ branches:
|
||||||
- master
|
- master
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
- mysql
|
||||||
- redis-server
|
- redis-server
|
||||||
|
|
||||||
before_install:
|
before_install:
|
||||||
|
|
@ -16,7 +17,13 @@ before_install:
|
||||||
install:
|
install:
|
||||||
- travis_retry composer install --no-interaction
|
- travis_retry composer install --no-interaction
|
||||||
|
|
||||||
script: vendor/bin/phpunit --coverage-clover=coverage.xml
|
before_script:
|
||||||
|
- export DB_USERNAME=root
|
||||||
|
- export DB_PASSWORD=root
|
||||||
|
- mysql -e 'CREATE DATABASE travis_tenancy;'
|
||||||
|
- export DB_DATABASE=travis_tenancy
|
||||||
|
|
||||||
|
script: vendor/bin/phpunit -v --coverage-clover=coverage.xml
|
||||||
|
|
||||||
after_success:
|
after_success:
|
||||||
- export CODECOV_TOKEN="24382d15-84e7-4a55-bea4-c4df96a24a9b"
|
- export CODECOV_TOKEN="24382d15-84e7-4a55-bea4-c4df96a24a9b"
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,8 @@
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"orchestra/testbench": "~3.0",
|
"orchestra/testbench": "~3.0",
|
||||||
"laravel/framework": "5.7.*"
|
"laravel/framework": "5.7.*",
|
||||||
|
"vlucas/phpdotenv": "^2.2"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Stancl\Tenancy\DatabaseCreators;
|
namespace Stancl\Tenancy\DatabaseCreators;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Stancl\Tenancy\Interfaces\DatabaseCreator;
|
use Stancl\Tenancy\Interfaces\DatabaseCreator;
|
||||||
|
|
||||||
class MySQLDatabaseCreator implements DatabaseCreator
|
class MySQLDatabaseCreator implements DatabaseCreator
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,9 @@
|
||||||
|
|
||||||
namespace Stancl\Tenancy;
|
namespace Stancl\Tenancy;
|
||||||
|
|
||||||
use Illuminate\Bus\Queueable;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Stancl\Tenancy\Jobs\QueuedDatabaseCreator;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
|
||||||
use Illuminate\Foundation\Bus\Dispatchable;
|
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
||||||
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
||||||
use Stancl\Tenancy\Interfaces\DatabaseCreator;
|
|
||||||
|
|
||||||
class DatabaseManager
|
class DatabaseManager
|
||||||
{
|
{
|
||||||
|
|
@ -48,7 +43,7 @@ class DatabaseManager
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config('tenancy.queue_database_creation', false)) {
|
if (config('tenancy.queue_database_creation', false)) {
|
||||||
QueuedDatabaseCreator::dispatch(app($databaseCreators[$driver], $name));
|
QueuedDatabaseCreator::dispatch(app($databaseCreators[$driver]), $name);
|
||||||
} else {
|
} else {
|
||||||
app($databaseCreators[$driver])->createDatabase($name);
|
app($databaseCreators[$driver])->createDatabase($name);
|
||||||
}
|
}
|
||||||
|
|
@ -77,31 +72,3 @@ class DatabaseManager
|
||||||
config()->set(['database.connections.tenant.database' => $database_name]);
|
config()->set(['database.connections.tenant.database' => $database_name]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class QueuedDatabaseCreator implements ShouldQueue
|
|
||||||
{
|
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new job instance.
|
|
||||||
*
|
|
||||||
* @param DatabaseCreator $databaseCreator
|
|
||||||
* @param string $databaseName
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __construct(DatabaseCreator $databaseCreator, string $databaseName)
|
|
||||||
{
|
|
||||||
$this->databaseCreator = $databaseCreator;
|
|
||||||
$this->databaseName = $databaseName;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute the job.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function handle()
|
|
||||||
{
|
|
||||||
$this->databaseCreator->createDatabase($databaseName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
38
src/Jobs/QueuedDatabaseCreator.php
Normal file
38
src/Jobs/QueuedDatabaseCreator.php
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Jobs;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Stancl\Tenancy\Interfaces\DatabaseCreator;
|
||||||
|
|
||||||
|
class QueuedDatabaseCreator implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @param DatabaseCreator $databaseCreator
|
||||||
|
* @param string $databaseName
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(DatabaseCreator $databaseCreator, string $databaseName)
|
||||||
|
{
|
||||||
|
$this->databaseCreator = $databaseCreator;
|
||||||
|
$this->databaseName = $databaseName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$this->databaseCreator->createDatabase($databaseName);
|
||||||
|
}
|
||||||
|
}
|
||||||
48
tests/DatabaseCreationTest.php
Normal file
48
tests/DatabaseCreationTest.php
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Stancl\Tenancy\DatabaseManager;
|
||||||
|
use Illuminate\Support\Facades\Queue;
|
||||||
|
use Stancl\Tenancy\Jobs\QueuedDatabaseCreator;
|
||||||
|
|
||||||
|
class DatabaseCreationTest extends TestCase
|
||||||
|
{
|
||||||
|
/** @test */
|
||||||
|
public function sqlite_database_is_created()
|
||||||
|
{
|
||||||
|
$db_name = 'testdatabase' . $this->randomString(10) . '.sqlite';
|
||||||
|
app(DatabaseManager::class)->create($db_name, 'sqlite');
|
||||||
|
$this->assertFileExists(database_path($db_name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function mysql_database_is_created()
|
||||||
|
{
|
||||||
|
if (! $this->isTravis()) {
|
||||||
|
$this->markTestSkipped('As to not bloat your MySQL instance with test databases, this test is not run by default.');
|
||||||
|
}
|
||||||
|
|
||||||
|
config()->set([
|
||||||
|
'database.default' => 'mysql',
|
||||||
|
'database.connections.mysql.database' => 'travis_tenancy',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$db_name = 'testdatabase' . $this->randomString(10);
|
||||||
|
app(DatabaseManager::class)->create($db_name, 'mysql');
|
||||||
|
$this->assertNotEmpty(DB::select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db_name'"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function database_creation_can_be_queued()
|
||||||
|
{
|
||||||
|
Queue::fake();
|
||||||
|
|
||||||
|
config()->set('tenancy.queue_database_creation', true);
|
||||||
|
$db_name = 'testdatabase' . $this->randomString(10) . '.sqlite';
|
||||||
|
app(DatabaseManager::class)->create($db_name, 'sqlite');
|
||||||
|
|
||||||
|
Queue::assertPushed(QueuedDatabaseCreator::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -30,19 +30,23 @@ class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
*/
|
*/
|
||||||
protected function getEnvironmentSetUp($app)
|
protected function getEnvironmentSetUp($app)
|
||||||
{
|
{
|
||||||
$app['config']->set('database.redis.client', 'phpredis');
|
(new \Dotenv\Dotenv(__DIR__ . '/..'))->load();
|
||||||
$app['config']->set('database.redis.tenancy', [
|
|
||||||
'host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
$app['config']->set([
|
||||||
'password' => env('TENANCY_TEST_REDIS_PASSWORD', null),
|
'database.redis.client' => 'phpredis',
|
||||||
'port' => env('TENANCY_TEST_REDIS_PORT', 6379),
|
'database.redis.tenancy' => [
|
||||||
// Use the #14 Redis database unless specified otherwise.
|
'host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||||
// Make sure you don't store anything in this db!
|
'password' => env('TENANCY_TEST_REDIS_PASSWORD', null),
|
||||||
'database' => env('TENANCY_TEST_REDIS_DB', 14),
|
'port' => env('TENANCY_TEST_REDIS_PORT', 6379),
|
||||||
]);
|
// Use the #14 Redis database unless specified otherwise.
|
||||||
$app['config']->set('tenancy.database', [
|
// Make sure you don't store anything in this db!
|
||||||
'based_on' => 'sqlite',
|
'database' => env('TENANCY_TEST_REDIS_DB', 14),
|
||||||
'prefix' => 'tenant',
|
],
|
||||||
'suffix' => '.sqlite',
|
'tenancy.database' => [
|
||||||
|
'based_on' => 'sqlite',
|
||||||
|
'prefix' => 'tenant',
|
||||||
|
'suffix' => '.sqlite',
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,4 +65,15 @@ class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
{
|
{
|
||||||
$app->singleton('Illuminate\Contracts\Http\Kernel', \Stancl\Tenancy\Testing\HttpKernel::class);
|
$app->singleton('Illuminate\Contracts\Http\Kernel', \Stancl\Tenancy\Testing\HttpKernel::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function randomString(int $length = 10)
|
||||||
|
{
|
||||||
|
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isTravis()
|
||||||
|
{
|
||||||
|
// Multiple, just to make sure. Someone might accidentally set one on their computer.
|
||||||
|
return env('CI') && env('TRAVIS') && env('CONTINUOUS_INTEGRATION');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue