Skip to content

Commit

Permalink
T-005: Implement initial API Client Test functionality for WSLogin
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-ghenov committed Mar 2, 2024
1 parent b7ab88e commit 88099f9
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 29 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@
},
"require-dev": {
"phpunit/phpunit": "^11.0"
},
"scripts": {
"test-api-client": "vendor/bin/phpunit tests/Api/ApiClientTest.php",
"test-service-purchase": "vendor/bin/phpunit tests/Service/PurchaseServiceTest.php",
"test-service-status_check": "vendor/bin/phpunit tests/Service/StatusCheckServiceTest.php"
}
}
11 changes: 7 additions & 4 deletions src/Api/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ApiClient {
*
* The Guzzle HTTP client.
*/
private $client;
private Client $client;

/**
* @var string
Expand Down Expand Up @@ -48,6 +48,7 @@ class ApiClient {
public function __construct() {
$this->client = new Client([
'base_uri' => $_ENV['API_BASE_URL'],
'timeout' => 30,
]);
$this->apiUser = $_ENV['API_USER'];
$this->apiPassword = $_ENV['API_PASSWORD'];
Expand All @@ -67,18 +68,20 @@ public function __construct() {
* @return mixed
* The response from the API.
*/
public function sendRequest($method, $endpoint, array $data = []): mixed {
public function sendRequest(
string $method, string $endpoint, array $data = []
): mixed {
$timestamp = gmdate('c');
$authKey = $this->generateAuthKey($timestamp);
$hashedPassword = $this->hashPassword();

// Add authentication parameters to the request data.
$data = array_merge($data, [
$data = array_merge([
'DTime' => $timestamp,
'AuthKey' => $authKey,
'User' => $this->apiUser,
'Password' => $hashedPassword,
]);
], $data);

try {
$response = $this->client->request($method, $endpoint, [
Expand Down
16 changes: 9 additions & 7 deletions src/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,44 @@ class Product {
*
* The product ID.
*/
public $id;
public int $id;

/**
* @var string
*
* The product name.
*/
public $name;
public string $name;

/**
* @var string
*
* The product description.
*/
public $description;
public string $description;

/**
* @var float
*
* The product amount.
*/
public $amount;
public float $amount;

/**
* Product constructor.
*
* @param int $id
* @param int $id
* The product ID.
* @param string $name
* The product name.
* @param string $description
* The product description.
* @param float $amount
* @param float $amount
* The product amount.
*/
public function __construct($id, $name, $description, $amount) {
public function __construct(
int $id, string $name, string $description, float $amount
) {
$this->id = $id;
$this->name = $name;
$this->description = $description;
Expand Down
6 changes: 3 additions & 3 deletions src/Service/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ProductService {
*
* The API client.
*/
private $apiClient;
private ApiClient $apiClient;

/**
* ProductService constructor.
Expand All @@ -36,7 +36,7 @@ public function __construct(ApiClient $apiClient) {
* @return array
* The products.
*/
public function getAllProducts() {
public function getAllProducts(): array {
$endpoint = 'WSGetTopUpProducts';

return $this->apiClient->sendRequest('POST', $endpoint);
Expand All @@ -51,7 +51,7 @@ public function getAllProducts() {
* @return array
* The product.
*/
public function getProductById($productID) {
public function getProductById(int $productID): mixed {
$endpoint = "WSGetSingleTopUpProduct";

return $this->apiClient->sendRequest(
Expand Down
2 changes: 1 addition & 1 deletion src/Service/PurchaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PurchaseService {
*
* The API client.
*/
private $apiClient;
private ApiClient $apiClient;

/**
* PurchaseService constructor.
Expand Down
70 changes: 56 additions & 14 deletions tests/Api/ApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,74 @@ class ApiClientTest extends TestCase {
*
* The API client.
*/
private $apiClient;
private ApiClient $apiClient;

/**
* Test a successful login request.
*/
public function testWSLoginSuccess() {
$response = $this->apiClient->sendRequest('POST', 'WSLogin');
try {
$response = $this->apiClient->sendRequest('POST', 'WSLogin');

// Assert the response indicates a successful login
$this->assertArrayHasKey('AccessCode', $response);
$this->assertEquals(0, $response['AccessCode']);
// Use PHPUnit assertions to check for successful login
// Display the response for debugging purposes
// (usually done during development only)
echo "Response: ";
var_dump($response);

// Assert the response indicates a successful login
$this->assertArrayHasKey(
'AccessCode', $response, "Response does not contain AccessCode"
);
$this->assertEquals(
0, $response['AccessCode'],
"AccessCode is not 0, indicating login failure"
);
}
catch (\Exception $e) {
// Catch and display the error for debugging purposes
// In production or CI environments, it might be better to log this error
// or handle it accordingly.
echo "Error during WSLogin request: ".$e->getMessage();
// Fail the test if an exception is caught
$this->fail(
"WSLogin request failed with an exception: ".$e->getMessage()
);
}
}

/**
* Test a failed login request.
*/
public function testWSLoginFailure() {
// Use intentionally incorrect credentials to test failure response
$response = $this->apiClient->sendRequest('POST', 'WSLogin', [
'User' => 'incorrectUser',
'Password' => 'incorrectPassword',
]);

// Assert the response indicates a failed login
$this->assertArrayHasKey('AccessCode', $response);
$this->assertNotEquals(0, $response['AccessCode']);
try {
// Use intentionally incorrect credentials to test failure response.
$response = $this->apiClient->sendRequest('POST', 'WSLogin', [
'User' => 'incorrectUser',
'Password' => 'incorrectPassword',
]);

// Debugging output (use sparingly)
echo "Response: ";
var_dump($response);

// Assert the response indicates a failed login.
$this->assertArrayHasKey(
'AccessCode', $response, "Response does not contain AccessCode"
);
$this->assertNotEquals(
0, $response['AccessCode'],
"AccessCode is 0, indicating unexpected success"
);
}
catch (\Exception $e) {
// If there's an exception, catch and display it for debugging purposes.
echo "Error during WSLogin failure test: ".$e->getMessage();
// Optionally fail the test to indicate an unexpected error occurred.
$this->fail(
"WSLogin failure test failed with an exception: ".$e->getMessage()
);
}
}

/**
Expand Down

0 comments on commit 88099f9

Please sign in to comment.