Skip to content

Commit

Permalink
feat: add QueryBus helper trait for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devwebpeanuts committed Mar 15, 2018
1 parent 46f857f commit 36ad9fe
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Traits/QueryBusTestCaseRelated.php
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
));
}
}

0 comments on commit 36ad9fe

Please sign in to comment.