-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow rejecting certain phone number types (#238)
* Allow rejecting certain phone number types * Throw custom exception if allowed and blocked types are both provided
- Loading branch information
1 parent
99e14d7
commit 1083634
Showing
3 changed files
with
92 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Propaganistas\LaravelPhone\Exceptions; | ||
|
||
class IncompatibleTypesException extends \Exception | ||
{ | ||
public static function invalid() | ||
{ | ||
return new static('Cannot use type and notType at the same time.'); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
namespace Propaganistas\LaravelPhone\Tests; | ||
|
||
use Illuminate\Validation\Validator; | ||
use libphonenumber\PhoneNumberType; | ||
use Propaganistas\LaravelPhone\Rules\Phone; | ||
use Propaganistas\LaravelPhone\Exceptions\IncompatibleTypesException; | ||
|
||
class PhoneRuleValidatorTest extends TestCase | ||
{ | ||
protected function validate(array $data, array $rules): Validator | ||
{ | ||
return $this->app['validator']->make($data, $rules); | ||
} | ||
|
||
/** @test */ | ||
public function it_validates_type() | ||
{ | ||
$this->assertTrue($this->validate( | ||
['field' => '+32470123456'], | ||
['field' => (new Phone)->type(PhoneNumberType::MOBILE)] | ||
)->passes()); | ||
|
||
$this->assertFalse($this->validate( | ||
['field' => '+3212345678'], | ||
['field' => (new Phone)->type(PhoneNumberType::MOBILE)] | ||
)->passes()); | ||
} | ||
|
||
/** @test */ | ||
public function it_validates_negative_type() | ||
{ | ||
$this->assertFalse($this->validate( | ||
['field' => '+32470123456'], | ||
['field' => (new Phone)->notType(PhoneNumberType::MOBILE)] | ||
)->passes()); | ||
|
||
$this->assertTrue($this->validate( | ||
['field' => '+3212345678'], | ||
['field' => (new Phone)->notType(PhoneNumberType::MOBILE)] | ||
)->passes()); | ||
} | ||
|
||
/** @test */ | ||
public function it_doesnt_allow_type_and_not_type() | ||
{ | ||
$this->expectException(IncompatibleTypesException::class); | ||
|
||
$this->validate( | ||
['field' => '+3212345678'], | ||
['field' => (new Phone)->type(PhoneNumberType::MOBILE)->notType(PhoneNumberType::MOBILE)] | ||
)->passes(); | ||
} | ||
} |