Skip to content

Commit

Permalink
Merge pull request #2 from Gabrielcefetzada/user-test
Browse files Browse the repository at this point in the history
User test
  • Loading branch information
Gabrielcefetzada authored Jun 4, 2024
2 parents 9887ddd + 0f175cb commit 901b066
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jobs:
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Feature tests only)
- name: Execute tests (Feature tests and unit)
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: php artisan test --testsuite=Feature
run: php artisan test
23 changes: 16 additions & 7 deletions app/Http/Requests/UserRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@

namespace App\Http\Requests;

use App\Rules\CpfRule;
use Illuminate\Foundation\Http\FormRequest;

class UserRegister extends FormRequest
{
protected array $commonRules = [
'email' => 'required|email',
'password' => "required|regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\x])(?=.*[!$#%]).*$/|min:6",
'cpf' => 'required|regex:/^\d{3}.?\d{3}.?\d{3}\-?\d{2}$/',
'name' => 'required|regex:/^\S+\s+\S+/'
];
protected array $commonRules;

public function __construct()
{
$this->commonRules = [
'email' => 'required|email',
'password' => [
'required',
'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\x])(?=.*[!$#%]).*$/',
'min:6'
],
'cpf' => ['required', 'string', new CpfRule()],
'name' => 'required|regex:/^\S+\s+\S+/'
];
}

protected array $commonMessages = [
'password.regex' => 'A senha deve cumprir os requisitos: Ao menos uma letra minúscula, uma maiúscula, um número e um caracter especial',
'password.min' => 'A senha deve ter pelo menos seis caracteres',
'required' => 'O campo :attribute é obrigatório',
'cpf.regex' => 'O cpf é inválido',
'name.regex' => 'Por favor, insira um nome completo'
];
/**
Expand Down
63 changes: 63 additions & 0 deletions app/Rules/CpfRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class CpfRule implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$cpf = $this->extractNumbers($value);

if (!$this->hasValidLength($cpf)) {
$fail('CPF inválido');
return;
}

if ($this->hasRepeatedDigits($cpf)) {
$fail('CPF inválido');
return;
}

if (!$this->hasValidCheckDigits($cpf)) {
$fail('CPF inválido');
}
}

private function extractNumbers(string $value): string
{
return preg_replace('/[^0-9]/is', '', $value);
}

private function hasValidLength(string $cpf): bool
{
return strlen($cpf) == 11;
}

private function hasRepeatedDigits(string $cpf): bool
{
return preg_match('/(\d)\1{10}/', $cpf);
}

private function hasValidCheckDigits(string $cpf): bool
{
for ($t = 9; $t < 11; $t++) {
$d = 0;
for ($c = 0; $c < $t; $c++) {
$d += $cpf[$c] * (($t + 1) - $c);
}
$d = ((10 * $d) % 11) % 10;
if ($cpf[$c] != $d) {
return false;
}
}
return true;
}
}
30 changes: 15 additions & 15 deletions tests/Feature/TransferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ class TransferenceTest extends TestCase
protected AntiFraudInterface $antiFraud;
protected TransferenceService $service;

protected function setUp(): void
{
parent::setUp();
Queue::fake();

$this->createUsers();
$this->createWallets();

$this->antiFraud = \Mockery::mock(AntiFraudInterface::class);
$this->antiFraud->shouldReceive('authorize')->andReturn(true);

Notification::fake();

$this->service = new TransferenceService($this->payerWallet, $this->antiFraud);
}

private function createUsers(): void
{
Expand All @@ -52,21 +67,6 @@ private function createWallets(): void
'balance' => 5000,
]);
}
protected function setUp(): void
{
parent::setUp();
Queue::fake();

$this->createUsers();
$this->createWallets();

$this->antiFraud = \Mockery::mock(AntiFraudInterface::class);
$this->antiFraud->shouldReceive('authorize')->andReturn(true);

Notification::fake();

$this->service = new TransferenceService($this->payerWallet, $this->antiFraud);
}

public function test_successful_transfer()
{
Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tests;

namespace Tests\Unit;

use Illuminate\Foundation\Testing\TestCase;

class UserTest extends TestCase
{
public function test_register_with_invalid_cpf()
{
$response = $this->postJson('/register-common-user', [
'name' => 'John Doe',
'cpf' => '12345678900',
'email' => '[email protected]',
'password' => 'Mpassword123!',
]);

$response->assertStatus(422);
}
}

0 comments on commit 901b066

Please sign in to comment.