Skip to content

Commit

Permalink
Additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Propaganistas committed Dec 22, 2023
1 parent 99f2d32 commit 7497c3e
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 170 deletions.
2 changes: 1 addition & 1 deletion src/Casts/PhoneNumberCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Database\Eloquent\SerializesCastableAttributes;
use Illuminate\Support\Arr;
use Propaganistas\LaravelPhone\Aspects\PhoneNumberCountry;
use Propaganistas\LaravelPhone\Concerns\PhoneNumberCountry;

abstract class PhoneNumberCast implements CastsAttributes, SerializesCastableAttributes
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Propaganistas\LaravelPhone\Aspects;
namespace Propaganistas\LaravelPhone\Concerns;

use Illuminate\Support\Collection;
use libphonenumber\PhoneNumberUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Propaganistas\LaravelPhone\Aspects;
namespace Propaganistas\LaravelPhone\Concerns;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Propaganistas\LaravelPhone\Aspects;
namespace Propaganistas\LaravelPhone\Concerns;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down
6 changes: 3 additions & 3 deletions src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use libphonenumber\PhoneNumberFormat as libPhoneNumberFormat;
use libphonenumber\PhoneNumberType as libPhoneNumberType;
use libphonenumber\PhoneNumberUtil;
use Propaganistas\LaravelPhone\Aspects\PhoneNumberCountry;
use Propaganistas\LaravelPhone\Aspects\PhoneNumberFormat;
use Propaganistas\LaravelPhone\Aspects\PhoneNumberType;
use Propaganistas\LaravelPhone\Concerns\PhoneNumberCountry;
use Propaganistas\LaravelPhone\Concerns\PhoneNumberFormat;
use Propaganistas\LaravelPhone\Concerns\PhoneNumberType;
use Propaganistas\LaravelPhone\Exceptions\CountryCodeException;
use Propaganistas\LaravelPhone\Exceptions\NumberFormatException;
use Propaganistas\LaravelPhone\Exceptions\NumberParseException;
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Illuminate\Support\Arr;
use Illuminate\Validation\Validator;
use libphonenumber\PhoneNumberType as libPhoneNumberType;
use Propaganistas\LaravelPhone\Aspects\PhoneNumberCountry;
use Propaganistas\LaravelPhone\Aspects\PhoneNumberType;
use Propaganistas\LaravelPhone\Concerns\PhoneNumberCountry;
use Propaganistas\LaravelPhone\Concerns\PhoneNumberType;
use Propaganistas\LaravelPhone\Exceptions\NumberParseException;
use Propaganistas\LaravelPhone\Exceptions\IncompatibleTypesException;
use Propaganistas\LaravelPhone\PhoneNumber;
Expand Down
225 changes: 225 additions & 0 deletions tests/RuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
<?php

namespace Propaganistas\LaravelPhone\Tests;

use Illuminate\Validation\Validator;
use libphonenumber\PhoneNumberType;
use Propaganistas\LaravelPhone\Exceptions\IncompatibleTypesException;
use Propaganistas\LaravelPhone\Rules\Phone;
use ReflectionClass;

class RuleTest extends TestCase
{
/** @test */
public function it_defaults_to_nothing()
{
$rule = (new Phone);

$this->assertEquals([], $this->getProtectedProperty($rule, 'countries'));
$this->assertNull($this->getProtectedProperty($rule, 'countryField'));
$this->assertEquals([], $this->getProtectedProperty($rule, 'allowedTypes'));
$this->assertEquals([], $this->getProtectedProperty($rule, 'blockedTypes'));
$this->assertFalse($this->getProtectedProperty($rule, 'lenient'));
$this->assertFalse($this->getProtectedProperty($rule, 'international'));
}

/** @test */
public function it_sets_countries()
{
$rule = (new Phone)->country('BE');
$this->assertEquals(['BE'], $this->getProtectedProperty($rule, 'countries'));

$rule = (new Phone)->country(['BE', 'NL']);
$this->assertEquals(['BE', 'NL'], $this->getProtectedProperty($rule, 'countries'));
}

/** @test */
public function it_merges_existing_countries()
{
$rule = (new Phone)->country('BE');
$rule->country('NL');

$this->assertEquals(['BE', 'NL'], $this->getProtectedProperty($rule, 'countries'));
}

/** @test */
public function it_sets_countryField()
{
$rule = (new Phone)->countryField('foo');
$this->assertEquals('foo', $this->getProtectedProperty($rule, 'countryField'));
}

/** @test */
public function it_sets_types()
{
$rule = (new Phone)->type('mobile');
$this->assertEquals(['mobile'], $this->getProtectedProperty($rule, 'allowedTypes'));

$rule = (new Phone)->type(['mobile', 'fixed_line']);
$this->assertEquals(['mobile', 'fixed_line'], $this->getProtectedProperty($rule, 'allowedTypes'));
}

/** @test */
public function it_merges_existing_types()
{
$rule = (new Phone)->type('mobile');
$rule->type('fixed_line');

$this->assertEquals(['mobile', 'fixed_line'], $this->getProtectedProperty($rule, 'allowedTypes'));
}

/** @test */
public function it_sets_blocked_types()
{
$rule = (new Phone)->notType('mobile');
$this->assertEquals(['mobile'], $this->getProtectedProperty($rule, 'blockedTypes'));

$rule = (new Phone)->notType(['mobile', 'fixed_line']);
$this->assertEquals(['mobile', 'fixed_line'], $this->getProtectedProperty($rule, 'blockedTypes'));
}

/** @test */
public function it_merges_existing_blocked_types()
{
$rule = (new Phone)->notType('mobile');
$rule->notType('fixed_line');

$this->assertEquals(['mobile', 'fixed_line'], $this->getProtectedProperty($rule, 'blockedTypes'));
}

/** @test */
public function it_sets_mobile_type_using_shortcut_method()
{
$rule = (new Phone)->mobile();
$this->assertEquals([PhoneNumberType::MOBILE], $this->getProtectedProperty($rule, 'allowedTypes'));
}

/** @test */
public function it_sets_fixed_line_type_using_shortcut_method()
{
$rule = (new Phone)->fixedLine();
$this->assertEquals([PhoneNumberType::FIXED_LINE], $this->getProtectedProperty($rule, 'allowedTypes'));
}

/** @test */
public function it_sets_lenient_mode()
{
$rule = (new Phone)->lenient();
$this->assertTrue($this->getProtectedProperty($rule, 'lenient'));
}

/** @test */
public function it_sets_international_mode()
{
$rule = (new Phone)->international();
$this->assertTrue($this->getProtectedProperty($rule, 'international'));
}

/** @test */
public function it_returns_default_validation_message()
{
app('translator')->setLocale('xx');

app('translator')->setLoaded([
'*' => [
'validation' => [
'xx' => [
'phone' => 'foo',
],
],
],
]);

$this->assertEquals('foo', (new Phone)->message());
}

/** @test */
public function it_converts_string_validation_parameters()
{
$base = (new Phone)->setValidator(validator(['foo' => null]));

$rule = (clone $base)->setParameters('lenient');
$this->assertTrue($this->getProtectedProperty($rule, 'lenient'));

$rule = (clone $base)->setParameters('international');
$this->assertTrue($this->getProtectedProperty($rule, 'international'));

$rule = (clone $base)->setParameters('foo');
$this->assertEquals('foo', $this->getProtectedProperty($rule, 'countryField'));

$rule = (clone $base)->setParameters('be');
$this->assertEquals(['be'], $this->getProtectedProperty($rule, 'countries'));

$rule = (clone $base)->setParameters('mobile');
$this->assertEquals(['mobile'], $this->getProtectedProperty($rule, 'allowedTypes'));

$rule = (clone $base)->setParameters('fixed_line');
$this->assertEquals(['fixed_line'], $this->getProtectedProperty($rule, 'allowedTypes'));

$rule = (clone $base)->setParameters([(string) PhoneNumberType::MOBILE]);
$this->assertEquals([PhoneNumberType::MOBILE], $this->getProtectedProperty($rule, 'allowedTypes'));

$rule = (clone $base)->setParameters(['!mobile']);
$this->assertEquals(['mobile'], $this->getProtectedProperty($rule, 'blockedTypes'));

$rule = (clone $base)->setParameters(['!fixed_line']);
$this->assertEquals(['fixed_line'], $this->getProtectedProperty($rule, 'blockedTypes'));

$rule = (clone $base)->setParameters(['!'.PhoneNumberType::MOBILE]);
$this->assertEquals([PhoneNumberType::MOBILE], $this->getProtectedProperty($rule, 'blockedTypes'));

$rule = (clone $base)->setParameters(['lenient', 'international', 'foo', 'be', 'nl', 'mobile', 'fixed_line']);
$this->assertTrue($this->getProtectedProperty($rule, 'lenient'));
$this->assertTrue($this->getProtectedProperty($rule, 'international'));
$this->assertEquals('foo', $this->getProtectedProperty($rule, 'countryField'));
$this->assertEquals(['be','nl'], $this->getProtectedProperty($rule, 'countries'));
$this->assertEquals(['mobile', 'fixed_line'], $this->getProtectedProperty($rule, 'allowedTypes'));

$rule = (clone $base)->setParameters(['lenient', 'international', 'foo', 'be', 'nl', '!mobile', '!fixed_line']);
$this->assertTrue($this->getProtectedProperty($rule, 'lenient'));
$this->assertTrue($this->getProtectedProperty($rule, 'international'));
$this->assertEquals('foo', $this->getProtectedProperty($rule, 'countryField'));
$this->assertEquals(['be','nl'], $this->getProtectedProperty($rule, 'countries'));
$this->assertEquals(['mobile', 'fixed_line'], $this->getProtectedProperty($rule, 'blockedTypes'));
}

/** @test */
public function it_treats_string_validation_parameters_case_insensitive()
{
$base = (new Phone)->setValidator(validator(['foo' => null]));

$rule = (clone $base)->setParameters('LeNIent');
$this->assertTrue($this->getProtectedProperty($rule, 'lenient'));

$rule = (clone $base)->setParameters('InteRNAtional');
$this->assertTrue($this->getProtectedProperty($rule, 'international'));

$rule = (clone $base)->setParameters('MoBIle');
$this->assertEquals(['MoBIle'], $this->getProtectedProperty($rule, 'allowedTypes'));

$rule = (clone $base)->setParameters(['!MoBIle']);
$this->assertEquals(['MoBIle'], $this->getProtectedProperty($rule, 'blockedTypes'));
}

/** @test */
public function it_ignores_invalid_string_validation_parameters()
{
$base = (new Phone)->setValidator(validator([]));
$rule = (clone $base)->setParameters(['xyz', 'foo']);

$this->assertEquals([], $this->getProtectedProperty($rule, 'countries'));
$this->assertNull($this->getProtectedProperty($rule, 'countryField'));
$this->assertEquals([], $this->getProtectedProperty($rule, 'allowedTypes'));
$this->assertEquals([], $this->getProtectedProperty($rule, 'blockedTypes'));
$this->assertFalse($this->getProtectedProperty($rule, 'lenient'));
$this->assertFalse($this->getProtectedProperty($rule, 'international'));
}

protected function getProtectedProperty(object $object, string $property)
{
$property = (new ReflectionClass($object))->getProperty($property);
$property->setAccessible(true);

return $property->getValue($object);
}
}
Loading

1 comment on commit 7497c3e

@ahmedsayedabdelsalam
Copy link

@ahmedsayedabdelsalam ahmedsayedabdelsalam commented on 7497c3e Apr 24, 2024

Choose a reason for hiding this comment

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

changing namespace is breaking change. 👎

Please sign in to comment.