Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Added test to check if possible to set country with context #42

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Validator/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function getCountry()
*/
public function setCountry($country)
{
$this->country = strtoupper($country);
$this->country = $country;

return $this;
}
Expand Down Expand Up @@ -201,12 +201,12 @@ public function isValid($value = null, $context = null)

$country = $this->getCountry();

if (!$countryPattern = $this->loadPattern($country)) {
if (!$countryPattern = $this->loadPattern(strtoupper($country))) {
if (isset($context[$country])) {
$country = $context[$country];
}

if (!$countryPattern = $this->loadPattern($country)) {
if (!$countryPattern = $this->loadPattern(strtoupper($country))) {
$this->error(self::UNSUPPORTED);

return false;
Expand Down
18 changes: 15 additions & 3 deletions test/Validator/PhoneNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3164,10 +3164,12 @@ public function testAllowPossibleSetterGetter()
$this->assertTrue($this->validator->allowPossible());
}

public function testSetCountryMethodIsCaseInsensitive()
public function testCountryIsCaseInsensitive()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know changing tests is not good but maybe not a BC break? Anyway getCountry() is used only in one place and I covered it so maybe not a problem?

Copy link
Member

Choose a reason for hiding this comment

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

Your changes make sense. We'll want to be able to retrieve the original version, but also validate in a case-insensitive manner. This is a good solution.

{
$this->validator->setCountry('us');
$this->assertSame('US', $this->validator->getCountry());
$this->validator->setCountry('lt');
$this->assertTrue($this->validator->isValid('+37067811268'));
$this->validator->setCountry('LT');
$this->assertTrue($this->validator->isValid('+37067811268'));
}

/**
Expand Down Expand Up @@ -3202,4 +3204,14 @@ public function testInvalidTypes($country, $code, $patterns)
}
}
}

public function testCanSpecifyCountryWithContext()
{
Locale::setDefault('ZW');
$validator = new PhoneNumber([
'country' => 'country-code',
]);

$this->assertTrue($validator->isValid('+37067811268', ['country-code' => 'LT']));
}
}