Skip to content

Commit

Permalink
WIP [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Nov 27, 2023
1 parent c1db9d6 commit 7ac6d3f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 40 deletions.
33 changes: 33 additions & 0 deletions src/Testing/Actions/GetComposerJsonDataAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace LaraStrict\Testing\Actions;

use Illuminate\Filesystem\Filesystem;
use LaraStrict\Cache\Contracts\CacheMeServiceContract;
use LaraStrict\Cache\Enums\CacheMeStrategy;
use LaraStrict\Testing\Contracts\GetBasePathForStubsActionContract;

final class GetComposerJsonDataAction
{
public function __construct(
private readonly Filesystem $filesystem,
private readonly GetBasePathForStubsActionContract $getBasePathAction,
private readonly CacheMeServiceContract $cacheMeServiceContract,
)
{
}


public function execute(): mixed
{
return $this->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,
);
}

}
2 changes: 1 addition & 1 deletion src/Testing/Actions/GetDevNamespaceForStubsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
15 changes: 7 additions & 8 deletions src/Testing/Actions/GetNamespaceForStubsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
{
Expand Down
43 changes: 15 additions & 28 deletions src/Testing/Commands/MakeExpectationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:';
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -573,36 +571,25 @@ private function normalizeToClass(string $class, string $basePath, Filesystem $f
/**
* @return array<class-string>
*/
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

interface GetNamespaceForStubsActionContract
{
public function execute(Command $command, string $basePath, string $inputClass): NamespaceEntity;
public function execute(Command $command, string $inputClass): NamespaceEntity;
}
13 changes: 11 additions & 2 deletions src/Testing/Factories/FinderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@

namespace LaraStrict\Testing\Factories;

use LaraStrict\Testing\Actions\GetComposerJsonDataAction;
use LaraStrict\Testing\Contracts\FinderFactoryContract;
use LaraStrict\Testing\Contracts\GetBasePathForStubsActionContract;
use Symfony\Component\Finder\Finder;

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())
Expand Down

0 comments on commit 7ac6d3f

Please sign in to comment.