Skip to content

Commit b4021b4

Browse files
[12.x] - add dispatchedOnce methods (#56494)
* add methods * adjust
1 parent 7b18a69 commit b4021b4

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/Illuminate/Support/Testing/Fakes/BusFake.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ public function assertDispatched($command, $callback = null)
132132
);
133133
}
134134

135+
/**
136+
* Assert if a job was pushed exactly once.
137+
*
138+
* @param string|\Closure $command
139+
* @param int $times
140+
* @return void
141+
*/
142+
public function assertDispatchedOnce($command)
143+
{
144+
$this->assertDispatchedTimes($command, 1);
145+
}
146+
135147
/**
136148
* Assert if a job was pushed a number of times.
137149
*

src/Illuminate/Support/Testing/Fakes/EventFake.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ public function assertDispatched($event, $callback = null)
147147
);
148148
}
149149

150+
/**
151+
* Assert if an event was dispatched exactly once.
152+
*
153+
* @param string $event
154+
* @param int $times
155+
* @return void
156+
*/
157+
public function assertDispatchedOnce($event)
158+
{
159+
$this->assertDispatchedTimes($event, 1);
160+
}
161+
150162
/**
151163
* Assert if an event was dispatched a number of times.
152164
*

tests/Support/SupportTestingBusFakeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,21 @@ public function testAssertDispatchedSyncWithCallbackFunction()
275275
});
276276
}
277277

278+
public function testAssertDispatchedOnce()
279+
{
280+
$this->fake->dispatch(new BusJobStub);
281+
$this->fake->dispatchNow(new BusJobStub);
282+
283+
try {
284+
$this->fake->assertDispatchedOnce(BusJobStub::class);
285+
$this->fail();
286+
} catch (ExpectationFailedException $e) {
287+
$this->assertStringContainsString('The expected [Illuminate\Tests\Support\BusJobStub] job was pushed 2 times instead of 1 times.', $e->getMessage());
288+
}
289+
290+
$this->fake->assertDispatchedTimes(BusJobStub::class, 2);
291+
}
292+
278293
public function testAssertDispatchedTimes()
279294
{
280295
$this->fake->dispatch(new BusJobStub);

tests/Support/SupportTestingEventFakeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ public function testAssertDispatchedWithCallbackInt()
7676
$this->fake->assertDispatched(EventStub::class, 2);
7777
}
7878

79+
public function testAssertDispatchedOnce()
80+
{
81+
$this->fake->dispatch(EventStub::class);
82+
$this->fake->dispatch(EventStub::class);
83+
84+
try {
85+
$this->fake->assertDispatchedOnce(EventStub::class);
86+
$this->fail();
87+
} catch (ExpectationFailedException $e) {
88+
$this->assertStringContainsString('The expected [Illuminate\Tests\Support\EventStub] event was dispatched 2 times instead of 1 times.', $e->getMessage());
89+
}
90+
91+
$this->fake->assertDispatchedTimes(EventStub::class, 2);
92+
}
93+
7994
public function testAssertDispatchedTimes()
8095
{
8196
$this->fake->dispatch(EventStub::class);

0 commit comments

Comments
 (0)