-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow MD- prefix for Moldova postal codes (#55)
Co-authored-by: Alexander Ivanitsa <[email protected]>
- Loading branch information
Showing
2 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |
c060097
There was a problem hiding this comment.
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.
c060097
There was a problem hiding this comment.
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 🫶
c060097
There was a problem hiding this comment.
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.
c060097
There was a problem hiding this comment.
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