-
-
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): AssertExpectations automatically checks if expectation…
…s were called BREAKING CHANGE: Due the changes the expectation logic has been changed and you need to update your code: - `LaraStrict\Testing\AbstractExpectationCallsMap` moved to `LaraStrict\Testing\Assert\AbstractExpectationCallsMap` - Every generated Assert needs to be updated (regenerate or manually change it): - Add `parent::construct()` the the constructor - Add missing `|null` to expectations phpdoc if missing - Remove `array_values(array_filter(` usage (not required) - Example src/Testing/Context/Contracts/ContextServiceContractAssert.php - `LaraStrict\Testing\AbstractExpectationCallMap` was removed in favor of `AbstractExpectationCallsMap` - example of change: `src/Testing/Database/Contracts/SafeUniqueSaveActionContractAssert.php` - Extend AbstractExpectationCallsMap - Add constructor method and call parent - Add `array $expectations = []` to the constructor - Move template value from `@extends` to phpdoc for $expectations - Remove `@extends` phpdoc - Add expectation class to `getExpectation call` ```php /** * @param array<SafeUniqueSaveActionContractExpectation|null> $expectations */ public function __construct(array $expectations = []) { parent::__construct(); $this->setExpectations(AppConfigContractGetVersionExpectation::class, $expectations); } …. $expectation = $this->getExpectation(AppConfigContractGetVersionExpectation::class); ``` - To automatically assert if expectations were used use `AssertExpectationTestCase` or `AssertExpectationManagerTrait`
- Loading branch information
Showing
56 changed files
with
554 additions
and
375 deletions.
There are no files selected for viewing
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
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,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Assert; | ||
|
||
use PHPUnit\Framework\Assert; | ||
use Throwable; | ||
|
||
final class AssertExpectationManager | ||
{ | ||
private static ?self $singleton = null; | ||
|
||
/** | ||
* @var array<AbstractExpectationCallsMap> | ||
*/ | ||
private array $currentExpectations = []; | ||
|
||
public static function getInstance(): self | ||
{ | ||
if (self::$singleton === null) { | ||
self::$singleton = new self(); | ||
} | ||
|
||
return self::$singleton; | ||
} | ||
|
||
public function register(AbstractExpectationCallsMap $map): void | ||
{ | ||
$this->currentExpectations[] = $map; | ||
} | ||
|
||
public function assertCalled(): void | ||
{ | ||
$errors = []; | ||
foreach ($this->currentExpectations as $map) { | ||
try { | ||
$map->assertCalled(); | ||
} catch (Throwable $throwable) { | ||
$errors[] = $throwable; | ||
} | ||
} | ||
|
||
// We must some assert due the assertion count | ||
if ($errors === []) { | ||
Assert::assertEmpty($errors); | ||
return; | ||
} | ||
|
||
Assert::fail(implode(PHP_EOL, array_map(static fn ($e) => $e->getMessage(), $errors))); | ||
} | ||
|
||
public function hasExpectations(): bool | ||
{ | ||
return $this->currentExpectations !== []; | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
$this->currentExpectations = []; | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Assert; | ||
|
||
use LaraStrict\Testing\Assert\Traits\AssertExpectationManagerTrait; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
abstract class AssertExpectationTestCase extends TestCase | ||
{ | ||
use AssertExpectationManagerTrait; | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Testing/Assert/Traits/AssertExpectationManagerTrait.php
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Assert\Traits; | ||
|
||
use LaraStrict\Testing\Assert\AssertExpectationManager; | ||
|
||
trait AssertExpectationManagerTrait | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
AssertExpectationManager::getInstance()->reset(); | ||
} | ||
|
||
protected function assertPostConditions(): void | ||
{ | ||
$manager = AssertExpectationManager::getInstance(); | ||
|
||
if ($manager->hasExpectations()) { | ||
$this->addToAssertionCount(1); | ||
$manager->assertCalled(); | ||
} | ||
|
||
parent::assertPostConditions(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
AssertExpectationManager::getInstance()->reset(); | ||
|
||
parent::tearDown(); | ||
} | ||
} |
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.