Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * /src/Validator/Constraints/UniqueEmailValidatorTest.php
  5. *
  6. * @author TLe, Tarmo Leppänen <tarmo.leppanen@protacon.com>
  7. */
  8. namespace App\Tests\Integration\Validator\Constraints;
  9.  
  10. use App\Entity\User;
  11. use App\Repository\UserRepository;
  12. use App\Validator\Constraints\UniqueEmail;
  13. use App\Validator\Constraints\UniqueEmailValidator;
  14. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  15. use Symfony\Component\Validator\Context\ExecutionContext;
  16. use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
  17.  
  18. /**
  19. * Class UniqueEmailValidatorTest
  20. *
  21. * @package App\Validator\Constraints
  22. * @author TLe, Tarmo Leppänen <tarmo.leppanen@protacon.com>
  23. */
  24. class UniqueEmailValidatorTest extends KernelTestCase
  25. {
  26. /**
  27. * @var UniqueEmail
  28. */
  29. private $constraint;
  30.  
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject|ExecutionContext
  33. */
  34. private $context;
  35.  
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject|ConstraintViolationBuilderInterface
  38. */
  39. private $builder;
  40.  
  41. /**
  42. * @throws \Doctrine\ORM\NonUniqueResultException
  43. */
  44. public function testThatValidateCallsExpectedMethods(): void
  45. {
  46. // Create new user
  47. $user = new User();
  48. $user->setEmail('john.doe@test.com');
  49.  
  50. /**
  51. * @var \PHPUnit_Framework_MockObject_MockObject|UserRepository $repository
  52. */
  53. $repository = $this->getMockBuilder(UserRepository::class)->disableOriginalConstructor()->getMock();
  54.  
  55. $repository
  56. ->expects(static::once())
  57. ->method('isEmailAvailable')
  58. ->with($user->getEmail(), $user->getId())
  59. ->willReturn(false);
  60.  
  61. $this->context
  62. ->expects(static::once())
  63. ->method('buildViolation')
  64. ->with(UniqueEmail::MESSAGE)
  65. ->willReturn($this->builder);
  66.  
  67. $this->builder
  68. ->expects(static::once())
  69. ->method('setCode')
  70. ->with(UniqueEmail::IS_UNIQUE_EMAIL_ERROR)
  71. ->willReturn($this->builder);
  72.  
  73. $this->builder
  74. ->expects(static::once())
  75. ->method('addViolation');
  76.  
  77. // Run validator
  78. $validator = new UniqueEmailValidator($repository);
  79. $validator->initialize($this->context);
  80. $validator->validate($user, $this->constraint);
  81.  
  82. unset($validator, $repository, $user);
  83. }
  84.  
  85. /**
  86. * {@inheritDoc}
  87. */
  88. protected function setUp(): void
  89. {
  90. $this->constraint = new UniqueEmail();
  91. $this->context = $this->getMockBuilder(ExecutionContext::class)->disableOriginalConstructor()->getMock();
  92. $this->builder = $this->getMockBuilder(ConstraintViolationBuilderInterface::class)->getMock();
  93. }
  94.  
  95. protected function tearDown(): void
  96. {
  97. parent::tearDown();
  98.  
  99. unset($this->constraint, $this->context, $this->builder);
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement