Dec 17, 2022
0
Level 1
Unit test with BelongsTo return null
Hi,
I'm trying to make a Unit Test on my service, I have a method in my service class who is doing this :
private function generateTitle(): string
{
if ($this->forSecretary) {
return sprintf(
'RESERVATION MOTOBLEU Course n°%s - %s / %s',
$this->reservation->reference,
ucfirst($this->reservation->entreprise->nom),
$this->reservation->passager->nom
);
} else {
$piloteLabel = $this->generatePiloteLabel();
return sprintf(
'Course n°%s - %s / %s / %s',
$this->reservation->reference,
ucfirst($this->reservation->entreprise->nom),
$this->reservation->passager->nom,
'plt: '. $piloteLabel
);
}
}
I would like to know how to mock the $this->reservation->entreprise->nom and $this->reservation->passager->nom both are in my Reservation model like so :
public function entreprise(): BelongsTo
{
return $this->belongsTo(Entreprise::class);
}
public function passager()
{
return $this->belongsTo(Passager::class);
}
I tried this but I have ErrorException : Attempt to read property "nom" on null :
protected function setUp(): void
{
parent::setUp();
$this->reservation = $this->createMock(Reservation::class);
$this->reservation->is_cancel = false;
$this->pilote = $this->createMock(Pilote::class);
$this->pilote->full_name = "test";
$this->entreprise = $this->createMock(Entreprise::class);
$this->entreprise->nom = "test";
$this->passager = $this->createMock(Passager::class);
$this->passager->nom = "test";
$this->reservation->method("pilote")->willReturn($this->pilote);
$this->reservation->passager = $this->passager;
$this->reservation->entreprise = $this->entreprise;
$this->calendarService = new GoogleCalendarService();
}
Thanks for helping.
Please or to participate in this conversation.