From fb38f0e510aa636f651b4827b529f869c35240b5 Mon Sep 17 00:00:00 2001 From: Vinicius Carrocine Date: Mon, 24 Jun 2024 15:11:39 -0300 Subject: [PATCH] Add Automated Feature Tests --- config/routes.php | 2 +- test/BaseTestCase.php | 61 +++++++++++++++++ test/Cases/ExampleTest.php | 27 -------- test/Feature/ProductClient.php | 72 ++++++++++++++++++++ test/Feature/ProductTest.php | 119 +++++++++++++++++++++++++++++++++ test/HttpTestCase.php | 45 ------------- 6 files changed, 253 insertions(+), 73 deletions(-) create mode 100644 test/BaseTestCase.php delete mode 100644 test/Cases/ExampleTest.php create mode 100644 test/Feature/ProductClient.php create mode 100644 test/Feature/ProductTest.php delete mode 100644 test/HttpTestCase.php diff --git a/config/routes.php b/config/routes.php index fed0902..bc8f218 100644 --- a/config/routes.php +++ b/config/routes.php @@ -16,7 +16,7 @@ Router::addGroup('/product', function () { Router::addRoute(['POST'], '/Create', 'App\Controller\ProductController@create'); - Router::addRoute(['GET'], '/', 'App\Controller\ProductController@find'); + Router::addRoute(['POST'], '/', 'App\Controller\ProductController@find'); Router::addRoute(['GET'], '/List', 'App\Controller\ProductController@list'); Router::addRoute(['POST'], '/Delete', 'App\Controller\ProductController@delete'); Router::addRoute(['POST'], '/Update', 'App\Controller\ProductController@update'); diff --git a/test/BaseTestCase.php b/test/BaseTestCase.php new file mode 100644 index 0000000..130d322 --- /dev/null +++ b/test/BaseTestCase.php @@ -0,0 +1,61 @@ +get(ConfigInterface::class); + $config->set(StdoutLoggerInterface::class . '.log_level', [ + LogLevel::ALERT, + LogLevel::CRITICAL, + LogLevel::EMERGENCY, + LogLevel::ERROR, + LogLevel::INFO, + LogLevel::NOTICE, + LogLevel::WARNING + ]); + } + + protected function tearDown(): void + { + $container = ApplicationContext::getContainer(); + $config = $container->get(ConfigInterface::class); + $config->set(StdoutLoggerInterface::class . '.log_level', [ + LogLevel::ALERT, + LogLevel::CRITICAL, + LogLevel::DEBUG, + LogLevel::EMERGENCY, + LogLevel::ERROR, + LogLevel::INFO, + LogLevel::NOTICE, + LogLevel::WARNING + ]); + parent::tearDown(); + } + + protected function createTestProduct(): int + { + $product = $this->createEntity(Product::class, [ + 'name' => 'Product Test', + 'description' => 'Product Test Description' + ]); + return $product->id; + } + + protected function createEntity(string $class, array $attributes) + { + $entity = new $class($attributes); + $entity->save(); + return $entity; + } +} diff --git a/test/Cases/ExampleTest.php b/test/Cases/ExampleTest.php deleted file mode 100644 index 789ff67..0000000 --- a/test/Cases/ExampleTest.php +++ /dev/null @@ -1,27 +0,0 @@ -get('/')->assertOk()->assertSee('Hyperf'); - } -} diff --git a/test/Feature/ProductClient.php b/test/Feature/ProductClient.php new file mode 100644 index 0000000..fff10a9 --- /dev/null +++ b/test/Feature/ProductClient.php @@ -0,0 +1,72 @@ +client = make(Client::class); + } + + public function createRequest(array $attributes): ResponseInterface + { + $uri = '/product/Create'; + $response = $this->client->request('POST', $uri, [ + 'json' => $attributes, + 'headers' => ['Content-Type' => 'application/json'], + ]); + + return $response; + } + + public function listRequest($attributes): ResponseInterface + { + $uri = '/product/List'; + $response = $this->client->request('GET', $uri, [ + 'json' => $attributes, + 'headers' => ['Content-Type' => 'application/json'], + ]); + + return $response; + } + + public function getRequest($attributes): ResponseInterface + { + $uri = '/product/'; + $response = $this->client->request('POST', $uri, [ + 'json' => $attributes, + 'headers' => ['Content-Type' => 'application/json'], + ]); + + return $response; + } + + public function deleteRequest($attributes): ResponseInterface + { + $uri = '/product/Delete'; + $response = $this->client->request('POST', $uri, [ + 'json' => $attributes, + 'headers' => ['Content-Type' => 'application/json'], + ]); + + return $response; + } + + public function updateRequest($attributes): ResponseInterface + { + $uri = '/product/Update'; + $response = $this->client->request('POST', $uri, [ + 'json' => $attributes, + 'headers' => ['Content-Type' => 'application/json'], + ]); + + return $response; + } +} diff --git a/test/Feature/ProductTest.php b/test/Feature/ProductTest.php new file mode 100644 index 0000000..fb9f977 --- /dev/null +++ b/test/Feature/ProductTest.php @@ -0,0 +1,119 @@ +productClient = new ProductClient(); + } + + public function testProductCreateShouldSucceed() + { + //Arrange + $attributes = [ + 'name' => 'Product Test', + 'description' => 'Product Test Description' + ]; + + //Act + $requestResponse = $this->productClient->createRequest($attributes); + $decodedResponse = json_decode($requestResponse->getBody()->getContents()); + + //Asserts + $this->assertInstanceOf(ResponseInterface::class, $requestResponse); + $this->assertEquals(true, $decodedResponse->success); + $this->assertEquals('Product Test', $decodedResponse->data->name); + $this->assertEquals('Product Test Description', $decodedResponse->data->description); + } + + public function testProductListShouldSucceed() + { + //Arrange + $productId = $this->createTestProduct(); + $attributes = []; + + //Act + $requestResponse = $this->productClient->listRequest($attributes); + $decodedResponse = json_decode($requestResponse->getBody()->getContents()); + + //Asserts + $this->assertEquals(true, is_array($decodedResponse->data)); + $lastKey = array_key_last($decodedResponse->data); + $this->assertEquals($productId, $decodedResponse->data[$lastKey]->id); + } + + public function testProductGetShouldSucceed() + { + //Arrange + $productId = $this->createTestProduct(); + $attributes = [ + 'id' => $productId + ]; + + //Act + $requestResponse = $this->productClient->getRequest($attributes); + $decodedResponse = json_decode($requestResponse->getBody()->getContents()); + + //Asserts + $this->assertEquals($productId, $decodedResponse->data->id); + } + + public function testProductDeleteShouldSucceed() + { + //Arrange + $productId = $this->createTestProduct(); + $attributes = [ + 'id' => $productId + ]; + + //Act + $requestResponse = $this->productClient->deleteRequest($attributes); + $decodedResponse = json_decode($requestResponse->getBody()->getContents()); + + //Asserts + $this->assertEquals(true, $decodedResponse->success); + } + + public function testProductUpdateShouldSucceed() + { + //Arrange + $productId = $this->createTestProduct(); + $attributes = [ + 'id' => $productId, + 'name' => 'Updated Name', + 'description' => 'Updated Product Description' + ]; + + //Act + $requestResponse = $this->productClient->updateRequest($attributes); + $decodedResponse = json_decode($requestResponse->getBody()->getContents()); + + //Asserts + $this->assertInstanceOf(ResponseInterface::class, $requestResponse); + $this->assertEquals('Updated Name', $decodedResponse->data->name); + $this->assertEquals('Updated Product Description', $decodedResponse->data->description); + } +} diff --git a/test/HttpTestCase.php b/test/HttpTestCase.php deleted file mode 100644 index da07e66..0000000 --- a/test/HttpTestCase.php +++ /dev/null @@ -1,45 +0,0 @@ -client = make(Client::class); - } - - public function __call($name, $arguments) - { - return $this->client->{$name}(...$arguments); - } -}