Skip to content

Commit

Permalink
Merge branch '7.x' into 8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
crynobone committed Mar 3, 2025
2 parents cac1c68 + 7a81993 commit eb882bd
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
15 changes: 15 additions & 0 deletions src/Concerns/HandlesRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Orchestra\Testbench\Concerns;

use Attribute;
use Closure;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Application as LaravelApplication;
use Laravel\SerializableClosure\SerializableClosure;
use Orchestra\Testbench\Attributes\DefineRoute;
use Orchestra\Testbench\Attributes\UsesVendor;
use Orchestra\Testbench\Features\TestingFeature;
use Orchestra\Testbench\Foundation\Application;
use Orchestra\Testbench\Foundation\Bootstrap\SyncTestbenchCachedRoutes;
Expand All @@ -20,6 +22,9 @@
*/
trait HandlesRoutes
{
use InteractsWithPHPUnit;
use InteractsWithTestCase;

/**
* Indicates if we have made it through the requireApplicationCachedRoutes function.
*
Expand Down Expand Up @@ -106,6 +111,16 @@ protected function defineStashRoutes(Closure|string $route): void
*/
protected function defineCacheRoutes(Closure|string $route, bool $cached = true): void
{
static::usesTestingFeature($attribute = new UsesVendor, Attribute::TARGET_METHOD);

if (
$this->app instanceof LaravelApplication
&& property_exists($this, 'setUpHasRun')
&& $this->setUpHasRun === true
) {
$attribute->beforeEach($this->app);
}

$files = new Filesystem;

$time = time();
Expand Down
1 change: 1 addition & 0 deletions src/Concerns/InteractsWithPHPUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ protected static function resolvePhpUnitAttributesForMethod(string $className, ?
$attributes = Collection::make(array_merge(
static::$testCaseTestingFeatures,
static::$cachedTestCaseClassAttributes[$className],
static::$testCaseMethodTestingFeatures,
! \is_null($methodName) ? static::$cachedTestCaseMethodAttributes["{$className}:{$methodName}"] : [],
))->groupBy('key')
->map(static function ($attributes) {
Expand Down
33 changes: 25 additions & 8 deletions src/Concerns/InteractsWithTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Orchestra\Testbench\Concerns;

use Attribute;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -38,6 +39,15 @@ trait InteractsWithTestCase
*/
protected static array $testCaseTestingFeatures = [];

/**
* The method attributes for test case's method.
*
* @var array<int, array{key: class-string, instance: object}>
*
* @phpstan-var array<int, array{key: class-string<TTestingFeature>, instance: TTestingFeature}>
*/
protected static array $testCaseMethodTestingFeatures = [];

/**
* Determine if the trait is using given trait (or default to \Orchestra\Testbench\Concerns\Testing trait).
*
Expand Down Expand Up @@ -80,11 +90,12 @@ public static function cachedUsesForTestCase(): array
* Uses testing feature (attribute) on the current test.
*
* @param object $attribute
* @param int $flag
* @return void
*
* @phpstan-param TAttributes $attribute
*/
public static function usesTestingFeature($attribute): void
public static function usesTestingFeature($attribute, int $flag = Attribute::TARGET_CLASS): void
{
if (! AttributeParser::validAttribute($attribute)) {
return;
Expand All @@ -96,13 +107,17 @@ public static function usesTestingFeature($attribute): void
return;
}

/** @var class-string<TTestingFeature> $name */
$name = \get_class($attribute);

static::$testCaseTestingFeatures[] = [
'key' => $name,
'instance' => $attribute,
];
if ($flag & Attribute::TARGET_CLASS) {
static::$testCaseTestingFeatures[] = [
'key' => $attribute::class,
'instance' => $attribute,
];
} elseif ($flag & Attribute::TARGET_METHOD) {
static::$testCaseMethodTestingFeatures[] = [
'key' => $attribute::class,
'instance' => $attribute,
];
}
}

/**
Expand Down Expand Up @@ -150,6 +165,8 @@ protected function tearDownTheTestEnvironmentUsingTestCase(): void
->map(static function ($instance) use ($app) {
$instance->afterEach($app);
});

static::$testCaseMethodTestingFeatures = [];
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/PHPUnit/AttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ protected static function resolveAttribute(ReflectionAttribute $attribute): arra
return [null, null];
}

/** @var class-string<TTestingFeature> $name */
$name = \get_class($instance);

return [$name, $instance];
return [$instance::class, $instance];
}, [null, null], false);
}
}
2 changes: 1 addition & 1 deletion src/Workbench/Workbench.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public static function discoverRoutes(ApplicationContract $app, ConfigContract $
$factoryNamespace = static::detectNamespace('database/factories') ?? 'Workbench\\Database\\Factories\\';

$namespacedFactoryBasename = Str::replaceLast(
'Factory', '', Str::replaceFirst($factoryNamespace, '', \get_class($factory))
'Factory', '', Str::replaceFirst($factoryNamespace, '', $factory::class)
);

$factoryBasename = Str::replaceLast('Factory', '', class_basename($factory));
Expand Down

0 comments on commit eb882bd

Please sign in to comment.