Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 3e25e3c

Browse files
authored
Merge pull request #5 from voucherifyio/tests
tests
2 parents c558a0e + cfc10bb commit 3e25e3c

File tree

10 files changed

+384
-331
lines changed

10 files changed

+384
-331
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ build/phplint.cache
2828
/test-requirements.txt
2929
/.env
3030
/.idea/
31+
/__tests__/_generated/
32+
/__tests__/_output/
33+
/__tests__/Support/

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ WORKDIR /app
1515

1616
COPY --from=composer /app /app
1717

18-
CMD ["php", "-S", "0.0.0.0:5050", "./__tests__/index.php"]
18+
CMD ["php", "vendor/bin/codecept", "run", "Unit"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ Class | Method | HTTP request | Description
873873

874874
1. Copy `.env.example` to `.env` and fill in the values.
875875
2. Run `docker build -t php .` to build the image.
876-
3. Run `docker run -p 5050:5050 --rm php` to run the tests and delete container immediately after.
876+
3. Run `docker run --rm php` to run the tests and delete container immediately after.
877877

878878
## Author
879879

__tests__/README_TESTS.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## Tool:
2+
We use `codeception` for testing. You should have installed `codeception` via `composer` in order to run the tests.
3+
4+
`__tests__/UnitTester.php` is used to configure `codeception` and to load `vendor/autoload.php` file. Please do not modify this file!
5+
6+
7+
## How to write tests:
8+
All tests should be in `__tests__/Unit/` folder. And should be named as `*Test.php`. All test functions should be named as `test*` (see example).
9+
10+
Import sdk:
11+
12+
```php
13+
require_once(dirname(dirname(__DIR__)) . '/vendor/autoload.php');
14+
```
15+
16+
Good practice is to use `_before` to configure sdks APIs:
17+
```php
18+
19+
//example
20+
protected function _before()
21+
{
22+
//load .env
23+
$env = parse_ini_file('.env');
24+
25+
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('X-App-Id', $env["X_APP_ID"]);
26+
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('X-App-Token', $env["X_APP_TOKEN"]);
27+
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setHost($env["VOUCHERIFY_HOST"]);
28+
29+
$this->productsApiInstance = new OpenAPI\Client\Api\ProductsApi(
30+
new GuzzleHttp\Client(),
31+
$config
32+
);
33+
$this->campaignsApiInstance = new OpenAPI\Client\Api\CampaignsApi(
34+
new GuzzleHttp\Client(),
35+
$config
36+
);
37+
$this->redemptionsApiInstance = new OpenAPI\Client\Api\RedemptionsApi(
38+
new GuzzleHttp\Client(),
39+
$config
40+
);
41+
}
42+
43+
```
44+
45+
Test example:
46+
```php
47+
//example
48+
public function testListRedemptions()
49+
{
50+
$this->redemptionsApiInstance->listRedemptions(1, 1);
51+
}
52+
```
53+
54+
55+
## Environment variables:
56+
`.env` file should be in the root of the project and should contain `X_APP_ID`, `X_APP_TOKEN` and `VOUCHERIFY_HOST` variables. Check `.env.example` for reference.
57+
58+
## Run tests:
59+
60+
From the root of the project run:
61+
```bash
62+
php vendor/bin/codecept run Unit
63+
```

__tests__/Unit.suite.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Codeception Test Suite Configuration
2+
#
3+
# Suite for unit or integration tests.
4+
5+
actor: UnitTester
6+
modules:
7+
enabled:
8+
- Asserts
9+
step_decorators: ~

__tests__/Unit/MainTest.php

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
<?php
2+
//load SDK
3+
require_once(dirname(dirname(__DIR__)) . '/vendor/autoload.php');
4+
5+
//test
6+
class MainTest extends \Codeception\Test\Unit
7+
{
8+
/**
9+
* @TODO, refactor test
10+
*/
11+
12+
protected $productsApiInstance;
13+
protected $campaignsApiInstance;
14+
protected $validationRulesApiInstance;
15+
protected $customersApiInstance;
16+
protected $qualificationsApiInstance;
17+
protected $redemptionsApiInstance;
18+
protected $stackedDiscountsApiInstance;
19+
20+
protected function _before()
21+
{
22+
//load .env
23+
$env = parse_ini_file('.env');
24+
25+
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('X-App-Id', $env["X_APP_ID"]);
26+
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('X-App-Token', $env["X_APP_TOKEN"]);
27+
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setHost($env["VOUCHERIFY_HOST"]);
28+
29+
$this->productsApiInstance = new OpenAPI\Client\Api\ProductsApi(
30+
new GuzzleHttp\Client(),
31+
$config
32+
);
33+
$this->campaignsApiInstance = new OpenAPI\Client\Api\CampaignsApi(
34+
new GuzzleHttp\Client(),
35+
$config
36+
);
37+
$this->validationRulesApiInstance = new OpenAPI\Client\Api\ValidationRulesApi(
38+
new GuzzleHttp\Client(),
39+
$config
40+
);
41+
$this->customersApiInstance = new OpenAPI\Client\Api\CustomersApi(
42+
new GuzzleHttp\Client(),
43+
$config
44+
);
45+
$this->qualificationsApiInstance = new OpenAPI\Client\Api\QualificationsApi(
46+
new GuzzleHttp\Client(),
47+
$config
48+
);
49+
$this->stackedDiscountsApiInstance = new OpenAPI\Client\Api\StackableDiscountsApi(
50+
new GuzzleHttp\Client(),
51+
$config
52+
);
53+
$this->redemptionsApiInstance = new OpenAPI\Client\Api\RedemptionsApi(
54+
new GuzzleHttp\Client(),
55+
$config
56+
);
57+
}
58+
59+
// helper functions
60+
public function generateRandomString($length = 10)
61+
{
62+
$randomString = '';
63+
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
64+
65+
$max = strlen($characters) - 1;
66+
67+
for ($i = 0; $i < $length; $i++) {
68+
$randomString .= $characters[random_int(0, $max)];
69+
}
70+
71+
return $randomString;
72+
}
73+
74+
public function createTwoProducts()
75+
{
76+
$products_create_request_body = new \OpenAPI\Client\Model\ProductsCreateRequestBody();
77+
$products_create_request_body->setSourceId($this->generateRandomString());
78+
$products_create_request_body->setName($this->generateRandomString());
79+
$products_create_request_body->setPrice(20000);
80+
$products_create_request_body->setAttributes(["color", "memory", "processor"]);
81+
$created_product = $this->productsApiInstance->createProduct($products_create_request_body);
82+
$products_create_request_body->setSourceId($this->generateRandomString());
83+
$products_create_request_body->setName($this->generateRandomString());
84+
$products_create_request_body->setPrice(66000);
85+
$created_product2 = $this->productsApiInstance->createProduct($products_create_request_body);
86+
return [$created_product, $created_product2];
87+
}
88+
89+
public function createValidationRule()
90+
{
91+
$created_products = $this->createTwoProducts();
92+
$validation_rules_create_request_body = new \OpenAPI\Client\Model\ValidationRulesCreateRequestBody();
93+
$applicable_to_created_product2 = new \OpenAPI\Client\Model\ApplicableTo();
94+
$applicable_to_created_product2->setProductId($created_products[0]->getId());
95+
$applicable_to_created_product2->setObject("product");
96+
$validation_rules_create_request_body->setApplicableTo(new \OpenAPI\Client\Model\ValidationRuleBaseApplicableTo());
97+
$validation_rules_create_request_body->getApplicableTo()->setIncluded([$applicable_to_created_product2]);
98+
$validation_rules_create_request_body->setType("basic");
99+
$validation_rules_create_request_body->setName($this->generateRandomString());
100+
return $this->validationRulesApiInstance->createValidationRules($validation_rules_create_request_body);
101+
}
102+
103+
public function createDiscountCampaign()
104+
{
105+
$campaigns_create_request_body_discount = new \OpenAPI\Client\Model\CampaignsCreateRequestBody();
106+
$campaigns_create_request_body_discount->setName($this->generateRandomString(12));
107+
$campaigns_create_request_body_discount->setCampaignType("DISCOUNT_COUPONS");
108+
$campaigns_create_request_body_discount->setType("AUTO_UPDATE");
109+
$campaigns_create_request_body_discount->setVoucher(new \OpenAPI\Client\Model\CampaignsCreateRequestBodyVoucher());
110+
$campaigns_create_request_body_discount->getVoucher()->setType("DISCOUNT_VOUCHER");
111+
$campaigns_create_request_body_discount->getVoucher()->setDiscount(new \OpenAPI\Client\Model\Discount());
112+
$campaigns_create_request_body_discount->getVoucher()->getDiscount()->setType("AMOUNT");
113+
$campaigns_create_request_body_discount->getVoucher()->getDiscount()->setAmountOff(1000);
114+
return $this->campaignsApiInstance->createCampaign($campaigns_create_request_body_discount);
115+
}
116+
117+
public function createPromotionCampaign()
118+
{
119+
$promotionTierCreateParams = new \OpenAPI\Client\Model\PromotionTierCreateParams();
120+
$promotionTierCreateParams->setName($this->generateRandomString());
121+
$promotionTierCreateParams->setBanner('testBanner');
122+
$promotionTierCreateParams->setAction(new \OpenAPI\Client\Model\PromotionTierAction());
123+
$promotionTierCreateParams->getAction()->setDiscount(new \OpenAPI\Client\Model\Discount());
124+
$promotionTierCreateParams->getAction()->getDiscount()->setType("AMOUNT");
125+
$promotionTierCreateParams->getAction()->getDiscount()->setAmountOff(1000);
126+
$campaigns_create_request_body_promotion = new \OpenAPI\Client\Model\CampaignsCreateRequestBody();
127+
$campaigns_create_request_body_promotion->setName($this->generateRandomString(12));
128+
$campaigns_create_request_body_promotion->setCampaignType("PROMOTION");
129+
$campaigns_create_request_body_promotion->setPromotion(new \OpenAPI\Client\Model\CampaignsCreateRequestBodyPromotion());
130+
$campaigns_create_request_body_promotion->getPromotion()->setTiers([$promotionTierCreateParams]);
131+
return $this->campaignsApiInstance->createCampaign($campaigns_create_request_body_promotion);
132+
}
133+
134+
public function createCustomer()
135+
{
136+
$customersCreateRequestBody = new \OpenAPI\Client\Model\CustomersCreateRequestBody();
137+
$customersCreateRequestBody->setSourceId('test123');
138+
$customersCreateRequestBody->setName('test123');
139+
$customersCreateRequestBody->setAddress(new \OpenAPI\Client\Model\CustomerBaseAddress());
140+
$customersCreateRequestBody->getAddress()->setCountry('US');
141+
$customersCreateRequestBody->getAddress()->setCity('Vice City');
142+
$customersCreateRequestBody->getAddress()->setLine1('123');
143+
$customersCreateRequestBody->getAddress()->setPostalCode('60089');
144+
return $this->customersApiInstance->createCustomer($customersCreateRequestBody);
145+
}
146+
147+
public function deleteCampaign($id)
148+
{
149+
return $this->campaignsApiInstance->deleteCampaign($id, true);
150+
}
151+
152+
// tests
153+
public function testCreateTwoProducts()
154+
{
155+
$this->createTwoProducts();
156+
}
157+
158+
public function testCreateValidationRule()
159+
{
160+
$this->createValidationRule();
161+
}
162+
163+
public function testCreateAndRemoveDiscountCampaign()
164+
{
165+
$created_discount_campaign = $this->createDiscountCampaign();
166+
$this->deleteCampaign($created_discount_campaign->getId());
167+
}
168+
169+
public function testCreateAndRemovePromotionCampaign()
170+
{
171+
$created_discount_campaign = $this->createPromotionCampaign();
172+
$this->deleteCampaign($created_discount_campaign->getId());
173+
}
174+
175+
public function testListCampaigns()
176+
{
177+
$limit = 2; // int | A limit on the number of objects to be returned. Limit can range between 1 and 100 items.
178+
$page = 1; // int | Which page of results to return.
179+
$campaign_type = \OpenAPI\Client\Model\ParameterCampaignType::DISCOUNT_COUPONS->value; // ParameterCampaignType
180+
$expand = \OpenAPI\Client\Model\ParameterExpandListCampaigns::CATEGORY->value; // ParameterExpandListCampaigns
181+
$order = \OpenAPI\Client\Model\ParameterOrderListCampaigns::CREATED_AT2->value; // ParameterOrderListCampaigns
182+
$this->campaignsApiInstance->listCampaigns($limit, $page, $campaign_type, $expand, $order);
183+
}
184+
185+
public function testCreateCustomer()
186+
{
187+
$this->createCustomer();
188+
}
189+
190+
public function testCheckEligibilityAndDoStackedValidationAndRedemption()
191+
{
192+
$created_discount_campaign = $this->createDiscountCampaign();
193+
194+
$created_customer = $this->createCustomer();
195+
$created_product = $this->createTwoProducts()[0];
196+
197+
$order_item = new \OpenAPI\Client\Model\OrderItem();
198+
$order_item->setPrice($created_product->getPrice());
199+
$order_item->setQuantity(3);
200+
$order_item->setProductId($created_product->getId());
201+
202+
// check eligibility
203+
$qualifications_check_eligibility_request_body = new \OpenAPI\Client\Model\QualificationsCheckEligibilityRequestBody();
204+
$qualifications_check_eligibility_request_body->setCustomer(new \OpenAPI\Client\Model\Customer());
205+
$qualifications_check_eligibility_request_body->getCustomer()->setId($created_customer->getId());
206+
$qualifications_check_eligibility_request_body->setOrder(new \OpenAPI\Client\Model\Order());
207+
$qualifications_check_eligibility_request_body->getOrder()->setStatus("CREATED");
208+
$qualifications_check_eligibility_request_body->getOrder()->setItems([$order_item]);
209+
$qualifications_check_eligibility_request_body->setMode("BASIC");
210+
$qualifications_check_eligibility_request_body->setScenario("ALL");
211+
$check_eligibility_result = $this->qualificationsApiInstance->checkEligibility($qualifications_check_eligibility_request_body);
212+
213+
$applicable_promotion_tiers = array_slice(array_filter($check_eligibility_result->getRedeemables()->getData(), function ($redeemable) {
214+
return $redeemable->getObject() === 'promotion_tier';
215+
}), 0, 3);
216+
$applicable_promotion_tiers_ids = array_map(function ($promotion_tier) {
217+
return $promotion_tier->getId();
218+
}, $applicable_promotion_tiers);
219+
220+
$campaign_voucher = $this->campaignsApiInstance->addVouchersToCampaign($created_discount_campaign->getId(), 1);
221+
222+
// stacked validation
223+
$validations_validate_request_body = new \OpenAPI\Client\Model\ValidationsValidateRequestBody();
224+
$validations_validate_request_body->setOrder(new \OpenAPI\Client\Model\Order());
225+
$validations_validate_request_body->getOrder()->setStatus("CREATED");
226+
$validations_validate_request_body->getOrder()->setItems([$order_item]);
227+
$validations_validate_request_body->setCustomer(new \OpenAPI\Client\Model\Customer());
228+
$validations_validate_request_body->getCustomer()->setSourceId($created_customer->getSourceId());
229+
$validations_validate_request_body_redeemables = [];
230+
foreach ($applicable_promotion_tiers_ids as $promotion_tier_id) {
231+
$redeemable = new \OpenAPI\Client\Model\StackableValidateRedeemBaseRedeemablesItem();
232+
$redeemable->setId($promotion_tier_id);
233+
$redeemable->setObject("promotion_tier");
234+
array_push($validations_validate_request_body_redeemables, $redeemable);
235+
}
236+
$voucher_redeemable = new \OpenAPI\Client\Model\StackableValidateRedeemBaseRedeemablesItem();
237+
$voucher_redeemable->setId($campaign_voucher->getCode());
238+
$voucher_redeemable->setObject("voucher");
239+
array_push($validations_validate_request_body_redeemables, $voucher_redeemable);
240+
$validations_validate_request_body->setRedeemables($validations_validate_request_body_redeemables);
241+
$this->stackedDiscountsApiInstance->validateStackedDiscounts($validations_validate_request_body);
242+
243+
// stacked redemption
244+
$redemptions_redeem_request_body = new \OpenAPI\Client\Model\RedemptionsRedeemRequestBody();
245+
$redemptions_redeem_request_body->setOrder(new \OpenAPI\Client\Model\Order());
246+
$redemptions_redeem_request_body->getOrder()->setStatus("CREATED");
247+
$redemptions_redeem_request_body->getOrder()->setItems([$order_item]);
248+
$redemptions_redeem_request_body->setCustomer(new \OpenAPI\Client\Model\Customer());
249+
$redemptions_redeem_request_body->getCustomer()->setSourceId($created_customer->getSourceId());
250+
$redemptions_redeem_request_body->setRedeemables($validations_validate_request_body_redeemables);
251+
$this->stackedDiscountsApiInstance->redeemStackedDiscounts($redemptions_redeem_request_body);
252+
}
253+
254+
public function testListRedemptions()
255+
{
256+
$this->redemptionsApiInstance->listRedemptions(1, 1);
257+
}
258+
}

__tests__/UnitTester.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Support;
6+
7+
/**
8+
* Inherited Methods
9+
* @method void wantTo($text)
10+
* @method void wantToTest($text)
11+
* @method void execute($callable)
12+
* @method void expectTo($prediction)
13+
* @method void expect($prediction)
14+
* @method void amGoingTo($argumentation)
15+
* @method void am($role)
16+
* @method void lookForwardTo($achieveValue)
17+
* @method void comment($description)
18+
* @method void pause($vars = [])
19+
*
20+
* @SuppressWarnings(PHPMD)
21+
*/
22+
class UnitTester extends \Codeception\Actor
23+
{
24+
use _generated\UnitTesterActions;
25+
26+
/**
27+
* Define custom actions here
28+
*/
29+
}

0 commit comments

Comments
 (0)