diff --git a/src/Testing/Actions/GetComposerJsonDataAction.php b/src/Testing/Actions/GetComposerJsonDataAction.php new file mode 100644 index 00000000..e5c05ed1 --- /dev/null +++ b/src/Testing/Actions/GetComposerJsonDataAction.php @@ -0,0 +1,33 @@ +cacheMeServiceContract->get( + key: 'larasctrict.composer.json', + getValue: function (): mixed { + $basePath = $this->getBasePathAction->execute(); + return json_decode($this->filesystem->get($basePath . '/composer.json'), true, 512, JSON_THROW_ON_ERROR); + }, + strategy: CacheMeStrategy::Memory, + ); + } + +} diff --git a/src/Testing/Actions/GetDevNamespaceForStubsAction.php b/src/Testing/Actions/GetDevNamespaceForStubsAction.php index fbeba36c..a77df073 100644 --- a/src/Testing/Actions/GetDevNamespaceForStubsAction.php +++ b/src/Testing/Actions/GetDevNamespaceForStubsAction.php @@ -11,7 +11,7 @@ class GetDevNamespaceForStubsAction implements GetNamespaceForStubsActionContract { - public function execute(Command $command, string $basePath, string $inputClass): NamespaceEntity + public function execute(Command $command, string $inputClass): NamespaceEntity { // We want to place Laravel assert / expectations to Laravel Folder. diff --git a/src/Testing/Actions/GetNamespaceForStubsAction.php b/src/Testing/Actions/GetNamespaceForStubsAction.php index 5e64d34e..4bb0920f 100644 --- a/src/Testing/Actions/GetNamespaceForStubsAction.php +++ b/src/Testing/Actions/GetNamespaceForStubsAction.php @@ -16,15 +16,18 @@ class GetNamespaceForStubsAction implements GetNamespaceForStubsActionContract final public const ComposerAutoLoadDev = 'autoload-dev'; final public const ComposerPsr4 = 'psr-4'; + public function __construct( - private readonly Filesystem $filesystem, - ) { + private readonly GetComposerJsonDataAction $getComposerJsonDataAction, + ) + { } - public function execute(Command $command, string $basePath, string $inputClass): NamespaceEntity + + public function execute(Command $command, string $inputClass): NamespaceEntity { // Ask for which namespace which to use for "tests" - $composer = $this->getComposerJsonData($basePath); + $composer = $this->getComposerJsonDataAction->execute(); $autoLoad = $this->getComposerDevAutoLoad($composer); if ($autoLoad !== []) { if (count($autoLoad) === 1) { @@ -47,10 +50,6 @@ public function execute(Command $command, string $basePath, string $inputClass): return new NamespaceEntity($folder, $baseNamespace); } - protected function getComposerJsonData(string $basePath): mixed - { - return json_decode($this->filesystem->get($basePath . '/composer.json'), true, 512, JSON_THROW_ON_ERROR); - } private function getComposerDevAutoLoad(array $composer): array { diff --git a/src/Testing/Commands/MakeExpectationCommand.php b/src/Testing/Commands/MakeExpectationCommand.php index f522af09..fa349a62 100644 --- a/src/Testing/Commands/MakeExpectationCommand.php +++ b/src/Testing/Commands/MakeExpectationCommand.php @@ -8,9 +8,9 @@ use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Str; use LaraStrict\Cache\Contracts\CacheMeServiceContract; +use LaraStrict\Testing\Actions\GetComposerJsonDataAction; use LaraStrict\Testing\Actions\ParsePhpDocAction; use LaraStrict\Testing\Assert\AbstractExpectationCallsMap; -use LaraStrict\Testing\Attributes\TestAssert; use LaraStrict\Testing\Constants\StubConstants; use LaraStrict\Testing\Contracts\GetBasePathForStubsActionContract; use LaraStrict\Testing\Contracts\GetNamespaceForStubsActionContract; @@ -55,8 +55,7 @@ public function handle( GetBasePathForStubsActionContract $getBasePathAction, GetNamespaceForStubsActionContract $getFolderAndNamespaceForStubsAction, ParsePhpDocAction $parsePhpDocAction, - FinderFactoryContract $setUpFinderAction, - CacheMeServiceContract $cacheMeService, + FinderFactoryContract $finderFactory, ): int { if (class_exists(ClassType::class) === false) { $message = 'First install package that is required:'; @@ -75,8 +74,7 @@ public function handle( if ($class === 'all') { $inputClasses = $this->findAllClasses( - $setUpFinderAction->create(), - $cacheMeService, + $finderFactory->create(), ); } else { $inClass = $this->normalizeToClass($class, $basePath, $filesystem); @@ -119,7 +117,7 @@ public function generateExpectationFiles( } // Ask for which namespace which to use for "tests" - $namespace = $getFolderAndNamespaceForStubsAction->execute($this, $basePath, $inputClass); + $namespace = $getFolderAndNamespaceForStubsAction->execute($this, $inputClass); // 1. The first part of namespace should is in 99% App => app. We need to create a valid // namespace in tests folder, lets remove the first namespace and rebuild the correct @@ -573,36 +571,25 @@ private function normalizeToClass(string $class, string $basePath, Filesystem $f /** * @return array */ - private function findAllClasses(Finder $finder, CacheMeServiceContract $cacheMeService): array + private function findAllClasses(Finder $finder): array { $cacheKey = 'larastrict.make.expectation.interfaces'; - $cachedInterfaces = $cacheMeService->get($cacheKey, static fn (): ?array => null); + $cachedInterfaces = get_declared_interfaces(); $classes = []; $interfaces = []; foreach ($finder as $file) { - if ($cachedInterfaces !== null && isset($cachedInterfaces[$file->getPathname()])) { - $class = $cachedInterfaces[$file->getPathname()]; - if ($class === false) { - continue; - } - } else { - $class = $this->fileToClass($file); - if ($class === null) { - $interfaces[$file->getPathname()] = false; - continue; - } - } + $done = require_once $file; - $classReflection = new ReflectionClass($class); - $attributes = $classReflection->getAttributes(TestAssert::class); - if ($attributes === []) { - $interfaces[$file->getPathname()] = $class; - continue; - } - $classes[] = $class; } - $cacheMeService->set($cacheKey, $interfaces); +// $classReflection = new ReflectionClass($class); +// $attributes = $classReflection->getAttributes(TestAssert::class); +// if ($attributes === []) { +// $interfaces[$file->getPathname()] = $class; +// continue; +// } +// $classes[] = $class; +// $cacheMeService->set($cacheKey, $interfaces); return $classes; } diff --git a/src/Testing/Contracts/GetNamespaceForStubsActionContract.php b/src/Testing/Contracts/GetNamespaceForStubsActionContract.php index b4ecd200..d805c058 100644 --- a/src/Testing/Contracts/GetNamespaceForStubsActionContract.php +++ b/src/Testing/Contracts/GetNamespaceForStubsActionContract.php @@ -9,5 +9,5 @@ interface GetNamespaceForStubsActionContract { - public function execute(Command $command, string $basePath, string $inputClass): NamespaceEntity; + public function execute(Command $command, string $inputClass): NamespaceEntity; } diff --git a/src/Testing/Factories/FinderFactory.php b/src/Testing/Factories/FinderFactory.php index f19d44d9..19bfc365 100644 --- a/src/Testing/Factories/FinderFactory.php +++ b/src/Testing/Factories/FinderFactory.php @@ -4,6 +4,7 @@ namespace LaraStrict\Testing\Factories; +use LaraStrict\Testing\Actions\GetComposerJsonDataAction; use LaraStrict\Testing\Contracts\FinderFactoryContract; use LaraStrict\Testing\Contracts\GetBasePathForStubsActionContract; use Symfony\Component\Finder\Finder; @@ -11,12 +12,20 @@ final class FinderFactory implements FinderFactoryContract { public function __construct( - private readonly GetBasePathForStubsActionContract $getBasePathForStubsAction - ) { + private readonly GetComposerJsonDataAction $getComposerJsonDataAction, + private readonly GetBasePathForStubsActionContract $getBasePathForStubsAction, + ) + { } + public function create(): Finder { + $basePath = $this->getBasePathForStubsAction->execute(); + + $data = $this->getComposerJsonDataAction->execute(); + $data->autoload['psr-4']; + return Finder::create()->files() ->name('*.php') ->in($this->getBasePathForStubsAction->execute())