Skip to content

Commit

Permalink
merge: Merge in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Dec 16, 2024
2 parents 2cc90d2 + d320e3d commit aa03384
Show file tree
Hide file tree
Showing 70 changed files with 2,822 additions and 201 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ jobs:
- name: Prepare testbench
run: composer clear && composer prepare && composer build

- name: Publish required assets
run: vendor/bin/testbench vendor:publish --provider="Sprout\\SproutServiceProvider"

- name: Execute tests
run: composer test

Expand Down
18 changes: 14 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"description" : "A flexible, seamless and easy to use multitenancy solution for Laravel",
"type" : "library",
"require" : {
"php" : "^8.2",
"laravel/framework": "^11.32",
"php" : "^8.2",
"laravel/framework" : "^11.32",
"league/flysystem-path-prefixing": "^3.0"
},
"require-dev" : {
"phpunit/phpunit" : "^11.0.1",
"orchestra/testbench": "^9.4",
"larastan/larastan" : "^2.9"
"larastan/larastan" : "^2.9",
"infection/infection": "^0.29.8"
},
"license" : "MIT",
"autoload" : {
Expand Down Expand Up @@ -63,13 +64,22 @@
"@clear",
"@prepare",
"@build",
"@php vendor/bin/phpunit"
"@php vendor/bin/phpunit --testsuite=Unit,Feature"
],
"mutation" : [
"@clear",
"@prepare",
"@build",
"@php vendor/bin/infection --threads=12"
]
},
"extra" : {
"laravel": {
"providers": [
"Sprout\\SproutServiceProvider"
],
"facades":[
"Sprout\\Facades\\Sprout"
]
}
},
Expand Down
16 changes: 16 additions & 0 deletions infection.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "vendor/infection/infection/resources/schema.json",
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "build/infection.log",
"html": "build/infection.html"
},
"mutators": {
"@default": true
},
"testFrameworkOptions": "--testsuite=Unit,Feature"
}
10 changes: 8 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
testdox="true"
>
<testsuites>
<testsuite name="Tenanted Laravel Test Suite">
<directory>tests</directory>
<testsuite name="Original">
<directory>tests/_Original</directory>
</testsuite>
<testsuite name="Feature">
<directory>./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory>./tests/Unit</directory>
</testsuite>
</testsuites>
<source>
Expand Down
13 changes: 1 addition & 12 deletions src/Concerns/FindsIdentityInRouteParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Support\Facades\URL;
use Sprout\Contracts\Tenancy;
use Sprout\Contracts\Tenant;
use Sprout\Exceptions\TenantMissing;

/**
* Find Identity in Route Parameter
Expand Down Expand Up @@ -269,22 +268,12 @@ public function setup(Tenancy $tenancy, ?Tenant $tenant): void
* @param array<string, mixed> $parameters
* @param bool $absolute
*
* @phpstan-param TenantClass|null $tenant
* @phpstan-param TenantClass $tenant
*
* @return string
*
* @throws \Sprout\Exceptions\TenantMissing
*/
public function route(string $name, Tenancy $tenancy, Tenant $tenant, array $parameters = [], bool $absolute = true): string
{
if ($tenant === null) {
if (! $tenancy->check()) {
throw TenantMissing::make($tenancy->getName());
}

$tenant = $tenancy->tenant();
}

$parameter = $this->getRouteParameterName($tenancy);

if (! isset($parameters[$parameter])) {
Expand Down
36 changes: 18 additions & 18 deletions src/Concerns/HandlesServiceOverrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,27 @@ trait HandlesServiceOverrides
/**
* Register a service override
*
* @param class-string<\Sprout\Contracts\ServiceOverride> $overrideClass
* @param class-string<\Sprout\Contracts\ServiceOverride> $class
*
* @return static
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function registerOverride(string $overrideClass): static
public function registerOverride(string $class): static
{
if (! is_subclass_of($overrideClass, ServiceOverride::class)) {
throw new InvalidArgumentException('Provided service override [' . $overrideClass . '] does not implement ' . ServiceOverride::class);
if (! is_subclass_of($class, ServiceOverride::class)) {
throw new InvalidArgumentException('Provided service override [' . $class . '] does not implement ' . ServiceOverride::class);
}

// Flag the service override as being registered
$this->registeredOverrides[] = $overrideClass;
$this->registeredOverrides[] = $class;

ServiceOverrideRegistered::dispatch($overrideClass);
ServiceOverrideRegistered::dispatch($class);

if (is_subclass_of($overrideClass, DeferrableServiceOverride::class)) {
$this->registerDeferrableOverride($overrideClass);
if (is_subclass_of($class, DeferrableServiceOverride::class)) {
$this->registerDeferrableOverride($class);
} else {
$this->processOverride($overrideClass);
$this->processOverride($class);
}

return $this;
Expand Down Expand Up @@ -155,13 +155,13 @@ protected function registerDeferrableOverride(string $overrideClass): static
/**
* Check if a service override is bootable
*
* @param class-string<\Sprout\Contracts\ServiceOverride> $overrideClass
* @param class-string<\Sprout\Contracts\ServiceOverride> $class
*
* @return bool
*/
public function isBootableOverride(string $overrideClass): bool
public function isBootableOverride(string $class): bool
{
return isset($this->bootableOverrides[$overrideClass]);
return isset($this->bootableOverrides[$class]);
}

/**
Expand All @@ -170,13 +170,13 @@ public function isBootableOverride(string $overrideClass): bool
* This method returns true if the service override has been booted, or
* false if either it hasn't, or it isn't bootable.
*
* @param class-string<\Sprout\Contracts\ServiceOverride> $overrideClass
* @param class-string<\Sprout\Contracts\ServiceOverride> $class
*
* @return bool
*/
public function hasBootedOverride(string $overrideClass): bool
public function hasBootedOverride(string $class): bool
{
return $this->bootedOverrides[$overrideClass] ?? false;
return $this->bootedOverrides[$class] ?? false;
}

/**
Expand Down Expand Up @@ -236,13 +236,13 @@ protected function bootOverride(string $overrideClass): void
* Check if a service override has been set up
*
* @param \Sprout\Contracts\Tenancy<*> $tenancy
* @param class-string<\Sprout\Contracts\ServiceOverride> $overrideClass
* @param class-string<\Sprout\Contracts\ServiceOverride> $class
*
* @return bool
*/
public function hasSetupOverride(Tenancy $tenancy, string $overrideClass): bool
public function hasSetupOverride(Tenancy $tenancy, string $class): bool
{
return $this->setupOverrides[$tenancy->getName()][$overrideClass] ?? false;
return $this->setupOverrides[$tenancy->getName()][$class] ?? false;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Contracts/IdentityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ public function canResolve(Request $request, Tenancy $tenancy, ResolutionHook $h
* @phpstan-param TenantClass $tenant
*
* @return string
*
* @throws \Sprout\Exceptions\TenantMissing
*/
public function route(string $name, Tenancy $tenancy, Tenant $tenant, array $parameters = [], bool $absolute = true): string;
}
20 changes: 10 additions & 10 deletions src/Database/Eloquent/Observers/BelongsToManyTenantsObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Sprout\Contracts\Tenancy;
use Sprout\Contracts\Tenant;
use Sprout\Exceptions\TenantMismatch;
use Sprout\Exceptions\TenantMissing;
use Sprout\Exceptions\TenantMismatchException;
use Sprout\Exceptions\TenantMissingException;
use Sprout\TenancyOptions;
use function Sprout\sprout;

Expand Down Expand Up @@ -85,8 +85,8 @@ private function isTenantMismatched(Model $model, Tenant&Model $tenant, BelongsT
*
* @phpstan-param ChildModel $model
*
* @throws \Sprout\Exceptions\TenantMismatch
* @throws \Sprout\Exceptions\TenantMissing
* @throws \Sprout\Exceptions\TenantMismatchException
* @throws \Sprout\Exceptions\TenantMissingException
*/
private function passesInitialChecks(Model $model, Tenancy $tenancy, BelongsToMany $relation, bool $succeedOnMatch = false): bool
{
Expand All @@ -100,7 +100,7 @@ private function passesInitialChecks(Model $model, Tenancy $tenancy, BelongsToMa

// If we hit here then there's no tenant, and the model isn't
// marked as tenant being optional, so we throw an exception
throw TenantMissing::make($tenancy->getName());
throw TenantMissingException::make($tenancy->getName());
}

/**
Expand All @@ -118,7 +118,7 @@ private function passesInitialChecks(Model $model, Tenancy $tenancy, BelongsToMa
// So, the current foreign key value doesn't match the current
// tenant, so we'll throw an exception...if we're allowed to
if (TenancyOptions::shouldThrowIfNotRelated($tenancy)) {
throw TenantMismatch::make($model::class, $tenancy->getName());
throw TenantMismatchException::make($model::class, $tenancy->getName());
}

// If we hit here, we should continue without doing anything
Expand Down Expand Up @@ -147,8 +147,8 @@ private function passesInitialChecks(Model $model, Tenancy $tenancy, BelongsToMa
*
* @phpstan-param ChildModel $model
*
* @throws \Sprout\Exceptions\TenantMissing
* @throws \Sprout\Exceptions\TenantMismatch
* @throws \Sprout\Exceptions\TenantMissingException
* @throws \Sprout\Exceptions\TenantMismatchException
*/
public function created(Model $model): void
{
Expand Down Expand Up @@ -199,8 +199,8 @@ public function created(Model $model): void
*
* @phpstan-param ChildModel $model
*
* @throws \Sprout\Exceptions\TenantMissing
* @throws \Sprout\Exceptions\TenantMismatch
* @throws \Sprout\Exceptions\TenantMissingException
* @throws \Sprout\Exceptions\TenantMismatchException
*/
public function retrieved(Model $model): void
{
Expand Down
20 changes: 10 additions & 10 deletions src/Database/Eloquent/Observers/BelongsToTenantObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Sprout\Contracts\Tenancy;
use Sprout\Contracts\Tenant;
use Sprout\Exceptions\TenantMismatch;
use Sprout\Exceptions\TenantMissing;
use Sprout\Exceptions\TenantMismatchException;
use Sprout\Exceptions\TenantMissingException;
use Sprout\TenancyOptions;
use function Sprout\sprout;

Expand Down Expand Up @@ -67,8 +67,8 @@ private function isTenantMismatched(Model $model, Tenant&Model $tenant, BelongsT
*
* @phpstan-param ChildModel $model
*
* @throws \Sprout\Exceptions\TenantMismatch
* @throws \Sprout\Exceptions\TenantMissing
* @throws \Sprout\Exceptions\TenantMismatchException
* @throws \Sprout\Exceptions\TenantMissingException
*/
private function passesInitialChecks(Model $model, Tenancy $tenancy, BelongsTo $relation, bool $succeedOnMatch = false): bool
{
Expand All @@ -82,7 +82,7 @@ private function passesInitialChecks(Model $model, Tenancy $tenancy, BelongsTo $

// If we hit here then there's no tenant, and the model isn't
// marked as tenant being optional, so we throw an exception
throw TenantMissing::make($tenancy->getName());
throw TenantMissingException::make($tenancy->getName());
}

/**
Expand All @@ -100,7 +100,7 @@ private function passesInitialChecks(Model $model, Tenancy $tenancy, BelongsTo $
// So, the current foreign key value doesn't match the current
// tenant, so we'll throw an exception...if we're allowed to
if (TenancyOptions::shouldThrowIfNotRelated($tenancy)) {
throw TenantMismatch::make($model::class, $tenancy->getName());
throw TenantMismatchException::make($model::class, $tenancy->getName());
}

// If we hit here, we should continue without doing anything
Expand Down Expand Up @@ -130,8 +130,8 @@ private function passesInitialChecks(Model $model, Tenancy $tenancy, BelongsTo $
*
* @phpstan-param ChildModel $model
*
* @throws \Sprout\Exceptions\TenantMissing
* @throws \Sprout\Exceptions\TenantMismatch
* @throws \Sprout\Exceptions\TenantMissingException
* @throws \Sprout\Exceptions\TenantMismatchException
*/
public function creating(Model $model): bool
{
Expand Down Expand Up @@ -175,8 +175,8 @@ public function creating(Model $model): bool
*
* @phpstan-param ChildModel $model
*
* @throws \Sprout\Exceptions\TenantMissing
* @throws \Sprout\Exceptions\TenantMismatch
* @throws \Sprout\Exceptions\TenantMissingException
* @throws \Sprout\Exceptions\TenantMismatchException
*/
public function retrieved(Model $model): void
{
Expand Down
6 changes: 3 additions & 3 deletions src/Database/Eloquent/Scopes/BelongsToManyTenantsScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Sprout\Exceptions\TenantMissing;
use Sprout\Exceptions\TenantMissingException;
use function Sprout\sprout;

/**
Expand Down Expand Up @@ -35,7 +35,7 @@ final class BelongsToManyTenantsScope extends TenantChildScope
*
* @return void
*
* @throws \Sprout\Exceptions\TenantMissing
* @throws \Sprout\Exceptions\TenantMissingException
*/
public function apply(Builder $builder, Model $model): void
{
Expand All @@ -54,7 +54,7 @@ public function apply(Builder $builder, Model $model): void
}

// We should throw an exception because the tenant is missing
throw TenantMissing::make($tenancy->getName());
throw TenantMissingException::make($tenancy->getName());
}

// Finally, add the clause so that all queries are scoped to the
Expand Down
6 changes: 3 additions & 3 deletions src/Database/Eloquent/Scopes/BelongsToTenantScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Sprout\Exceptions\TenantMissing;
use Sprout\Exceptions\TenantMissingException;
use function Sprout\sprout;

/**
Expand Down Expand Up @@ -35,7 +35,7 @@ final class BelongsToTenantScope extends TenantChildScope
*
* @return void
*
* @throws \Sprout\Exceptions\TenantMissing
* @throws \Sprout\Exceptions\TenantMissingException
*/
public function apply(Builder $builder, Model $model): void
{
Expand All @@ -54,7 +54,7 @@ public function apply(Builder $builder, Model $model): void
}

// We should throw an exception because the tenant is missing
throw TenantMissing::make($tenancy->getName());
throw TenantMissingException::make($tenancy->getName());
}

// Finally, add the clause so that all queries are scoped to the
Expand Down
Loading

0 comments on commit aa03384

Please sign in to comment.