|
|
@@ -0,0 +1,77 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Tests\Unit\ValueObjects;
|
|
|
+
|
|
|
+use App\Models\Franchisee;
|
|
|
+use App\Models\StudentResponsible;
|
|
|
+use App\Models\UnitPartner;
|
|
|
+use App\Models\User;
|
|
|
+use App\ValueObjects\Cpf;
|
|
|
+use Illuminate\Support\Facades\Validator;
|
|
|
+use InvalidArgumentException;
|
|
|
+use Tests\TestCase;
|
|
|
+
|
|
|
+class CpfTest extends TestCase
|
|
|
+{
|
|
|
+ public function test_it_accepts_and_normalizes_a_valid_cpf(): void
|
|
|
+ {
|
|
|
+ $cpf = new Cpf('529.982.247-25');
|
|
|
+
|
|
|
+ $this->assertSame('52998224725', $cpf->value());
|
|
|
+ $this->assertSame('529.982.247-25', $cpf->formatted());
|
|
|
+ $this->assertSame('52998224725', (string) $cpf);
|
|
|
+ $this->assertSame('"52998224725"', json_encode($cpf));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_it_rejects_an_invalid_cpf(): void
|
|
|
+ {
|
|
|
+ app()->setLocale('pt');
|
|
|
+
|
|
|
+ $this->expectException(InvalidArgumentException::class);
|
|
|
+ $this->expectExceptionMessage('O CPF informado é inválido.');
|
|
|
+
|
|
|
+ new Cpf('111.111.111-11');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_its_request_rule_validates_cpf(): void
|
|
|
+ {
|
|
|
+ $valid = Validator::make(
|
|
|
+ ['cpf' => '529.982.247-25'],
|
|
|
+ ['cpf' => ['required', Cpf::rule()]],
|
|
|
+ );
|
|
|
+ $invalid = Validator::make(
|
|
|
+ ['cpf' => '529.982.247-24'],
|
|
|
+ ['cpf' => ['required', Cpf::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(
|
|
|
+ ['cpf' => '529.982.247-24'],
|
|
|
+ ['cpf' => [Cpf::rule()]],
|
|
|
+ );
|
|
|
+
|
|
|
+ $this->assertSame(
|
|
|
+ 'El CPF proporcionado no es válido.',
|
|
|
+ $validator->errors()->first('cpf'),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_models_cast_cpf_to_the_value_object(): void
|
|
|
+ {
|
|
|
+ foreach ([Franchisee::class, StudentResponsible::class, UnitPartner::class, User::class] as $modelClass) {
|
|
|
+ $model = new $modelClass;
|
|
|
+
|
|
|
+ $model->cpf = '529.982.247-25';
|
|
|
+
|
|
|
+ $this->assertInstanceOf(Cpf::class, $model->cpf);
|
|
|
+ $this->assertSame('52998224725', $model->getAttributes()['cpf']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|