You are here

protected function TermSelectionTest::getMockTerm in Taxonomy container 8

Returns a mocked taxonomy term entity.

Parameters

int|string $id: The term ID. Can be a numeric ID or a string ID.

string $label: The taxonomy term label.

int|string $parent: Optional ID of the first parent term. Omit this or set to 0 to indicate that this is a root level term.

Return value

\PHPUnit_Framework_MockObject_MockObject The mocked term entity.

1 call to TermSelectionTest::getMockTerm()
TermSelectionTest::testGetReferenceableEntities in tests/src/Unit/TermSelectionTest.php
Tests that terms are returned in the correct hierarchical format.

File

tests/src/Unit/TermSelectionTest.php, line 161

Class

TermSelectionTest
Tests selection of taxonomy terms by the Taxonomy Container module.

Namespace

Drupal\Tests\taxonomy_container\Unit

Code

protected function getMockTerm($id, $label, $parent = 0) {

  // We're using MockBuilder instead of Prophecy so we can mock the accessing
  // of the public properties $term->parents and $term->depth. This is not
  // supported by Prophecy.
  $term = $this
    ->getMockBuilder(Term::class)
    ->disableOriginalConstructor()
    ->getMock();
  $term
    ->expects($this
    ->any())
    ->method('id')
    ->willReturn($id);
  $term
    ->expects($this
    ->any())
    ->method('label')
    ->willReturn($label);
  $term
    ->expects($this
    ->any())
    ->method('access')
    ->willReturn(TRUE);

  // Mock accessing the public properties through the magic __get() method.
  $term
    ->expects($this
    ->any())
    ->method('__get')
    ->will($this
    ->returnValueMap([
    [
      'parent',
      (object) [
        'target_id' => $parent,
      ],
    ],
    [
      'depth',
      $parent === 0 ? 0 : 1,
    ],
  ]));
  return $term;
}