comparison core/modules/rest/tests/src/Unit/EntityResourceValidationTraitTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\rest\Unit;
4
5 use Drupal\Core\Entity\EntityConstraintViolationList;
6 use Drupal\node\Entity\Node;
7 use Drupal\Tests\UnitTestCase;
8 use Drupal\user\Entity\User;
9 use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
10 use Symfony\Component\Validator\ConstraintViolationInterface;
11
12 /**
13 * @group rest
14 * @coversDefaultClass \Drupal\rest\Plugin\rest\resource\EntityResourceValidationTrait
15 */
16 class EntityResourceValidationTraitTest extends UnitTestCase {
17
18 /**
19 * @covers ::validate
20 */
21 public function testValidate() {
22 $trait = $this->getMockForTrait('Drupal\rest\Plugin\rest\resource\EntityResourceValidationTrait');
23
24 $method = new \ReflectionMethod($trait, 'validate');
25 $method->setAccessible(TRUE);
26
27 $violations = $this->prophesize(EntityConstraintViolationList::class);
28 $violations->filterByFieldAccess()->shouldBeCalled()->willReturn([]);
29 $violations->count()->shouldBeCalled()->willReturn(0);
30
31 $entity = $this->prophesize(Node::class);
32 $entity->validate()->shouldBeCalled()->willReturn($violations->reveal());
33
34 $method->invoke($trait, $entity->reveal());
35 }
36
37 /**
38 * @covers ::validate
39 */
40 public function testFailedValidate() {
41 $violation1 = $this->prophesize(ConstraintViolationInterface::class);
42 $violation1->getPropertyPath()->willReturn('property_path');
43 $violation1->getMessage()->willReturn('message');
44
45 $violation2 = $this->prophesize(ConstraintViolationInterface::class);
46 $violation2->getPropertyPath()->willReturn('property_path');
47 $violation2->getMessage()->willReturn('message');
48
49 $entity = $this->prophesize(User::class);
50
51 $violations = $this->getMockBuilder(EntityConstraintViolationList::class)
52 ->setConstructorArgs([$entity->reveal(), [$violation1->reveal(), $violation2->reveal()]])
53 ->setMethods(['filterByFieldAccess'])
54 ->getMock();
55
56 $violations->expects($this->once())
57 ->method('filterByFieldAccess')
58 ->will($this->returnValue([]));
59
60 $entity->validate()->willReturn($violations);
61
62 $trait = $this->getMockForTrait('Drupal\rest\Plugin\rest\resource\EntityResourceValidationTrait');
63
64 $method = new \ReflectionMethod($trait, 'validate');
65 $method->setAccessible(TRUE);
66
67 $this->setExpectedException(UnprocessableEntityHttpException::class);
68
69 $method->invoke($trait, $entity->reveal());
70 }
71
72 }