<?phpuseApplication\User\Queries\ListUserQuery;useIlluminate\Http\Request;useSpatie\QueryBuilder\Exceptions\InvalidFilterQuery;useTests\TestCase;classListUserQueryTestextendsTestCase{/** @test */publicfunctionit_throws_an_exception_when_the_key_for_filtering_is_not_supported():void{$this->expectException(InvalidFilterQuery::class);$this->expectExceptionMessage('Requested filter(s) `key_not_supported` are not allowed. Allowed filter(s) are `id, email, nickname`.');$request=newRequest(['filter'=>['key_not_supported'=>'value is irrelevant']]);newListMyModelQuery($request);}}
<?php/** @test */publicfunctionit_throws_an_error_if_model_doesnt_exist():void{$className=User::class;$this->assertThrows(fn()=>User::findOrFail(0),ModelNotFoundException::class,"No query results for model [{$className}] 1");}
<?php/** @test */publicfunctionevent_assert_dispatched():void{Event::fake();// orEvent::fake([ExampleCreated::class,]);$myExample=MyExampleFactory::new()->create();// Assert a event was dispatched...Event::assertDispatched(ExampleCreated::class);// Assert a event was dispatched...Event::assertDispatched(ExampleCreated::class,function($event)use($myExample){return$event->myExample->is($myExample);});}
Event:: assertNotDispatched
<?php/** @test */publicfunctionevent_assert_not_dispatched():void{Event::fake();// orEvent::fake([ExampleCreated::class,]);$myExample=MyExampleFactory::new()->create();// Assert a event was not dispatched...Event::assertNotDispatched(ExampleCreated::class);Event::assertNotDispatched(ExampleCreated::class,function($event,$payload){return$payload[0]->name==='John Doe';});}
Event:: assertDispatchedTimes
<?php/** @test */publicfunctionevent_assert_dispatched_times():void{Event::fake();// orEvent::fake([ExampleCreated::class,]);$myExample=MyExampleFactory::new()->create();// Assert a event was dispatched exactly n times...Event::assertDispatchedTimes(ExampleCreated::class,1);}
Event:: assertListening
<?php/** @test */publicfunctionevent_assert_listening():void{Event::fake();// orEvent::fake([ExampleCreated::class,]);$myExample=MyExampleFactory::new()->create();// assert that a listener is listening to a given eventEvent::assertListening(ExampleCreated::class,ExampleListener::class);}
Queue::fake
Queue:: assertPushed
<?php/** @test */publicfunctionexamples_for_queue_fakes():void{Queue::fake();// orQueue::fake([ExampleJob::class,]);// queue a jobExampleJob::dispatch();// assert that a job was pushed...Queue::assertPushed(ExampleJob::class);// assert that a job was pushed a given number of times...Queue::assertPushed(ExampleJob::class,1);// assert that a job was pushed with a given payload...Queue::assertPushed(ExampleJob::class,function($job){return$job->example=='example';});}
Queue:: assertNotPushed
<?php/** @test */publicfunctionqueue_assert_not_pushed():void{Queue::fake();// orQueue::fake([ExampleJob::class,]);// queue a jobExampleJob::dispatch();// assert that a job was not pushedQueue::assertNotPushed(ExampleJob::class);// asser that a job was not pushed with a given payload...Queue::assertNotPushed(ExampleJob::class,function(ExampleJob$job){return$job->exampleProperty==='exampleValue';});}
Queue:: assertNothingPushed
<?php/** @test */publicfunctionqueue_assert_nothing_pushed():void{Queue::fake();// orQueue::fake([ExampleJob::class,]);// queue a jobExampleJob::dispatch();// assert that no jobs were pushed...Queue::assertNothingPushed();}
Queue:: assertPushedOn
<?php/** @test */publicfunctionqueue_assert_pushed_on():void{Queue::fake();// orQueue::fake([ExampleJob::class,]);// queue a jobExampleJob::dispatch();// assert that a job was pushed on a given queue...Queue::assertPushedOn('queue-name',ExampleJob::class);// assert that a job was pushed a given number of times on a given queue...Queue::assertPushedOn('queue-name',ExampleJob::class,1);// assert that a job was pushed with a given payload on a given queue...Queue::assertPushedOn('queue-name',ExampleJob::class,function($job){return$job->example=='example';});}
Queue:: assertPushedWithChain
<?php/** @test */publicfunctionqueue_assert_pushed_with_chain():void{Queue::fake();// orQueue::fake([ExampleJob::class,]);// queue a jobExampleJob::dispatch();// assert that a job was pushed with a given chain...Queue::assertPushedWithChain(ExampleJob::class,[newAnotherJob,newYetAnotherJob,]);// assert that a job was pushed with a given chain...Queue::assertPushedWithChain(ExampleJob::class,[newAnotherJob,newYetAnotherJob,],function($job){return$job->user->id===1;});}
Queue:: assertPushedWithoutChain
<?php/** @test */publicfunctionqueue_assert_pushed_without_chain():void{Queue::fake();// orQueue::fake([ExampleJob::class,]);// queue a jobExampleJob::dispatch();// assert that a job was pushed without a given chain...Queue::assertPushedWithoutChain(ExampleJob::class);// assert that a job was pushed without a given chain and with a given payload...Queue::assertPushedWithoutChain(ExampleJob::class,function($job){return$job->exampleProperty==='exampleValue';});}