1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 01:14:04 +00:00

Laravel 11 support + Docker improvements (#29)

* wip

* bump jobpipeline dependency

* bump pest dependency

* fix composer.json syntax

* minor changes to docker setup

* more docker changes

* compact pest output, remove unnecessary env vars from composer.json

* minor pest tweaks

* mssql fix

* try enabling colors in CI

* try setting --columns=max for pest in CI

* try setting columns using env var instead of --columns in CI

* Revert "try setting columns using env var instead of --columns in CI"

This reverts commit eb3c177aefa97b0a3140d7f0e89c5012a854ff42.

* replace --compact with --no-progress

* try setting a hardcoded columns value in CI

* remove --columns (doesn't work), add back --compact

* try setting COLUMNS to a hardcoded value in CI

* remove alternative env syntax from CI

* fix PrefixCacheBootstrapperTest on L11, skip on L10

* add one more skip() call

* fix validate.yml

* Simplify schema dump, skip dump-related tests in L10

* Rename 'dump' table to 'example'

* Bring schema dump-related tests together, add comments

* Merge schema path-related tests into one, add comments

* Rename dataset parameter

---------

Co-authored-by: lukinovec <lukinovec@gmail.com>
This commit is contained in:
Samuel Štancl 2024-02-18 00:18:31 +01:00 committed by GitHub
parent d2ab2dacf2
commit 32a063b834
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 135 additions and 178 deletions

View file

@ -93,24 +93,6 @@ test('migrate command works with tenants option', function () {
expect(Schema::hasTable('users'))->toBeTrue();
});
test('migrate command loads schema state', function () {
$tenant = Tenant::create();
expect(Schema::hasTable('schema_users'))->toBeFalse();
expect(Schema::hasTable('users'))->toBeFalse();
Artisan::call('tenants:migrate --schema-path="tests/Etc/tenant-schema.dump"');
expect(Schema::hasTable('schema_users'))->toBeFalse();
expect(Schema::hasTable('users'))->toBeFalse();
tenancy()->initialize($tenant);
// Check for both tables to see if missing migrations also get executed
expect(Schema::hasTable('schema_users'))->toBeTrue();
expect(Schema::hasTable('users'))->toBeTrue();
});
test('migrate command only throws exceptions if skip-failing is not passed', function() {
Tenant::create();
@ -151,6 +133,37 @@ test('migrate command does not stop after the first failure if skip-failing is p
expect($migratedTenants)->toBe(2);
});
test('the tenants migrate command uses the schema dump correctly', function (bool $schemaPathAsConfig) {
$artisanCommand = 'tenants:migrate';
if ($schemaPathAsConfig) {
// The schema dump path can be configured in 'tenancy.migration_parameters.--schema-path'
// The tenants:migrate command will use the schema dump located at that path by default
config(['tenancy.migration_parameters.--schema-path' => 'tests/Etc/tenant-schema.dump']);
} else {
// The schema dump path can be passed as an option to the tenants:migrate command
$artisanCommand .= ' --schema-path="tests/Etc/tenant-schema.dump"';
}
$tenant = Tenant::create();
Artisan::call($artisanCommand);
// 'example' is a table included in the tests/Etc/tenant-schema dump
// 'users' is a table created by the migrations
// The tables weren't created in the central database
expect(Schema::hasTable('example'))->toBeFalse();
expect(Schema::hasTable('users'))->toBeFalse();
tenancy()->initialize($tenant);
// Both the table from the schema dump and the table from actual migrations
// Were created in the tenant database
expect(Schema::hasTable('example'))->toBeTrue();
expect(Schema::hasTable('users'))->toBeTrue();
})->with([true, false])
->skip(fn () => str(app()->version())->startsWith('10.'), 'todo@l10 drop laravel 10 support before release');
test('dump command works', function () {
$tenant = Tenant::create();
$schemaPath = 'tests/Etc/tenant-schema-test.dump';
@ -190,26 +203,6 @@ test('dump command generates dump at the path specified in the tenancy migration
expect($schemaPath)->toBeFile();
});
test('migrate command correctly uses the schema dump located at the configured schema path by default', function () {
config(['tenancy.migration_parameters.--schema-path' => 'tests/Etc/tenant-schema.dump']);
$tenant = Tenant::create();
expect(Schema::hasTable('schema_users'))->toBeFalse();
expect(Schema::hasTable('users'))->toBeFalse();
Artisan::call('tenants:migrate');
expect(Schema::hasTable('schema_users'))->toBeFalse();
expect(Schema::hasTable('users'))->toBeFalse();
tenancy()->initialize($tenant);
// schema_users is a table included in the tests/Etc/tenant-schema dump
// Check for both tables to see if missing migrations also get executed
expect(Schema::hasTable('schema_users'))->toBeTrue();
expect(Schema::hasTable('users'))->toBeTrue();
});
test('rollback command works', function () {
$tenant = Tenant::create();
Artisan::call('tenants:migrate');

View file

@ -1,66 +1,17 @@
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
DROP TABLE IF EXISTS `failed_jobs`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `failed_jobs` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`uuid` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`connection` text COLLATE utf8mb4_unicode_ci NOT NULL,
`queue` text COLLATE utf8mb4_unicode_ci NOT NULL,
`payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`exception` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`failed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `failed_jobs_uuid_unique` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `migrations`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `migrations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`migration` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`batch` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `password_resets`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `password_resets` (
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
KEY `password_resets_email_index` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `schema_users` (
);
DROP TABLE IF EXISTS `example`;
CREATE TABLE `example` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email_verified_at` timestamp NULL DEFAULT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
INSERT INTO `migrations` VALUES (2,'2014_10_12_100000_testbench_create_password_resets_table',1);
INSERT INTO `migrations` VALUES (3,'2019_08_19_000000_testbench_create_failed_jobs_table',1);
PRIMARY KEY (`id`)
);

View file

@ -40,7 +40,7 @@ test('correct cache prefix is used in all contexts', function () {
$bootstrapper = app(PrefixCacheTenancyBootstrapper::class);
$expectCachePrefixToBe = function (string $prefix) {
expect($prefix . ':') // RedisStore suffixes prefix with ':'
expect($prefix)
->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix())
->toBe(cache()->getPrefix())
@ -72,9 +72,9 @@ test('correct cache prefix is used in all contexts', function () {
config(['cache.prefix' => null]); // stop prefixing cache keys in central so we can provide prefix manually
app('cache')->forgetDriver(config('cache.default'));
expect(cache($tenantOnePrefix . ':key'))->toBe('tenantone-value');
expect(cache($tenantTwoPrefix . ':key'))->toBe('tenanttwo-value');
});
expect(cache($tenantOnePrefix . 'key'))->toBe('tenantone-value');
expect(cache($tenantTwoPrefix . 'key'))->toBe('tenanttwo-value');
})->skip(fn () => str(app()->version())->startsWith('10.'), 'todo@l10 drop laravel 10 support before release');
test('cache is persisted when reidentification is used', function () {
$tenant1 = Tenant::create();
@ -127,7 +127,7 @@ test('central cache is persisted', function () {
expect(cache()->get('key'))->toBe('central');
expect(cache()->get('key2'))->toBe('central-two');
tenancy()->initialize($tenant1);
expect(cache()->get('key'))->toBe('tenant');
expect(cache()->get('key2'))->toBeNull();
@ -143,12 +143,12 @@ test('cache base prefix is customizable', function () {
tenancy()->initialize($tenant1);
expect($originalPrefix . $prefixBase . $tenant1->getTenantKey() . ':')
expect($originalPrefix . $prefixBase . $tenant1->getTenantKey())
->toBe(cache()->getPrefix())
->toBe(cache()->store('redis2')->getPrefix()) // Non-default store gets prefixed correctly too
->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix());
});
})->skip(fn () => str(app()->version())->startsWith('10.'), 'todo@l10 drop laravel 10 support before release');
test('cache is prefixed correctly when using a repository injected in a singleton', function () {
$this->app->singleton(CacheService::class);
@ -275,12 +275,12 @@ test('non default stores get prefixed too when specified in tenantCacheStores',
tenancy()->initialize($tenant);
// We didn't add a prefix generator for our 'redis2' driver, so we expect the prefix to be generated using the 'default' generator
expect($bootstrapper->generatePrefix($tenant) . ':')
expect($bootstrapper->generatePrefix($tenant))
->toBe(cache()->getPrefix())
->toBe(cache()->store('redis2')->getPrefix()); // Non-default store
tenancy()->end();
});
})->skip(fn () => str(app()->version())->startsWith('10.'), 'todo@l10 drop laravel 10 support before release');
test('cache store prefix generation can be customized', function() {
// Use custom prefix generator
@ -295,14 +295,14 @@ test('cache store prefix generation can be customized', function() {
tenancy()->initialize($tenant = Tenant::create());
// Expect the 'redis' store to use the prefix generated by the custom generator
expect($customPrefixGenerator($tenant) . ':')
expect($customPrefixGenerator($tenant))
->toBe(cache()->getPrefix())
->toBe(cache()->store('redis2')->getPrefix()) // Non-default cache stores specified in $tenantCacheStores are prefixed too
->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix());
tenancy()->end();
});
})->skip(fn () => str(app()->version())->startsWith('10.'), 'todo@l10 drop laravel 10 support before release');
test('stores get prefixed using the default way if no prefix generator is specified', function() {
$originalPrefix = config('cache.prefix');
@ -315,10 +315,10 @@ test('stores get prefixed using the default way if no prefix generator is specif
tenancy()->initialize($tenant);
// All stores use the default way of generating the prefix when the prefix generator isn't specified
expect($defaultPrefix . ':')
->toBe(app(PrefixCacheTenancyBootstrapper::class)->generatePrefix($tenant) . ':')
expect($defaultPrefix)
->toBe(app(PrefixCacheTenancyBootstrapper::class)->generatePrefix($tenant))
->toBe(cache()->getPrefix()) // Get prefix of the default store ('redis')
->toBe(cache()->store('redis2')->getPrefix());
tenancy()->end();
});
})->skip(fn () => str(app()->version())->startsWith('10.'), 'todo@l10 drop laravel 10 support before release');

View file

@ -44,7 +44,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
'--realpath' => true,
]);
// Laravel 6.x support
// Laravel 6.x support todo clean up
$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';
@ -68,9 +68,11 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
$app['config']->set([
'database.default' => 'central',
'cache.default' => 'redis',
'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.redis.client' => 'predis',
'database.connections.central' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
@ -91,11 +93,14 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
]) : [],
],
'database.connections.sqlite.database' => ':memory:',
'database.connections.mysql.charset' => 'utf8mb4',
'database.connections.mysql.collation' => 'utf8mb4_unicode_ci',
'database.connections.mysql.host' => env('TENANCY_TEST_MYSQL_HOST', '127.0.0.1'),
'database.connections.sqlsrv.username' => env('TENANCY_TEST_SQLSRV_USERNAME', 'sa'),
'database.connections.sqlsrv.password' => env('TENANCY_TEST_SQLSRV_PASSWORD', 'P@ssword'),
'database.connections.sqlsrv.host' => env('TENANCY_TEST_SQLSRV_HOST', '127.0.0.1'),
'database.connections.sqlsrv.database' => null,
'database.connections.sqlsrv.trust_server_certificate' => true,
'database.connections.pgsql.host' => env('TENANCY_TEST_PGSQL_HOST', '127.0.0.1'),
'tenancy.filesystem.disks' => [
'local',