| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- namespace Illuminate\Tests\Support;
- use BadMethodCallException;
- use Illuminate\Bus\Queueable;
- use Illuminate\Foundation\Application;
- use Illuminate\Support\Testing\Fakes\QueueFake;
- use PHPUnit\Framework\Constraint\ExceptionMessage;
- use PHPUnit\Framework\ExpectationFailedException;
- use PHPUnit\Framework\TestCase;
- class SupportTestingQueueFakeTest extends TestCase
- {
- /**
- * @var \Illuminate\Support\Testing\Fakes\QueueFake
- */
- private $fake;
- /**
- * @var \Illuminate\Tests\Support\JobStub
- */
- private $job;
- protected function setUp(): void
- {
- parent::setUp();
- $this->fake = new QueueFake(new Application);
- $this->job = new JobStub;
- }
- public function testAssertPushed()
- {
- try {
- $this->fake->assertPushed(JobStub::class);
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobStub] job was not pushed.'));
- }
- $this->fake->push($this->job);
- $this->fake->assertPushed(JobStub::class);
- }
- public function testAssertPushedWithClosure()
- {
- $this->fake->push($this->job);
- $this->fake->assertPushed(function (JobStub $job) {
- return true;
- });
- }
- public function testQueueSize()
- {
- $this->assertEquals(0, $this->fake->size());
- $this->fake->push($this->job);
- $this->assertEquals(1, $this->fake->size());
- }
- public function testAssertNotPushed()
- {
- $this->fake->push($this->job);
- try {
- $this->fake->assertNotPushed(JobStub::class);
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\JobStub] job was pushed.'));
- }
- }
- public function testAssertNotPushedWithClosure()
- {
- $this->fake->assertNotPushed(JobStub::class);
- $this->fake->push($this->job);
- try {
- $this->fake->assertNotPushed(function (JobStub $job) {
- return true;
- });
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\JobStub] job was pushed.'));
- }
- }
- public function testAssertPushedOn()
- {
- $this->fake->push($this->job, '', 'foo');
- try {
- $this->fake->assertPushedOn('bar', JobStub::class);
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobStub] job was not pushed.'));
- }
- $this->fake->assertPushedOn('foo', JobStub::class);
- }
- public function testAssertPushedOnWithClosure()
- {
- $this->fake->push($this->job, '', 'foo');
- try {
- $this->fake->assertPushedOn('bar', function (JobStub $job) {
- return true;
- });
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobStub] job was not pushed.'));
- }
- $this->fake->assertPushedOn('foo', function (JobStub $job) {
- return true;
- });
- }
- public function testAssertPushedTimes()
- {
- $this->fake->push($this->job);
- $this->fake->push($this->job);
- try {
- $this->fake->assertPushed(JobStub::class, 1);
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobStub] job was pushed 2 times instead of 1 times.'));
- }
- $this->fake->assertPushed(JobStub::class, 2);
- }
- public function testAssertNothingPushed()
- {
- $this->fake->assertNothingPushed();
- $this->fake->push($this->job);
- try {
- $this->fake->assertNothingPushed();
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('Jobs were pushed unexpectedly.'));
- }
- }
- public function testAssertPushedUsingBulk()
- {
- $this->fake->assertNothingPushed();
- $queue = 'my-test-queue';
- $this->fake->bulk([
- $this->job,
- new JobStub,
- ], null, $queue);
- $this->fake->assertPushedOn($queue, JobStub::class);
- $this->fake->assertPushed(JobStub::class, 2);
- }
- public function testAssertPushedWithChainUsingClassesOrObjectsArray()
- {
- $this->fake->push(new JobWithChainStub([
- new JobStub,
- ]));
- $this->fake->assertPushedWithChain(JobWithChainStub::class, [
- JobStub::class,
- ]);
- $this->fake->assertPushedWithChain(JobWithChainStub::class, [
- new JobStub,
- ]);
- }
- public function testAssertPushedWithoutChain()
- {
- $this->fake->push(new JobWithChainStub([]));
- $this->fake->assertPushedWithoutChain(JobWithChainStub::class);
- }
- public function testAssertPushedWithChainSameJobDifferentChains()
- {
- $this->fake->push(new JobWithChainStub([
- new JobStub,
- ]));
- $this->fake->push(new JobWithChainStub([
- new JobStub,
- new JobStub,
- ]));
- $this->fake->assertPushedWithChain(JobWithChainStub::class, [
- JobStub::class,
- ]);
- $this->fake->assertPushedWithChain(JobWithChainStub::class, [
- JobStub::class,
- JobStub::class,
- ]);
- }
- public function testAssertPushedWithChainUsingCallback()
- {
- $this->fake->push(new JobWithChainAndParameterStub('first', [
- new JobStub,
- new JobStub,
- ]));
- $this->fake->push(new JobWithChainAndParameterStub('second', [
- new JobStub,
- ]));
- $this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [
- JobStub::class,
- ], function ($job) {
- return $job->parameter === 'second';
- });
- try {
- $this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [
- JobStub::class,
- JobStub::class,
- ], function ($job) {
- return $job->parameter === 'second';
- });
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('The expected chain was not pushed'));
- }
- }
- public function testAssertPushedWithChainErrorHandling()
- {
- try {
- $this->fake->assertPushedWithChain(JobWithChainStub::class, []);
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobWithChainStub] job was not pushed'));
- }
- $this->fake->push(new JobWithChainStub([
- new JobStub,
- ]));
- try {
- $this->fake->assertPushedWithChain(JobWithChainStub::class, []);
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('The expected chain can not be empty'));
- }
- try {
- $this->fake->assertPushedWithChain(JobWithChainStub::class, [
- new JobStub,
- new JobStub,
- ]);
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('The expected chain was not pushed'));
- }
- try {
- $this->fake->assertPushedWithChain(JobWithChainStub::class, [
- JobStub::class,
- JobStub::class,
- ]);
- $this->fail();
- } catch (ExpectationFailedException $e) {
- $this->assertThat($e, new ExceptionMessage('The expected chain was not pushed'));
- }
- }
- public function testCallUndefinedMethodErrorHandling()
- {
- try {
- $this->fake->undefinedMethod();
- } catch (BadMethodCallException $e) {
- $this->assertThat($e, new ExceptionMessage(sprintf(
- 'Call to undefined method %s::%s()', get_class($this->fake), 'undefinedMethod'
- )));
- }
- }
- }
- class JobStub
- {
- public function handle()
- {
- //
- }
- }
- class JobWithChainStub
- {
- use Queueable;
- public function __construct($chain)
- {
- $this->chain($chain);
- }
- public function handle()
- {
- //
- }
- }
- class JobWithChainAndParameterStub
- {
- use Queueable;
- public $parameter;
- public function __construct($parameter, $chain)
- {
- $this->parameter = $parameter;
- $this->chain($chain);
- }
- public function handle()
- {
- //
- }
- }
|