Skip to content

Commit

Permalink
fix and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnpdlk committed Nov 8, 2017
1 parent fa02e15 commit c7a3bae
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/mrcnpdlk/Validator/Types/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ public static function isValid($checkedValue, bool $asEx = false): bool
{
try {
static::isValidType($checkedValue, static::TYPE_STRING, true);
if (!preg_match('/(?:' . static::COUNTRY_PREFIX . ')?([\d]{9})/', $checkedValue, $f)) {
if (!preg_match('/^' . static::COUNTRY_PREFIX . '[\d]{9}$/', $checkedValue)
&& !preg_match('/^[\d]{9}$/', $checkedValue)) {
throw new \Exception('regex error');
} else {
print_r($f);
}

return true;
Expand Down
75 changes: 75 additions & 0 deletions tests/Validator/PhoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Validator
*
* Copyright (c) 2017 pudelek.org.pl
*
* @license MIT License (MIT)
*
* For the full copyright and license information, please view source file
* that is bundled with this package in the file LICENSE
*
* @author Marcin Pudełek <marcin@pudelek.org.pl>
*/

namespace mrcnpdlk\Validator;

use mrcnpdlk\Validator\Types\Phone;

class PhoneTest extends TestCase
{
public function testCreate()
{
$res = Phone::create('48123123123');
$this->assertEquals('123123123', $res->get());
}

public function testMobile()
{
$defNr = '48601123123';
$res = new Phone($defNr);
$this->assertTrue($res->isMobile());
$this->assertFalse($res->isFixed());
$this->assertFalse($res->isVoip());
$this->assertFalse($res->isUAN());
$this->assertFalse($res->isSharedCost());
$this->assertFalse($res->isPremiumRate());
$this->assertFalse($res->isTollFree());
}
public function testFixed()
{
$defNr = '48421123123';
$res = new Phone($defNr);
$this->assertFalse($res->isMobile());
$this->assertTrue($res->isFixed());
$this->assertFalse($res->isVoip());
$this->assertFalse($res->isUAN());
$this->assertFalse($res->isSharedCost());
$this->assertFalse($res->isPremiumRate());
$this->assertFalse($res->isTollFree());
}

/**
* @expectedException \mrcnpdlk\Validator\Exception
*/
public function testPhoneEmpty()
{
new Phone('');
}

/**
* @expectedException \mrcnpdlk\Validator\Exception
*/
public function testPhoneInvalid()
{
new Phone('44123123123');
}

public function testPhoneValid()
{
$defNr = '48123123123';
$res = new Phone($defNr);
$this->assertEquals('123123123', $res->get());
}

}

0 comments on commit c7a3bae

Please sign in to comment.