Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/T-005-api-client-test #10

Merged
merged 3 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
.env
composer.lock
composer.lock
.phpunit.result.cache
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,19 @@ To begin using the project, follow these simple steps:

5. **Start Using the Project**: Once the setup is complete, you're ready to start using the project! Refer to the project documentation or codebase for specific instructions on how to interact with the functionality provided.

**Note:** This project contains unit tests aimed to validate all the mentioned functionality. To check the performance of the components, simply run the tests located in the `project_directory/tests` folder.
**Note:** This project contains unit tests aimed to validate all the mentioned functionality. To check the performance of the components, you can run the tests using PHPUnit from the project directory:
```
./vendor/bin/phpunit tests
```

**Hint:** As an example, you can run the tests using PHPUnit from the project directory:
Alternatively, you can run tests across Composer using the provided script:
```
./vendor/bin/phpunit tests/Api/ApiClientTest.php
composer test
```

By running these commands, you can ensure that your project components are functioning as expected.


## Configuration
If your project requires specific configuration settings, API keys, or environment variables, follow these steps to set them up:

Expand Down
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"
}
}
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="src/bootstrap.php">
<testsuites>
<testsuite name="Application Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>
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
Loading