diff --git a/src/PhoneFactory.php b/src/PhoneFactory.php index d53e1f6..1cf5a70 100644 --- a/src/PhoneFactory.php +++ b/src/PhoneFactory.php @@ -67,17 +67,13 @@ protected function resolve($name) { $config = $this->getConfig($name); - if (is_null($config)) { - throw new InvalidArgumentException("Phone driver [{$name}] is not defined."); - } - $driverMethod = 'create'.ucfirst($name).'Driver'; if (method_exists($this, $driverMethod)) { return $this->{$driverMethod}($config); } - throw new InvalidArgumentException("Driver [{$config['driver']}] is not defined."); + throw new InvalidArgumentException("Method [{$driverMethod}] does not exist."); } /** @@ -101,6 +97,10 @@ protected function createTwilioDriver(array $config) */ protected function getConfig($name) { + if (! isset($this->config['drivers'][$name])) { + throw new InvalidArgumentException("Config driver [{$name}] is not defined."); + } + return $this->config['drivers'][$name]; } diff --git a/src/PhoneValidator.php b/src/PhoneValidator.php index 4da02bb..57b5c15 100644 --- a/src/PhoneValidator.php +++ b/src/PhoneValidator.php @@ -42,8 +42,8 @@ public function validatePhone(string $attribute, string $value, array $params) */ public function validatePhoneCountry(string $attribute, string $value, array $params) { - if (count($params) < 1) { - throw new InvalidArgumentException('Validation rule phone_country requires at least 1 parameters.'); + if (empty($params)) { + throw new InvalidArgumentException('Validation rule phone_country requires at least 1 parameter.'); } try { diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index b45a44c..9fbfa5a 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -2,6 +2,7 @@ namespace EllisIO\Tests\Phone; +use InvalidArgumentException; use EllisIO\Phone\PhoneFactory; use EllisIO\Phone\Contracts\Driver; @@ -30,6 +31,38 @@ public function testSetDefaultDriver() $this->assertSame('test', $factory->getDefaultDriver()); } + public function testUndefinedConfig() + { + try { + $factory = $this->getFactory(); + $factory->driver('test'); + } catch (InvalidArgumentException $e) { + $this->assertStringStartsWith('Config driver', $e->getMessage()); + } + } + + public function testUndefinedDriver() + { + try { + $config = $this->app->config->get('phone'); + $config['drivers']['test'] = []; + + $factory = new PhoneFactory($config); + $factory->driver('test'); + } catch (InvalidArgumentException $e) { + $this->assertStringStartsWith('Method', $e->getMessage()); + } + } + + public function testMagicCall() + { + $factory = $this->getFactory(); + $phone1 = $factory->formatNumber('+14153902337'); + $phone2 = $factory->driver()->formatNumber('+14153902337'); + + $this->assertSame($phone1, $phone2); + } + protected function getFactory() { return new PhoneFactory($this->app->config->get('phone')); diff --git a/tests/PhoneTest.php b/tests/PhoneTest.php index 54ee170..d7ee802 100644 --- a/tests/PhoneTest.php +++ b/tests/PhoneTest.php @@ -3,9 +3,22 @@ namespace EllisIO\Tests\Phone; use EllisIO\Phone\Phone; +use InvalidArgumentException; class PhoneTest extends AbstractTestCase { + public function testE164Compliancy() + { + $this->expectException(InvalidArgumentException::class); + new Phone('test', 'test', 'test'); + } + + public function testISO3166Alpha2Compliancy() + { + $this->expectException(InvalidArgumentException::class); + new Phone('+14153902337', '(415) 390-2337', 'test'); + } + public function testGetNumber() { $phone = $this->getPhone(); diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 3751cba..3693e59 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -2,6 +2,7 @@ namespace EllisIO\Tests\Phone; +use InvalidArgumentException; use Illuminate\Support\Facades\Validator; class ValidatorTest extends AbstractTestCase @@ -76,6 +77,13 @@ public function testValidatePhoneCountryCanadaFails() ); } + public function testValidatePhoneCountryParamsLessThanZero() + { + $this->expectException(InvalidArgumentException::class); + $validator = $this->getValidator('4153902337', 'phone_country'); + $this->assertTrue($validator->passes()); + } + protected function getValidator($phone, $rules) { return Validator::make([