123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Quote\Api;
- use Magento\TestFramework\TestCase\WebapiAbstract;
- class CouponManagementTest extends WebapiAbstract
- {
- const SERVICE_VERSION = 'V1';
- const SERVICE_NAME = 'quoteCouponManagementV1';
- const RESOURCE_PATH = '/V1/carts/';
- /**
- * @var \Magento\TestFramework\ObjectManager
- */
- protected $objectManager;
- protected function setUp()
- {
- $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php
- */
- public function testGet()
- {
- /** @var \Magento\Quote\Model\Quote $quote */
- $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
- $quote->load('test_order_1', 'reserved_order_id');
- $cartId = $quote->getId();
- $couponCode = $quote->getCouponCode();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons/' ,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Get',
- ],
- ];
- $requestData = ["cartId" => $cartId];
- $this->assertEquals($couponCode, $this->_webApiCall($serviceInfo, $requestData));
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php
- */
- public function testDelete()
- {
- /** @var \Magento\Quote\Model\Quote $quote */
- $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
- $quote->load('test_order_1', 'reserved_order_id');
- $cartId = $quote->getId();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Remove',
- ],
- ];
- $requestData = ["cartId" => $cartId];
- $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
- $quote->load('test_order_1', 'reserved_order_id');
- $this->assertEquals('', $quote->getCouponCode());
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
- * @expectedException \Exception
- * @expectedExceptionMessage The coupon code isn't valid. Verify the code and try again.
- */
- public function testSetCouponThrowsExceptionIfCouponDoesNotExist()
- {
- /** @var \Magento\Quote\Model\Quote $quote */
- $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
- $quote->load('test_order_1', 'reserved_order_id');
- $cartId = $quote->getId();
- $couponCode = 'invalid_coupon_code';
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons/' . $couponCode,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Set',
- ],
- ];
- $requestData = [
- "cartId" => $cartId,
- "couponCode" => $couponCode,
- ];
- $this->_webApiCall($serviceInfo, $requestData);
- }
- /**
- * @magentoApiDataFixture Magento/Sales/_files/quote.php
- * @magentoApiDataFixture Magento/Checkout/_files/discount_10percent.php
- */
- public function testSetCouponSuccess()
- {
- /** @var \Magento\Quote\Model\Quote $quote */
- $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
- $quote->load('test01', 'reserved_order_id');
- $cartId = $quote->getId();
- /** @var \Magento\SalesRule\Model\Rule $salesRule */
- $salesRule = $this->objectManager->create(\Magento\SalesRule\Model\Rule::class);
- $salesRuleId = $this->objectManager->get(\Magento\Framework\Registry::class)
- ->registry('Magento/Checkout/_file/discount_10percent');
- $salesRule->load($salesRuleId);
- $couponCode = $salesRule->getPrimaryCoupon()->getCode();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . $cartId . '/coupons/' . $couponCode,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'serviceVersion' => self::SERVICE_VERSION,
- 'operation' => self::SERVICE_NAME . 'Set',
- ],
- ];
- $requestData = [
- "cartId" => $cartId,
- "couponCode" => $couponCode,
- ];
- $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
- $quoteWithCoupon = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
- $quoteWithCoupon->load('test01', 'reserved_order_id');
- $this->assertEquals($quoteWithCoupon->getCouponCode(), $couponCode);
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php
- */
- public function testGetMyCoupon()
- {
- $this->_markTestAsRestOnly();
- // get customer ID token
- /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
- $customerTokenService = $this->objectManager->create(
- \Magento\Integration\Api\CustomerTokenServiceInterface::class
- );
- $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
- /** @var \Magento\Quote\Model\Quote $quote */
- $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
- $quote->load('test_order_1', 'reserved_order_id');
- $couponCode = $quote->getCouponCode();
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons' ,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
- 'token' => $token,
- ],
- ];
- $requestData = [];
- $this->assertEquals($couponCode, $this->_webApiCall($serviceInfo, $requestData));
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_coupon_saved.php
- */
- public function testDeleteMyCoupon()
- {
- $this->_markTestAsRestOnly();
- // get customer ID token
- /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
- $customerTokenService = $this->objectManager->create(
- \Magento\Integration\Api\CustomerTokenServiceInterface::class
- );
- $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
- /** @var \Magento\Quote\Model\Quote $quote */
- $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
- $quote->load('test_order_1', 'reserved_order_id');
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons',
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
- 'token' => $token,
- ],
- ];
- $requestData = [];
- $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
- $quote->load('test_order_1', 'reserved_order_id');
- $this->assertEquals('', $quote->getCouponCode());
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
- * @expectedException \Exception
- * @expectedExceptionMessage The coupon code isn't valid. Verify the code and try again.
- */
- public function testSetMyCouponThrowsExceptionIfCouponDoesNotExist()
- {
- $this->_markTestAsRestOnly();
- // get customer ID token
- /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
- $customerTokenService = $this->objectManager->create(
- \Magento\Integration\Api\CustomerTokenServiceInterface::class
- );
- $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
- $couponCode = 'invalid_coupon_code';
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons/' . $couponCode,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- 'token' => $token,
- ],
- ];
- $requestData = [
- "couponCode" => $couponCode,
- ];
- $this->_webApiCall($serviceInfo, $requestData);
- }
- /**
- * @magentoApiDataFixture Magento/Customer/_files/customer.php
- * @magentoApiDataFixture Magento/Sales/_files/quote.php
- * @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
- */
- public function testSetMyCouponSuccess()
- {
- $this->_markTestAsRestOnly();
- // get customer ID token
- /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
- $customerTokenService = $this->objectManager->create(
- \Magento\Integration\Api\CustomerTokenServiceInterface::class
- );
- $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
- /** @var \Magento\Quote\Model\Quote $quote */
- $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
- $quote->load('test01', 'reserved_order_id');
- $cartId = $quote->getId();
- /** @var \Magento\SalesRule\Model\Rule $salesRule */
- $salesRule = $this->objectManager->create(\Magento\SalesRule\Model\Rule::class);
- $salesRuleId = $this->objectManager->get(\Magento\Framework\Registry::class)
- ->registry('Magento/Checkout/_file/discount_10percent_generalusers');
- $salesRule->load($salesRuleId);
- $couponCode = $salesRule->getPrimaryCoupon()->getCode();
- /* Since this isn't a full quote fixture, need to assign it to the right customer */
- $cartManagementService = $this->objectManager->create(
- \Magento\Quote\Api\CartManagementInterface::class
- );
- $cartManagementService->assignCustomer($cartId, 1, 1);
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . 'mine/coupons/' . $couponCode,
- 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
- 'token' => $token,
- ],
- ];
- $requestData = [
- "couponCode" => $couponCode,
- ];
- $this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
- $quoteWithCoupon = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
- $quoteWithCoupon->load('test01', 'reserved_order_id');
- $this->assertEquals($quoteWithCoupon->getCouponCode(), $couponCode);
- }
- }
|