diff --git a/src/Testing/Actions/PathToClassAction.php b/src/Testing/Actions/PathToClassAction.php index 71e3ae8c..6253ad66 100644 --- a/src/Testing/Actions/PathToClassAction.php +++ b/src/Testing/Actions/PathToClassAction.php @@ -40,23 +40,37 @@ public function execute(string $path): string */ private function replacePathToClass(array $dirs, string $path): ?string { + $map = []; foreach ($dirs as $ns => $dir) { if (str_starts_with($path, $dir) === false) { continue; } - /** @var class-string $class */ - $class = preg_replace_callback( - sprintf('~^%s[/\\\](?.*)\.php$~', preg_quote($dir, '~')), - static fn (array $matches) => $ns . strtr($matches['path'], [ + + $map[] = [ + 'count' => strlen($ns), + 'data' => ['ns' => $ns, 'dir' => $dir], + ]; + } + + if ($map === []) { + return null; + } + + usort($map, static fn(array $a, array $b) => $b['count'] <=> $a['count']); + ['dir' => $dir, 'ns' => $ns] = $map[0]['data']; + + /** @var class-string $class */ + $class = preg_replace_callback( + sprintf('~^%s[/\\\](?.*)\.php$~', preg_quote($dir, '~')), + static fn (array $matches) => $ns . strtr($matches['path'], [ '/' => '\\', ]), - $path - ); - assert(is_string($class)); + $path + ); + assert(is_string($class)); + + return $class; - return $class; - } - return null; } } diff --git a/tests/Feature/Testing/Commands/MakeExpectationCommand/composer.json b/tests/Feature/Testing/Commands/MakeExpectationCommand/composer.json new file mode 100644 index 00000000..450ac9ed --- /dev/null +++ b/tests/Feature/Testing/Commands/MakeExpectationCommand/composer.json @@ -0,0 +1,7 @@ +{ + "autoload-dev": { + "psr-4": { + "App\\Tests\\": "app/tests/" + } + } +} diff --git a/tests/Feature/Testing/Commands/MakeExpectationCommandTest.php b/tests/Feature/Testing/Commands/MakeExpectationCommandTest.php index fb4de6a1..c2db1e4a 100644 --- a/tests/Feature/Testing/Commands/MakeExpectationCommandTest.php +++ b/tests/Feature/Testing/Commands/MakeExpectationCommandTest.php @@ -242,6 +242,15 @@ protected function expectClass(bool $useClass, string $fileName = 'TestAction'): ->once() ->withArgs($this->expectClassFileExistsArgClosure()) ->andReturn(file_get_contents($realPath)); + $this->fileSystem->shouldReceive('isFile') + ->once() + ->withArgs($this->expectClassFileExistsArgClosure()) + ->andReturn(true); + + $this->fileSystem->shouldReceive('dirname') + ->once() + ->withArgs($this->expectClassFileExistsArgClosure()) + ->andReturn(dirname($realPath)); } } @@ -254,23 +263,47 @@ protected function assertCommand( bool $expectComposerJson = true, ): void { if ($expectComposerJson) { + if ($variantPrefix !== null) { + $composerPath = __DIR__ . DIRECTORY_SEPARATOR . 'MakeExpectationCommand' . DIRECTORY_SEPARATOR . $variantPrefix . '.composer.json'; + } else { + $composerPath = __DIR__ . DIRECTORY_SEPARATOR . 'MakeExpectationCommand' . DIRECTORY_SEPARATOR . 'composer.json'; + } + + $expectedComposerCheck = __DIR__ . DIRECTORY_SEPARATOR . 'MakeExpectationCommand' . DIRECTORY_SEPARATOR . 'composer.json'; + + // We are checking if the composer we detect exists + // our implementation expets always composer.json file + $this->fileSystem->shouldReceive('isFile') + ->once() + ->with(function($arg) use ($expectedComposerCheck){ + + $x = 1; + }) + ->andReturn(true); + $this->fileSystem->shouldReceive('get') ->once() - ->withArgs( - static fn (string $path): bool => str_contains( - $path, - '/vendor/orchestra/testbench-core/laravel/composer.json' - ) - ) - ->andReturnUsing(static function (string $path) use ($variantPrefix): string { - if ($variantPrefix !== null) { - $variantPrefix = __DIR__ . DIRECTORY_SEPARATOR . 'MakeExpectationCommand' . DIRECTORY_SEPARATOR . $variantPrefix . '.composer.json'; + ->with($expectedComposerCheck) + ->andReturnUsing(static function (string $path) use ($composerPath): string { + $fileGetContents = file_get_contents($composerPath); + if ($fileGetContents === false) { + throw new LogicException('File not loaded' . $composerPath); } - $filePath = $variantPrefix ?? $path; - $fileGetContents = file_get_contents($filePath); + return $fileGetContents; + }); + + //Transform real testbanch composer to our expectation composer + $this->fileSystem->shouldReceive('get') + ->once() + ->withArgs(static fn (string $path): bool => str_contains( + $path, + '/vendor/orchestra/testbench-core/laravel/composer.json' + )) + ->andReturnUsing(static function (string $path) use ($composerPath): string { + $fileGetContents = file_get_contents($composerPath); if ($fileGetContents === false) { - throw new LogicException('File not loaded' . $filePath); + throw new LogicException('File not loaded' . $composerPath); } return $fileGetContents; @@ -328,7 +361,6 @@ protected function expectResultFile( ]); $this->fileSystem->shouldReceive('ensureDirectoryExists') - ->once() ->withArgs(static fn (string $path): bool => str_contains($path, $expectedPath)); $this->fileSystem->shouldReceive('put')