EventSpyTestTrait - HL Vanilla Community
<main> <article class="userContent"> <div class="embedExternal embedImage display-large float-none"> <div class="embedExternal-content"> <a class="embedImage-link" href="https://us.v-cdn.net/6030677/uploads/ZHSM2ZYHLXLH/microsoftteams-image-288-29.png" rel="nofollow noreferrer noopener ugc" target="_blank"> <img class="embedImage-img" src="https://us.v-cdn.net/6030677/uploads/ZHSM2ZYHLXLH/microsoftteams-image-288-29.png" alt="MicrosoftTeams-image (8).png" height="108" width="1356" loading="lazy" data-display-size="large" data-float="none"></img></a> </div> </div> <p>The EventSpyTestTrait is used for tests that want to assert various events are fired.</p><h2 data-id="psr-event-dispatcher"><strong>PSR Event Dispatcher</strong></h2><p>The event manager allows firing of <a href="https://www.php-fig.org/psr/psr-14/" rel="nofollow noreferrer ugc">PSR-14</a> compliant events for newer things like ResourceEvents.</p><p>Vanilla offers a few utilities to test them</p><ul><li><code class="code codeInline" spellcheck="false" tabindex="0">clearDispatchedEvents()</code> - Clear any previously dispatched events. Automatically run between tests.</li><li><code class="code codeInline" spellcheck="false" tabindex="0">assertNoEventsDispatched()</code> - Assert that no events were dispatched.</li><li><code class="code codeInline" spellcheck="false" tabindex="0">assertEventsDispatched($events)</code> and <code class="code codeInline" spellcheck="false" tabindex="0">assertEventDispatched($event)</code> - assert resource events were dispatched.</li></ul><h2 data-id="vanilla-event-handlers"><strong>Vanilla Event Handlers</strong></h2><p>Vanilla has it's own event firing system that is used across it's 10 years of code. These can be tested using the following methods.</p><ul><li><code class="code codeInline" spellcheck="false" tabindex="0">assertEventFired()</code> - Assert an event was fired.</li><li><code class="code codeInline" spellcheck="false" tabindex="0">clearFiredEvents()</code> - Clear all fire events. Automatically run between tests.</li><li><code class="code codeInline" spellcheck="false" tabindex="0">assertHandlerCalled($name)</code> - Assert that an event handler was called.</li><li><code class="code codeInline" spellcheck="false" tabindex="0">clearCalledHandlers()</code> - Clear the called handlers.</li><li><code class="code codeInline" spellcheck="false" tabindex="0">handlerCalled()</code> - Mark an event handler as called.</li></ul><h3 data-id="example">Example</h3><p>Here's some example usage. There are a few notable things:</p><ul><li>You can apply assertions about the arguments passed to the event.</li><li>You can receive the arguments that were passed to the event.</li><li>You can write event handlers on your test class, and they will be automatically bound.</li></ul><pre class="code codeBlock" spellcheck="false" tabindex="0">/** * Test that date times in the database get updated. */ public function testUpdateActiveDate() { $user = $this->createUser(); $this->visitUpdater()->updateVisit($user['userID']); // Ensure that events are fired. $this->assertHandlerCalled('userModel_visit_handler', [ new IsInstanceOf(\UserModel::class), new IsType('array'), ]); // Ensure that events are fired. $calledArgs = $this->assertHandlerCalled('userModel_updateVisit_handler', [ new IsInstanceOf(\UserModel::class), new IsType('array'), ]); $this->assertEquals([ 'DateLastActive' => DateTimeFormatter::timeStampToDateTime($updatedTime->getTimestamp()), 'CountVisits' => 1, ], $calledArgs[1]['Fields']); } /** * Test handler. * * @param array $args */ public function userModel_visit_handler(...$args) { $this->handlerCalled(__FUNCTION__, $args); } /** * Test handler. * * @param array $args */ public function userModel_updateVisit_handler(...$args) { $this->handlerCalled(__FUNCTION__, $args); } </pre><p><br></p> </article> </main>