-
-
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 concern for testing Assert classes
- Loading branch information
Showing
4 changed files
with
147 additions
and
90 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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Concerns; | ||
|
||
use LaraStrict\Testing\AbstractExpectationCallsMap; | ||
use LaraStrict\Testing\Entities\AssertExpectationEntity; | ||
use LogicException; | ||
|
||
trait AssertExpectations | ||
{ | ||
abstract public function expectException(string $exception): void; | ||
|
||
abstract public function expectExceptionMessage(string $message): void; | ||
|
||
/** | ||
* @dataProvider data | ||
*/ | ||
public function testEmpty(AssertExpectationEntity $expectation): void | ||
{ | ||
$this->assertBadCall($expectation->methodName); | ||
$assert = $this->createEmptyAssert(); | ||
|
||
$this->callExpectation($expectation, $assert); | ||
} | ||
|
||
/** | ||
* @dataProvider data | ||
*/ | ||
public function testCallsWithSecondFails(AssertExpectationEntity $expectation): void | ||
{ | ||
/** @var AbstractExpectationCallsMap $assert */ | ||
$assert = call_user_func($expectation->createAssert, $expectation->createAssert); | ||
|
||
$result = $this->callExpectation($expectation, $assert); | ||
|
||
if ($expectation->checkResult) { | ||
$expectedResult = $expectation->checkResultIsSelf ? $assert : $expectation->expectedResult; | ||
$this->assertSame($expectedResult, $result); | ||
} | ||
|
||
$this->assertBadCall(method: $expectation->methodName, callNumber: 2); | ||
$this->callExpectation($expectation, $assert); | ||
} | ||
|
||
public function data(): array | ||
{ | ||
$data = []; | ||
foreach ($this->generateData() as $index => $test) { | ||
$data[$test->methodName . ' #' . $index] = [$test]; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @return array<AssertExpectationEntity> | ||
*/ | ||
abstract protected function generateData(): array; | ||
|
||
abstract protected function createEmptyAssert(): AbstractExpectationCallsMap; | ||
|
||
protected function assertBadCall(string $method, int $callNumber = 1): void | ||
{ | ||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage($method . '] not set for a n (' . $callNumber . ') call'); | ||
} | ||
|
||
protected function callExpectation(AssertExpectationEntity $expectation, AbstractExpectationCallsMap $assert): mixed | ||
{ | ||
return call_user_func($expectation->call, $assert); | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Entities; | ||
|
||
use Closure; | ||
|
||
class AssertExpectationEntity | ||
{ | ||
/** | ||
* @template TAssert | ||
* @template TResult | ||
* @param Closure():TAssert $createAssert | ||
* @param Closure(TAssert):(TResult|void) $call | ||
* @param TResult|null $expectedResult If null is passed then created assert will be used. | ||
*/ | ||
public function __construct( | ||
public readonly string $methodName, | ||
public readonly Closure $createAssert, | ||
public readonly Closure $call, | ||
public readonly bool $checkResult = false, | ||
public readonly bool $checkResultIsSelf = false, | ||
public readonly mixed $expectedResult = null, | ||
) { | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
tests/Unit/Testing/Contracts/Auth/Access/GateAssertExpectationEntity.php
This file was deleted.
Oops, something went wrong.
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