How to test __get() in Node object? I've created UnitTest based class for my service.

In one of my tested methods from service class I have code like:

if ($node-&gt;hasField('field_foo')) {
  $section = $node-&gt;field_foo-&gt;entity;
}


I've created mock for Node class and I know how to "emulate" hasField. However, how to mock this line:

$section = $node-&gt;field_foo-&gt;entity


I'd to set $section to be a Term object (or mock). Is this even possible?
Now my test falls with an error:


  Trying to get property of non-object


but this is clear why. I'm using just mock, not real Node object.
Yes, magic methods afaik can't be mocked.

You can use get('field_foo')->entity which does exactly the same as __get(), at least in this case. And then make that return a stdClass object with a ->entity property. That's not exactly the same as what really happens, but it will work in the same way for your code.