-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add QueryBus helper trait for unit tests
- Loading branch information
1 parent
46f857f
commit 36ad9fe
Showing
1 changed file
with
44 additions
and
0 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,44 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Onyx\Traits; | ||
|
||
use Onyx\Services\CQS\QueryBuses\InMemory; | ||
|
||
trait QueryBusTestCaseRelated | ||
{ | ||
private | ||
$queryBus; | ||
|
||
private function initializeQueryBusForTest(): void | ||
{ | ||
$this->queryBus = new InMemory(); | ||
} | ||
|
||
private function assertQueriesHaveBeenSent(array $expectedQueryTypes): void | ||
{ | ||
foreach($expectedQueryTypes as $queryType) | ||
{ | ||
$this->assertQueryHasBeenSent($queryType); | ||
} | ||
} | ||
|
||
private function assertQueryHasBeenSent(string $queryType): void | ||
{ | ||
foreach($this->queryBus->getSentQueries() as $query) | ||
{ | ||
if($query instanceof $queryType) | ||
{ | ||
$this->addToAssertionCount(1); | ||
|
||
return; | ||
} | ||
} | ||
|
||
$this->fail(sprintf( | ||
"Failed to assert that query of type %s has been sent", | ||
$queryType | ||
)); | ||
} | ||
} |