Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Support dispatchAfterResponse in BusFake #31418

Merged
merged 2 commits into from Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/BusFake.php
Expand Up @@ -30,6 +30,13 @@ class BusFake implements Dispatcher
*/
protected $commands = [];

/**
* The commands that have been dispatched after the response has been sent.
*
* @var array
*/
protected $commandsAfterResponse = [];

/**
* Create a new bus fake instance.
*
Expand Down Expand Up @@ -78,6 +85,21 @@ public function assertDispatchedTimes($command, $times = 1)
);
}

/**
* Assert if a job was pushed after the response was sent a number of times.
*
* @param string $command
* @param int $times
* @return void
*/
public function assertDispatchedAfterResponseTimes($command, $times = 1)
{
PHPUnit::assertTrue(
($count = $this->dispatchedAfterResponse($command)->count()) === $times,
"The expected [{$command}] job was pushed {$count} times instead of {$times} times."
);
}

/**
* Determine if a job was dispatched based on a truth-test callback.
*
Expand All @@ -93,6 +115,40 @@ public function assertNotDispatched($command, $callback = null)
);
}

/**
* Determine if a job was dispatched based on a truth-test callback.
*
* @param string $command
* @param callable|null $callback
* @return void
*/
public function assertNotDispatchedAfterResponse($command, $callback = null)
{
PHPUnit::assertTrue(
$this->dispatchedAfterResponse($command, $callback)->count() === 0,
"The unexpected [{$command}] job was dispatched for after sending the response."
);
}

/**
* Assert if a job was dispatched after the response was sent based on a truth-test callback.
*
* @param string $command
* @param callable|int|null $callback
* @return void
*/
public function assertDispatchedAfterResponse($command, $callback = null)
{
if (is_numeric($callback)) {
return $this->assertDispatchedAfterResponseTimes($command, $callback);
}

PHPUnit::assertTrue(
$this->dispatchedAfterResponse($command, $callback)->count() > 0,
"The expected [{$command}] job was not dispatched for after sending the response."
);
}

/**
* Get all of the jobs matching a truth-test callback.
*
Expand All @@ -115,6 +171,28 @@ public function dispatched($command, $callback = null)
});
}

/**
* Get all of the jobs dispatched after the response was sent matching a truth-test callback.
*
* @param string $command
* @param callable|null $callback
* @return \Illuminate\Support\Collection
*/
public function dispatchedAfterResponse(string $command, $callback = null)
{
if (! $this->hasDispatchedAfterResponse($command)) {
return collect();
}

$callback = $callback ?: function () {
return true;
};

return collect($this->commandsAfterResponse[$command])->filter(function ($command) use ($callback) {
return $callback($command);
});
}

/**
* Determine if there are any stored commands for a given class.
*
Expand All @@ -126,6 +204,17 @@ public function hasDispatched($command)
return isset($this->commands[$command]) && ! empty($this->commands[$command]);
}

/**
* Determine if there are any stored commands for a given class.
*
* @param string $command
* @return bool
*/
public function hasDispatchedAfterResponse($command)
{
return isset($this->commandsAfterResponse[$command]) && ! empty($this->commandsAfterResponse[$command]);
}

/**
* Dispatch a command to its appropriate handler.
*
Expand Down Expand Up @@ -157,6 +246,21 @@ public function dispatchNow($command, $handler = null)
}
}

/**
* Dispatch a command to its appropriate handler.
*
* @param mixed $command
* @return mixed
*/
public function dispatchAfterResponse($command)
{
if ($this->shouldFakeJob($command)) {
$this->commandsAfterResponse[get_class($command)][] = $command;
} else {
return $this->dispatcher->dispatch($command);
}
}

/**
* Determine if an command should be faked or actually dispatched.
*
Expand Down
81 changes: 81 additions & 0 deletions tests/Support/SupportTestingBusFakeTest.php
Expand Up @@ -40,6 +40,20 @@ public function testAssertDispatched()
$this->fake->assertDispatched(BusJobStub::class);
}

public function testAssertDispatchedAfterResponse()
{
try {
$this->fake->assertDispatchedAfterResponse(BusJobStub::class);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was not dispatched for after sending the response.'));
}

$this->fake->dispatchAfterResponse(new BusJobStub);

$this->fake->assertDispatchedAfterResponse(BusJobStub::class);
}

public function testAssertDispatchedNow()
{
$this->fake->dispatchNow(new BusJobStub);
Expand All @@ -62,6 +76,21 @@ public function testAssertDispatchedWithCallbackInt()
$this->fake->assertDispatched(BusJobStub::class, 2);
}

public function testAssertDispatchedAfterResponseWithCallbackInt()
{
$this->fake->dispatchAfterResponse(new BusJobStub);
$this->fake->dispatchAfterResponse(new BusJobStub);

try {
$this->fake->assertDispatchedAfterResponse(BusJobStub::class, 1);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was pushed 2 times instead of 1 times.'));
}

$this->fake->assertDispatchedAfterResponse(BusJobStub::class, 2);
}

public function testAssertDispatchedWithCallbackFunction()
{
$this->fake->dispatch(new OtherBusJobStub);
Expand All @@ -85,6 +114,29 @@ public function testAssertDispatchedWithCallbackFunction()
});
}

public function testAssertDispatchedAfterResponseWithCallbackFunction()
{
$this->fake->dispatchAfterResponse(new OtherBusJobStub);
$this->fake->dispatchAfterResponse(new OtherBusJobStub(1));

try {
$this->fake->assertDispatchedAfterResponse(OtherBusJobStub::class, function ($job) {
return $job->id === 0;
});
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\OtherBusJobStub] job was not dispatched for after sending the response.'));
}

$this->fake->assertDispatchedAfterResponse(OtherBusJobStub::class, function ($job) {
return $job->id === null;
});

$this->fake->assertDispatchedAfterResponse(OtherBusJobStub::class, function ($job) {
return $job->id === 1;
});
}

public function testAssertDispatchedTimes()
{
$this->fake->dispatch(new BusJobStub);
Expand All @@ -100,6 +152,21 @@ public function testAssertDispatchedTimes()
$this->fake->assertDispatchedTimes(BusJobStub::class, 2);
}

public function testAssertDispatchedAfterResponseTimes()
{
$this->fake->dispatchAfterResponse(new BusJobStub);
$this->fake->dispatchAfterResponse(new BusJobStub);

try {
$this->fake->assertDispatchedAfterResponseTimes(BusJobStub::class, 1);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was pushed 2 times instead of 1 times.'));
}

$this->fake->assertDispatchedAfterResponseTimes(BusJobStub::class, 2);
}

public function testAssertNotDispatched()
{
$this->fake->assertNotDispatched(BusJobStub::class);
Expand All @@ -115,6 +182,20 @@ public function testAssertNotDispatched()
}
}

public function testAssertNotDispatchedAfterResponse()
{
$this->fake->assertNotDispatchedAfterResponse(BusJobStub::class);

$this->fake->dispatchAfterResponse(new BusJobStub);

try {
$this->fake->assertNotDispatchedAfterResponse(BusJobStub::class);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\BusJobStub] job was dispatched for after sending the response.'));
}
}

public function testAssertDispatchedWithIgnoreClass()
{
$dispatcher = m::mock(Dispatcher::class);
Expand Down