-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
TenancySetupTest.php
235 lines (205 loc) · 8.54 KB
/
TenancySetupTest.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace Hyn\MultiTenant\Tests;
use DB;
use File;
use Hyn\Framework\Testing\TestCase;
use Illuminate\Database\Connection;
use Hyn\MultiTenant\MultiTenantServiceProvider;
use Hyn\MultiTenant\Tenant\DatabaseConnection;
class TenancySetupTest extends TestCase
{
/**
* @covers \Hyn\MultiTenant\MultiTenantServiceProvider
*/
public function testPackages()
{
$this->assertTrue(class_exists('Hyn\Framework\FrameworkServiceProvider'),
'Class FrameworkServiceProvider does not exist');
$this->assertNotFalse($this->app->make('hyn.package.multi-tenant'),
'packages are not loaded through FrameworkServiceProvider');
$this->assertTrue(in_array(MultiTenantServiceProvider::class, $this->app->getLoadedProviders()),
'MultiTenantService provider is not loaded in Laravel');
$this->assertTrue($this->app->isBooted());
$this->assertNotFalse($this->app->make('hyn.package.multi-tenant'));
}
/**
* @depends testPackages
*
* @covers \Hyn\MultiTenant\Commands\SetupCommand
* @covers \Hyn\MultiTenant\Tenant\DatabaseConnection::create
* @covers \Hyn\MultiTenant\Tenant\Directory::create
* @covers \Hyn\MultiTenant\Observers\WebsiteObserver::created
* @covers \Hyn\MultiTenant\Observers\HostnameObserver::saved
*
* @covers \HmtTenantsTable
* @covers \HmtWebsitesTable
* @covers \HmtHostnamesTable
*/
public function testCommand()
{
// create first tenant
$this->assertEquals(0, $this->artisan('multi-tenant:setup', [
'--tenant' => 'example',
'--hostname' => 'system.testing', // configured in travis as primary hostname
'--email' => 'info@example.org',
'--webserver' => 'no',
]));
}
/**
* @depends testCommand
* @covers \Hyn\MultiTenant\Repositories\TenantRepository::findByName
* @covers \Hyn\MultiTenant\Contracts\TenantRepositoryContract::findByName
*/
public function testTenantExistence()
{
/* @var \Hyn\MultiTenant\Contracts\TenantRepositoryContract tenant */
$this->tenant = $this->app->make('Hyn\MultiTenant\Contracts\TenantRepositoryContract');
/** @var \Hyn\MultiTenant\Models\Tenant|null $tenant */
$tenant = $this->tenant->findByName('example');
$this->assertNotNull($tenant, 'Tenant from command has not been created');
}
/**
* @depends testTenantExistence
* @covers \Hyn\MultiTenant\Contracts\HostnameRepositoryContract::findByHostname
* @covers \Hyn\MultiTenant\Repositories\HostnameRepository::findByHostname
*/
public function testHostnameExistence()
{
/* @var \Hyn\MultiTenant\Contracts\HostnameRepositoryContract hostname */
$this->hostname = $this->app->make('Hyn\MultiTenant\Contracts\HostnameRepositoryContract');
/** @var \Hyn\MultiTenant\Models\Hostname|null $hostname */
$hostname = $this->hostname->findByHostname('system.testing');
$this->assertNotNull($hostname, 'Hostname from command has not been created');
}
/**
* @depends testTenantExistence
*/
public function testTenantDatabaseExists()
{
$databases = DB::connection(DatabaseConnection::systemConnectionName())->select('SHOW DATABASES');
$found = false;
$list = [];
foreach ($databases as $database) {
if (substr($database->Database, 0, 1) == 1) {
$found = true;
}
$list[] = $database->Database;
}
$this->assertTrue($found, 'Databases found: '.implode(', ', $list));
}
/**
* @depends testTenantDatabaseExists
* @covers \Hyn\MultiTenant\Tenant\Directory
*/
public function testTenantFoldersExist()
{
/* @var \Hyn\MultiTenant\Contracts\HostnameRepositoryContract website */
$this->hostname = $this->app->make('Hyn\MultiTenant\Contracts\HostnameRepositoryContract');
/* @var \Hyn\MultiTenant\Models\Hostname|null $website */
$hostname = $this->hostname->findByHostname('system.testing');
/** @var \Hyn\MultiTenant\Models\Website $website */
$website = $hostname->website;
foreach ($website->directory->pathsToCreate() as $directory) {
// let's check whether the directory has been succesfully created
$this->assertTrue(File::exists($website->directory->{$directory}()));
// directories are created with 0755; let's see whether that is sufficient
$this->assertTrue(File::isWritable($website->directory->{$directory}()));
}
}
/**
* @depends testTenantDatabaseExists
* @covers \Hyn\MultiTenant\Tenant\DatabaseConnection
*/
public function testTenantConnection()
{
/* @var \Hyn\MultiTenant\Contracts\HostnameRepositoryContract website */
$this->hostname = $this->app->make('Hyn\MultiTenant\Contracts\HostnameRepositoryContract');
/* @var \Hyn\MultiTenant\Models\Hostname|null $website */
$hostname = $this->hostname->findByHostname('system.testing');
/** @var \Hyn\MultiTenant\Tenant\DatabaseConnection $connection */
$connection = $hostname->website->database;
$connection->setCurrent();
$this->assertTrue($connection->isCurrent());
$this->assertEquals($connection->name, $connection->getCurrent());
$this->assertTrue($connection->get() instanceof Connection);
}
/**
* @depends testTenantDatabaseExists
* @covers \Hyn\MultiTenant\Commands\Migrate\InstallCommand
* @covers \Hyn\MultiTenant\Commands\Migrate\MigrateCommand
* @covers \TestTenantMigration
*/
public function testTenantMigrationRuns()
{
$this->assertEquals(0, $this->artisan('migrate', [
'--tenant' => 'all',
'--path' => '../../../tests/database/migrations/',
'--force' => true,
]));
}
/**
* @depends testTenantMigrationRuns
* @covers \Hyn\MultiTenant\Commands\Migrate\MigrateCommand
*/
public function testTenantMigratedTableExists()
{
/* @var \Hyn\MultiTenant\Contracts\HostnameRepositoryContract website */
$this->hostname = $this->app->make('Hyn\MultiTenant\Contracts\HostnameRepositoryContract');
/* @var \Hyn\MultiTenant\Models\Hostname|null $website */
$hostname = $this->hostname->findByHostname('system.testing');
$this->assertGreaterThan(0, $hostname
->website
->database
->get()
->table('tenant_migration_test')
->insertGetId(['some_field' => 'foo'])
);
}
/**
* @depends testTenantMigrationRuns
* @covers \Hyn\MultiTenant\Commands\Migrate\MigrateCommand
* @covers \Hyn\MultiTenant\Tenant\DatabaseConnection::setCurrent
*/
public function testTenantMigrationEntryExists()
{
/* @var \Hyn\MultiTenant\Contracts\HostnameRepositoryContract website */
$this->hostname = $this->app->make('Hyn\MultiTenant\Contracts\HostnameRepositoryContract');
/* @var \Hyn\MultiTenant\Models\Hostname|null $website */
$hostname = $this->hostname->findByHostname('system.testing');
if (! $hostname) {
throw new \Exception('Unit test hostname not found');
}
$hostname->website->database->setCurrent();
foreach (File::allFiles(__DIR__.'/database/migrations') as $file) {
$fileBaseName = $file->getBaseName('.'.$file->getExtension());
$this->seeInDatabase('migrations', ['migration' => $fileBaseName], $hostname->website->database->name);
}
}
/**
* @depends testTenantExistence
* @covers \Hyn\MultiTenant\Middleware\HostnameMiddleware
* @note we need a webserver to handle this
*/
public function testMiddleware()
{
//
// /** @var \Hyn\MultiTenant\Contracts\HostnameRepositoryContract website */
// $this->hostname = $this->app->make('Hyn\MultiTenant\Contracts\HostnameRepositoryContract');
// /** @var \Hyn\MultiTenant\Models\Hostname|null $website */
// $hostname = $this->hostname->findByHostname('system.testing');
//
// // test for unregistered hostname
// $this->visit('http://tenant.testing/')
// ->seeStatusCode(200)
// ->seeJson([
// 'hostname' => null
// ]);
//
// // test for registered hostname
// $this->visit('http://system.testing/')
// ->seeStatusCode(200)
// ->seeJson([
// 'hostname' => $hostname
// ]);
}
}