forked from archtechx/tenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCase.php
152 lines (135 loc) · 5.28 KB
/
TestCase.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
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Redis;
use PDO;
use Stancl\Tenancy\Tests\Etc\Tenant;
abstract class TestCase extends \Orchestra\Testbench\TestCase
{
/**
* Setup the test environment.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
Redis::connection('default')->flushdb();
Redis::connection('cache')->flushdb();
file_put_contents(database_path('central.sqlite'), '');
$this->artisan('migrate:fresh', [
'--force' => true,
'--path' => __DIR__ . '/../assets/migrations',
'--realpath' => true,
]);
// Laravel 6.x support
$testResponse = class_exists('Illuminate\Testing\TestResponse') ? 'Illuminate\Testing\TestResponse' : 'Illuminate\Foundation\Testing\TestResponse';
$testResponse::macro('assertContent', function ($content) {
$assertClass = class_exists('Illuminate\Testing\Assert') ? 'Illuminate\Testing\Assert' : 'Illuminate\Foundation\Testing\Assert';
$assertClass::assertSame($content, $this->baseResponse->getContent());
return $this;
});
}
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
if (file_exists(__DIR__ . '/../.env')) {
\Dotenv\Dotenv::create(__DIR__ . '/..')->load();
}
$app['config']->set([
'database.default' => 'central',
'database.redis.cache.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
'database.redis.default.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
'database.redis.options.prefix' => 'foo',
'database.connections.central' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => 'mysql',
'port' => env('DB_PORT', '3306'),
'database' => 'main',
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'database.connections.sqlite.database' => ':memory:',
'database.connections.mysql.host' => env('TENANCY_TEST_MYSQL_HOST', '127.0.0.1'),
'database.connections.pgsql.host' => env('TENANCY_TEST_PGSQL_HOST', '127.0.0.1'),
'tenancy.filesystem.disks' => [
'local',
'public',
's3',
],
'tenancy.redis.tenancy' => env('TENANCY_TEST_REDIS_TENANCY', true),
'database.redis.client' => env('TENANCY_TEST_REDIS_CLIENT', 'phpredis'),
'tenancy.redis.prefixed_connections' => ['default'],
'tenancy.migration_parameters' => [
'--path' => [database_path('../migrations')],
'--realpath' => true,
'--force' => true,
],
'tenancy.bootstrappers.redis' => \Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class,
'queue.connections.central' => [
'driver' => 'sync',
'central' => true,
],
'tenancy.seeder_parameters' => [],
'tenancy.tenant_model' => Tenant::class, // Use test tenant w/ DBs & domains
]);
$app->singleton(\Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class);
}
protected function getPackageProviders($app)
{
return [
\Stancl\Tenancy\TenancyServiceProvider::class,
];
}
protected function getPackageAliases($app)
{
return [
'Tenancy' => \Stancl\Tenancy\Facades\Tenancy::class,
'GlobalCache' => \Stancl\Tenancy\Facades\GlobalCache::class,
];
}
/**
* Resolve application HTTP Kernel implementation.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function resolveApplicationHttpKernel($app)
{
$app->singleton('Illuminate\Contracts\Http\Kernel', Etc\HttpKernel::class);
}
/**
* Resolve application Console Kernel implementation.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function resolveApplicationConsoleKernel($app)
{
$app->singleton('Illuminate\Contracts\Console\Kernel', Etc\ConsoleKernel::class);
}
public function randomString(int $length = 10)
{
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', (int) (ceil($length / strlen($x))))), 1, $length);
}
public function assertArrayIsSubset($subset, $array, string $message = ''): void
{
parent::assertTrue(array_intersect($subset, $array) == $subset, $message);
}
}