Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit 951a182

Browse files
committed
Merge branch 'main' into money-validator
2 parents f885a8e + dbc75db commit 951a182

File tree

7 files changed

+89
-27
lines changed

7 files changed

+89
-27
lines changed

.github/workflows/test.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Run Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v3
17+
18+
- name: Set up PHP
19+
uses: shivammathur/setup-php@v2
20+
with:
21+
php-version: 8.2
22+
extensions: mysqli
23+
24+
- name: Install Composer dependencies
25+
working-directory: ./code
26+
run: composer install --prefer-dist --no-progress
27+
28+
- name: Copy .env.example to .env
29+
working-directory: ./code
30+
run: cp .env.example .env
31+
32+
- name: Generate app key
33+
working-directory: ./code
34+
run: php artisan key:generate
35+
36+
- name: Run database migrations
37+
working-directory: ./code
38+
run: php artisan migrate
39+
40+
- name: Run tests
41+
working-directory: ./code
42+
run: vendor/bin/phpunit

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Ensure you have Docker and Docker Compose installed on your machine.
1515

1616
1. Clone the repo
1717
```sh
18-
git clone https://github.com/bumblecoder/tech-task.git
19-
cd tech-task
18+
git clone git@github.com:bumblecoder/tech-task-pay.git
19+
cd tech-task-pay
2020
cp code/.env.example code/.env
2121
```
2222
2. Build and start the Docker containers:

code/app/Services/RequestMoneyValidator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212
{
1313
public function __construct(private int $deviation)
1414
{
15-
if ($deviation < 0 || $deviation > 100) {
16-
throw new InvalidArgumentException('Deviation must be between 0 and 100.');
17-
}
1815
}
1916

2017
public function validate(Request $request, Transaction $transaction): bool
2118
{
19+
if (!filter_var(
20+
$this->deviation,
21+
FILTER_VALIDATE_INT,
22+
['options' => ['min_range' => 0, 'max_range' => 100]]
23+
)) {
24+
throw new InvalidArgumentException('Deviation must be between 0 and 100.');
25+
}
26+
2227
if ($request->getCurrency() !== $transaction->getCurrency()) {
2328
return false;
2429
}

code/config/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,5 @@
123123
'store' => env('APP_MAINTENANCE_STORE', 'database'),
124124
],
125125

126-
'transaction_deviation' => env('TRANSACTION_DEVIATION', 5),
126+
'transaction_deviation' => (int) env('TRANSACTION_DEVIATION', 5),
127127
];

code/tests/Feature/ExampleTest.php

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature;
6+
7+
use Tests\TestCase;
8+
9+
class TransactionControllerTest extends TestCase
10+
{
11+
public function test_validate_transaction_success(): void
12+
{
13+
$response = $this->getJson('/validate-transaction');
14+
15+
$response->assertStatus(200)
16+
->assertJson(['isValid' => false]);
17+
}
18+
19+
public function test_validate_transaction_invalid_deviation(): void
20+
{
21+
config(['app.transaction_deviation' => 150]);
22+
23+
$response = $this->getJson('/validate-transaction');
24+
25+
$response->assertStatus(400)
26+
->assertJson([
27+
'error' => 'Validation failed',
28+
'message' => 'Deviation must be between 0 and 100.'
29+
]);
30+
}
31+
}

code/tests/Unit/RequestMoneyValidatorTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public function test_validate_exact_amount(): void
4545
{
4646
$request = Request::create(10000, 'USD');
4747
$transaction = Transaction::create(10000, 'USD');
48-
49-
$validator = new RequestMoneyValidator(0);
48+
$validator = new RequestMoneyValidator(10);
5049

5150
$this->assertTrue($validator->validate($request, $transaction));
5251
}
@@ -66,6 +65,10 @@ public function test_negative_deviation_throws_exception(): void
6665
$this->expectException(InvalidArgumentException::class);
6766
$this->expectExceptionMessage('Deviation must be between 0 and 100.');
6867

68+
$request = Request::create(10000, 'USD');
69+
$transaction = Transaction::create(9000, 'USD');
6970
$validator = new RequestMoneyValidator(-5);
71+
72+
$validator->validate($request, $transaction);
7073
}
7174
}

0 commit comments

Comments
 (0)