Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Complete the test suite #68

Merged
merged 36 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
66ee087
chore: Temporarily migrate tests
ollieread Nov 18, 2024
04548eb
merge: Merge branch 'main' into test/complete-tests
ollieread Nov 18, 2024
19c939f
test: Separate out test suites
ollieread Nov 18, 2024
fb6376c
refactor: Remove unused method
ollieread Nov 18, 2024
e44e723
fix: Correct naming of markOutsideContext method
ollieread Nov 18, 2024
9554346
test: Start separate unit tests
ollieread Nov 18, 2024
d50337a
chore: Don't pass null to ltrim
ollieread Nov 18, 2024
baea868
build(github): Separate out testsuites for workflow
ollieread Nov 18, 2024
294d12c
build(github): Remove unnecessary vendor:publish
ollieread Nov 18, 2024
297e7d0
build(github): Revert unit test command for workflow
ollieread Nov 18, 2024
c4a3a1b
chore: Fix paths for feature and unit testing
ollieread Nov 18, 2024
5595450
chore: Ensure tests/Feature directory exists
ollieread Nov 18, 2024
a00be22
chore: Reset resolver and hook when current tenant is nullified
ollieread Nov 19, 2024
994871e
chore: Do not generate code coverage for the generic tenant class
ollieread Nov 19, 2024
7d73645
test: Add unit tests for the resolution helper
ollieread Nov 19, 2024
f6eaf00
chore: Tidy up code to use helper functions
ollieread Nov 19, 2024
45d6c80
chore: Add RefreshDatabase trait to relevant tests
ollieread Nov 19, 2024
dc25c65
test: Add unit test for eloquent tenant provider
ollieread Nov 19, 2024
a21189a
test: Add unit test for the database tenant provider
ollieread Nov 19, 2024
6c118f2
chore: Remove unnecessary service override from AuthOverride
ollieread Nov 23, 2024
98854ef
refactor: Boot service override _AFTER_ the application has booted
ollieread Nov 23, 2024
346a6ee
fix: Correct return type for the AddTenantHeaderToResponse middleware
ollieread Nov 25, 2024
c40aa25
merge: Changes from main branch
ollieread Dec 15, 2024
a92444d
chore: Add Sprout facade
ollieread Dec 15, 2024
0e3a1ef
test: Initial rework of unit tests
ollieread Dec 15, 2024
4ee245f
merge: Merge updated v1.2-alpha
ollieread Dec 15, 2024
7f2a099
chore: Tidy up docblocks and method for new route helper
ollieread Dec 15, 2024
b818144
chore: Add Sprout\Sprout::route() method to sprout facade
ollieread Dec 15, 2024
1b00334
chore: Add core Sprout settings repository (#79)
ollieread Dec 16, 2024
a76b9a1
merge: Merging in changes from main
ollieread Dec 16, 2024
c269be6
chore: Skip test with no assertions
ollieread Dec 16, 2024
faf71b8
fix: Fix bug with custom creators
ollieread Dec 16, 2024
a0fca4d
test: Add tests for identity resolver custom creators
ollieread Dec 16, 2024
cd7ea80
refactor: Add missing Exception suffix to custom exception classes
ollieread Dec 16, 2024
73b6ed8
merge: Merge branch 'main' into test/complete-tests
ollieread Dec 16, 2024
d320e3d
test: Add tests for the new route helper method on identity resolvers
ollieread Dec 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/**
* 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);

Check warning on line 66 in src/Concerns/HandlesServiceOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Concerns/HandlesServiceOverrides.php#L66

Added line #L66 was not covered by tests
}

// 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 @@
/**
* 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

Check warning on line 162 in src/Concerns/HandlesServiceOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Concerns/HandlesServiceOverrides.php#L162

Added line #L162 was not covered by tests
{
return isset($this->bootableOverrides[$overrideClass]);
return isset($this->bootableOverrides[$class]);

Check warning on line 164 in src/Concerns/HandlesServiceOverrides.php

View check run for this annotation

Codecov / codecov/patch

src/Concerns/HandlesServiceOverrides.php#L164

Added line #L164 was not covered by tests
}

/**
Expand All @@ -170,13 +170,13 @@
* 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 @@
* 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 @@
*
* @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 @@

// 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());

Check warning on line 103 in src/Database/Eloquent/Observers/BelongsToManyTenantsObserver.php

View check run for this annotation

Codecov / codecov/patch

src/Database/Eloquent/Observers/BelongsToManyTenantsObserver.php#L103

Added line #L103 was not covered by tests
}

/**
Expand All @@ -118,7 +118,7 @@
// 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());

Check warning on line 121 in src/Database/Eloquent/Observers/BelongsToManyTenantsObserver.php

View check run for this annotation

Codecov / codecov/patch

src/Database/Eloquent/Observers/BelongsToManyTenantsObserver.php#L121

Added line #L121 was not covered by tests
}

// If we hit here, we should continue without doing anything
Expand Down Expand Up @@ -147,8 +147,8 @@
*
* @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 @@
*
* @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 @@
*
* @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 @@

// 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());

Check warning on line 85 in src/Database/Eloquent/Observers/BelongsToTenantObserver.php

View check run for this annotation

Codecov / codecov/patch

src/Database/Eloquent/Observers/BelongsToTenantObserver.php#L85

Added line #L85 was not covered by tests
}

/**
Expand All @@ -100,7 +100,7 @@
// 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());

Check warning on line 103 in src/Database/Eloquent/Observers/BelongsToTenantObserver.php

View check run for this annotation

Codecov / codecov/patch

src/Database/Eloquent/Observers/BelongsToTenantObserver.php#L103

Added line #L103 was not covered by tests
}

// If we hit here, we should continue without doing anything
Expand Down Expand Up @@ -130,8 +130,8 @@
*
* @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 @@
*
* @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 @@
*
* @return void
*
* @throws \Sprout\Exceptions\TenantMissing
* @throws \Sprout\Exceptions\TenantMissingException
*/
public function apply(Builder $builder, Model $model): void
{
Expand All @@ -54,7 +54,7 @@
}

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

Check warning on line 57 in src/Database/Eloquent/Scopes/BelongsToManyTenantsScope.php

View check run for this annotation

Codecov / codecov/patch

src/Database/Eloquent/Scopes/BelongsToManyTenantsScope.php#L57

Added line #L57 was not covered by tests
}

// 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 @@
*
* @return void
*
* @throws \Sprout\Exceptions\TenantMissing
* @throws \Sprout\Exceptions\TenantMissingException
*/
public function apply(Builder $builder, Model $model): void
{
Expand All @@ -54,7 +54,7 @@
}

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

Check warning on line 57 in src/Database/Eloquent/Scopes/BelongsToTenantScope.php

View check run for this annotation

Codecov / codecov/patch

src/Database/Eloquent/Scopes/BelongsToTenantScope.php#L57

Added line #L57 was not covered by tests
}

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