Skip to content

Commit

Permalink
Allow MD- prefix for Moldova postal codes (#55)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Ivanitsa <[email protected]>
  • Loading branch information
Kuller86 and Alexander Ivanitsa authored Jul 23, 2024
1 parent 6109e47 commit c060097
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ZipCodeValidator/Constraints/ZipCodeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ZipCodeValidator extends ConstraintValidator
'LV' => '(LV-)?\\d{4}',
'MA' => '\\d{5}',
'MC' => '980\\d{2}',
'MD' => '\\d{4}',
'MD' => '(MD-)?\\d{4}',
'ME' => '8\\d{4}',
'MF' => '9[78][01]\\d{2}',
'MG' => '\\d{3}',
Expand Down
94 changes: 94 additions & 0 deletions tests/Constraints/MdZipCodeValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace ZipCodeValidator\Tests\Constraints;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Context\ExecutionContext;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
use ZipCodeValidator\Constraints\ZipCode;
use ZipCodeValidator\Constraints\ZipCodeValidator;

class MdZipCodeValidatorTest extends TestCase
{
protected ZipCodeValidator $validator;

public function setUp(): void
{
$this->validator = new ZipCodeValidator;
}

/**
* @dataProvider getValidMoldovaZipCodes
*/
public function testValidZipcodes(string $zipCode): void
{
$constraint = new ZipCode('MD');

/** @var ExecutionContext|MockObject $contextMock */
$contextMock = $this->getMockBuilder(ExecutionContext::class)
->disableOriginalConstructor()
->getMock();

# be sure that buildViolation never gets called
$contextMock->expects($this->never())->method('buildViolation');
$contextMock->setConstraint($constraint);

$this->validator->initialize($contextMock);
$this->validator->validate($zipCode, $constraint);
}

/**
* Valid Moldova postal codes are four-digit numbers, optionally prefixed with "MD-".
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Moldova
*/
public static function getValidMoldovaZipCodes(): array
{
return [
['1234'],
['0123'],
['9876'],
['MD-1234'],
['MD-0123'],
['MD-9876'],
];
}

/**
* @dataProvider getInvalidMoldovaZipCodes
*/
public function testInvalidZipcodes(string $zipCode): void
{
$constraint = new ZipCode('MD');

$violation = $this->createMock(ConstraintViolationBuilderInterface::class);
$violation->expects($this->once())->method('setParameter')->willReturnSelf();

/** @var ExecutionContext|MockObject $contextMock */
$contextMock = $this->getMockBuilder(ExecutionContext::class)
->disableOriginalConstructor()
->getMock();

# be sure that buildViolation never gets called
$contextMock->expects($this->once())->method('buildViolation')->willReturn($violation);
$contextMock->setConstraint($constraint);

$this->validator->initialize($contextMock);
$this->validator->validate($zipCode, $constraint);
}

/**
* Valid Moldova postal codes are four-digit numbers, optionally prefixed with "MD-".
* @see https://en.wikipedia.org/wiki/Postal_codes_in_Moldova
*/
public static function getInvalidMoldovaZipCodes(): array
{
return [
['123'],
['12345'],
['MD12345'],
['MO-12345'],
['MD-12345'],
];
}
}

4 comments on commit c060097

@Kuller86
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@barbieswimcrew Could you create v2.0.2 with it commit?
I use php 7.4.33 and can't install package with version v3.1.2.

@barbieswimcrew
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kuller86 of course! The easiest way, i guess, would be if you'd checkout the v2.0.1 tag and create a new pull request for the v2 major release. Let me know, thanks 🫶

@Kuller86
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@barbieswimcrew I can't create pull request without separate branch in your repository.
Please create branch for the v2 major release, then I can creating new pull request.

@barbieswimcrew
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kuller86 my fault... sorry! Created the branch right now: https://github.com/barbieswimcrew/zip-code-validator/tree/2.0

Please sign in to comment.