File manager - Edit - /var/www/html/portalHomolog/tests/Unit/Importacoes/ImportacaoProdataContaAngraServiceTest.php
Back
<?php namespace Tests\Unit\Importacoes; use App\Http\Services\Importacoes\ImportacaoProdataContaAngraService; use App\Models\Permissao; use App\Models\User; use Illuminate\Support\Facades\DB; use Tests\Support\Importacoes\ImportacaoProdataTestHelpers; use Tests\TestCase; class ImportacaoProdataContaAngraServiceTest extends TestCase { use ImportacaoProdataTestHelpers; protected ImportacaoProdataContaAngraService $service; protected function setUp(): void { parent::setUp(); $this->service = new ImportacaoProdataContaAngraService(); $this->limparUsuariosTeste(); } protected function tearDown(): void { $this->limparUsuariosTeste(); parent::tearDown(); } public function test_normaliza_cpf_removendo_caracteres_nao_numericos(): void { $this->assertSame('12345678909', $this->service->normalizarCpf('123.456.789-09')); $this->assertSame('', $this->service->normalizarCpf(null)); $this->assertSame('00011122233', $this->service->normalizarCpf(' 000.111.222-33 ')); } public function test_normaliza_cpf_com_zero_a_esquerda_quando_planilha_exporta_dez_digitos(): void { $this->assertSame('09291243752', $this->service->normalizarCpf('9291243752')); $this->assertTrue($this->service->cpfValido($this->service->normalizarCpf('9291243752'))); } public function test_normaliza_email_em_minusculo_e_trim(): void { $this->assertSame('teste@angra.rj.gov.br', $this->service->normalizarEmail(' Teste@Angra.RJ.gov.BR ')); $this->assertSame('', $this->service->normalizarEmail(null)); } public function test_normaliza_matricula_mantendo_apenas_digitos(): void { $this->assertSame('123456', $this->service->normalizarMatricula('123-456')); $this->assertNull($this->service->normalizarMatricula(' ')); $this->assertNull($this->service->normalizarMatricula(null)); } public function test_normaliza_nome_em_first_e_last(): void { $nomes = $this->service->normalizarNome(' Joao da Silva Sauro '); $this->assertNotNull($nomes); $this->assertSame('Joao', $nomes['firstName']); $this->assertSame('da Silva Sauro', $nomes['lastName']); $this->assertNull($this->service->normalizarNome('')); $this->assertNull($this->service->normalizarNome(null)); } public function test_valida_cpf_com_checksum_oficial(): void { $cpfValido = $this->gerarCpfTeste(1); $this->assertTrue($this->service->cpfValido($cpfValido)); $this->assertFalse($this->service->cpfValido('11111111111')); $this->assertFalse($this->service->cpfValido('12345678900')); $this->assertFalse($this->service->cpfValido('')); $this->assertFalse($this->service->cpfValido('123')); } public function test_validar_retorna_mensagem_para_cpf_invalido(): void { $erro = $this->service->validar( '11111111111', 'teste@angra.rj.gov.br', '12345', ['firstName' => 'Joao', 'lastName' => 'Silva'], $this->getStructureIdTeste() ); $this->assertNotNull($erro); } public function test_validar_retorna_mensagem_para_email_invalido(): void { $erro = $this->service->validar( $this->gerarCpfTeste(2), 'email-invalido', '12345', ['firstName' => 'Joao', 'lastName' => 'Silva'], $this->getStructureIdTeste() ); $this->assertNotNull($erro); } public function test_validar_retorna_mensagem_para_structure_id_nulo(): void { $erro = $this->service->validar( $this->gerarCpfTeste(3), 'teste@angra.rj.gov.br', '12345', ['firstName' => 'Joao', 'lastName' => 'Silva'], null ); $this->assertNotNull($erro); } public function test_validar_retorna_null_para_dados_validos(): void { $erro = $this->service->validar( $this->gerarCpfTeste(4), 'teste@angra.rj.gov.br', '12345', ['firstName' => 'Joao', 'lastName' => 'Silva'], $this->getStructureIdTeste() ); $this->assertNull($erro); } public function test_processar_linha_cria_usuario_novo_com_permissoes_padrao(): void { $cpf = $this->gerarCpfTeste(10); $structureId = $this->getStructureIdTeste(); $resultado = $this->service->processarLinha([ 'cpf' => $cpf, 'matricula' => '10001', 'nome' => 'TESTE IMPORTACAO UM', 'unidade' => 'IGNORADO_AQUI', 'email' => 'teste.importacao.um@example.com', ], $structureId, 2); $this->assertSame(ImportacaoProdataContaAngraService::RESULTADO_CRIADO, $resultado['resultado']); $user = User::where('cpf', $cpf)->first(); $this->assertNotNull($user); $this->assertSame('TESTE', $user->firstName); $this->assertSame('IMPORTACAO UM', $user->lastName); $this->assertSame('teste.importacao.um@example.com', $user->getAttributes()['email']); $this->assertSame('10001', $user->matriculation); $this->assertSame($structureId, $user->structure_id); $this->assertSame(1, (int) $user->isPasswordDefault); $this->assertSame('Sim', $user->isServidorPublico); $permissoes = Permissao::where('user_id', $user->id)->pluck('permissao')->all(); sort($permissoes); $this->assertSame(['GERENCIADOR_DE_CONTA_ANGRA', 'GUEST'], $permissoes); } public function test_processar_linha_idempotente_marca_como_sincronizado_na_segunda_execucao(): void { $cpf = $this->gerarCpfTeste(11); $structureId = $this->getStructureIdTeste(); $linha = [ 'cpf' => $cpf, 'matricula' => '20002', 'nome' => 'TESTE IDEMPOTENCIA', 'unidade' => '-', 'email' => 'idempotencia@example.com', ]; $this->service->processarLinha($linha, $structureId, 2); $segunda = $this->service->processarLinha($linha, $structureId, 3); $this->assertSame(ImportacaoProdataContaAngraService::RESULTADO_SINCRONIZADO, $segunda['resultado']); $this->assertSame([], $segunda['campos']); } public function test_dirty_check_atualiza_apenas_campos_divergentes(): void { $cpf = $this->gerarCpfTeste(12); [$s1, $s2] = $this->getDuasStructuresTeste(); $this->service->processarLinha([ 'cpf' => $cpf, 'matricula' => '30003', 'nome' => 'TESTE DIRTY', 'unidade' => '-', 'email' => 'dirty@example.com', ], $s1, 2); $resultado = $this->service->processarLinha([ 'cpf' => $cpf, 'matricula' => '30003', 'nome' => 'TESTE DIRTY', 'unidade' => '-', 'email' => 'dirty.novo@example.com', ], $s1, 3); $this->assertSame(ImportacaoProdataContaAngraService::RESULTADO_ATUALIZADO, $resultado['resultado']); $this->assertArrayHasKey('email', $resultado['campos']); $this->assertArrayNotHasKey('matriculation', $resultado['campos']); $this->assertArrayNotHasKey('structure_id', $resultado['campos']); } public function test_atualizacao_preserva_senha_phone_status(): void { $cpf = $this->gerarCpfTeste(13); $structureId = $this->getStructureIdTeste(); $this->service->processarLinha([ 'cpf' => $cpf, 'matricula' => '40004', 'nome' => 'TESTE PRESERVA', 'unidade' => '-', 'email' => 'preserva@example.com', ], $structureId, 2); DB::table('users')->where('cpf', $cpf)->update([ 'phoneNumber' => '21999999999', 'password' => 'hash-customizado-fake', 'isPasswordDefault' => 0, ]); $this->service->processarLinha([ 'cpf' => $cpf, 'matricula' => '99999', 'nome' => 'TESTE PRESERVA', 'unidade' => '-', 'email' => 'preserva@example.com', ], $structureId, 3); $userDepois = User::where('cpf', $cpf)->firstOrFail(); $this->assertSame('21999999999', $userDepois->getAttributes()['phoneNumber']); $this->assertSame('hash-customizado-fake', $userDepois->getAttributes()['password']); $this->assertSame(0, (int) $userDepois->isPasswordDefault); $this->assertSame('99999', $userDepois->matriculation); } public function test_atualizacao_preserva_permissoes_existentes(): void { $cpf = $this->gerarCpfTeste(14); $structureId = $this->getStructureIdTeste(); $this->service->processarLinha([ 'cpf' => $cpf, 'matricula' => '50005', 'nome' => 'TESTE PERMS', 'unidade' => '-', 'email' => 'perms@example.com', ], $structureId, 2); $user = User::where('cpf', $cpf)->firstOrFail(); Permissao::create([ 'user_id' => $user->id, 'permissao' => 'SYSADMIN', 'visualizar' => 1, 'adicionar' => 1, 'editar' => 1, 'excluir' => 1, 'status' => 'ATIVADO', ]); $this->service->processarLinha([ 'cpf' => $cpf, 'matricula' => '50006', 'nome' => 'TESTE PERMS', 'unidade' => '-', 'email' => 'perms@example.com', ], $structureId, 3); $perms = Permissao::where('user_id', $user->id)->pluck('permissao')->all(); $this->assertContains('SYSADMIN', $perms); $this->assertContains('GERENCIADOR_DE_CONTA_ANGRA', $perms); $this->assertContains('GUEST', $perms); } public function test_email_em_conflito_em_update_preserva_email_atual(): void { $cpfA = $this->gerarCpfTeste(15); $cpfB = $this->gerarCpfTeste(16); $structureId = $this->getStructureIdTeste(); $this->service->processarLinha([ 'cpf' => $cpfA, 'matricula' => '60006', 'nome' => 'USUARIO A', 'unidade' => '-', 'email' => 'usuario.a@example.com', ], $structureId, 2); $this->service->processarLinha([ 'cpf' => $cpfB, 'matricula' => '60007', 'nome' => 'USUARIO B', 'unidade' => '-', 'email' => 'usuario.b@example.com', ], $structureId, 3); $resultado = $this->service->processarLinha([ 'cpf' => $cpfB, 'matricula' => '60007', 'nome' => 'USUARIO B', 'unidade' => '-', 'email' => 'usuario.a@example.com', ], $structureId, 4); $this->assertSame(ImportacaoProdataContaAngraService::RESULTADO_SINCRONIZADO, $resultado['resultado']); $userB = User::where('cpf', $cpfB)->firstOrFail(); $this->assertSame('usuario.b@example.com', $userB->getAttributes()['email']); } public function test_email_em_conflito_em_create_marca_invalido(): void { $cpfA = $this->gerarCpfTeste(17); $cpfB = $this->gerarCpfTeste(18); $structureId = $this->getStructureIdTeste(); $this->service->processarLinha([ 'cpf' => $cpfA, 'matricula' => '70007', 'nome' => 'USUARIO A', 'unidade' => '-', 'email' => 'duplicado@example.com', ], $structureId, 2); $resultado = $this->service->processarLinha([ 'cpf' => $cpfB, 'matricula' => '70008', 'nome' => 'USUARIO B', 'unidade' => '-', 'email' => 'duplicado@example.com', ], $structureId, 3); $this->assertSame(ImportacaoProdataContaAngraService::RESULTADO_INVALIDO, $resultado['resultado']); $this->assertNull(User::where('cpf', $cpfB)->first()); $conflitos = $this->service->getConflitosEmailDetalhados(); $this->assertCount(1, $conflitos); $this->assertSame(3, $conflitos[0]['linha_csv']); $this->assertSame($cpfB, $conflitos[0]['registro_csv']['cpf_normalizado']); $this->assertSame('duplicado@example.com', $conflitos[0]['registro_csv']['email']); $this->assertSame( $cpfA, $this->service->normalizarCpf($conflitos[0]['usuario_ja_cadastrado']['cpf']) ); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.09 |
proxy
|
phpinfo
|
Settings