'laravel test protected Job Arguments with assertPushed
I have a question about testing Jobs in Laravel.
I use Laravel 8, PHP 7.4.13 and PHPUnit 9.5.8.
I have a test class, that tests if a Job is pusehd:
public function a_job_is_pushed_to_queue()
{
Queue::fake();
$this->functionWhereTheJobIsCalled();
Queue::assertPushed(TestJob::class);
}
The TestJob is called with constructor arguments and I want also to test, if the Job is called with the correct arguments. Like you can also do with Mockery.
On Larave documentation I found out, that I can pass a closure to assertPushed to test this like so:
Queue::assertPushed(function(TestJob $job)
{
return $job->pseudonym === $this->patients[0]->pseudonym;
});
The problem is, that the argument "pseudonym", that is given to TestJob constructor is a protected argument. When I run the test, this error occurs:
Error: Cannot access protected property App\Jobs\TestJob::$pseudonym
JobTest looks like this:
class JobTest implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $pseudonym;
public function __construct($pseudonym)
{
$this->pseudonym = $pseudonym;
}
}
I wondered if there is any way to test protected Job arguments without converting them to public?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
