You are here

public function ContentEntityBaseUnitTest::testValidate in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php \Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest::testValidate()

@covers ::validate

File

core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php, line 358

Class

ContentEntityBaseUnitTest
@coversDefaultClass \Drupal\Core\Entity\ContentEntityBase @group Entity @group Access

Namespace

Drupal\Tests\Core\Entity

Code

public function testValidate() {
  $validator = $this
    ->createMock(ValidatorInterface::class);

  /** @var \Symfony\Component\Validator\ConstraintViolationList|\PHPUnit\Framework\MockObject\MockObject $empty_violation_list */
  $empty_violation_list = $this
    ->getMockBuilder('\\Symfony\\Component\\Validator\\ConstraintViolationList')
    ->setMethods(NULL)
    ->getMock();
  $non_empty_violation_list = clone $empty_violation_list;
  $violation = $this
    ->createMock('\\Symfony\\Component\\Validator\\ConstraintViolationInterface');
  $non_empty_violation_list
    ->add($violation);
  $validator
    ->expects($this
    ->at(0))
    ->method('validate')
    ->with($this->entity
    ->getTypedData())
    ->will($this
    ->returnValue($empty_violation_list));
  $validator
    ->expects($this
    ->at(1))
    ->method('validate')
    ->with($this->entity
    ->getTypedData())
    ->will($this
    ->returnValue($non_empty_violation_list));
  $this->typedDataManager
    ->expects($this
    ->exactly(2))
    ->method('getValidator')
    ->will($this
    ->returnValue($validator));
  $this
    ->assertCount(0, $this->entity
    ->validate());
  $this
    ->assertCount(1, $this->entity
    ->validate());
}