Skip to content

Commit 6c795cf

Browse files
author
Bryant Luk
committed
MAGETWO-36189: Customer-facing resources in Sales & Checkout APIs - "/mine" APIs
- Add unit tests for ParamOverriders - Fix behavior for ParamOverriderCartId if NoSuchEntityException is thrown
1 parent f446b5f commit 6c795cf

File tree

4 files changed

+199
-8
lines changed

4 files changed

+199
-8
lines changed

app/code/Magento/Quote/Model/Webapi/ParamOverriderCartId.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Authorization\Model\UserContextInterface;
1010
use Magento\Framework\Webapi\Rest\Request\ParamOverriderInterface;
11+
use Magento\Framework\Exception\NoSuchEntityException;
1112
use Magento\Quote\Api\CartManagementInterface;
1213

1314
/**
@@ -35,14 +36,18 @@ public function __construct(
3536

3637
public function getOverridenValue()
3738
{
38-
if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER) {
39-
$customerId = $this->userContext->getUserId();
39+
try {
40+
if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER) {
41+
$customerId = $this->userContext->getUserId();
4042

41-
/** @var \Magento\Quote\Api\Data\CartInterface */
42-
$cart = $this->cartManagement->getCartForCustomer($customerId);
43-
if ($cart) {
44-
return $cart->getId();
43+
/** @var \Magento\Quote\Api\Data\CartInterface */
44+
$cart = $this->cartManagement->getCartForCustomer($customerId);
45+
if ($cart) {
46+
return $cart->getId();
47+
}
4548
}
49+
} catch (NoSuchEntityException $e) {
50+
/* do nothing and just return null */
4651
}
4752
return null;
4853
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Webapi\Test\Unit\Controller\Rest;
8+
9+
use Magento\Authorization\Model\UserContextInterface;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Webapi\Controller\Rest\ParamOverriderCustomerId;
12+
13+
class ParamOverriderCustomerIdTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var ParamOverriderCustomerId
17+
*/
18+
private $model;
19+
20+
/**
21+
* @var UserContextInterface
22+
*/
23+
private $userContext;
24+
25+
public function setUp()
26+
{
27+
$this->userContext = $this->getMockBuilder('Magento\Authorization\Model\UserContextInterface')
28+
->getMockForAbstractClass();
29+
$this->model = (new ObjectManager($this))->getObject(
30+
'Magento\Webapi\Controller\Rest\ParamOverriderCustomerId',
31+
[
32+
'userContext' => $this->userContext
33+
]
34+
);
35+
}
36+
37+
public function testGetOverridenValueIsCustomer()
38+
{
39+
$retValue = 'retValue';
40+
41+
$this->userContext->expects($this->once())
42+
->method('getUserType')
43+
->will($this->returnValue(UserContextInterface::USER_TYPE_CUSTOMER));
44+
$this->userContext->expects($this->once())
45+
->method('getUserId')
46+
->will($this->returnValue($retValue));
47+
48+
$this->assertSame($retValue, $this->model->getOverridenValue());
49+
}
50+
51+
public function testGetOverridenValueIsNotCustomer()
52+
{
53+
$this->userContext->expects($this->once())
54+
->method('getUserType')
55+
->will($this->returnValue(UserContextInterface::USER_TYPE_ADMIN));
56+
57+
$this->assertNull($this->model->getOverridenValue());
58+
}
59+
}

app/code/Magento/Webapi/Test/Unit/Controller/Rest/ParamsOverriderTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ public function testOverrideParams($requestData, $parameters, $expectedOverridde
3131
$userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($userId));
3232
$userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($userType));
3333

34+
$paramOverriderCustomerId = $objectManager->getObject(
35+
'Magento\Webapi\Controller\Rest\ParamOverriderCustomerId',
36+
['userContext' => $userContextMock]
37+
);
38+
3439
/** @var \Magento\Webapi\Controller\Rest\ParamsOverrider $paramsOverrider */
3540
$paramsOverrider = $objectManager->getObject(
3641
'Magento\Webapi\Controller\Rest\ParamsOverrider',
37-
['userContext' => $userContextMock]
42+
['paramOverriders' => ['%customer_id%' => $paramOverriderCustomerId ]]
3843
);
3944

4045
$this->assertEquals($expectedOverriddenParams, $paramsOverrider->override($requestData, $parameters));
@@ -84,7 +89,7 @@ public function overrideParamsDataProvider()
8489
'force true, value present, override value is %customer_id%, not a customer' => [
8590
['Name1' => 'valueIn'],
8691
['Name1' => ['force' => true, 'value' => '%customer_id%']],
87-
['Name1' => '%customer_id%'],
92+
['Name1' => null],
8893
1234,
8994
UserContextInterface::USER_TYPE_INTEGRATION,
9095
],

0 commit comments

Comments
 (0)