ParamOverriderCartIdTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model\Webapi;
  7. use Magento\Authorization\Model\UserContextInterface;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. use Magento\Quote\Api\CartManagementInterface;
  11. use Magento\Quote\Model\Webapi\ParamOverriderCartId;
  12. /**
  13. * Test for \Magento\Quote\Model\Webapi\ParamOverriderCartId
  14. */
  15. class ParamOverriderCartIdTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var ParamOverriderCartId
  19. */
  20. private $model;
  21. /**
  22. * @var UserContextInterface
  23. */
  24. private $userContext;
  25. protected function setUp()
  26. {
  27. $this->userContext = $this->getMockBuilder(\Magento\Authorization\Model\UserContextInterface::class)
  28. ->getMockForAbstractClass();
  29. $this->cartManagement = $this->getMockBuilder(\Magento\Quote\Api\CartManagementInterface::class)
  30. ->getMockForAbstractClass();
  31. $this->model = (new ObjectManager($this))->getObject(
  32. \Magento\Quote\Model\Webapi\ParamOverriderCartId::class,
  33. [
  34. 'userContext' => $this->userContext,
  35. 'cartManagement' => $this->cartManagement,
  36. ]
  37. );
  38. }
  39. public function testGetOverriddenValueIsCustomerAndCartExists()
  40. {
  41. $retValue = 'retValue';
  42. $customerId = 1;
  43. $this->userContext->expects($this->once())
  44. ->method('getUserType')
  45. ->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER));
  46. $this->userContext->expects($this->once())
  47. ->method('getUserId')
  48. ->will($this->returnValue($customerId));
  49. $cart = $this->getMockBuilder(\Magento\Quote\Api\Data\CartInterface::class)
  50. ->getMockForAbstractClass();
  51. $this->cartManagement->expects($this->once())
  52. ->method('getCartForCustomer')
  53. ->with($customerId)
  54. ->will($this->returnValue($cart));
  55. $cart->expects($this->once())
  56. ->method('getId')
  57. ->will($this->returnValue($retValue));
  58. $this->assertSame($retValue, $this->model->getOverriddenValue());
  59. }
  60. /**
  61. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  62. */
  63. public function testGetOverriddenValueIsCustomerAndCartDoesNotExist()
  64. {
  65. $customerId = 1;
  66. $this->userContext->expects($this->once())
  67. ->method('getUserType')
  68. ->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER));
  69. $this->userContext->expects($this->once())
  70. ->method('getUserId')
  71. ->will($this->returnValue($customerId));
  72. $this->cartManagement->expects($this->once())
  73. ->method('getCartForCustomer')
  74. ->with($customerId)
  75. ->will($this->throwException(new NoSuchEntityException()));
  76. $this->model->getOverriddenValue();
  77. }
  78. public function testGetOverriddenValueIsCustomerAndCartIsNull()
  79. {
  80. $customerId = 1;
  81. $this->userContext->expects($this->once())
  82. ->method('getUserType')
  83. ->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER));
  84. $this->userContext->expects($this->once())
  85. ->method('getUserId')
  86. ->will($this->returnValue($customerId));
  87. $this->cartManagement->expects($this->once())
  88. ->method('getCartForCustomer')
  89. ->with($customerId)
  90. ->will($this->returnValue(null));
  91. $this->assertNull($this->model->getOverriddenValue());
  92. }
  93. public function testGetOverriddenValueIsNotCustomer()
  94. {
  95. $this->userContext->expects($this->once())
  96. ->method('getUserType')
  97. ->will($this->returnValue(UserContextInterface::USER_TYPE_ADMIN));
  98. $this->assertNull($this->model->getOverriddenValue());
  99. }
  100. }