Skip to content

Commit

Permalink
WIP [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Feb 15, 2024
1 parent e194c90 commit 45972ae
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 23 deletions.
34 changes: 24 additions & 10 deletions src/Testing/Actions/PathToClassAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[/\\\](?<path>.*)\.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[/\\\](?<path>.*)\.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "app/tests/"
}
}
}
58 changes: 45 additions & 13 deletions tests/Feature/Testing/Commands/MakeExpectationCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 45972ae

Please sign in to comment.