|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ZipCodeValidator\Tests\Constraints; |
| 6 | + |
| 7 | +use PHPUnit\Framework\MockObject\MockObject; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Symfony\Component\Validator\Context\ExecutionContext; |
| 10 | +use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; |
| 11 | +use ZipCodeValidator\Constraints\ZipCode; |
| 12 | +use ZipCodeValidator\Constraints\ZipCodeValidator; |
| 13 | + |
| 14 | +class NaZipCodeValidatorTest extends TestCase |
| 15 | +{ |
| 16 | + protected ZipCodeValidator $validator; |
| 17 | + |
| 18 | + public function setUp(): void |
| 19 | + { |
| 20 | + $this->validator = new ZipCodeValidator; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @dataProvider naValidZipCodes |
| 25 | + */ |
| 26 | + public function testValidationOfNaZipCode(string $zipCode): void |
| 27 | + { |
| 28 | + $constraint = new ZipCode('NA'); |
| 29 | + |
| 30 | + /** @var ExecutionContext|MockObject $contextMock */ |
| 31 | + $contextMock = $this->getMockBuilder(ExecutionContext::class) |
| 32 | + ->disableOriginalConstructor() |
| 33 | + ->getMock(); |
| 34 | + |
| 35 | + //be sure that buildViolation never gets called |
| 36 | + $contextMock->expects($this->never())->method('buildViolation'); |
| 37 | + |
| 38 | + $contextMock->setConstraint($constraint); |
| 39 | + $this->validator->initialize($contextMock); |
| 40 | + |
| 41 | + // Test some variations |
| 42 | + $this->validator->validate($zipCode, $constraint); |
| 43 | + } |
| 44 | + |
| 45 | + public static function naValidZipCodes(): array |
| 46 | + { |
| 47 | + return [ |
| 48 | + ['13006'], |
| 49 | + ['22002'], |
| 50 | + ['19001'], |
| 51 | + ['10033'], |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @dataProvider naInvalidZipCodes |
| 57 | + */ |
| 58 | + public function testValidationErrorWithInvalidNaZipCode(string $zipcode): void |
| 59 | + { |
| 60 | + $constraint = new ZipCode('NA'); |
| 61 | + |
| 62 | + $violationBuilderMock = $this->getMockBuilder(ConstraintViolationBuilderInterface::class) |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->getMock(); |
| 65 | + $violationBuilderMock->expects($this->once())->method('setParameter')->willReturnSelf(); |
| 66 | + |
| 67 | + /** @var ExecutionContext|MockObject $contextMock */ |
| 68 | + $contextMock = $this->getMockBuilder(ExecutionContext::class) |
| 69 | + ->disableOriginalConstructor() |
| 70 | + ->getMock(); |
| 71 | + $contextMock->expects($this->once()) |
| 72 | + ->method('buildViolation') |
| 73 | + ->with($constraint->message) |
| 74 | + ->willReturn($violationBuilderMock); |
| 75 | + |
| 76 | + $contextMock->setConstraint($constraint); |
| 77 | + $this->validator->initialize($contextMock); |
| 78 | + $this->validator->validate($zipcode, $constraint); |
| 79 | + } |
| 80 | + |
| 81 | + public static function naInvalidZipCodes(): array |
| 82 | + { |
| 83 | + return [ |
| 84 | + ['12345'], |
| 85 | + ['14-20'], |
| 86 | + ['110655'], |
| 87 | + ['024567'], |
| 88 | + ['PP023'], |
| 89 | + ['AC-70'], |
| 90 | + ]; |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments