Skip to content

Commit

Permalink
feat(php-sdk): refactor tests 2
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianMiklaszewskiBldr committed Jan 5, 2024
1 parent 53a34a8 commit d492cf3
Show file tree
Hide file tree
Showing 22 changed files with 446 additions and 365 deletions.
8 changes: 4 additions & 4 deletions tests/Controllers/ComponentsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AdvancedBillingLib\Tests\Controllers;

use AdvancedBillingLib\Tests\DataLoader\TestProductFamilyLoader;
use AdvancedBillingLib\Tests\TestCase;
use AdvancedBillingLib\Tests\TestFactory\TestComponentFactory;
use AdvancedBillingLib\Tests\TestFactory\TestComponentRequestFactory;
Expand All @@ -19,7 +20,7 @@ final class ComponentsControllerTest extends TestCase
*/
public function test_CreateComponent_ShouldCreateComponent_WhenAllDataAreCorrect(): void
{
$productFamily = $this->testData->loadProductFamily();
$productFamily = $this->testData->loadProductFamily(name: 'ComponentsControllerTest_ProductFamily_1');

$component = $this->client
->getComponentsController()
Expand Down Expand Up @@ -50,10 +51,9 @@ protected function setUp(): void
parent::setUp();

$this->testData = new ComponentsControllerTestData(
$this->client,
new TestProductFamilyRequestFactory(),
new TestComponentRequestFactory(),
new TestComponentFactory()
new TestComponentFactory(),
new TestProductFamilyLoader($this->client, new TestProductFamilyRequestFactory())
);
$this->assertions = new ComponentsControllerTestAssertions($this);
}
Expand Down
16 changes: 5 additions & 11 deletions tests/Controllers/ComponentsControllerTestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,27 @@

namespace AdvancedBillingLib\Tests\Controllers;

use AdvancedBillingLib\AdvancedBillingClient;
use AdvancedBillingLib\Models\Component;
use AdvancedBillingLib\Models\CreateQuantityBasedComponent;
use AdvancedBillingLib\Models\ProductFamily;
use AdvancedBillingLib\Tests\DataLoader\TestProductFamilyLoader;
use AdvancedBillingLib\Tests\TestData\ComponentTestData;
use AdvancedBillingLib\Tests\TestData\ProductFamilyTestData;
use AdvancedBillingLib\Tests\TestFactory\TestComponentFactory;
use AdvancedBillingLib\Tests\TestFactory\TestComponentRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestProductFamilyRequestFactory;

final class ComponentsControllerTestData
{
public function __construct(
private AdvancedBillingClient $client,
private TestProductFamilyRequestFactory $productFamilyRequestFactory,
private TestComponentRequestFactory $componentRequestFactory,
private TestComponentFactory $componentFactory
private TestComponentFactory $componentFactory,
private TestProductFamilyLoader $productFamilyLoader
)
{
}

public function loadProductFamily(): ProductFamily
public function loadProductFamily(string $name): ProductFamily
{
return $this->client
->getProductFamiliesController()
->createProductFamily($this->productFamilyRequestFactory->create(ProductFamilyTestData::NAME_NINE))
->getProductFamily();
return $this->productFamilyLoader->load($name);
}

public function getComponentKindPath(): string
Expand Down
9 changes: 5 additions & 4 deletions tests/Controllers/CouponsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

namespace AdvancedBillingLib\Tests\Controllers;

use AdvancedBillingLib\Tests\DataLoader\TestProductFamilyLoader;
use AdvancedBillingLib\Tests\TestCase;
use AdvancedBillingLib\Tests\TestFactory\TestCouponFactory;
use AdvancedBillingLib\Tests\TestFactory\TestCouponRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestProductFamilyRequestFactory;

final class CouponsControllerTest extends TestCase
{
private CouponsControllerTestData $testData;
private CouponsControllerTestAssertions $assertions;

/**
* @covers \AdvancedBillingLib\Controllers\CouponsController::createCoupon
*/
public function test_CreateCoupon_ShouldCreateCoupon_WhenDataAreValid(): void
{
$productFamily = $this->testData->loadProductFamily();
$productFamily = $this->testData->loadProductFamily(name: 'CouponsControllerTest_ProductFamily_1');

$coupon = $this->client
->getCouponsController()
Expand Down Expand Up @@ -46,10 +48,9 @@ protected function setUp(): void
parent::setUp();

$this->testData = new CouponsControllerTestData(
$this->client,
new TestProductFamilyRequestFactory(),
new TestCouponRequestFactory(),
new TestCouponFactory()
new TestCouponFactory(),
new TestProductFamilyLoader($this->client, new TestProductFamilyRequestFactory())
);
$this->assertions = new CouponsControllerTestAssertions($this);
}
Expand Down
21 changes: 9 additions & 12 deletions tests/Controllers/CouponsControllerTestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,35 @@

namespace AdvancedBillingLib\Tests\Controllers;

use AdvancedBillingLib\AdvancedBillingClient;
use AdvancedBillingLib\Models\Coupon;
use AdvancedBillingLib\Models\CreateOrUpdateCoupon;
use AdvancedBillingLib\Models\ProductFamily;
use AdvancedBillingLib\Tests\DataLoader\TestProductFamilyLoader;
use AdvancedBillingLib\Tests\TestData\CouponTestData;
use AdvancedBillingLib\Tests\TestData\ProductFamilyTestData;
use AdvancedBillingLib\Tests\TestFactory\TestCouponFactory;
use AdvancedBillingLib\Tests\TestFactory\TestCouponRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestProductFamilyRequestFactory;

final class CouponsControllerTestData
{
public function __construct(
private AdvancedBillingClient $client,
private TestProductFamilyRequestFactory $productFamilyRequestFactory,
private TestCouponRequestFactory $couponRequestFactory,
private TestCouponFactory $couponFactory
private TestCouponFactory $couponFactory,
private TestProductFamilyLoader $productFamilyLoader
)
{
}

public function loadProductFamily(): ProductFamily
public function loadProductFamily(string $name): ProductFamily
{
return $this->client
->getProductFamiliesController()
->createProductFamily($this->productFamilyRequestFactory->create(ProductFamilyTestData::NAME_TEN))
->getProductFamily();
return $this->productFamilyLoader->load($name);
}

public function getCreateOrUpdatePercentageCouponRequest(string $productFamilyId): CreateOrUpdateCoupon
{
return $this->couponRequestFactory->createCreateOrUpdatePercentageCouponRequest($productFamilyId, CouponTestData::CODE_ONE);
return $this->couponRequestFactory->createCreateOrUpdatePercentageCouponRequest(
$productFamilyId,
CouponTestData::CODE_ONE
);
}

public function getExpectedCoupon(
Expand Down
15 changes: 10 additions & 5 deletions tests/Controllers/CustomersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace AdvancedBillingLib\Tests\Controllers;

use AdvancedBillingLib\Tests\DataLoader\TestCustomerLoader;
use AdvancedBillingLib\Tests\DataLoader\TestPaymentProfileLoader;
use AdvancedBillingLib\Tests\DataLoader\TestProductFamilyLoader;
use AdvancedBillingLib\Tests\DataLoader\TestProductLoader;
use AdvancedBillingLib\Tests\DataLoader\TestSubscriptionsLoader;
use AdvancedBillingLib\Tests\TestCase;
use AdvancedBillingLib\Tests\TestFactory\TestCustomerFactory;
use AdvancedBillingLib\Tests\TestFactory\TestCustomerRequestFactory;
Expand Down Expand Up @@ -205,11 +210,11 @@ protected function setUp(): void
new TestCustomerRequestFactory(),
new TestCustomerFactory(),
new TestCustomerResponseFactory(),
$this->client,
new TestSubscriptionRequestFactory(),
new TestProductFamilyRequestFactory(),
new TestProductRequestFactory(),
new TestPaymentProfileRequestFactory()
new TestCustomerLoader($this->client, new TestCustomerRequestFactory()),
new TestProductFamilyLoader($this->client, new TestProductFamilyRequestFactory()),
new TestPaymentProfileLoader($this->client, new TestPaymentProfileRequestFactory()),
new TestProductLoader($this->client, new TestProductRequestFactory()),
new TestSubscriptionsLoader($this->client, new TestSubscriptionRequestFactory()),
);
$this->assertions = new CustomersControllerTestAssertions($this);
}
Expand Down
65 changes: 24 additions & 41 deletions tests/Controllers/CustomersControllerTestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,33 @@

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\DataLoader\TestCustomerLoader;
use AdvancedBillingLib\Tests\DataLoader\TestPaymentProfileLoader;
use AdvancedBillingLib\Tests\DataLoader\TestProductFamilyLoader;
use AdvancedBillingLib\Tests\DataLoader\TestProductLoader;
use AdvancedBillingLib\Tests\DataLoader\TestSubscriptionsLoader;
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 AdvancedBillingClient $client,
private TestSubscriptionRequestFactory $subscriptionRequestFactory,
private TestProductFamilyRequestFactory $productFamilyRequestFactory,
private TestProductRequestFactory $productRequestFactory,
private TestPaymentProfileRequestFactory $paymentProfileRequestFactory
private TestCustomerLoader $customerLoader,
private TestProductFamilyLoader $productFamilyLoader,
private TestPaymentProfileLoader $paymentProfileLoader,
private TestProductLoader $productLoader,
private TestSubscriptionsLoader $subscriptionsLoader
)
{
}
Expand Down Expand Up @@ -64,15 +63,16 @@ public function getCustomersListResponse(Customer $customer): array

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

public function getCreateCustomerRequest(): CreateCustomerRequest
{
return $this->customerRequestFactory->createCreateCustomerRequest();
return $this->customerRequestFactory->createCreateCustomerRequest(
CustomerTestData::FIRST_NAME,
CustomerTestData::LAST_NAME,
CustomerTestData::EMAIL
);
}

public function getUpdateCustomerRequest(): UpdateCustomerRequest
Expand All @@ -87,31 +87,14 @@ public function getNotExistingCustomerReference(): string

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->createCreatePaymentProfileRequest($customerId))
->getPaymentProfile();
$productFamily = $this->productFamilyLoader->load(ProductFamilyTestData::NAME_SEVEN);
$product = $this->productLoader->load(
'CustomersControllerTest Product 1',
'customerscontrollertest-product-1',
$productFamily->getId()
);
$paymentProfile = $this->paymentProfileLoader->load($customerId);

return $this->client
->getSubscriptionsController()
->createSubscription(
$this->subscriptionRequestFactory->create(
$customerId,
$product->getId(),
$paymentProfile->getId()
)
)
->getSubscription();
return $this->subscriptionsLoader->load($customerId, $product->getId(), $paymentProfile->getId());
}
}
14 changes: 10 additions & 4 deletions tests/Controllers/PaymentProfilesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace AdvancedBillingLib\Tests\Controllers;

use AdvancedBillingLib\AdvancedBillingClient;
use AdvancedBillingLib\Tests\DataLoader\TestCustomerLoader;
use AdvancedBillingLib\Tests\DataLoader\TestPaymentProfileLoader;
use AdvancedBillingLib\Tests\DataLoader\TestProductFamilyLoader;
use AdvancedBillingLib\Tests\DataLoader\TestSubscriptionsLoader;
use AdvancedBillingLib\Tests\TestCase;
use AdvancedBillingLib\Tests\TestFactory\TestCustomerRequestFactory;
use AdvancedBillingLib\Tests\TestFactory\TestPaymentProfileRequestFactory;
Expand Down Expand Up @@ -157,12 +162,13 @@ protected function setUp(): void
parent::setUp();

$this->testData = new PaymentProfilesControllerTestData(
$this->client,
new TestCustomerRequestFactory(),
new AdvancedBillingClient(),
new TestPaymentProfileRequestFactory(),
new TestProductFamilyRequestFactory(),
new TestProductRequestFactory(),
new TestSubscriptionRequestFactory()
new TestCustomerLoader($this->client, new TestCustomerRequestFactory()),
new TestPaymentProfileLoader($this->client, new TestPaymentProfileRequestFactory()),
new TestProductFamilyLoader($this->client, new TestProductFamilyRequestFactory()),
new TestSubscriptionsLoader($this->client, new TestSubscriptionRequestFactory()),
);
$this->assertions = new PaymentProfilesControllerTestAssertions($this);
}
Expand Down
Loading

0 comments on commit d492cf3

Please sign in to comment.