Skip to content

Commit

Permalink
100% Coverage (#11)
Browse files Browse the repository at this point in the history
* Phone is now 100% covered

* PhoneFactory now has 100% coverage

* PhoneValidator now has 100% coverage
  • Loading branch information
ellisio authored Sep 28, 2017
1 parent 4cc86cd commit c3878dc
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/PhoneFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

/**
Expand All @@ -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];
}

Expand Down
4 changes: 2 additions & 2 deletions src/PhoneValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 33 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EllisIO\Tests\Phone;

use InvalidArgumentException;
use EllisIO\Phone\PhoneFactory;
use EllisIO\Phone\Contracts\Driver;

Expand Down Expand Up @@ -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'));
Expand Down
13 changes: 13 additions & 0 deletions tests/PhoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EllisIO\Tests\Phone;

use InvalidArgumentException;
use Illuminate\Support\Facades\Validator;

class ValidatorTest extends AbstractTestCase
Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit c3878dc

Please sign in to comment.