1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 17:14:03 +00:00

Merge 3.x into master

This commit is contained in:
lukinovec 2023-02-17 15:48:00 +01:00
parent 617e9a7a73
commit 8fd19192ec
11 changed files with 105 additions and 54 deletions

View file

@ -15,7 +15,11 @@ jobs:
strategy: strategy:
matrix: matrix:
laravel: ['^9.0'] include:
- laravel: 9
php: "8.0"
- laravel: 10
php: "8.1"
steps: steps:
- name: Checkout - name: Checkout

View file

@ -258,7 +258,7 @@ return [
], ],
/** /**
* Redis tenancy config. Used by RedisTenancyBoostrapper. * Redis tenancy config. Used by RedisTenancyBootstrapper.
* *
* Note: You need phpredis to use Redis tenancy. * Note: You need phpredis to use Redis tenancy.
* *
@ -286,6 +286,7 @@ return [
// Stancl\Tenancy\Features\TelescopeTags::class, // Stancl\Tenancy\Features\TelescopeTags::class,
// Stancl\Tenancy\Features\TenantConfig::class, // https://tenancyforlaravel.com/docs/v3/features/tenant-config // Stancl\Tenancy\Features\TenantConfig::class, // https://tenancyforlaravel.com/docs/v3/features/tenant-config
// Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancyforlaravel.com/docs/v3/features/cross-domain-redirect // Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancyforlaravel.com/docs/v3/features/cross-domain-redirect
// Stancl\Tenancy\Features\ViteBundler::class,
], ],
/** /**

View file

@ -17,18 +17,19 @@
"require": { "require": {
"php": "^8.2", "php": "^8.2",
"ext-json": "*", "ext-json": "*",
"illuminate/support": "^9.38", "illuminate/support": "^9.38|^10.0",
"spatie/ignition": "^1.4", "spatie/ignition": "^1.4",
"ramsey/uuid": "^4.0", "ramsey/uuid": "^4.7.3",
"stancl/jobpipeline": "^1.0", "stancl/jobpipeline": "^1.6.2",
"stancl/virtualcolumn": "^1.3" "stancl/virtualcolumn": "^1.3.1",
"facade/ignition-contracts": "^1.0.2"
}, },
"require-dev": { "require-dev": {
"laravel/framework": "^9.38", "laravel/framework": "^9.38|^10.0",
"orchestra/testbench": "^7.0", "orchestra/testbench": "^7.0|^8.0",
"league/flysystem-aws-s3-v3": "^3.0", "league/flysystem-aws-s3-v3": "^3.12.2",
"doctrine/dbal": "^2.10", "doctrine/dbal": "^3.6.0",
"spatie/valuestore": "^1.2.5", "spatie/valuestore": "^1.3.2",
"pestphp/pest": "^1.21", "pestphp/pest": "^1.21",
"nunomaduro/larastan": "^2.4", "nunomaduro/larastan": "^2.4",
"spatie/invade": "^1.1" "spatie/invade": "^1.1"

View file

@ -1,39 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
backupStaticAttributes="false" <coverage>
bootstrap="vendor/autoload.php" <include>
colors="true" <directory suffix=".php">./src</directory>
convertErrorsToExceptions="true" </include>
convertNoticesToExceptions="true" <exclude>
convertWarningsToExceptions="true" <file>./src/routes.php</file>
processIsolation="false" <file>./src/Vite.php</file>
stopOnFailure="false"> </exclude>
<testsuites> <report>
<testsuite name="Unit"> <clover outputFile="coverage/phpunit/clover.xml"/>
<directory suffix="Test.php">./tests</directory> <html outputDirectory="coverage/phpunit/html" lowUpperBound="35" highLowerBound="70"/>
</testsuite> </report>
</testsuites> </coverage>
<filter> <testsuites>
<whitelist processUncoveredFilesFromWhitelist="true"> <testsuite name="Unit">
<directory suffix=".php">./src</directory> <directory suffix="Test.php">./tests</directory>
<exclude> </testsuite>
<file>./src/routes.php</file> </testsuites>
</exclude> <php>
</whitelist> <env name="APP_ENV" value="testing"/>
</filter> <env name="APP_KEY" value="base64:uYVmTs9lrQbXWfHgSSiG0VZMjc2KG/fBbjV1i1JDVos="/>
<php> <env name="BCRYPT_ROUNDS" value="4"/>
<env name="APP_ENV" value="testing"/> <env name="CACHE_DRIVER" value="redis"/>
<env name="APP_KEY" value="base64:uYVmTs9lrQbXWfHgSSiG0VZMjc2KG/fBbjV1i1JDVos="/> <env name="MAIL_DRIVER" value="array"/>
<env name="BCRYPT_ROUNDS" value="4"/> <env name="QUEUE_CONNECTION" value="sync"/>
<env name="CACHE_DRIVER" value="redis"/> <env name="SESSION_DRIVER" value="array"/>
<env name="MAIL_DRIVER" value="array"/> <env name="DB_CONNECTION" value="central"/>
<env name="QUEUE_CONNECTION" value="sync"/> <env name="AWS_DEFAULT_REGION" value="us-west-2"/>
<env name="SESSION_DRIVER" value="array"/> </php>
<env name="DB_CONNECTION" value="central"/> <logging/>
<env name="AWS_DEFAULT_REGION" value="us-west-2"/>
</php>
<logging>
<log type="coverage-clover" target="coverage/phpunit/clover.xml" showUncoveredFiles="true"/>
<log type="coverage-html" target="coverage/phpunit/html" lowUpperBound="35" highLowerBound="70"/>
</logging>
</phpunit> </phpunit>

View file

@ -32,6 +32,8 @@ class Tenant extends Model implements Contracts\Tenant
Concerns\InitializationHelpers, Concerns\InitializationHelpers,
Concerns\InvalidatesResolverCache; Concerns\InvalidatesResolverCache;
protected static $modelsShouldPreventAccessingMissingAttributes = false;
protected $table = 'tenants'; protected $table = 'tenants';
protected $primaryKey = 'id'; protected $primaryKey = 'id';
protected $guarded = []; protected $guarded = [];

View file

@ -41,7 +41,8 @@ class PermissionControlledMySQLDatabaseManager extends MySQLDatabaseManager impl
protected function isVersion8(): bool protected function isVersion8(): bool
{ {
$version = $this->database()->select($this->database()->raw('select version()'))[0]->{'version()'}; $versionSelect = $this->database()->raw('select version()')->getValue($this->database()->getQueryGrammar());
$version = $this->database()->select($versionSelect)[0]->{'version()'};
return version_compare($version, '8.0.0') >= 0; return version_compare($version, '8.0.0') >= 0;
} }

View file

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Features;
use Illuminate\Foundation\Application;
use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Tenancy;
use Stancl\Tenancy\Vite;
class ViteBundler implements Feature
{
/** @var Application */
protected $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function bootstrap(Tenancy $tenancy): void
{
$this->app->singleton(\Illuminate\Foundation\Vite::class, Vite::class);
}
}

