Skip to content

Commit

Permalink
test: Initial rework of unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Dec 15, 2024
1 parent a92444d commit 0e3a1ef
Show file tree
Hide file tree
Showing 19 changed files with 1,217 additions and 41 deletions.
13 changes: 10 additions & 3 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 @@ -64,6 +65,12 @@
"@prepare",
"@build",
"@php vendor/bin/phpunit --testsuite=Unit,Feature"
],
"mutation" : [
"@clear",
"@prepare",
"@build",
"@php vendor/bin/infection --threads=12"
]
},
"extra" : {
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"
}
34 changes: 17 additions & 17 deletions src/Concerns/HandlesServiceOverrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ 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;

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 @@ -145,13 +145,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 @@ -160,13 +160,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 @@ -224,13 +224,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
10 changes: 10 additions & 0 deletions src/Http/Resolvers/CookieIdentityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public function getCookieName(): string
return $this->cookie;
}

/**
* Get the extra cookie options
*
* @return array<string, mixed>
*/
public function getOptions(): array
{
return $this->options;
}

/**
* Get the cookie name with replacements
*
Expand Down
5 changes: 2 additions & 3 deletions src/Http/Resolvers/PathIdentityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ public function routes(Router $router, Closure $groupRoutes, Tenancy $tenancy):
{
return $this->applyParameterPatternMapping(
$router->middleware([TenantRoutes::ALIAS . ':' . $this->getName() . ',' . $tenancy->getName()])
->prefix($this->getRoutePrefix($tenancy))
->group($groupRoutes),
->prefix($this->getRoutePrefix($tenancy)),
$tenancy
);
)->group($groupRoutes);
}

/**
Expand Down
27 changes: 18 additions & 9 deletions src/Http/Resolvers/SubdomainIdentityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ final class SubdomainIdentityResolver extends BaseIdentityResolver implements Id
/**
* Create a new instance
*
* @param string $name
* @param string $domain
* @param string|null $pattern
* @param string|null $parameter
* @param array<\Sprout\Support\ResolutionHook> $hooks
* @param string $name
* @param string $domain
* @param string|null $pattern
* @param string|null $parameter
* @param array<\Sprout\Support\ResolutionHook> $hooks
*/
public function __construct(string $name, string $domain, ?string $pattern = null, ?string $parameter = null, array $hooks = [])
{
Expand Down Expand Up @@ -79,6 +79,16 @@ public function resolveFromRequest(Request $request, Tenancy $tenancy): ?string
return null;
}

/**
* Get the domain the subdomains belong to
*
* @return string
*/
public function getDomain(): string
{
return $this->domain;
}

/**
* Get the domain name with parameter for the route definition
*
Expand All @@ -88,7 +98,7 @@ public function resolveFromRequest(Request $request, Tenancy $tenancy): ?string
*
* @return string
*/
protected function getRouteDomain(Tenancy $tenancy): string
public function getRouteDomain(Tenancy $tenancy): string
{
return $this->getRouteParameter($tenancy) . '.' . $this->domain;
}
Expand All @@ -111,10 +121,9 @@ public function routes(Router $router, Closure $groupRoutes, Tenancy $tenancy):
{
return $this->applyParameterPatternMapping(
$router->domain($this->getRouteDomain($tenancy))
->middleware([TenantRoutes::ALIAS . ':' . $this->getName() . ',' . $tenancy->getName()])
->group($groupRoutes),
->middleware([TenantRoutes::ALIAS . ':' . $this->getName() . ',' . $tenancy->getName()]),
$tenancy
);
)->group($groupRoutes);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Managers/IdentityResolverManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class IdentityResolverManager extends BaseFactory
*
* @return string
*/
protected function getFactoryName(): string
public function getFactoryName(): string
{
return 'resolver';
}
Expand All @@ -40,7 +40,7 @@ protected function getFactoryName(): string
*
* @return string
*/
protected function getConfigKey(string $name): string
public function getConfigKey(string $name): string
{
return 'multitenancy.resolvers.' . $name;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Managers/ProviderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class ProviderManager extends BaseFactory
*
* @return string
*/
protected function getFactoryName(): string
public function getFactoryName(): string
{
return 'provider';
}
Expand All @@ -40,7 +40,7 @@ protected function getFactoryName(): string
*
* @return string
*/
protected function getConfigKey(string $name): string
public function getConfigKey(string $name): string
{
return 'multitenancy.providers.' . $name;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Managers/TenancyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(Application $app, ProviderManager $providerManager)
*
* @return string
*/
protected function getFactoryName(): string
public function getFactoryName(): string
{
return 'tenancy';
}
Expand All @@ -52,7 +52,7 @@ protected function getFactoryName(): string
*
* @return string
*/
protected function getConfigKey(string $name): string
public function getConfigKey(string $name): string
{
return 'multitenancy.tenancies.' . $name;
}
Expand Down
35 changes: 32 additions & 3 deletions src/Support/BaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,25 @@ public function __construct(Application $app)
$this->app = $app;
}

/**
* Check if a factory has a driver
*
* @param string $name
*
* @return bool
*/
public function hasDriver(string $name): bool
{
return isset(static::$customCreators[$name])
|| method_exists($this, 'create' . ucfirst($name) . ucfirst($this->getFactoryName()));
}

/**
* Get the name used by this factory
*
* @return string
*/
abstract protected function getFactoryName(): string;
abstract public function getFactoryName(): string;

/**
* Get the config key for the given name
Expand All @@ -83,7 +96,7 @@ abstract protected function getFactoryName(): string;
*
* @return string
*/
abstract protected function getConfigKey(string $name): string;
abstract public function getConfigKey(string $name): string;

/**
* Get the default name
Expand All @@ -92,7 +105,7 @@ abstract protected function getConfigKey(string $name): string;
*
* @throws \Sprout\Exceptions\MisconfigurationException
*/
protected function getDefaultName(): string
public function getDefaultName(): string
{
/** @var \Illuminate\Config\Repository $config */
$config = app('config');
Expand Down Expand Up @@ -230,4 +243,20 @@ public function flushResolved(): static

return $this;
}

/**
* Check if a driver has already been resolved
*
* @param string|null $name
*
* @return bool
*/
public function hasResolved(?string $name = null): bool
{
if ($name === null) {
return ! empty($this->objects);
}

return isset($this->objects[$name]);
}
}
10 changes: 10 additions & 0 deletions src/Support/BaseIdentityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public function getName(): string
return $this->name;
}

/**
* Get the hooks this resolver uses
*
* @return array<\Sprout\Support\ResolutionHook>
*/
public function getHooks(): array
{
return $this->hooks;
}

/**
* Perform setup actions for the tenant
*
Expand Down
Loading

0 comments on commit 0e3a1ef

Please sign in to comment.