From 3f2abe5485bbc6a6c19e05ac2effa12a7fe93a8c Mon Sep 17 00:00:00 2001 From: p-zielinski <82354717+p-zielinski@users.noreply.github.com> Date: Wed, 24 Jan 2024 15:53:35 +0100 Subject: [PATCH 1/7] tests --- Dockerfile | 2 +- __tests__/Acceptance.suite.yml | 16 ++ __tests__/Functional.suite.yml | 12 + __tests__/Support/AcceptanceTester.php | 29 +++ __tests__/Support/Data/.gitkeep | 0 __tests__/Support/FunctionalTester.php | 29 +++ __tests__/Support/UnitTester.php | 29 +++ __tests__/Support/_generated/.gitignore | 2 + __tests__/Unit.suite.yml | 9 + __tests__/Unit/MainTest.php | 254 +++++++++++++++++++ __tests__/_output/.gitignore | 2 + __tests__/index.php | 324 ------------------------ codeception.yml | 12 + composer.json | 16 +- 14 files changed, 405 insertions(+), 331 deletions(-) create mode 100644 __tests__/Acceptance.suite.yml create mode 100644 __tests__/Functional.suite.yml create mode 100644 __tests__/Support/AcceptanceTester.php create mode 100644 __tests__/Support/Data/.gitkeep create mode 100644 __tests__/Support/FunctionalTester.php create mode 100644 __tests__/Support/UnitTester.php create mode 100644 __tests__/Support/_generated/.gitignore create mode 100644 __tests__/Unit.suite.yml create mode 100644 __tests__/Unit/MainTest.php create mode 100644 __tests__/_output/.gitignore delete mode 100644 __tests__/index.php create mode 100644 codeception.yml diff --git a/Dockerfile b/Dockerfile index c3dc471..e78665e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,4 +15,4 @@ WORKDIR /app COPY --from=composer /app /app -CMD ["php", "-S", "0.0.0.0:5050", "./__tests__/index.php"] +CMD ["php", "vendor/bin/codecept", "run", "Unit"] diff --git a/__tests__/Acceptance.suite.yml b/__tests__/Acceptance.suite.yml new file mode 100644 index 0000000..5b10b9d --- /dev/null +++ b/__tests__/Acceptance.suite.yml @@ -0,0 +1,16 @@ +# Codeception Test Suite Configuration +# +# Suite for acceptance tests. +# Perform tests in browser using the WebDriver or PhpBrowser. +# If you need both WebDriver and PHPBrowser tests - create a separate suite. + +actor: AcceptanceTester +modules: + enabled: + - PhpBrowser: + url: http://localhost/myapp +# add Codeception\Step\Retry trait to AcceptanceTester to enable retries +step_decorators: + - Codeception\Step\ConditionalAssertion + - Codeception\Step\TryTo + - Codeception\Step\Retry diff --git a/__tests__/Functional.suite.yml b/__tests__/Functional.suite.yml new file mode 100644 index 0000000..1b7276a --- /dev/null +++ b/__tests__/Functional.suite.yml @@ -0,0 +1,12 @@ +# Codeception Test Suite Configuration +# +# Suite for functional tests +# Emulate web requests and make application process them +# Include one of framework modules (Symfony, Yii2, Laravel, Phalcon5) to use it +# Remove this suite if you don't use frameworks + +actor: FunctionalTester +modules: + enabled: + # add a framework module here +step_decorators: ~ diff --git a/__tests__/Support/AcceptanceTester.php b/__tests__/Support/AcceptanceTester.php new file mode 100644 index 0000000..6ccbabd --- /dev/null +++ b/__tests__/Support/AcceptanceTester.php @@ -0,0 +1,29 @@ +setApiKey('X-App-Id', $env["X_APP_ID"]); + $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('X-App-Token', $env["X_APP_TOKEN"]); + $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setHost($env["VOUCHERIFY_HOST"]); + + $this->productsApiInstance = new OpenAPI\Client\Api\ProductsApi( + new GuzzleHttp\Client(), + $config + ); + $this->campaignsApiInstance = new OpenAPI\Client\Api\CampaignsApi( + new GuzzleHttp\Client(), + $config + ); + $this->validationRulesApiInstance = new OpenAPI\Client\Api\ValidationRulesApi( + new GuzzleHttp\Client(), + $config + ); + $this->customersApiInstance = new OpenAPI\Client\Api\CustomersApi( + new GuzzleHttp\Client(), + $config + ); + $this->qualificationsApiInstance = new OpenAPI\Client\Api\QualificationsApi( + new GuzzleHttp\Client(), + $config + ); + $this->stackedDiscountsApiInstance = new OpenAPI\Client\Api\StackableDiscountsApi( + new GuzzleHttp\Client(), + $config + ); + $this->redemptionsApiInstance = new OpenAPI\Client\Api\RedemptionsApi( + new GuzzleHttp\Client(), + $config + ); + } + + // helper functions + public function generateRandomString($length = 10) + { + $randomString = ''; + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + + $max = strlen($characters) - 1; + + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[random_int(0, $max)]; + } + + return $randomString; + } + + public function createTwoProducts() + { + $products_create_request_body = new \OpenAPI\Client\Model\ProductsCreateRequestBody(); + $products_create_request_body->setSourceId($this->generateRandomString()); + $products_create_request_body->setName($this->generateRandomString()); + $products_create_request_body->setPrice(20000); + $products_create_request_body->setAttributes(["color", "memory", "processor"]); + $created_product = $this->productsApiInstance->createProduct($products_create_request_body); + $products_create_request_body->setSourceId($this->generateRandomString()); + $products_create_request_body->setName($this->generateRandomString()); + $products_create_request_body->setPrice(66000); + $created_product2 = $this->productsApiInstance->createProduct($products_create_request_body); + return [$created_product, $created_product2]; + } + + public function createValidationRule() + { + $created_products = $this->createTwoProducts(); + $validation_rules_create_request_body = new \OpenAPI\Client\Model\ValidationRulesCreateRequestBody(); + $applicable_to_created_product2 = new \OpenAPI\Client\Model\ApplicableTo(); + $applicable_to_created_product2->setProductId($created_products[0]->getId()); + $applicable_to_created_product2->setObject("product"); + $validation_rules_create_request_body->setApplicableTo(new \OpenAPI\Client\Model\ValidationRuleBaseApplicableTo()); + $validation_rules_create_request_body->getApplicableTo()->setIncluded([$applicable_to_created_product2]); + $validation_rules_create_request_body->setType("basic"); + $validation_rules_create_request_body->setName($this->generateRandomString()); + return $this->validationRulesApiInstance->createValidationRules($validation_rules_create_request_body); + } + + public function createDiscountCampaign() + { + $campaigns_create_request_body_discount = new \OpenAPI\Client\Model\CampaignsCreateRequestBody(); + $campaigns_create_request_body_discount->setName($this->generateRandomString(12)); + $campaigns_create_request_body_discount->setCampaignType("DISCOUNT_COUPONS"); + $campaigns_create_request_body_discount->setType("AUTO_UPDATE"); + $campaigns_create_request_body_discount->setVoucher(new \OpenAPI\Client\Model\CampaignsCreateRequestBodyVoucher()); + $campaigns_create_request_body_discount->getVoucher()->setType("DISCOUNT_VOUCHER"); + $campaigns_create_request_body_discount->getVoucher()->setDiscount(new \OpenAPI\Client\Model\Discount()); + $campaigns_create_request_body_discount->getVoucher()->getDiscount()->setType("AMOUNT"); + $campaigns_create_request_body_discount->getVoucher()->getDiscount()->setAmountOff(1000); + return $this->campaignsApiInstance->createCampaign($campaigns_create_request_body_discount); + } + + public function createPromotionCampaign() + { + $promotionTierCreateParams = new \OpenAPI\Client\Model\PromotionTierCreateParams(); + $promotionTierCreateParams->setName($this->generateRandomString()); + $promotionTierCreateParams->setBanner('testBanner'); + $promotionTierCreateParams->setAction(new \OpenAPI\Client\Model\PromotionTierAction()); + $promotionTierCreateParams->getAction()->setDiscount(new \OpenAPI\Client\Model\Discount()); + $promotionTierCreateParams->getAction()->getDiscount()->setType("AMOUNT"); + $promotionTierCreateParams->getAction()->getDiscount()->setAmountOff(1000); + $campaigns_create_request_body_promotion = new \OpenAPI\Client\Model\CampaignsCreateRequestBody(); + $campaigns_create_request_body_promotion->setName($this->generateRandomString(12)); + $campaigns_create_request_body_promotion->setCampaignType("PROMOTION"); + $campaigns_create_request_body_promotion->setPromotion(new \OpenAPI\Client\Model\CampaignsCreateRequestBodyPromotion()); + $campaigns_create_request_body_promotion->getPromotion()->setTiers([$promotionTierCreateParams]); + return $this->campaignsApiInstance->createCampaign($campaigns_create_request_body_promotion); + } + + public function createCustomer() + { + $customersCreateRequestBody = new \OpenAPI\Client\Model\CustomersCreateRequestBody(); + $customersCreateRequestBody->setSourceId('test123'); + $customersCreateRequestBody->setName('test123'); + $customersCreateRequestBody->setAddress(new \OpenAPI\Client\Model\CustomerBaseAddress()); + $customersCreateRequestBody->getAddress()->setCountry('US'); + $customersCreateRequestBody->getAddress()->setCity('Vice City'); + $customersCreateRequestBody->getAddress()->setLine1('123'); + $customersCreateRequestBody->getAddress()->setPostalCode('60089'); + return $this->customersApiInstance->createCustomer($customersCreateRequestBody); + } + + public function deleteCampaign($id) + { + return $this->campaignsApiInstance->deleteCampaign($id, true); + } + + // tests + public function testCreateTwoProducts() + { + $this->createTwoProducts(); + } + + public function testCreateValidationRule() + { + $this->createValidationRule(); + } + + public function testCreateAndRemoveDiscountCampaign() + { + $created_discount_campaign = $this->createDiscountCampaign(); + $this->deleteCampaign($created_discount_campaign->getId()); + } + + public function testCreateAndRemovePromotionCampaign() + { + $created_discount_campaign = $this->createPromotionCampaign(); + $this->deleteCampaign($created_discount_campaign->getId()); + } + + public function testListCampaigns() + { + $limit = 2; // int | A limit on the number of objects to be returned. Limit can range between 1 and 100 items. + $page = 1; // int | Which page of results to return. + $campaign_type = \OpenAPI\Client\Model\ParameterCampaignType::DISCOUNT_COUPONS->value; // ParameterCampaignType + $expand = \OpenAPI\Client\Model\ParameterExpandListCampaigns::CATEGORY->value; // ParameterExpandListCampaigns + $order = \OpenAPI\Client\Model\ParameterOrderListCampaigns::CREATED_AT2->value; // ParameterOrderListCampaigns + $this->campaignsApiInstance->listCampaigns($limit, $page, $campaign_type, $expand, $order); + } + + public function testCreateCustomer() + { + $this->createCustomer(); + } + + public function testCheckEligibilityAndDoStackedValidationAndRedemption() + { + $created_discount_campaign = $this->createDiscountCampaign(); + + $created_customer = $this->createCustomer(); + $created_product = $this->createTwoProducts()[0]; + + $order_item = new \OpenAPI\Client\Model\OrderItem(); + $order_item->setPrice($created_product->getPrice()); + $order_item->setQuantity(3); + $order_item->setProductId($created_product->getId()); + + // check eligibility + $qualifications_check_eligibility_request_body = new \OpenAPI\Client\Model\QualificationsCheckEligibilityRequestBody(); + $qualifications_check_eligibility_request_body->setCustomer(new \OpenAPI\Client\Model\Customer()); + $qualifications_check_eligibility_request_body->getCustomer()->setId($created_customer->getId()); + $qualifications_check_eligibility_request_body->setOrder(new \OpenAPI\Client\Model\Order()); + $qualifications_check_eligibility_request_body->getOrder()->setStatus("CREATED"); + $qualifications_check_eligibility_request_body->getOrder()->setItems([$order_item]); + $qualifications_check_eligibility_request_body->setMode("BASIC"); + $qualifications_check_eligibility_request_body->setScenario("ALL"); + $check_eligibility_result = $this->qualificationsApiInstance->checkEligibility($qualifications_check_eligibility_request_body); + + $applicable_promotion_tiers = array_slice(array_filter($check_eligibility_result->getRedeemables()->getData(), function ($redeemable) { + return $redeemable->getObject() === 'promotion_tier'; + }), 0, 3); + $applicable_promotion_tiers_ids = array_map(function ($promotion_tier) { + return $promotion_tier->getId(); + }, $applicable_promotion_tiers); + + $campaign_voucher = $this->campaignsApiInstance->addVouchersToCampaign($created_discount_campaign->getId(), 1); + + // stacked validation + $validations_validate_request_body = new \OpenAPI\Client\Model\ValidationsValidateRequestBody(); + $validations_validate_request_body->setOrder(new \OpenAPI\Client\Model\Order()); + $validations_validate_request_body->getOrder()->setStatus("CREATED"); + $validations_validate_request_body->getOrder()->setItems([$order_item]); + $validations_validate_request_body->setCustomer(new \OpenAPI\Client\Model\Customer()); + $validations_validate_request_body->getCustomer()->setSourceId($created_customer->getSourceId()); + $validations_validate_request_body_redeemables = []; + foreach ($applicable_promotion_tiers_ids as $promotion_tier_id) { + $redeemable = new \OpenAPI\Client\Model\StackableValidateRedeemBaseRedeemablesItem(); + $redeemable->setId($promotion_tier_id); + $redeemable->setObject("promotion_tier"); + array_push($validations_validate_request_body_redeemables, $redeemable); + } + $voucher_redeemable = new \OpenAPI\Client\Model\StackableValidateRedeemBaseRedeemablesItem(); + $voucher_redeemable->setId($campaign_voucher->getCode()); + $voucher_redeemable->setObject("voucher"); + array_push($validations_validate_request_body_redeemables, $voucher_redeemable); + $validations_validate_request_body->setRedeemables($validations_validate_request_body_redeemables); + $this->stackedDiscountsApiInstance->validateStackedDiscounts($validations_validate_request_body); + + // stacked redemption + $redemptions_redeem_request_body = new \OpenAPI\Client\Model\RedemptionsRedeemRequestBody(); + $redemptions_redeem_request_body->setOrder(new \OpenAPI\Client\Model\Order()); + $redemptions_redeem_request_body->getOrder()->setStatus("CREATED"); + $redemptions_redeem_request_body->getOrder()->setItems([$order_item]); + $redemptions_redeem_request_body->setCustomer(new \OpenAPI\Client\Model\Customer()); + $redemptions_redeem_request_body->getCustomer()->setSourceId($created_customer->getSourceId()); + $redemptions_redeem_request_body->setRedeemables($validations_validate_request_body_redeemables); + $this->stackedDiscountsApiInstance->redeemStackedDiscounts($redemptions_redeem_request_body); + } + + public function testListRedemptions() + { + $this->redemptionsApiInstance->listRedemptions(1, 1); + } +} diff --git a/__tests__/_output/.gitignore b/__tests__/_output/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/__tests__/_output/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/__tests__/index.php b/__tests__/index.php deleted file mode 100644 index 26c7006..0000000 --- a/__tests__/index.php +++ /dev/null @@ -1,324 +0,0 @@ -setApiKey('X-App-Id', $env["X_APP_ID"]); -$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('X-App-Token', $env["X_APP_TOKEN"]); -$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setHost($env["VOUCHERIFY_HOST"]); - -//create productsApi -$productsApiInstance = new OpenAPI\Client\Api\ProductsApi( - new GuzzleHttp\Client(), - $config -); - -//create variables to store products data -$created_product; -$created_product2; -//create Products -$products_create_request_body = new \OpenAPI\Client\Model\ProductsCreateRequestBody(); -$products_create_request_body->setSourceId(generateRandomString()); -$products_create_request_body->setName(generateRandomString()); -$products_create_request_body->setPrice(20000); -$products_create_request_body->setAttributes(["color", "memory", "processor"]); -try { - $created_product = $productsApiInstance->createProduct($products_create_request_body); - echo '
createProduct'; -} catch (Exception $e) { - echo 'Exception when calling ProductsApi->createProduct: ', $e->getMessage(), PHP_EOL; -} -$products_create_request_body->setSourceId(generateRandomString()); -$products_create_request_body->setName(generateRandomString()); -$products_create_request_body->setPrice(66000); -try { - $created_product2 = $productsApiInstance->createProduct($products_create_request_body); - echo '
' . json_encode($created_product, JSON_PRETTY_PRINT) . '
createProduct'; -} catch (Exception $e) { - echo 'Exception when calling ProductsApi->createProduct: ', $e->getMessage(), PHP_EOL; -} - -//create campaignsApi -$campaignsApiInstance = new OpenAPI\Client\Api\CampaignsApi( - new GuzzleHttp\Client(), - $config -); - -//create validationRulesApi -$validationRulesApiInstance = new OpenAPI\Client\Api\ValidationRulesApi( - new GuzzleHttp\Client(), - $config -); - -//create variable to store campaign data -$created_validation_rule; -//create validation rule -$validation_rules_create_request_body = new \OpenAPI\Client\Model\ValidationRulesCreateRequestBody(); -$applicable_to_created_product2 = new \OpenAPI\Client\Model\ApplicableTo(); -$applicable_to_created_product2->setProductId($created_product2->getId()); -$applicable_to_created_product2->setObject("product"); -$validation_rules_create_request_body->setApplicableTo(new \OpenAPI\Client\Model\ValidationRuleBaseApplicableTo()); -$validation_rules_create_request_body->getApplicableTo()->setIncluded([$applicable_to_created_product2]); -$validation_rules_create_request_body->setType("basic"); -$validation_rules_create_request_body->setName(generateRandomString()); -try { - echo '
' . json_encode($created_product, JSON_PRETTY_PRINT) . '
createValidationRules'; - $created_validation_rule = $validationRulesApiInstance->createValidationRules($validation_rules_create_request_body); - echo '
' . json_encode($validation_rules_create_request_body, JSON_PRETTY_PRINT) . '
createValidationRules'; -} catch (Exception $e) { - echo 'Exception when calling ValidationRulesApi->createValidationRules: ', $e->getMessage(), PHP_EOL; -} - -//create variable to store campaign data -$created_discount_campaign; -//createCampaign - DISCOUNT_COUPONS -$campaigns_create_request_body_discount = new \OpenAPI\Client\Model\CampaignsCreateRequestBody(); -$campaigns_create_request_body_discount->setName(generateRandomString(12)); -$campaigns_create_request_body_discount->setCampaignType("DISCOUNT_COUPONS"); -$campaigns_create_request_body_discount->setType("AUTO_UPDATE"); -$campaigns_create_request_body_discount->setVoucher(new \OpenAPI\Client\Model\CampaignsCreateRequestBodyVoucher()); -$campaigns_create_request_body_discount->getVoucher()->setType("DISCOUNT_VOUCHER"); -$campaigns_create_request_body_discount->getVoucher()->setDiscount(new \OpenAPI\Client\Model\Discount()); -$campaigns_create_request_body_discount->getVoucher()->getDiscount()->setType("AMOUNT"); -$campaigns_create_request_body_discount->getVoucher()->getDiscount()->setAmountOff(1000); -try { - $created_discount_campaign = $campaignsApiInstance->createCampaign($campaigns_create_request_body_discount); - echo '
' . json_encode($created_validation_rule, JSON_PRETTY_PRINT) . '
createCampaign - DISCOUNT_COUPONS'; -} catch (Exception $e) { - echo 'Exception when calling CampaignsApi->createCampaign: ', $e->getMessage(), PHP_EOL; -} - -//addVouchersToCampaign -$campaign_voucher; -try { - $campaign_voucher = $campaignsApiInstance->addVouchersToCampaign($created_discount_campaign->getId(), 1); - echo '
' . json_encode($created_discount_campaign, JSON_PRETTY_PRINT) . '
addVouchersToCampaign'; -} catch (Exception $e) { - echo 'Exception when calling CampaignsApi->getCampaign: ', $e->getMessage(), PHP_EOL; -} - -//create variable to store campaign data -$created_promotion_campaign; -//promotionTierCreateParams -$promotionTierCreateParams = new \OpenAPI\Client\Model\PromotionTierCreateParams(); -$promotionTierCreateParams->setName(generateRandomString()); -$promotionTierCreateParams->setBanner('testBanner'); -$promotionTierCreateParams->setAction(new \OpenAPI\Client\Model\PromotionTierAction()); -$promotionTierCreateParams->getAction()->setDiscount(new \OpenAPI\Client\Model\Discount()); -$promotionTierCreateParams->getAction()->getDiscount()->setType("AMOUNT"); -$promotionTierCreateParams->getAction()->getDiscount()->setAmountOff(1000); -//createCampaign - PROMOTION -$campaigns_create_request_body_promotion = new \OpenAPI\Client\Model\CampaignsCreateRequestBody(); -$campaigns_create_request_body_promotion->setName(generateRandomString(12)); -$campaigns_create_request_body_promotion->setCampaignType("PROMOTION"); -$campaigns_create_request_body_promotion->setPromotion(new \OpenAPI\Client\Model\CampaignsCreateRequestBodyPromotion()); -$campaigns_create_request_body_promotion->getPromotion()->setTiers([$promotionTierCreateParams]); -try { - $created_promotion_campaign = $campaignsApiInstance->createCampaign($campaigns_create_request_body_promotion); - echo '
' . json_encode($campaign_voucher, JSON_PRETTY_PRINT) . '
createCampaign - PROMOTION'; -} catch (Exception $e) { - echo 'Exception when calling CampaignsApi->createCampaign: ', $e->getMessage(), PHP_EOL; -} - - -//listCampaigns -$limit = 2; // int | A limit on the number of objects to be returned. Limit can range between 1 and 100 items. -$page = 1; // int | Which page of results to return. -$campaign_type = \OpenAPI\Client\Model\ParameterCampaignType::DISCOUNT_COUPONS->value; // ParameterCampaignType -$expand = \OpenAPI\Client\Model\ParameterExpandListCampaigns::CATEGORY->value; // ParameterExpandListCampaigns -$order = \OpenAPI\Client\Model\ParameterOrderListCampaigns::CREATED_AT2->value; // ParameterOrderListCampaigns -try { - $result = $campaignsApiInstance->listCampaigns($limit, $page, $campaign_type, $expand, $order); - echo '
' . json_encode($created_promotion_campaign, JSON_PRETTY_PRINT) . '
listCampaigns'; -} catch (Exception $e) { - echo 'Exception when calling CampaignsApi->listCampaigns: ', $e->getMessage(), PHP_EOL; -} - -//getCampaign -try { - $result = $campaignsApiInstance->getCampaign($created_discount_campaign->getId()); - echo '
' . json_encode($result, JSON_PRETTY_PRINT) . '
getCampaign'; - echo '
' . json_encode($result, JSON_PRETTY_PRINT) . '
getValidationRulesAssignments'; -} catch (Exception $e) { - echo 'Exception when calling CampaignsApi->getCampaign: ', $e->getMessage(), PHP_EOL; -} - -//create customersApi -$customersApiInstance = new OpenAPI\Client\Api\CustomersApi( - new GuzzleHttp\Client(), - $config -); - -//create variable to store customer data -$created_customer; - -//createCustomer -$customersCreateRequestBody = new \OpenAPI\Client\Model\CustomersCreateRequestBody(); -$customersCreateRequestBody->setSourceId('test123'); -$customersCreateRequestBody->setName('test123'); -$customersCreateRequestBody->setAddress(new \OpenAPI\Client\Model\CustomerBaseAddress()); -$customersCreateRequestBody->getAddress()->setCountry('US'); -$customersCreateRequestBody->getAddress()->setCity('Vice City'); -$customersCreateRequestBody->getAddress()->setLine1('123'); -$customersCreateRequestBody->getAddress()->setPostalCode('60089'); -try { - $created_customer = $customersApiInstance->createCustomer($customersCreateRequestBody); - echo '
' . json_encode($result->getValidationRulesAssignments(), JSON_PRETTY_PRINT) . '
createCustomer'; -} catch (Exception $e) { - echo 'Exception when calling CustomersApi->createCustomer: ', $e->getMessage(), PHP_EOL; -} - -//create qualificationsApi -$qualificationsApiInstance = new OpenAPI\Client\Api\QualificationsApi( - new GuzzleHttp\Client(), - $config -); - -//create variable to store checkEligibility result data -$check_eligibility_result; - - -//create orderItem -$order_item = new \OpenAPI\Client\Model\OrderItem(); -$order_item->setPrice($created_product->getPrice()); -$order_item->setQuantity(3); -$order_item->setProductId($created_product->getId()); - -//qualifications_check_eligibility_request_body -$qualifications_check_eligibility_request_body = new \OpenAPI\Client\Model\QualificationsCheckEligibilityRequestBody(); -$qualifications_check_eligibility_request_body->setCustomer(new \OpenAPI\Client\Model\Customer()); -$qualifications_check_eligibility_request_body->getCustomer()->setId($created_customer->getId()); -$qualifications_check_eligibility_request_body->setOrder(new \OpenAPI\Client\Model\Order()); -$qualifications_check_eligibility_request_body->getOrder()->setStatus("CREATED"); -$qualifications_check_eligibility_request_body->getOrder()->setItems([$order_item]); -$qualifications_check_eligibility_request_body->setMode("BASIC"); -$qualifications_check_eligibility_request_body->setScenario("ALL"); -try { - $check_eligibility_result = $qualificationsApiInstance->checkEligibility($qualifications_check_eligibility_request_body); - echo '
' . json_encode($created_customer, JSON_PRETTY_PRINT) . '
checkEligibility'; -} catch (Exception $e) { - echo 'Exception when calling CustomersApi->createCustomer: ', $e->getMessage(), PHP_EOL; -} -$applicable_promotion_tiers = array_slice(array_filter($check_eligibility_result->getRedeemables()->getData(), function ($redeemable) { - return $redeemable->getObject() === 'promotion_tier'; -}), 0, 3); -$applicable_promotion_tiers_ids = array_map(function ($promotion_tier) { - return $promotion_tier->getId(); -}, $applicable_promotion_tiers); - -//create stackableDiscountsApi -$stackedDiscountsApiInstance = new OpenAPI\Client\Api\StackableDiscountsApi( - new GuzzleHttp\Client(), - $config -); - -//validateStackedDiscounts -$validations_validate_request_body = new \OpenAPI\Client\Model\ValidationsValidateRequestBody(); -$validations_validate_request_body->setOrder(new \OpenAPI\Client\Model\Order()); -$validations_validate_request_body->getOrder()->setStatus("CREATED"); -$validations_validate_request_body->getOrder()->setItems([$order_item]); -$validations_validate_request_body->setCustomer(new \OpenAPI\Client\Model\Customer()); -$validations_validate_request_body->getCustomer()->setSourceId($created_customer->getSourceId()); -$validations_validate_request_body_redeemables = []; -foreach ($applicable_promotion_tiers_ids as $promotion_tier_id) { - $redeemable = new \OpenAPI\Client\Model\StackableValidateRedeemBaseRedeemablesItem(); - $redeemable->setId($promotion_tier_id); - $redeemable->setObject("promotion_tier"); - array_push($validations_validate_request_body_redeemables, $redeemable); -} -$voucher_redeemable = new \OpenAPI\Client\Model\StackableValidateRedeemBaseRedeemablesItem(); -$voucher_redeemable->setId($campaign_voucher->getCode()); -$voucher_redeemable->setObject("voucher"); -array_push($validations_validate_request_body_redeemables, $voucher_redeemable); -$validations_validate_request_body->setRedeemables($validations_validate_request_body_redeemables); -try { - $result = $stackedDiscountsApiInstance->validateStackedDiscounts($validations_validate_request_body); - echo '
' . json_encode($check_eligibility_result, JSON_PRETTY_PRINT) . '
validateStackedDiscounts'; - $result->getOrder(); - echo '
' . json_encode($result, JSON_PRETTY_PRINT) . '
order.amount = ' . $result->getOrder()->getAmount() . ''; -} catch (Exception $e) { - echo 'Exception when calling StackableDiscountsApi->redeemStackedDiscounts: ', $e->getMessage(), PHP_EOL; -} - -//validateStackedDiscounts -$redemptions_redeem_request_body = new \OpenAPI\Client\Model\RedemptionsRedeemRequestBody(); -$redemptions_redeem_request_body->setOrder(new \OpenAPI\Client\Model\Order()); -$redemptions_redeem_request_body->getOrder()->setStatus("CREATED"); -$redemptions_redeem_request_body->getOrder()->setItems([$order_item]); -$redemptions_redeem_request_body->setCustomer(new \OpenAPI\Client\Model\Customer()); -$redemptions_redeem_request_body->getCustomer()->setSourceId($created_customer->getSourceId()); -$redemptions_redeem_request_body->setRedeemables($validations_validate_request_body_redeemables); -try { - $result = $stackedDiscountsApiInstance->redeemStackedDiscounts($redemptions_redeem_request_body); - echo '
order.total_discount_amount = ' . $result->getOrder()->getTotalDiscountAmount() - . '
order.total_amount = ' . $result->getOrder()->getTotalAmount() . '
redeemStackedDiscounts'; - $result->getOrder(); - echo '
' . json_encode($result, JSON_PRETTY_PRINT) . '
order.amount = ' . $result->getOrder()->getAmount() . ''; -} catch (Exception $e) { - echo 'Exception when calling StackableDiscountsApi->redeemStackedDiscounts: ', $e->getMessage(), PHP_EOL; -} - -//create redemptionsApi -$redemptionsApiInstance = new OpenAPI\Client\Api\RedemptionsApi( - new GuzzleHttp\Client(), - $config -); - -//listRedemptions -try { - $result = $redemptionsApiInstance->listRedemptions(1, 1); - echo '
order.total_discount_amount = ' . $result->getOrder()->getTotalDiscountAmount() - . '
order.total_amount = ' . $result->getOrder()->getTotalAmount() . '
listRedemptions'; -} catch (Exception $e) { - echo 'Exception when calling RedemptionsApi->listRedemptions: ', $e->getMessage(), PHP_EOL; -} - - -//deleteCampaign - DISCOUNT_COUPONS -try { - $result = $campaignsApiInstance->deleteCampaign($created_discount_campaign->getId(), true); - echo '
' . json_encode($result, JSON_PRETTY_PRINT) . '
deleteCampaign - DISCOUNT_COUPONS'; -} catch (Exception $e) { - echo 'Exception when calling CampaignsApi->listCampaigns: ', $e->getMessage(), PHP_EOL; -} - -//deleteCampaign - PROMOTION -try { - $result = $campaignsApiInstance->deleteCampaign($created_promotion_campaign->getId(), true); - echo '
' . json_encode($result, JSON_PRETTY_PRINT) . '
deleteCampaign - PROMOTION'; -} catch (Exception $e) { - echo 'Exception when calling CampaignsApi->listCampaigns: ', $e->getMessage(), PHP_EOL; -} - -//deleteCustomer -try { - $result = $customersApiInstance->deleteCustomer($created_customer->getId()); - echo '
' . json_encode($result, JSON_PRETTY_PRINT) . '
deleteCustomer'; -} catch (Exception $e) { - echo 'Exception when calling CustomersApi->createCustomer: ', $e->getMessage(), PHP_EOL; -} - -//deleteProduct -try { - $productsApiInstance->deleteProduct($created_product->getId(), true); - echo '
' . json_encode($result, JSON_PRETTY_PRINT) . '
deleteProduct'; -} catch (Exception $e) { - echo 'Exception when calling ProductsApi->deleteProduct: ', $e->getMessage(), PHP_EOL; -} diff --git a/codeception.yml b/codeception.yml new file mode 100644 index 0000000..1aa6253 --- /dev/null +++ b/codeception.yml @@ -0,0 +1,12 @@ +namespace: Tests +support_namespace: Support +paths: + tests: __tests__ + output: __tests__/_output + data: __tests__/Support/Data + support: __tests__/Support + envs: .env +actor_suffix: Tester +extensions: + enabled: + - Codeception\Extension\RunFailed diff --git a/composer.json b/composer.json index d4d5dd5..77d14ce 100644 --- a/composer.json +++ b/composer.json @@ -26,15 +26,19 @@ "guzzlehttp/psr7": "^2.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.5", - "overtrue/phplint": "^9.0", - "phpunit/phpunit": "^9.0" + "codeception/codeception": "^5.0", + "codeception/module-phpbrowser": "*", + "codeception/module-asserts": "*" }, "autoload": { - "psr-4": { "OpenAPI\\Client\\" : "src/" } + "psr-4": { + "OpenAPI\\Client\\": "src/" + } }, "autoload-dev": { - "psr-4": { "OpenAPI\\Client\\Test\\" : "tests/" } + "psr-4": { + "OpenAPI\\Client\\Test\\": "tests/" + } }, "scripts": { "test": [ @@ -42,4 +46,4 @@ ], "phplint": "phplint" } -} +} \ No newline at end of file From d9869f4f8c230b8d905c570a8d7e0149c20a57c7 Mon Sep 17 00:00:00 2001 From: p-zielinski <82354717+p-zielinski@users.noreply.github.com> Date: Wed, 24 Jan 2024 16:34:21 +0100 Subject: [PATCH 2/7] requested changes and readme --- .gitignore | 3 ++ __tests__/Acceptance.suite.yml | 16 -------- __tests__/Functional.suite.yml | 12 ------ __tests__/README_TESTS.md | 54 +++++++++++++++++++++++++ __tests__/Support/AcceptanceTester.php | 29 ------------- __tests__/Support/Data/.gitkeep | 0 __tests__/Support/FunctionalTester.php | 29 ------------- __tests__/Support/_generated/.gitignore | 2 - __tests__/{Support => }/UnitTester.php | 0 __tests__/_output/.gitignore | 2 - codeception.yml | 4 +- composer.json | 3 +- 12 files changed, 60 insertions(+), 94 deletions(-) delete mode 100644 __tests__/Acceptance.suite.yml delete mode 100644 __tests__/Functional.suite.yml create mode 100644 __tests__/README_TESTS.md delete mode 100644 __tests__/Support/AcceptanceTester.php delete mode 100644 __tests__/Support/Data/.gitkeep delete mode 100644 __tests__/Support/FunctionalTester.php delete mode 100644 __tests__/Support/_generated/.gitignore rename __tests__/{Support => }/UnitTester.php (100%) delete mode 100644 __tests__/_output/.gitignore diff --git a/.gitignore b/.gitignore index d3987a5..a48827e 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,6 @@ build/phplint.cache /test-requirements.txt /.env /.idea/ +/__tests__/_generated/ +/__tests__/_output/ +/__tests__/Support/ diff --git a/__tests__/Acceptance.suite.yml b/__tests__/Acceptance.suite.yml deleted file mode 100644 index 5b10b9d..0000000 --- a/__tests__/Acceptance.suite.yml +++ /dev/null @@ -1,16 +0,0 @@ -# Codeception Test Suite Configuration -# -# Suite for acceptance tests. -# Perform tests in browser using the WebDriver or PhpBrowser. -# If you need both WebDriver and PHPBrowser tests - create a separate suite. - -actor: AcceptanceTester -modules: - enabled: - - PhpBrowser: - url: http://localhost/myapp -# add Codeception\Step\Retry trait to AcceptanceTester to enable retries -step_decorators: - - Codeception\Step\ConditionalAssertion - - Codeception\Step\TryTo - - Codeception\Step\Retry diff --git a/__tests__/Functional.suite.yml b/__tests__/Functional.suite.yml deleted file mode 100644 index 1b7276a..0000000 --- a/__tests__/Functional.suite.yml +++ /dev/null @@ -1,12 +0,0 @@ -# Codeception Test Suite Configuration -# -# Suite for functional tests -# Emulate web requests and make application process them -# Include one of framework modules (Symfony, Yii2, Laravel, Phalcon5) to use it -# Remove this suite if you don't use frameworks - -actor: FunctionalTester -modules: - enabled: - # add a framework module here -step_decorators: ~ diff --git a/__tests__/README_TESTS.md b/__tests__/README_TESTS.md new file mode 100644 index 0000000..9588f48 --- /dev/null +++ b/__tests__/README_TESTS.md @@ -0,0 +1,54 @@ +## Tool: +We use `codeception` for testing. You shall have installed `codeception` via `composer` in order to run the tests. + + +## How to write tests: +All tests shall be in `__tests__/Unit/` folder. And shall be named as `*Test.php`. All test functions shall be named as `test*` (see example). + +Import sdk: + +```php +require_once(dirname(dirname(__DIR__)) . '/vendor/autoload.php'); +``` + +Good practice is to use `_before` to configure sdks APIs: +```php + + //example + protected function _before() + { + //load .env + $env = parse_ini_file('.env'); + + $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('X-App-Id', $env["X_APP_ID"]); + $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('X-App-Token', $env["X_APP_TOKEN"]); + $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setHost($env["VOUCHERIFY_HOST"]); + + $this->productsApiInstance = new OpenAPI\Client\Api\ProductsApi( + new GuzzleHttp\Client(), + $config + ); + $this->campaignsApiInstance = new OpenAPI\Client\Api\CampaignsApi( + new GuzzleHttp\Client(), + $config + ); + $this->redemptionsApiInstance = new OpenAPI\Client\Api\RedemptionsApi( + new GuzzleHttp\Client(), + $config + ); + } + +``` + +Test example: +```php + //example + public function testListRedemptions() + { + $this->redemptionsApiInstance->listRedemptions(1, 1); + } +``` + + +## Environment variables: +`.env` file shall be in the root of the project and shall contain `X_APP_ID`, `X_APP_TOKEN` and `VOUCHERIFY_HOST` variables. Check `.env.example` for reference. diff --git a/__tests__/Support/AcceptanceTester.php b/__tests__/Support/AcceptanceTester.php deleted file mode 100644 index 6ccbabd..0000000 --- a/__tests__/Support/AcceptanceTester.php +++ /dev/null @@ -1,29 +0,0 @@ - Date: Wed, 24 Jan 2024 16:35:43 +0100 Subject: [PATCH 3/7] Update README_TESTS.md --- __tests__/README_TESTS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/__tests__/README_TESTS.md b/__tests__/README_TESTS.md index 9588f48..34d0364 100644 --- a/__tests__/README_TESTS.md +++ b/__tests__/README_TESTS.md @@ -1,6 +1,8 @@ ## Tool: We use `codeception` for testing. You shall have installed `codeception` via `composer` in order to run the tests. +`__tests__/UnitTester.php` is used to configure `codeception` and to load `vendor/autoload.php` file. Please do not modify this file! + ## How to write tests: All tests shall be in `__tests__/Unit/` folder. And shall be named as `*Test.php`. All test functions shall be named as `test*` (see example). From 279f49d5ab996d77a452ef492ee2f8ee97f28d19 Mon Sep 17 00:00:00 2001 From: p-zielinski <82354717+p-zielinski@users.noreply.github.com> Date: Wed, 24 Jan 2024 16:39:39 +0100 Subject: [PATCH 4/7] Update README_TESTS.md --- __tests__/README_TESTS.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/__tests__/README_TESTS.md b/__tests__/README_TESTS.md index 34d0364..ed7efcd 100644 --- a/__tests__/README_TESTS.md +++ b/__tests__/README_TESTS.md @@ -54,3 +54,8 @@ Test example: ## Environment variables: `.env` file shall be in the root of the project and shall contain `X_APP_ID`, `X_APP_TOKEN` and `VOUCHERIFY_HOST` variables. Check `.env.example` for reference. + +## Run tests: +```bash +php vendor/bin/codecept run Unit +``` From 65a30631538519924bffd5407a1e41a1664d24c2 Mon Sep 17 00:00:00 2001 From: p-zielinski <82354717+p-zielinski@users.noreply.github.com> Date: Wed, 24 Jan 2024 16:40:15 +0100 Subject: [PATCH 5/7] Update README_TESTS.md --- __tests__/README_TESTS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/__tests__/README_TESTS.md b/__tests__/README_TESTS.md index ed7efcd..c1e4167 100644 --- a/__tests__/README_TESTS.md +++ b/__tests__/README_TESTS.md @@ -56,6 +56,8 @@ Test example: `.env` file shall be in the root of the project and shall contain `X_APP_ID`, `X_APP_TOKEN` and `VOUCHERIFY_HOST` variables. Check `.env.example` for reference. ## Run tests: + +From the root of the project run: ```bash php vendor/bin/codecept run Unit ``` From 361df6ce4a0b00d4fef4f29f5451e3155a7c944d Mon Sep 17 00:00:00 2001 From: p-zielinski <82354717+p-zielinski@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:12:59 +0100 Subject: [PATCH 6/7] requested changes --- README.md | 2 +- __tests__/README_TESTS.md | 6 +++--- __tests__/Unit/MainTest.php | 4 ++++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 074c8f8..1280524 100644 --- a/README.md +++ b/README.md @@ -873,7 +873,7 @@ Class | Method | HTTP request | Description 1. Copy `.env.example` to `.env` and fill in the values. 2. Run `docker build -t php .` to build the image. -3. Run `docker run -p 5050:5050 --rm php` to run the tests and delete container immediately after. +3. Run `docker run --rm php` to run the tests and delete container immediately after. ## Author diff --git a/__tests__/README_TESTS.md b/__tests__/README_TESTS.md index c1e4167..e8687b2 100644 --- a/__tests__/README_TESTS.md +++ b/__tests__/README_TESTS.md @@ -1,11 +1,11 @@ ## Tool: -We use `codeception` for testing. You shall have installed `codeception` via `composer` in order to run the tests. +We use `codeception` for testing. You should have installed `codeception` via `composer` in order to run the tests. `__tests__/UnitTester.php` is used to configure `codeception` and to load `vendor/autoload.php` file. Please do not modify this file! ## How to write tests: -All tests shall be in `__tests__/Unit/` folder. And shall be named as `*Test.php`. All test functions shall be named as `test*` (see example). +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). Import sdk: @@ -53,7 +53,7 @@ Test example: ## Environment variables: -`.env` file shall be in the root of the project and shall contain `X_APP_ID`, `X_APP_TOKEN` and `VOUCHERIFY_HOST` variables. Check `.env.example` for reference. +`.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. ## Run tests: diff --git a/__tests__/Unit/MainTest.php b/__tests__/Unit/MainTest.php index 38fc482..75358de 100644 --- a/__tests__/Unit/MainTest.php +++ b/__tests__/Unit/MainTest.php @@ -5,6 +5,10 @@ //test class MainTest extends \Codeception\Test\Unit { + /** + * @TODO, refactor test + */ + protected $productsApiInstance; protected $campaignsApiInstance; protected $validationRulesApiInstance; From cfc10bb392d55d570731bb1294ca9dc213ae4115 Mon Sep 17 00:00:00 2001 From: p-zielinski <82354717+p-zielinski@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:30:53 +0100 Subject: [PATCH 7/7] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3d3529d..69b5f76 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "guzzlehttp/psr7": "^2.0" }, "require-dev": { - "codeception/codeception": "^5.0", + "codeception/codeception": "5.0.13", "codeception/module-asserts": "*" }, "autoload": {
' . json_encode($result, JSON_PRETTY_PRINT) . '