diff --git a/src/Concerns/HandlesRoutes.php b/src/Concerns/HandlesRoutes.php index 3210aed5..b7eca73b 100644 --- a/src/Concerns/HandlesRoutes.php +++ b/src/Concerns/HandlesRoutes.php @@ -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; @@ -20,6 +22,9 @@ */ trait HandlesRoutes { + use InteractsWithPHPUnit; + use InteractsWithTestCase; + /** * Indicates if we have made it through the requireApplicationCachedRoutes function. * @@ -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(); diff --git a/src/Concerns/InteractsWithPHPUnit.php b/src/Concerns/InteractsWithPHPUnit.php index eb688c90..70ef7636 100644 --- a/src/Concerns/InteractsWithPHPUnit.php +++ b/src/Concerns/InteractsWithPHPUnit.php @@ -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) { diff --git a/src/Concerns/InteractsWithTestCase.php b/src/Concerns/InteractsWithTestCase.php index 5906835a..4e79fd69 100644 --- a/src/Concerns/InteractsWithTestCase.php +++ b/src/Concerns/InteractsWithTestCase.php @@ -2,6 +2,7 @@ namespace Orchestra\Testbench\Concerns; +use Attribute; use Illuminate\Foundation\Testing\LazilyRefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Collection; @@ -38,6 +39,15 @@ trait InteractsWithTestCase */ protected static array $testCaseTestingFeatures = []; + /** + * The method attributes for test case's method. + * + * @var array + * + * @phpstan-var array, instance: TTestingFeature}> + */ + protected static array $testCaseMethodTestingFeatures = []; + /** * Determine if the trait is using given trait (or default to \Orchestra\Testbench\Concerns\Testing trait). * @@ -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; @@ -96,13 +107,17 @@ public static function usesTestingFeature($attribute): void return; } - /** @var class-string $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, + ]; + } } /** @@ -150,6 +165,8 @@ protected function tearDownTheTestEnvironmentUsingTestCase(): void ->map(static function ($instance) use ($app) { $instance->afterEach($app); }); + + static::$testCaseMethodTestingFeatures = []; } /** diff --git a/src/PHPUnit/AttributeParser.php b/src/PHPUnit/AttributeParser.php index a49726be..d98af29f 100644 --- a/src/PHPUnit/AttributeParser.php +++ b/src/PHPUnit/AttributeParser.php @@ -120,10 +120,7 @@ protected static function resolveAttribute(ReflectionAttribute $attribute): arra return [null, null]; } - /** @var class-string $name */ - $name = \get_class($instance); - - return [$name, $instance]; + return [$instance::class, $instance]; }, [null, null], false); } } diff --git a/src/Workbench/Workbench.php b/src/Workbench/Workbench.php index 73ea3156..2ae98ec6 100644 --- a/src/Workbench/Workbench.php +++ b/src/Workbench/Workbench.php @@ -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));