forked from archtechx/tenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTenantDatabaseManagerTest.php
113 lines (92 loc) · 3.77 KB
/
TenantDatabaseManagerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Queue;
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator;
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseDeleter;
use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager;
use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager;
use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager;
use Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager;
class TenantDatabaseManagerTest extends TestCase
{
public $autoInitTenancy = false;
/**
* @test
* @dataProvider database_manager_provider
*/
public function databases_can_be_created_and_deleted($driver, $databaseManager)
{
if (! $this->isContainerized()) {
$this->markTestSkipped('As to not bloat your computer with test databases, this test is not run by default.');
}
$name = 'db' . $this->randomString();
$this->assertFalse(app($databaseManager)->databaseExists($name));
app($databaseManager)->createDatabase($name);
$this->assertTrue(app($databaseManager)->databaseExists($name));
app($databaseManager)->deleteDatabase($name);
$this->assertFalse(app($databaseManager)->databaseExists($name));
}
/** @test */
public function dbs_can_be_created_when_another_driver_is_used_for_the_central_db()
{
$this->assertSame('sqlite', config('database.default'));
$database = 'db' . $this->randomString();
app(MySQLDatabaseManager::class)->createDatabase($database);
$this->assertTrue(app(MySQLDatabaseManager::class)->databaseExists($database));
$database = 'db2' . $this->randomString();
app(PostgreSQLDatabaseManager::class)->createDatabase($database);
$this->assertTrue(app(PostgreSQLDatabaseManager::class)->databaseExists($database));
}
/**
* @test
* @dataProvider database_manager_provider
*/
public function databases_can_be_created_and_deleted_using_queued_commands($driver, $databaseManager)
{
if (! $this->isContainerized()) {
$this->markTestSkipped('As to not bloat your computer with test databases, this test is not run by default.');
}
config()->set('database.default', $driver);
$name = 'db' . $this->randomString();
$this->assertFalse(app($databaseManager)->databaseExists($name));
$job = new QueuedTenantDatabaseCreator(app($databaseManager), $name);
$job->handle();
$this->assertTrue(app($databaseManager)->databaseExists($name));
$job = new QueuedTenantDatabaseDeleter(app($databaseManager), $name);
$job->handle();
$this->assertFalse(app($databaseManager)->databaseExists($name));
}
public function database_manager_provider()
{
return [
['mysql', MySQLDatabaseManager::class],
['sqlite', SQLiteDatabaseManager::class],
['pgsql', PostgreSQLDatabaseManager::class],
['pgsql', PostgreSQLSchemaManager::class],
];
}
/** @test */
public function database_creation_can_be_queued()
{
Queue::fake();
config()->set([
'tenancy.queue_database_creation' => true,
]);
Tenant::create(['test2.localhost']);
Queue::assertPushed(QueuedTenantDatabaseCreator::class);
}
/** @test */
public function database_deletion_can_be_queued()
{
Queue::fake();
$tenant = Tenant::create(['test2.localhost']);
config()->set([
'tenancy.queue_database_deletion' => true,
'tenancy.delete_database_after_tenant_deletion' => true,
]);
$tenant->delete();
Queue::assertPushed(QueuedTenantDatabaseDeleter::class);
}
}