|
|
@@ -0,0 +1,83 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Tests\Unit\ValueObjects;
|
|
|
+
|
|
|
+use App\Models\Franchisee;
|
|
|
+use App\Models\Unit;
|
|
|
+use App\ValueObjects\Cnpj;
|
|
|
+use Illuminate\Support\Facades\Validator;
|
|
|
+use InvalidArgumentException;
|
|
|
+use Tests\TestCase;
|
|
|
+
|
|
|
+class CnpjTest extends TestCase
|
|
|
+{
|
|
|
+ public function test_it_accepts_and_normalizes_a_numeric_cnpj(): void
|
|
|
+ {
|
|
|
+ $cnpj = new Cnpj('04.252.011/0001-10');
|
|
|
+
|
|
|
+ $this->assertSame('04252011000110', $cnpj->value());
|
|
|
+ $this->assertSame('04.252.011/0001-10', $cnpj->formatted());
|
|
|
+ $this->assertSame('04252011000110', (string) $cnpj);
|
|
|
+ $this->assertSame('"04252011000110"', json_encode($cnpj));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_it_accepts_and_normalizes_an_alphanumeric_cnpj(): void
|
|
|
+ {
|
|
|
+ $cnpj = new Cnpj('12.abc.345/01de-35');
|
|
|
+
|
|
|
+ $this->assertSame('12ABC34501DE35', $cnpj->value());
|
|
|
+ $this->assertSame('12.ABC.345/01DE-35', $cnpj->formatted());
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_it_rejects_an_invalid_cnpj(): void
|
|
|
+ {
|
|
|
+ app()->setLocale('pt');
|
|
|
+
|
|
|
+ $this->expectException(InvalidArgumentException::class);
|
|
|
+ $this->expectExceptionMessage('O CNPJ informado é inválido.');
|
|
|
+
|
|
|
+ new Cnpj('04.252.011/0001-11');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_its_request_rule_validates_cnpj(): void
|
|
|
+ {
|
|
|
+ $valid = Validator::make(
|
|
|
+ ['cnpj' => '12.ABC.345/01DE-35'],
|
|
|
+ ['cnpj' => ['required', Cnpj::rule()]],
|
|
|
+ );
|
|
|
+ $invalid = Validator::make(
|
|
|
+ ['cnpj' => '12.ABC.345/01DE-36'],
|
|
|
+ ['cnpj' => ['required', Cnpj::rule()]],
|
|
|
+ );
|
|
|
+
|
|
|
+ $this->assertTrue($valid->passes());
|
|
|
+ $this->assertTrue($invalid->fails());
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_its_request_rule_uses_the_current_locale(): void
|
|
|
+ {
|
|
|
+ app()->setLocale('es');
|
|
|
+
|
|
|
+ $validator = Validator::make(
|
|
|
+ ['cnpj' => '04.252.011/0001-11'],
|
|
|
+ ['cnpj' => [Cnpj::rule()]],
|
|
|
+ );
|
|
|
+
|
|
|
+ $this->assertSame(
|
|
|
+ 'El CNPJ proporcionado no es válido.',
|
|
|
+ $validator->errors()->first('cnpj'),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_models_cast_cnpj_to_the_value_object(): void
|
|
|
+ {
|
|
|
+ foreach ([Franchisee::class, Unit::class] as $modelClass) {
|
|
|
+ $model = new $modelClass;
|
|
|
+
|
|
|
+ $model->cnpj = '04.252.011/0001-10';
|
|
|
+
|
|
|
+ $this->assertInstanceOf(Cnpj::class, $model->cnpj);
|
|
|
+ $this->assertSame('04252011000110', $model->getAttributes()['cnpj']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|