I'm working on a project where we need to have code coverage using phpunit.

I have a method that includes this code:

  $node = \Drupal::entityTypeManager()->getStorage('node')->load($node_id);

  $form['data-title']['#value'] = $node->label();

  $form['data-description']['#value'] = $node->field_description->value;

So I need to mock out the node object.

I can get the node label to work, but not the field_description->value

Here's my set up function in my test class:

   * {@inheritdoc}
  protected function setUp() {
    $container = new Container();
    $container->set('string_translation', $this->getStringTranslationStub());

    $renderer = $this->getMockBuilder(Renderer::class)
      ->willReturn('Hello world');

    $container->set('renderer', $renderer);

    // Mock entity type manager.
    $this->entityTypeManager = $this->getMockBuilder(EntityTypeManagerInterface::class)

    // Mock a node and add the label to it.
    $node = $this->getMockBuilder(Node::class)
      ->willReturn('shaken not stirred');



    // @todo figure out how to add a field to a mock node.
    /*$field_description = new \stdClass();

    $field_description->value = 'This is a description';

    $node->set('field_description', $field_description);*/

    $node_storage = $this->getMockBuilder(EntityStorageInterface::class)

    $this->uuid = $this->getMockBuilder(UuidInterface::class)

    $container->set('entity_type.manager', $this->entityTypeManager);

    $entity_repository = $this->getMockBuilder(EntityRepository::class)

    $container->set('entity.repository', $entity_repository);

    $this->form = MyForm::create($container);

  • 1
    If you are doing a UnitTestCase and find yourself doing too many crazy mocks, you probably need to think about doing a KernelTestBase test.
    – mpdonadio
    Commented Aug 22, 2018 at 0:06
  • Also, using get() instead of relying on the magic __get() should make mocking a bit easier, but mocking content entities is hard as they are very complex structures. Agree that it might be better to write a kernel test
    – Berdir
    Commented Aug 22, 2018 at 5:51
  • For this project we're required to use unit testing. I'm not sure KernelTestBase would count.
    – oknate
    Commented Aug 22, 2018 at 11:58

2 Answers 2


The following should work using PHPUnit Mock Objects as in your example.

First mock FieldItemListInterface __get magic method.

$fieldDescMock = $this->getMockBuilder('\Drupal\Core\Field\FieldItemListInterface')

Then mock the Node/ContentEntityBase __get magic method.

  • Awesome, this was very helpful! It worked exactly as you wrote it.
    – oknate
    Commented Aug 22, 2018 at 11:48
  • 3
    You might look into prophecy, which might work with magic methods, but not static methods. It would help reduce the verbosity of the unit tests. It's a a bit of out scope of this question/answer and "opinionated" so I didn't want to mention it directly.
    – mradcliffe
    Commented Aug 22, 2018 at 12:20
  • @mradcliffe, would you be able to provide an example using prophecy ?
    – pbonnefoi
    Commented Jul 2, 2020 at 13:24

Mradcliffe's answer worked for me, here's my example code:

Here's what ended up working for me:

$this->node = $this->getMockBuilder(Node::class)

$fieldAdsEnabled = $this->getMockBuilder(FieldItemListInterface::class)

Note: for me using Node object and not NodeInterface helped. I suspect because otherwise __get isn't available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.