-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
253 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.