1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
<?php
namespace Illuminate\Tests\Support;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Support\Testing\Fakes\BusFake;
use Mockery as m;
use PHPUnit\Framework\Constraint\ExceptionMessage;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
class SupportTestingBusFakeTest extends TestCase
{
/** @var \Illuminate\Support\Testing\Fakes\BusFake */
protected $fake;
protected function setUp(): void
{
parent::setUp();
$this->fake = new BusFake(m::mock(Dispatcher::class));
}
protected function tearDown(): void
{
parent::tearDown();
m::close();
}
public function testAssertDispatched()
{
try {
$this->fake->assertDispatched(BusJobStub::class);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was not dispatched.'));
}
$this->fake->dispatch(new BusJobStub);
$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);
$this->fake->assertDispatched(BusJobStub::class);
}
public function testAssertDispatchedWithCallbackInt()
{
$this->fake->dispatch(new BusJobStub);
$this->fake->dispatchNow(new BusJobStub);
try {
$this->fake->assertDispatched(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->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);
$this->fake->dispatchNow(new OtherBusJobStub(1));
try {
$this->fake->assertDispatched(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.'));
}
$this->fake->assertDispatched(OtherBusJobStub::class, function ($job) {
return $job->id === null;
});
$this->fake->assertDispatched(OtherBusJobStub::class, function ($job) {
return $job->id === 1;
});
}
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);
$this->fake->dispatchNow(new BusJobStub);
try {
$this->fake->assertDispatchedTimes(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->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);
$this->fake->dispatch(new BusJobStub);
$this->fake->dispatchNow(new BusJobStub);
try {
$this->fake->assertNotDispatched(BusJobStub::class);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\BusJobStub] job was dispatched.'));
}
}
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);
$job = new BusJobStub;
$dispatcher->shouldReceive('dispatch')->once()->with($job);
$dispatcher->shouldReceive('dispatchNow')->once()->with($job, null);
$otherJob = new OtherBusJobStub;
$dispatcher->shouldReceive('dispatch')->never()->with($otherJob);
$dispatcher->shouldReceive('dispatchNow')->never()->with($otherJob, null);
$fake = new BusFake($dispatcher, OtherBusJobStub::class);
$fake->dispatch($job);
$fake->dispatchNow($job);
$fake->dispatch($otherJob);
$fake->dispatchNow($otherJob);
$fake->assertNotDispatched(BusJobStub::class);
$fake->assertDispatchedTimes(OtherBusJobStub::class, 2);
}
public function testAssertDispatchedWithIgnoreCallback()
{
$dispatcher = m::mock(Dispatcher::class);
$job = new BusJobStub;
$dispatcher->shouldReceive('dispatch')->once()->with($job);
$dispatcher->shouldReceive('dispatchNow')->once()->with($job, null);
$otherJob = new OtherBusJobStub;
$dispatcher->shouldReceive('dispatch')->once()->with($otherJob);
$dispatcher->shouldReceive('dispatchNow')->once()->with($otherJob, null);
$anotherJob = new OtherBusJobStub(1);
$dispatcher->shouldReceive('dispatch')->never()->with($anotherJob);
$dispatcher->shouldReceive('dispatchNow')->never()->with($anotherJob, null);
$fake = new BusFake($dispatcher, [
function ($command) {
return $command instanceof OtherBusJobStub && $command->id === 1;
},
]);
$fake->dispatch($job);
$fake->dispatchNow($job);
$fake->dispatch($otherJob);
$fake->dispatchNow($otherJob);
$fake->dispatch($anotherJob);
$fake->dispatchNow($anotherJob);
$fake->assertNotDispatched(BusJobStub::class);
$fake->assertDispatchedTimes(OtherBusJobStub::class, 2);
$fake->assertNotDispatched(OtherBusJobStub::class, function ($job) {
return $job->id === null;
});
$fake->assertDispatched(OtherBusJobStub::class, function ($job) {
return $job->id === 1;
});
}
}
class BusJobStub
{
//
}
class OtherBusJobStub
{
public $id;
public function __construct($id = null)
{
$this->id = $id;
}
}
|