-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Testing): Add new attribute for mark interface for automatic gen…
…erated expectation
- Loading branch information
Showing
12 changed files
with
309 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Actions; | ||
|
||
use Illuminate\Filesystem\Filesystem; | ||
use LaraStrict\Testing\Contracts\GetBasePathForStubsActionContract; | ||
use LaraStrict\Testing\Services\ComposerJsonDataService; | ||
|
||
final class ComposerAutoloadAbsoluteAction | ||
{ | ||
/** | ||
* @var array<string, string> | ||
*/ | ||
private array $dirs; | ||
|
||
public function __construct( | ||
private readonly ComposerJsonDataService $getComposerJsonDataAction, | ||
private readonly Filesystem $filesystem, | ||
GetBasePathForStubsActionContract $getBasePathForStubsAction, | ||
) { | ||
$this->dirs = $this->makeDirs($getBasePathForStubsAction->execute()); | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
public function execute(?string $path = null): array | ||
{ | ||
if ($path !== null) { | ||
$this->dirs += $this->loadNewComposer($path); | ||
} | ||
|
||
return $this->dirs; | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
private function makeDirs(string $basePath): array | ||
{ | ||
$data = $this->getComposerJsonDataAction->data($basePath); | ||
$dirs = []; | ||
|
||
if (isset($data['autoload']['psr-4']) && is_array($data['autoload']['psr-4'])) { | ||
foreach ($data['autoload']['psr-4'] as $ns => $path) { | ||
$dirs[$ns] = $basePath . DIRECTORY_SEPARATOR . trim((string) $path, '\\/'); | ||
} | ||
} | ||
|
||
return $dirs; | ||
} | ||
|
||
private function loadNewComposer(string $path): array | ||
{ | ||
if ($this->filesystem->isFile($path)) { | ||
$path = $this->filesystem->dirname($path); | ||
} elseif ($this->filesystem->isDirectory($path) === false) { | ||
throw new \Exception(sprintf('The path is not dir "%s".', $path)); | ||
} | ||
|
||
while ($this->getComposerJsonDataAction->isExist($path) === false) { | ||
$path = $this->filesystem->dirname($path); | ||
} | ||
|
||
return $this->makeDirs($path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Actions; | ||
|
||
use Exception; | ||
|
||
final class PathToClassAction | ||
{ | ||
public function __construct( | ||
private readonly ComposerAutoloadAbsoluteAction $composerAutoloadAbsoluteAction, | ||
) { | ||
} | ||
|
||
public function execute(string $path): string | ||
{ | ||
$dirs = $this->composerAutoloadAbsoluteAction->execute(); | ||
|
||
$class = $this->replacePathToClass($dirs, $path); | ||
if ($class === null) { | ||
$dirs = $this->composerAutoloadAbsoluteAction->execute($path); | ||
$class = $this->replacePathToClass($dirs, $path); | ||
|
||
if ($class === null) { | ||
throw new Exception(sprintf('Path "%s" not found in composer psr-4.', $path)); | ||
} | ||
} | ||
|
||
return $class; | ||
} | ||
|
||
private function replacePathToClass(array $dirs, string $path): ?string | ||
{ | ||
foreach ($dirs as $ns => $dir) { | ||
if (str_starts_with($path, $dir) === false) { | ||
continue; | ||
} | ||
|
||
$class = preg_replace_callback( | ||
sprintf('~^%s[/\\\](?<path>.*)\.php$~', preg_quote($dir, '~')), | ||
static fn (array $matches) => $ns . strtr($matches['path'], [ | ||
'/' => '\\', | ||
]), | ||
$path | ||
); | ||
assert(is_string($class)); | ||
|
||
return $class; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Attributes; | ||
|
||
use Attribute; | ||
|
||
#[Attribute] | ||
final class TestAssert | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Contracts; | ||
|
||
use Symfony\Component\Finder\Finder; | ||
|
||
interface FinderFactoryContract | ||
{ | ||
public function create(): Finder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.