View file

@ -62,6 +62,7 @@ class TenancyServiceProvider extends ServiceProvider
$this->app->singleton(Commands\Rollback::class, function ($app) { $this->app->singleton(Commands\Rollback::class, function ($app) {
return new Commands\Rollback($app['migrator']); return new Commands\Rollback($app['migrator']);
}); });
$this->app->singleton(Commands\Seed::class, function ($app) { $this->app->singleton(Commands\Seed::class, function ($app) {
return new Commands\Seed($app['db']); return new Commands\Seed($app['db']);
}); });

20
src/Vite.php Normal file
View file

@ -0,0 +1,20 @@
<?php
namespace Stancl\Tenancy;
use Illuminate\Foundation\Vite as BaseVite;
class Vite extends BaseVite // todo move to a different directory in v4
{
/**
* Generate an asset path for the application.
*
* @param string $path
* @param bool|null $secure
* @return string
*/
protected function assetPath($path, $secure = null)
{
return global_asset($path);
}
}

View file

@ -79,7 +79,7 @@ test('ing events can be used to cancel db creation', function () {
}); });
$tenant = Tenant::create(); $tenant = Tenant::create();
dispatch_now(new CreateDatabase($tenant)); dispatch_sync(new CreateDatabase($tenant));
pest()->assertFalse($tenant->database()->manager()->databaseExists( pest()->assertFalse($tenant->database()->manager()->databaseExists(
$tenant->database()->getName() $tenant->database()->getName()
@ -171,12 +171,13 @@ test('database is not migrated if creation is disabled', function () {
})->toListener() })->toListener()
); );
Tenant::create([ $tenant = Tenant::create([
'tenancy_create_database' => false, 'tenancy_create_database' => false,
'tenancy_db_name' => 'already_created', 'tenancy_db_name' => 'already_created',
]); ]);
expect(pest()->hasFailed())->toBeFalse(); // Assert test didn't fail
$this->assertTrue($tenant->exists());
}); });
class FooListener extends QueueableListener class FooListener extends QueueableListener

View file

@ -302,7 +302,7 @@ test('database credentials can be provided to PermissionControlledMySQLDatabaseM
$mysql2DB->statement("CREATE USER `{$username}`@`%` IDENTIFIED BY '{$password}';"); $mysql2DB->statement("CREATE USER `{$username}`@`%` IDENTIFIED BY '{$password}';");
$mysql2DB->statement("GRANT ALL PRIVILEGES ON *.* TO `{$username}`@`%` identified by '{$password}' WITH GRANT OPTION;"); $mysql2DB->statement("GRANT ALL PRIVILEGES ON *.* TO `{$username}`@`%` identified by '{$password}' WITH GRANT OPTION;");
$mysql2DB->statement("FLUSH PRIVILEGES;"); $mysql2DB->statement("FLUSH PRIVILEGES;");
DB::purge('mysql2'); // forget the mysql2 connection so that it uses the new credentials the next time DB::purge('mysql2'); // forget the mysql2 connection so that it uses the new credentials the next time
config(['database.connections.mysql2.username' => $username]); config(['database.connections.mysql2.username' => $username]);
@ -347,7 +347,7 @@ test('tenant database can be created by using the username and password from ten
$mysqlDB->statement("CREATE USER `{$username}`@`%` IDENTIFIED BY '{$password}';"); $mysqlDB->statement("CREATE USER `{$username}`@`%` IDENTIFIED BY '{$password}';");
$mysqlDB->statement("GRANT ALL PRIVILEGES ON *.* TO `{$username}`@`%` identified by '{$password}' WITH GRANT OPTION;"); $mysqlDB->statement("GRANT ALL PRIVILEGES ON *.* TO `{$username}`@`%` identified by '{$password}' WITH GRANT OPTION;");
$mysqlDB->statement("FLUSH PRIVILEGES;"); $mysqlDB->statement("FLUSH PRIVILEGES;");
DB::purge('mysql2'); // forget the mysql2 connection so that it uses the new credentials the next time DB::purge('mysql2'); // forget the mysql2 connection so that it uses the new credentials the next time
// Remove `mysql` credentials to make sure we will be using the credentials from the tenant config // Remove `mysql` credentials to make sure we will be using the credentials from the tenant config