diff --git a/codeception.yml b/codeception.yml index e303d34e1..2d4f57130 100644 --- a/codeception.yml +++ b/codeception.yml @@ -17,4 +17,4 @@ paths: settings: shuffle: false - lint: true \ No newline at end of file + lint: true diff --git a/composer.json b/composer.json index ce7dfb195..530ce31bd 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Desafio para candidatos a back-end.", "type": "project", "require": { - "php": ">= 7.4" + "php": ">= 8.2" }, "require-dev": { "squizlabs/php_codesniffer": "^3.4", diff --git a/src/CurrencyConverter.php b/src/CurrencyConverter.php new file mode 100644 index 000000000..90c74b864 --- /dev/null +++ b/src/CurrencyConverter.php @@ -0,0 +1,44 @@ + (float) $amount, + 'fromCurrency' => strtoupper($fromCurrency), + 'toCurrency' => strtoupper($toCurrency), + 'rate' => (float) $rate, + ]; + } +} diff --git a/src/index.php b/src/index.php index 92841bc87..9f54c420f 100644 --- a/src/index.php +++ b/src/index.php @@ -2,17 +2,49 @@ /** * Back-end Challenge. * - * PHP version 7.4 - * - * Este será o arquivo chamado na execução dos testes automátizados. + * PHP version 8.2 * * @category Challenge * @package Back-end - * @author Seu Nome + * @author Guilherme Pontes * @license http://opensource.org/licenses/MIT MIT * @link https://github.com/apiki/back-end-challenge */ declare(strict_types=1); +namespace App; + require __DIR__ . '/../vendor/autoload.php'; +$router = new Router(); +$currencyConverter = new CurrencyConverter(); + +try { + $requestData = $router->route($_SERVER['REQUEST_URI']); + + // Validação adicional pode ser adicionada aqui, se necessário + if (!isset($requestData['amount'], $requestData['fromCurrency'], $requestData['toCurrency'], $requestData['rate'])) { + throw new \InvalidArgumentException("Missing required parameters."); + } + + $convertedAmount = $currencyConverter->convert( + $requestData['amount'], + $requestData['fromCurrency'], + $requestData['toCurrency'], + $requestData['rate'] + ); + + $response = [ + 'valorConvertido' => $convertedAmount, + 'simboloMoeda' => + $requestData['toCurrency'] === CurrencyConverter::CURRENCY_USD + ? '$' + : 'R$', + ]; + + header('Content-Type: application/json'); + echo json_encode($response); +} catch (\InvalidArgumentException $e) { + header("HTTP/1.1 400 Bad Request"); + echo json_encode(['error' => $e->getMessage()]); +} diff --git a/tests/ApiCest.php b/tests/ApiCest.php index e92352118..ba3783c36 100644 --- a/tests/ApiCest.php +++ b/tests/ApiCest.php @@ -1,6 +1,8 @@ sendGET('/'); @@ -8,6 +10,7 @@ public function tryApiWithoutValue(ApiTester $I) $I->seeResponseIsJson(); } + // Testa a API sem fornecer a moeda de origem public function tryApiWithoutFrom(ApiTester $I) { $I->sendGET('/10'); @@ -15,6 +18,7 @@ public function tryApiWithoutFrom(ApiTester $I) $I->seeResponseIsJson(); } + // Testa a API sem fornecer a moeda de destino public function tryApiWithoutTo(ApiTester $I) { $I->sendGET('/10/EUR'); @@ -22,6 +26,7 @@ public function tryApiWithoutTo(ApiTester $I) $I->seeResponseIsJson(); } + // Testa a API sem fornecer a taxa de conversão public function tryApiWithoutRate(ApiTester $I) { $I->sendGET('/10/EUR/USD'); @@ -29,6 +34,7 @@ public function tryApiWithoutRate(ApiTester $I) $I->seeResponseIsJson(); } + // Testa a API com valor inválido public function tryApiInvalidValue(ApiTester $I) { $I->sendGET('/a/EUR/USD/0.5'); @@ -36,6 +42,7 @@ public function tryApiInvalidValue(ApiTester $I) $I->seeResponseIsJson(); } + // Testa a API com valor negativo public function tryApiNegativeValue(ApiTester $I) { $I->sendGET('/-10/EUR/USD/0.5'); @@ -43,6 +50,7 @@ public function tryApiNegativeValue(ApiTester $I) $I->seeResponseIsJson(); } + // Testa a API com moeda de origem inválida public function tryApiInvalidFrom(ApiTester $I) { $I->sendGET('/10/eur/USD/0.5'); @@ -50,6 +58,7 @@ public function tryApiInvalidFrom(ApiTester $I) $I->seeResponseIsJson(); } + // Testa a API com moeda de destino inválida public function tryApiInvalidTo(ApiTester $I) { $I->sendGET('/10/EUR/usd/0.5'); @@ -57,6 +66,7 @@ public function tryApiInvalidTo(ApiTester $I) $I->seeResponseIsJson(); } + // Testa a API com taxa de conversão inválida public function tryApiInvalidRate(ApiTester $I) { $I->sendGET('/10/EUR/USD/a'); @@ -64,6 +74,7 @@ public function tryApiInvalidRate(ApiTester $I) $I->seeResponseIsJson(); } + // Testa a API com taxa de conversão negativa public function tryApiNegativeRate(ApiTester $I) { $I->sendGET('/10/EUR/USD/-0.5'); @@ -71,6 +82,7 @@ public function tryApiNegativeRate(ApiTester $I) $I->seeResponseIsJson(); } + // Testa a conversão de BRL para USD public function tryApiBrlToUsd(ApiTester $I) { $I->sendGET('/7.8/BRL/USD/0.5'); @@ -82,6 +94,7 @@ public function tryApiBrlToUsd(ApiTester $I) ]); } + // Testa a conversão de USD para BRL public function tryApiUsdToBrl(ApiTester $I) { $I->sendGET('/7/USD/BRL/0.5'); @@ -93,6 +106,7 @@ public function tryApiUsdToBrl(ApiTester $I) ]); } + // Testa a conversão de BRL para EUR public function tryApiBrlToEur(ApiTester $I) { $I->sendGET('/7/BRL/EUR/5'); @@ -104,6 +118,7 @@ public function tryApiBrlToEur(ApiTester $I) ]); } + // Testa a conversão de EUR para BRL public function tryApiEurToBrl(ApiTester $I) { $I->sendGET('/7/EUR/BRL/5'); @@ -114,4 +129,4 @@ public function tryApiEurToBrl(ApiTester $I) 'simboloMoeda' => 'R$', ]); } -} \ No newline at end of file +}