Skip to content

Commit

Permalink
feat(php-sdk): add customer tests (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianMiklaszewskiBldr authored Dec 29, 2023
1 parent 87237a6 commit 99120b4
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ jobs:
SUB_DOMAIN: ${{ secrets.SUB_DOMAIN }}
DOMAIN: ${{ secrets.DOMAIN }}


- name: Cleanup Site
if: always()
run: ./cleanupSite.sh
env:
BASIC_AUTH_USERNAME: ${{ secrets.BASIC_AUTH_USERNAME }}
Expand Down
139 changes: 128 additions & 11 deletions tests/Controllers/CustomersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use AdvancedBillingLib\Tests\TestFactory\TestCustomerFactory;
use AdvancedBillingLib\Tests\TestFactory\TestCustomerRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestCustomerResponseFactory;
use AdvancedBillingLib\Tests\TestFactory\TestPaymentProfileRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestProductFamilyRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestProductRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestSubscriptionRequestFactory;

final class CustomersControllerTest extends TestCase
{
Expand All @@ -21,7 +25,7 @@ public function test_CreateCustomer_ShouldCreateCustomer_WhenAllDataAreUnique():
{
$response = $this->client
->getCustomersController()
->createCustomer($this->testData->createRequest());
->createCustomer($this->testData->getCreateCustomerRequest());
$customer = $response->getCustomer();

$this->assertions->assertExpectedCustomerCreated(
Expand All @@ -41,11 +45,7 @@ public function test_CreateCustomer_ShouldCreateCustomer_WhenAllDataAreUnique():
*/
public function test_ReadCustomer_ShouldReturnCustomer_WhenCustomerExists(): void
{
$request = $this->testData->createRequest();
$customer = $this->client
->getCustomersController()
->createCustomer($request)
->getCustomer();
$customer = $this->testData->loadCustomer();

$response = $this->client
->getCustomersController()
Expand All @@ -72,10 +72,7 @@ public function test_ReadCustomer_ShouldThrow404StatusCodeException_WhenCustomer
*/
public function test_ListCustomers_ShouldReturnListWithCreatedCustomer_WhenCustomerExists(): void
{
$customer = $this->client
->getCustomersController()
->createCustomer($this->testData->createRequest())
->getCustomer();
$customer = $this->testData->loadCustomer();

$response = $this->client
->getCustomersController()
Expand All @@ -86,14 +83,134 @@ public function test_ListCustomers_ShouldReturnListWithCreatedCustomer_WhenCusto
$this->cleaner->removeCustomerById($customer->getId());
}

/**
* @covers \AdvancedBillingLib\Controllers\CustomersController::updateCustomer
*/
public function test_UpdateCustomer_ShouldUpdateCustomer_WhenCustomerFoundAndDataAreCorrect(): void
{
$customer = $this->testData->loadCustomer();

$updatedCustomer = $this->client
->getCustomersController()
->updateCustomer(
$customer->getId(),
$this->testData->getUpdateCustomerRequest()
)
->getCustomer();

$this->assertions->assertCustomerWasUpdated($customer, $updatedCustomer);
$this->cleaner->removeCustomerById($customer->getId());
}

/**
* @covers \AdvancedBillingLib\Controllers\CustomersController::updateCustomer
*/
public function test_UpdateCustomer_ShouldThrowExceptionWith404StatusCode_WhenCustomerNotFound(): void
{
$this->assertions->assertCustomerNotFound();
$this->client
->getCustomersController()
->updateCustomer(
$this->testData->getNotExistingCustomerId(),
$this->testData->getUpdateCustomerRequest()
)
->getCustomer();
}

/**
* @covers \AdvancedBillingLib\Controllers\CustomersController::deleteCustomer
*/
public function test_DeleteCustomer_ShouldDeleteCustomer_WhenCustomerExists(): void
{
$customer = $this->testData->loadCustomer();

$this->client->getCustomersController()->deleteCustomer($customer->getId());

$this->assertions->assertCustomerWasDeleted($this->client);
}

/**
* @covers \AdvancedBillingLib\Controllers\CustomersController::deleteCustomer
*/
public function test_DeleteCustomer_ShouldThrowExceptionWith404StatusCode_WhenCustomerNotFound(): void
{
$this->assertions->assertCustomerNotFound();
$this->client->getCustomersController()->deleteCustomer($this->testData->getNotExistingCustomerId());
}

/**
* @covers \AdvancedBillingLib\Controllers\CustomersController::readCustomerByReference
*/
public function test_ReadCustomerByReference_ShouldReturnCustomer_WhenFoundByReference(): void
{
$customer = $this->testData->loadCustomer();

$foundCustomer = $this->client
->getCustomersController()
->readCustomerByReference($customer->getReference())
->getCustomer();

$this->assertions->assertExpectedCustomerWasReturned($customer, $foundCustomer);

$this->cleaner->removeCustomerById($customer->getId());
}

/**
* @covers \AdvancedBillingLib\Controllers\CustomersController::readCustomerByReference
*/
public function test_ReadCustomerByReference_ShouldThrowExceptionWith404StatusCode_WhenCustomerNotFound(): void
{
$this->assertions->assertCustomerNotFound();
$this->client
->getCustomersController()
->readCustomerByReference($this->testData->getNotExistingCustomerReference())
->getCustomer();
}

/**
* @covers \AdvancedBillingLib\Controllers\CustomersController::listCustomerSubscriptions
*/
public function test_ListCustomerSubscriptions_ShouldReturnEmptyList_WhenCustomerDoesNotHaveAnySubscription(): void
{
$customer = $this->testData->loadCustomer();

$subscriptions = $this->client->getCustomersController()->listCustomerSubscriptions($customer->getId());

$this->assertions->assertCustomerSubscriptionsNotFound($subscriptions);

$this->cleaner->removeCustomerById($customer->getId());
}

/**
* @covers \AdvancedBillingLib\Controllers\CustomersController::listCustomerSubscriptions
*/
public function test_ListCustomerSubscriptions_ShouldSubscriptionsList_WhenCustomerHasOneSubscription(): void
{
$this->markTestSkipped('Subscription updatedAt is modified on read. Test will stay skipped until problem will be resolved.');
$customer = $this->testData->loadCustomer();
$subscription = $this->testData->loadSubscription($customer->getId());

$subscriptions = $this->client->getCustomersController()->listCustomerSubscriptions($customer->getId());

$this->assertions->assertCustomerSubscriptionsFound($subscriptions, $subscription);

$this->cleaner->removeSubscriptionById($subscription->getId(), $customer->getId());
$this->cleaner->removeCustomerById($customer->getId());
}

protected function setUp(): void
{
parent::setUp();

$this->testData = new CustomersControllerTestData(
new TestCustomerRequestFactory(),
new TestCustomerFactory(),
new TestCustomerResponseFactory()
new TestCustomerResponseFactory(),
$this->client,
new TestSubscriptionRequestFactory(),
new TestProductFamilyRequestFactory(),
new TestProductRequestFactory(),
new TestPaymentProfileRequestFactory()
);
$this->assertions = new CustomersControllerTestAssertions($this);
}
Expand Down
40 changes: 39 additions & 1 deletion tests/Controllers/CustomersControllerTestAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

namespace AdvancedBillingLib\Tests\Controllers;

use AdvancedBillingLib\AdvancedBillingClient;
use AdvancedBillingLib\Exceptions\ApiException;
use AdvancedBillingLib\Exceptions\CustomerErrorResponseException;
use AdvancedBillingLib\Models\Customer;
use AdvancedBillingLib\Models\CustomerResponse;
use AdvancedBillingLib\Models\Subscription;
use AdvancedBillingLib\Models\SubscriptionResponse;
use AdvancedBillingLib\Tests\TestData\CustomerTestData;
use AdvancedBillingLib\Tests\TestStatusCode;

final class CustomersControllerTestAssertions
{
private const EXPECTED_CUSTOMER_SUBSCRIPTIONS_NUMBER = 1;

public function __construct(private CustomersControllerTest $testCase)
{
}
Expand Down Expand Up @@ -43,4 +48,37 @@ public function assertCustomersReturned(array $expectedCustomersList, array $cus
{
$this->testCase::assertEquals($expectedCustomersList, $customersList);
}

public function assertCustomerWasUpdated(Customer $customer, Customer $updatedCustomer): void
{
$this->testCase::assertNotEquals($customer, $updatedCustomer);
$this->testCase::assertEquals(CustomerTestData::UPDATED_FIRST_NAME, $updatedCustomer->getFirstName());
$this->testCase::assertEquals(CustomerTestData::UPDATED_LAST_NAME, $updatedCustomer->getLastName());
}

public function assertCustomerWasDeleted(AdvancedBillingClient $client): void
{
$customers = $client->getCustomersController()->listCustomers([]);

$this->testCase::assertEmpty($customers);
}

/**
* @param array<int, SubscriptionResponse> $subscriptions
*/
public function assertCustomerSubscriptionsNotFound(array $subscriptions): void
{
$this->testCase::assertEmpty($subscriptions);
}

/**
* @param array<int, SubscriptionResponse> $subscriptions
*/
public function assertCustomerSubscriptionsFound(array $subscriptions, Subscription $expectedSubscription): void
{
$this->testCase::assertCount(self::EXPECTED_CUSTOMER_SUBSCRIPTIONS_NUMBER, $subscriptions);

$subscription = $subscriptions[0];
$this->testCase::assertEquals($expectedSubscription, $subscription->getSubscription());
}
}
76 changes: 69 additions & 7 deletions tests/Controllers/CustomersControllerTestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,46 @@

namespace AdvancedBillingLib\Tests\Controllers;

use AdvancedBillingLib\AdvancedBillingClient;
use AdvancedBillingLib\Models\CreateCustomerRequest;
use AdvancedBillingLib\Models\Customer;
use AdvancedBillingLib\Models\CustomerResponse;
use AdvancedBillingLib\Models\Subscription;
use AdvancedBillingLib\Models\UpdateCustomerRequest;
use AdvancedBillingLib\Tests\TestData\CustomerTestData;
use AdvancedBillingLib\Tests\TestData\ProductFamilyTestData;
use AdvancedBillingLib\Tests\TestData\ProductTestData;
use AdvancedBillingLib\Tests\TestFactory\TestCustomerFactory;
use AdvancedBillingLib\Tests\TestFactory\TestCustomerRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestCustomerResponseFactory;
use AdvancedBillingLib\Tests\TestFactory\TestPaymentProfileRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestProductFamilyRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestProductRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestSubscriptionRequestFactory;

final class CustomersControllerTestData
{
public function __construct(
private TestCustomerRequestFactory $customerRequestFactory,
private TestCustomerFactory $customerFactory,
private TestCustomerResponseFactory $customerResponseFactory
private TestCustomerResponseFactory $customerResponseFactory,
private AdvancedBillingClient $client,
private TestSubscriptionRequestFactory $subscriptionRequestFactory,
private TestProductFamilyRequestFactory $productFamilyRequestFactory,
private TestProductRequestFactory $productRequestFactory,
private TestPaymentProfileRequestFactory $paymentProfileRequestFactory
)
{
}

public function createRequest(): CreateCustomerRequest
{
return $this->customerRequestFactory->create();
}

public function getExpectedCustomer(int $id, string $createdAt, string $updatedAt): Customer
{
return $this->customerFactory->create($id, $createdAt, $updatedAt);
}

public function getNotExistingCustomerId(): int
{
return CustomerTestData::NON_EXISTING_CUSTOMER_ID;
return CustomerTestData::NOT_EXISTING_CUSTOMER_ID;
}

/**
Expand All @@ -52,4 +61,57 @@ public function getCustomersListResponse(Customer $customer): array
{
return [$this->customerResponseFactory->createWithCustomer($customer)];
}

public function loadCustomer(): Customer
{
return $this->client
->getCustomersController()
->createCustomer($this->getCreateCustomerRequest())
->getCustomer();
}

public function getCreateCustomerRequest(): CreateCustomerRequest
{
return $this->customerRequestFactory->createCreateCustomerRequest();
}

public function getUpdateCustomerRequest(): UpdateCustomerRequest
{
return $this->customerRequestFactory->createUpdateCustomerRequest();
}

public function getNotExistingCustomerReference(): string
{
return CustomerTestData::NOT_EXISTING_CUSTOMER_REFERENCE;
}

public function loadSubscription(int $customerId): Subscription
{
$productFamily = $this->client
->getProductFamiliesController()
->createProductFamily($this->productFamilyRequestFactory->create(ProductFamilyTestData::NAME_SEVEN))
->getProductFamily();
$product = $this->client
->getProductsController()
->createProduct(
$productFamily->getId(),
$this->productRequestFactory->create(ProductTestData::NAME_FOUR, ProductTestData::HANDLE_FOUR)
)
->getProduct();
$paymentProfile = $this->client
->getPaymentProfilesController()
->createPaymentProfile($this->paymentProfileRequestFactory->create($customerId))
->getPaymentProfile();

return $this->client
->getSubscriptionsController()
->createSubscription(
$this->subscriptionRequestFactory->create(
$customerId,
$product->getId(),
$paymentProfile->getId()
)
)
->getSubscription();
}
}
2 changes: 1 addition & 1 deletion tests/Controllers/PaymentProfilesControllerTestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function loadCustomer(): Customer
{
return $this->client
->getCustomersController()
->createCustomer($this->customerRequestFactory->create())
->createCustomer($this->customerRequestFactory->createCreateCustomerRequest())
->getCustomer();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Controllers/ProductsControllerTestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function loadProductFamily(): ProductFamily
return $this->client
->getProductFamiliesController()
->createProductFamily(
$this->productFamilyRequestFactory->create(ProductFamilyTestData::NAME_FOUR)
$this->productFamilyRequestFactory->create(ProductFamilyTestData::NAME_THREE)
)
->getProductFamily();
}
Expand All @@ -41,7 +41,7 @@ public function loadProductFamilyTwo(): ProductFamily
return $this->client
->getProductFamiliesController()
->createProductFamily(
$this->productFamilyRequestFactory->create(ProductFamilyTestData::NAME_FIVE)
$this->productFamilyRequestFactory->create(ProductFamilyTestData::NAME_FOUR)
)
->getProductFamily();
}
Expand Down
Loading

0 comments on commit 99120b4

Please sign in to comment.