File manager - Edit - /var/www/html/homologBancodetalentos/tests/Feature/TalentBank/CriticalFlowsTest.php
Back
<?php namespace Tests\Feature\TalentBank; use App\Http\Middleware\VerifyHasOnePermission; use App\Models\TalentBank\Candidate; use App\Models\TalentBank\Company; use App\Models\TalentBank\Ethnicity; use App\Models\TalentBank\Gender; use App\Models\TalentBank\Neighbourhood; use App\Models\TalentBank\Occupation; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Http; use Mockery; use Tests\TestCase; class CriticalFlowsTest extends TestCase { use DatabaseTransactions; protected function setUp(): void { parent::setUp(); putenv('AUTH_SERVICE_URL=http://auth.test'); putenv('APP_ID=siati'); $_ENV['AUTH_SERVICE_URL'] = 'http://auth.test'; $_SERVER['AUTH_SERVICE_URL'] = 'http://auth.test'; $_ENV['APP_ID'] = 'siati'; $_SERVER['APP_ID'] = 'siati'; config([ 'talent_bank.bypass_auth' => true, ]); } protected function tearDown(): void { Mockery::close(); parent::tearDown(); } public function test_candidate_login_requires_document_and_password(): void { $response = $this->postJson('/api/talent-bank/candidate/login', []); $response->assertStatus(422); $response->assertJsonValidationErrors(['document', 'password']); } public function test_company_login_requires_document_and_password(): void { $response = $this->postJson('/api/talent-bank/company/login', []); $response->assertStatus(422); $response->assertJsonValidationErrors(['document', 'password']); } public function test_candidate_register_creates_local_candidate_when_identity_service_succeeds(): void { Http::fake([ '*/api/user' => Http::response([ 'message' => 'registered', ], 200), ]); $document = $this->generateValidCpf(); $email = 'candidate.register+'.uniqid().'@example.com'; $name = 'Candidate Register Flow'; $response = $this->postJson('/api/talent-bank/candidate/register', [ 'document' => $document, 'name' => $name, 'email' => $email, 'email_confirmation' => $email, 'password' => 'secret123', 'password_confirmation' => 'secret123', ]); $response->assertOk(); $response->assertJsonStructure(['message', 'candidate']); $this->assertDatabaseHas('candidates', [ 'cpf' => $document, 'name' => $name, 'email' => $email, ]); } public function test_company_register_creates_local_company_when_identity_service_succeeds(): void { Http::fake([ '*/api/user' => Http::response([ 'message' => 'registered', ], 200), ]); $document = $this->generateValidCnpj(); $email = 'company.register+'.uniqid().'@example.com'; $name = 'Company Register Flow'; $response = $this->postJson('/api/talent-bank/company/register', [ 'document' => $document, 'name' => $name, 'email' => $email, 'email_confirmation' => $email, 'password' => 'secret123', 'password_confirmation' => 'secret123', ]); $response->assertOk(); $response->assertJsonStructure(['message', 'candidate']); $this->assertDatabaseHas('companies', [ 'cnpj' => $document, 'corporate_name' => $name, 'email' => $email, ]); } public function test_candidate_self_edit_updates_local_profile_after_identity_service_success(): void { $candidate = $this->createCandidateFixture(); $newName = 'Candidate Edited Name'; $newEmail = 'candidate.edited+'.uniqid().'@example.com'; Http::fake([ '*/api/user/self-edit' => Http::response([ 'data' => [ 'user' => [ 'document' => $candidate->cpf, 'name' => $newName, 'email' => $newEmail, ], ], ], 200), ]); $response = $this->postJson('/api/talent-bank/candidate/self-edit', [ 'name' => $newName, 'email' => $newEmail, ]); $response->assertOk(); $response->assertJsonStructure(['message', 'candidate']); $this->assertDatabaseHas('candidates', [ 'id' => $candidate->id, 'name' => $newName, 'email' => $newEmail, ]); } public function test_company_self_edit_updates_local_email_after_identity_service_success(): void { $company = $this->createCompanyFixture(); $newName = 'Company Edited Name'; $newEmail = 'company.edited+'.uniqid().'@example.com'; Http::fake([ '*/api/user/self-edit' => Http::response([ 'data' => [ 'user' => [ 'document' => $company->cnpj, 'name' => $newName, 'email' => $newEmail, ], ], ], 200), ]); $response = $this->postJson('/api/talent-bank/company/self-edit', [ 'name' => $newName, 'email' => $newEmail, ]); $response->assertOk(); $response->assertJsonStructure(['message', 'company']); $this->assertDatabaseHas('companies', [ 'id' => $company->id, 'email' => $newEmail, ]); } public function test_dashboard_endpoint_returns_expected_counters_shape(): void { $this->createCandidateFixture(); $response = $this->getJson('/api/talent-bank/dashboard'); $response->assertOk(); $response->assertJsonStructure([ 'data' => [ 'candidates', 'jobs', 'companies', 'companies_active', ], 'message', ]); } public function test_report_endpoint_returns_list_and_counters_shape(): void { $this->createCandidateFixture(); $response = $this->getJson('/api/talent-bank/report/candidate?paginate=0'); $response->assertOk(); $response->assertJsonStructure([ 'data' => [ 'list', 'counters' => [ 'total', ], ], 'message', ]); } public function test_contact_endpoint_validates_required_fields(): void { $response = $this->postJson('/api/talent-bank/contact', []); $response->assertStatus(422); $response->assertJsonStructure([ 'message', 'data' => [ 'email', 'name', 'phone', 'subject', 'message', ], ]); } public function test_permission_middleware_denies_request_without_required_permission(): void { config([ 'talent_bank.bypass_auth' => false, ]); $request = Request::create('/api/talent-bank/candidate/session', 'GET'); $request->merge([ 'user' => [ 'data' => [ 'permissions' => [ 'siati' => ['talent-bank-user-company'], ], ], ], ]); $middleware = new VerifyHasOnePermission(); $response = $middleware->handle( $request, fn () => response()->json(['ok' => true]), 'talent-bank-user-candidate' ); $this->assertInstanceOf(JsonResponse::class, $response); $this->assertSame(401, $response->getStatusCode()); } private function createCandidateFixture(): Candidate { $neighbourhood = Neighbourhood::query()->create([ 'name' => 'Centro Flow '.uniqid(), ]); $occupation = Occupation::query()->create([ 'name' => 'Analista Flow '.uniqid(), ]); $gender = Gender::query()->create([ 'name' => 'Outro', ]); $ethnicity = Ethnicity::query()->create([ 'name' => 'Branco', ]); return Candidate::query()->create([ 'cpf' => (string) random_int(10000000000, 99999999999), 'name' => 'Candidato Fluxo Critico', 'email' => 'flow+'.uniqid().'@example.com', 'neighbourhood_id' => $neighbourhood->id, 'occupation_id' => $occupation->id, 'gender_id' => $gender->id, 'ethnicity_id' => $ethnicity->id, 'active' => true, ]); } private function createCompanyFixture(): Company { $neighbourhood = Neighbourhood::query()->create([ 'name' => 'Bairro Company Flow '.uniqid(), ]); return Company::query()->create([ 'cnpj' => $this->generateValidCnpj(), 'corporate_name' => 'Company Flow', 'trading_name' => 'Company Flow Trade', 'email' => 'company.flow+'.uniqid().'@example.com', 'neighbourhood_id' => $neighbourhood->id, 'active' => true, ]); } private function generateValidCpf(): string { $digits = []; for ($i = 0; $i < 9; $i++) { $digits[$i] = random_int(0, 9); } for ($t = 9; $t < 11; $t++) { $sum = 0; for ($c = 0; $c < $t; $c++) { $sum += $digits[$c] * (($t + 1) - $c); } $digits[$t] = ((10 * $sum) % 11) % 10; } return implode('', $digits); } private function generateValidCnpj(): string { $base = []; for ($i = 0; $i < 12; $i++) { $base[$i] = random_int(0, 9); } $weights1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]; $sum1 = 0; for ($i = 0; $i < 12; $i++) { $sum1 += $base[$i] * $weights1[$i]; } $rest1 = $sum1 % 11; $digit1 = $rest1 < 2 ? 0 : 11 - $rest1; $base[] = $digit1; $weights2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]; $sum2 = 0; for ($i = 0; $i < 13; $i++) { $sum2 += $base[$i] * $weights2[$i]; } $rest2 = $sum2 % 11; $digit2 = $rest2 < 2 ? 0 : 11 - $rest2; $base[] = $digit2; return implode('', $base); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings