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