Skip to content

Commit b9a9c73

Browse files
author
Michael Logvin
committed
MAGETWO-37611: Refactor direct calls to attribute tables
1 parent 8ba6631 commit b9a9c73

File tree

1 file changed

+165
-1
lines changed

1 file changed

+165
-1
lines changed

app/code/Magento/Reports/Test/Unit/Model/Resource/Quote/CollectionTest.php

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,168 @@
33
* Copyright © 2015 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
6+
namespace Magento\Reports\Test\Unit\Model\Resource\Quote;
7+
8+
class CollectionTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Reports\Model\Resource\Quote\Collection
12+
*/
13+
protected $model;
14+
15+
/**
16+
* @var \PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
protected $customerResourceMock;
19+
20+
/**
21+
* @var \PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
protected $readConnectionMock;
24+
25+
/**
26+
* @var \PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
protected $resourceMock;
29+
30+
/**
31+
* @var \PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
protected $selectMock;
34+
35+
/**
36+
* @var \PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
protected $fetchStrategyMock;
39+
40+
/**
41+
* @var \PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
protected $entityFactoryMock;
44+
45+
protected function setUp()
46+
{
47+
$this->selectMock = $this->getMockBuilder('Magento\Framework\DB\Select')
48+
->disableOriginalConstructor()
49+
->getMock();
50+
$this->selectMock->expects($this->any())
51+
->method('from')
52+
->withAnyParameters()
53+
->willReturnSelf();
54+
55+
$this->readConnectionMock = $this->getMockBuilder('Magento\Framework\DB\Adapter\Pdo\Mysql')
56+
->disableOriginalConstructor()
57+
->getMock();
58+
$this->readConnectionMock->expects($this->any())
59+
->method('select')
60+
->willReturn($this->selectMock);
61+
$this->resourceMock = $this->getMockBuilder('Magento\Quote\Model\Resource\Quote')
62+
->disableOriginalConstructor()
63+
->getMock();
64+
$this->resourceMock->expects($this->any())
65+
->method('getReadConnection')
66+
->willReturn($this->readConnectionMock);
67+
$this->resourceMock->expects($this->any())
68+
->method('getMainTable')
69+
->willReturn('test_table');
70+
$this->resourceMock->expects($this->any())
71+
->method('getTable')
72+
->willReturn('test_table');
73+
$this->customerResourceMock = $this->getMockBuilder('Magento\Customer\Model\Resource\Customer')
74+
->disableOriginalConstructor()
75+
->getMock();
76+
77+
$this->fetchStrategyMock = $this->getMockBuilder('Magento\Framework\Data\Collection\Db\FetchStrategy\Query')
78+
->disableOriginalConstructor()
79+
->getMock();
80+
81+
$this->entityFactoryMock = $this->getMockBuilder('Magento\Framework\Data\Collection\EntityFactory')
82+
->disableOriginalConstructor()
83+
->getMock();
84+
85+
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
86+
$this->model = $helper->getObject(
87+
'Magento\Reports\Model\Resource\Quote\Collection',
88+
[
89+
'customerResource' => $this->customerResourceMock,
90+
'resource' => $this->resourceMock,
91+
'fetchStrategy' => $this->fetchStrategyMock,
92+
'entityFactory' => $this->entityFactoryMock
93+
]
94+
);
95+
}
96+
97+
public function testResolveCustomerNames()
98+
{
99+
$customerName = "CONCAT_WS('firstname', 'lastname')";
100+
$customerTableName = 'customer_entity';
101+
$customerId = ['customer_id' => ['test_id']];
102+
$customersData = [['item_1']];
103+
$itemData = ['test'];
104+
105+
$selectMock = $this->getMockBuilder('Magento\Framework\DB\Select')
106+
->disableOriginalConstructor()
107+
->getMock();
108+
$selectMock->expects($this->any())
109+
->method('getAdapter')
110+
->willReturn($this->readConnectionMock);
111+
$selectMock->expects($this->once())
112+
->method('from')
113+
->with(['customer' => $customerTableName], ['email'])
114+
->willReturnSelf();
115+
$selectMock->expects($this->once())
116+
->method('columns')
117+
->with(['customer_name' => $customerName])
118+
->willReturnSelf();
119+
$selectMock->expects($this->once())
120+
->method('where')
121+
->with('customer.entity_id IN (?)')
122+
->willReturnSelf();
123+
124+
$this->readConnectionMock->expects($this->once())
125+
->method('getConcatSql')
126+
->with(['firstname', 'lastname'], ' ')
127+
->willReturn($customerName);
128+
129+
$readConnectionMock = $this->getMockBuilder('Magento\Framework\DB\Adapter\Pdo\Mysql')
130+
->disableOriginalConstructor()
131+
->getMock();
132+
$readConnectionMock->expects($this->any())
133+
->method('select')
134+
->willReturn($selectMock);
135+
136+
$this->customerResourceMock->expects($this->once())
137+
->method('getReadConnection')
138+
->willReturn($readConnectionMock);
139+
$this->customerResourceMock->expects($this->once())
140+
->method('getTable')
141+
->with('customer_entity')
142+
->willReturn($customerTableName);
143+
144+
$this->readConnectionMock->expects($this->any())
145+
->method('select')
146+
->willReturn($selectMock);
147+
$this->readConnectionMock->expects($this->once())
148+
->method('fetchAll')
149+
->with($selectMock)
150+
->willReturn($customersData);
151+
152+
$this->fetchStrategyMock->expects($this->once())
153+
->method('fetchAll')
154+
->withAnyParameters()
155+
->willReturn($customerId);
156+
157+
$itemMock = $this->getMockBuilder('Magento\Framework\Object')
158+
->disableOriginalConstructor()
159+
->getMock();
160+
$itemMock->expects($this->once())
161+
->method('getData')
162+
->willReturn($itemData);
163+
164+
$this->entityFactoryMock->expects($this->any())
165+
->method('create')
166+
->willReturn($itemMock);
167+
168+
$this->assertNull($this->model->resolveCustomerNames());
169+
}
170+
}

0 commit comments

Comments
 (0)