Skip to content

Commit

Permalink
Add Automated Feature Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VNCHub committed Jun 24, 2024
1 parent 9a3e3ae commit fb38f0e
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 73 deletions.
2 changes: 1 addition & 1 deletion config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
61 changes: 61 additions & 0 deletions test/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace HyperfTest;
use App\Model\Product;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;

abstract class BaseTestCase extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$container = ApplicationContext::getContainer();
$config = $container->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;
}
}
27 changes: 0 additions & 27 deletions test/Cases/ExampleTest.php

This file was deleted.

72 changes: 72 additions & 0 deletions test/Feature/ProductClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace HyperfTest\Feature;

use Hyperf\Testing\Client;
use Psr\Http\Message\ResponseInterface;
use function Hyperf\Support\make;

class ProductClient
{
private $client;

public function __construct()
{
$this->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;
}
}
119 changes: 119 additions & 0 deletions test/Feature/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace HyperfTest\Feature;

use HyperfTest\BaseTestCase;
use HyperfTest\Feature\ProductClient;
use Psr\Http\Message\ResponseInterface;

/**
* @internal
* @coversNothing
*/
class ProductTest extends BaseTestCase
{
private ProductClient $productClient;

public function __construct($name = null, array $data = [], $dataName ='')
{
parent::__construct($name, $data, $dataName);
$this->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);
}
}
45 changes: 0 additions & 45 deletions test/HttpTestCase.php

This file was deleted.

0 comments on commit fb38f0e

Please sign in to comment.