From 36ad9fe13fe528d0df3edbbbad259d373ec3e10f Mon Sep 17 00:00:00 2001 From: Romain RENAUD Date: Wed, 14 Mar 2018 16:27:46 +0100 Subject: [PATCH] feat: add QueryBus helper trait for unit tests --- src/Traits/QueryBusTestCaseRelated.php | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/Traits/QueryBusTestCaseRelated.php diff --git a/src/Traits/QueryBusTestCaseRelated.php b/src/Traits/QueryBusTestCaseRelated.php new file mode 100644 index 0000000..6364faa --- /dev/null +++ b/src/Traits/QueryBusTestCaseRelated.php @@ -0,0 +1,44 @@ +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 + )); + } +}