Skip to content

Commit f0c3b5c

Browse files
committed
MAGETWO-36382: Magento\Quote\Api\GuestBillingAddressManagement
- added unit tests - refactored code to use mock helper
1 parent 709ae39 commit f0c3b5c

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Quote\Test\Unit\Model\GuestCart;
8+
9+
use Magento\Quote\Test\Unit\Model\GuestCart\GuestCartTestHelper;
10+
11+
class GuestBillingAddressManagementTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var \Magento\Quote\Model\GuestCart\GuestBillingAddressManagement
15+
*/
16+
protected $model;
17+
18+
/**
19+
* @var \PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
protected $quoteRepositoryMock;
22+
23+
/**
24+
* @var \PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $validatorMock;
27+
28+
/**
29+
* @var \PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $quoteIdMaskFactoryMock;
32+
33+
/**
34+
* @var \PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $quoteIdMaskMock;
37+
38+
/**
39+
* @var string
40+
*/
41+
protected $maskedCartId;
42+
43+
/**
44+
* @var int
45+
*/
46+
protected $cartId;
47+
48+
/**
49+
* @return void
50+
*/
51+
protected function setUp()
52+
{
53+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
54+
$this->quoteRepositoryMock = $this->getMock('\Magento\Quote\Model\QuoteRepository', [], [], '', false);
55+
$this->validatorMock = $this->getMock('\Magento\Quote\Model\QuoteAddressValidator', [], [], '', false);
56+
$logger = $this->getMock('\Psr\Log\LoggerInterface');
57+
$this->quoteIdMaskFactoryMock = $this->getMock('Magento\Quote\Model\QuoteIdMaskFactory', [], [], '', false);
58+
$this->quoteIdMaskMock = $this->getMock('Magento\Quote\Model\QuoteIdMask', [], [], '', false);
59+
60+
$this->maskedCartId = 'f216207248d65c789b17be8545e0aa73';
61+
$this->cartId = 11;
62+
63+
$guestCartTestHelper = new GuestCartTestHelper($this);
64+
list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask(
65+
$this->maskedCartId,
66+
$this->cartId
67+
);
68+
69+
$this->model = $objectManager->getObject(
70+
'Magento\Quote\Model\GuestCart\GuestBillingAddressManagement',
71+
[
72+
'quoteRepository' => $this->quoteRepositoryMock,
73+
'addressValidator' => $this->validatorMock,
74+
'logger' => $logger,
75+
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock
76+
]
77+
);
78+
}
79+
80+
/**
81+
* @return void
82+
*/
83+
public function testGetAddress()
84+
{
85+
$quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false);
86+
$this->quoteRepositoryMock->expects($this->once())->method('getActive')
87+
->with($this->cartId )->will($this->returnValue($quoteMock));
88+
89+
$addressMock = $this->getMock('\Magento\Quote\Model\Quote\Address', [], [], '', false);
90+
$quoteMock->expects($this->any())->method('getBillingAddress')->will($this->returnValue($addressMock));
91+
92+
$this->assertEquals($addressMock, $this->model->get($this->maskedCartId ));
93+
}
94+
95+
/**
96+
* @return void
97+
*/
98+
public function testAssingAddress()
99+
{
100+
$address = $this->getMock('Magento\Quote\Model\Quote\Address', [], [], '', false, false);
101+
102+
$quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false);
103+
$this->quoteRepositoryMock->expects($this->once())
104+
->method('getActive')
105+
->with($this->cartId )
106+
->will($this->returnValue($quoteMock));
107+
108+
$this->validatorMock->expects($this->once())->method('validate')
109+
->with($address)
110+
->will($this->returnValue(true));
111+
112+
$quoteMock->expects($this->once())->method('setBillingAddress')->with($address);
113+
$quoteMock->expects($this->once())->method('setDataChanges')->with(true);
114+
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
115+
$addressId = 1;
116+
$billingAddressMock = $this->getMock('\Magento\Quote\Model\Quote\Address', [], [], '', false);
117+
$billingAddressMock->expects($this->once())->method('getId')->will($this->returnValue($addressId));
118+
$quoteMock->expects($this->once())->method('getBillingAddress')
119+
->will($this->returnValue($billingAddressMock));
120+
121+
$this->assertEquals($addressId, $this->model->assign($this->maskedCartId , $address));
122+
}
123+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Quote\Test\Unit\Model\GuestCart;
8+
9+
/**
10+
* Class GuestCartTestHelper
11+
*
12+
*/
13+
class GuestCartTestHelper
14+
{
15+
/**
16+
* @var \PHPUnit_Framework_TestCase
17+
*/
18+
protected $testCase;
19+
20+
/**
21+
* Initialize helper
22+
*
23+
* @param \PHPUnit_Framework_TestCase $testCase
24+
*/
25+
public function __construct(\PHPUnit_Framework_TestCase $testCase)
26+
{
27+
$this->testCase = $testCase;
28+
}
29+
30+
/**
31+
* Return mocks with expected invokes
32+
*
33+
* @param $maskedCartId
34+
* @param $cartId
35+
* @return array
36+
*/
37+
public function mockQuoteIdMask(
38+
$maskedCartId,
39+
$cartId
40+
) {
41+
$quoteIdMaskMock = $this->testCase->getMock('Magento\Quote\Model\QuoteIdMask', [], [], '', false);
42+
$quoteIdMaskFactoryMock = $this->testCase->getMock('Magento\Quote\Model\QuoteIdMaskFactory', [], [], '', false);
43+
44+
$quoteIdMaskFactoryMock->expects($this->testCase->once())->method('create')->willReturn(
45+
$quoteIdMaskMock
46+
);
47+
$quoteIdMaskMock->expects($this->testCase->once())
48+
->method('load')
49+
->with($maskedCartId, 'masked_id')
50+
->willReturn($quoteIdMaskMock);
51+
$quoteIdMaskMock->expects($this->testCase->once())
52+
->method('getId')
53+
->willReturn($cartId);
54+
55+
return [$quoteIdMaskFactoryMock,$quoteIdMaskMock];
56+
}
57+
}

0 commit comments

Comments
 (0)