-
-
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 ability to generate test Assert for all contracts …
…methods
- Loading branch information
Showing
77 changed files
with
2,544 additions
and
102 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
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,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing; | ||
|
||
use LogicException; | ||
|
||
abstract class AbstractExpectationCallsMap | ||
{ | ||
/** | ||
* @var array<class-string<object>, array<object>> | ||
*/ | ||
private array $_expectationMap = []; | ||
|
||
/** | ||
* @var array<class-string<object>, int> | ||
*/ | ||
private array $_callStep = []; | ||
|
||
/** | ||
* Contains current call number. | ||
*/ | ||
private int $_currentDebugStep = 0; | ||
|
||
public function addExpectation(object $expectation): self | ||
{ | ||
$this->_expectationMap[$expectation::class][] = $expectation; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @template TExpectation | ||
* @param class-string<TExpectation> $class | ||
* @param array<TExpectation> $expectations | ||
*/ | ||
public function setExpectations(string $class, array $expectations): self | ||
{ | ||
$this->_expectationMap[$class] = $expectations; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @template TExpectation of object | ||
* | ||
* @param class-string<TExpectation> $class | ||
* | ||
* @return TExpectation | ||
*/ | ||
protected function getExpectation(string $class): object | ||
{ | ||
$map = $this->_expectationMap[$class] ?? []; | ||
$callStep = $this->_callStep[$class] ?? 0; | ||
|
||
$this->_currentDebugStep = $callStep + 1; | ||
|
||
if (array_key_exists($callStep, $map) === false) { | ||
throw new LogicException($this->getDebugMessage($this->_currentDebugStep, 'not set', 2)); | ||
} | ||
|
||
/** @var TExpectation $expectation */ | ||
$expectation = $map[$callStep]; | ||
$this->_callStep[$class] = $this->_currentDebugStep; | ||
|
||
return $expectation; | ||
} | ||
|
||
protected function getDebugMessage(int $callStep = null, string $reason = 'failed', int $debugLevel = 1): string | ||
{ | ||
$caller = debug_backtrace()[$debugLevel]; | ||
|
||
return sprintf( | ||
'Expectation for [%s@%s] %s for a n (%s) call', | ||
$caller['class'] ?? $this::class, | ||
$caller['function'], | ||
$reason, | ||
$callStep ?? $this->_currentDebugStep | ||
); | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Actions; | ||
|
||
use Illuminate\Contracts\Container\BindingResolutionException; | ||
use Illuminate\Contracts\Container\Container; | ||
use LaraStrict\Testing\Entities\PhpDocEntity; | ||
use LaraStrict\Testing\Enums\PhpType; | ||
use PHPStan\PhpDoc\PhpDocStringResolver; | ||
use ReflectionMethod; | ||
|
||
class ParsePhpDocAction | ||
{ | ||
private ?PhpDocStringResolver $phpDocStringResolver = null; | ||
|
||
public function __construct(Container $container) | ||
{ | ||
if (class_exists(PhpDocStringResolver::class)) { | ||
try { | ||
$this->phpDocStringResolver = $container->make(PhpDocStringResolver::class); | ||
} catch (BindingResolutionException) { | ||
// Package phpstan/phpdoc-parser not installed | ||
} | ||
} | ||
} | ||
|
||
public function execute(ReflectionMethod $method): PhpDocEntity | ||
{ | ||
if ($this->phpDocStringResolver === null) { | ||
return new PhpDocEntity(); | ||
} | ||
|
||
$comment = $method->getDocComment(); | ||
|
||
if ($comment === false) { | ||
return new PhpDocEntity(); | ||
} | ||
|
||
$doc = $this->phpDocStringResolver->resolve($comment); | ||
|
||
$returnTags = $doc->getReturnTagValues(); | ||
$returnType = PhpType::Unknown; | ||
|
||
if ($returnTags !== []) { | ||
$name = (string) $returnTags[0]->type; | ||
$returnType = match ($name) { | ||
'$this', 'self', 'static' => PhpType::Self, | ||
'void' => PhpType::Void, | ||
default => PhpType::Mixed, | ||
}; | ||
} | ||
|
||
return new PhpDocEntity(returnType: $returnType); | ||
} | ||
} |
Oops, something went wrong.