In my SUT I've got the following function:
class MyClass
{
public function doSomething(Registration $registration)
{
return $registration->attendees->first()->last_name;
}
}
Registration is a Laravel Eloquent model that has many Attendees.
I can't seem to create a test for this as everything seems to rely on magic methods.
In my test I've got this:
public function testDoSomething()
{
$this->attendeeMock->last_name = "Bar";
$this->collection[0] = $this->attendeeMock; // This is a real Eloquent Collection
$this->registrationMock->attendees = $this->collection;
$this->assertEquals('Bar', $this->myClass->doSomething($this->registrationMock));
}
The above code throws Call to a member function first() on null
.
This seems to be such a simple use case, but on the internet I can't find appropriate answers. Are we supposed to avoid using magic accessors and use getRelationship
and getAttribute
for everything?