Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Sep 23, 2024
1 parent fc3744d commit d11bba5
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/Attributes/RequiresDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Attribute;
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use Orchestra\Testbench\Contracts\Attributes\Actionable as ActionableContract;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class RequiresDatabase implements ActionableContract
{
/**
Expand All @@ -18,12 +20,14 @@ final class RequiresDatabase implements ActionableContract
* @param bool $default
*/
public function __construct(
public string $driver,
public array|string $driver,
public ?string $versionRequirement = null,
public ?string $connection = null,
public bool $default = true
) {
//
if (is_array($driver) && $default === true) {
throw new InvalidArgumentException('Unable to validate default connection when given an array of database drivers');
}
}

/**
Expand All @@ -37,14 +41,31 @@ public function handle($app, Closure $action): void
{
$connection = DB::connection($this->connection);

if ($connection->getDriverName() !== $this->driver) {
if ($this->default === true) {
\call_user_func($action, 'markTestSkipped', [\sprintf('Requires %s as the default database connection', $connection->getName())]);
} else {
\call_user_func($action, 'markTestSkipped', [\sprintf('Requires %s to use %s database connection', $connection->getName(), $this->driver)]);
if (
$this->default === true
&& is_string($this->driver)
&& $connection->getDriverName() !== $this->driver
) {
\call_user_func($action, 'markTestSkipped', [\sprintf('Requires %s as the default database connection', $connection->getName())]);
}

$usingCorrectConnection = false;

foreach (Arr::wrap($this->driver) as $driver) {
if ($connection->getDriverName() === $driver) {
$usingCorrectConnection = true;
}
}


if ($usingCorrectConnection === false) {
\call_user_func(
$action,
'markTestSkipped',
[\sprintf('Requires %s to use [%s] database connection', $connection->getName(), Arr::join($driver, ','))]

Check failure on line 65 in src/Attributes/RequiresDatabase.php

View workflow job for this annotation

GitHub Actions / PHP:8.2 PHPStan & Pint

Variable $driver might not be defined.
);
}

if (
! is_null($this->versionRequirement)
&& preg_match('/(?P<operator>[<>=!]{0,2})\s*(?P<version>[\d\.-]+(dev|(RC|alpha|beta)[\d\.])?)[ \t]*\r?$/m', $this->versionRequirement, $matches)
Expand Down

0 comments on commit d11bba5

Please sign in to comment.