Skip to content

Commit f5ec69e

Browse files
author
Oleksii Korshenko
committed
Merge remote-tracking branch 'mainline/2.1-develop' into 2.1-develop-prs
2 parents c567804 + 580bcd1 commit f5ec69e

File tree

14 files changed

+546
-84
lines changed

14 files changed

+546
-84
lines changed

app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute/Collection.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,19 @@ public function setAttributeSetsFilter(array $setIds)
205205
*/
206206
public function setInAllAttributeSetsFilter(array $setIds)
207207
{
208-
foreach ($setIds as $setId) {
209-
$setId = (int)$setId;
210-
if (!$setId) {
211-
continue;
212-
}
213-
$alias = sprintf('entity_attribute_%d', $setId);
214-
$joinCondition = $this->getConnection()->quoteInto(
215-
"{$alias}.attribute_id = main_table.attribute_id AND {$alias}.attribute_set_id =?",
216-
$setId
217-
);
218-
$this->join([$alias => 'eav_entity_attribute'], $joinCondition, 'attribute_id');
208+
if (!empty($setIds)) {
209+
$this->getSelect()
210+
->join(
211+
['entity_attribute' => $this->getTable('eav_entity_attribute')],
212+
'entity_attribute.attribute_id = main_table.attribute_id',
213+
['count' => new \Zend_Db_Expr('COUNT(*)')]
214+
)
215+
->where(
216+
'entity_attribute.attribute_set_id IN (?)',
217+
$setIds
218+
)
219+
->group('entity_attribute.attribute_id')
220+
->having('count = ' . count($setIds));
219221
}
220222

221223
//$this->getSelect()->distinct(true);
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
namespace Magento\Eav\Test\Unit\Model\ResourceModel\Entity\Attribute;
9+
10+
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
13+
/**
14+
* Test for \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection class.
15+
*
16+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17+
*/
18+
class CollectionTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
22+
*/
23+
private $model;
24+
25+
/**
26+
* @var \Magento\Framework\Data\Collection\EntityFactory|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $entityFactoryMock;
29+
30+
/**
31+
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $loggerMock;
34+
35+
/**
36+
* @var \Magento\Framework\Data\Collection\Db\FetchStrategyInterface|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $fetchStrategyMock;
39+
40+
/**
41+
* @var \Magento\Framework\Data\Collection\EntityFactory|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $eventManagerMock;
44+
45+
/**
46+
* @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
private $eavConfigMock;
49+
50+
/**
51+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
private $storeManagerMock;
54+
55+
/**
56+
* @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
57+
*/
58+
private $connectionMock;
59+
60+
/**
61+
* @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb|\PHPUnit_Framework_MockObject_MockObject
62+
*/
63+
private $resourceMock;
64+
65+
/**
66+
* @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject
67+
*/
68+
private $selectMock;
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
protected function setUp()
74+
{
75+
$this->entityFactoryMock = $this->getMockBuilder(\Magento\Framework\Data\Collection\EntityFactory::class)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
79+
$this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
80+
->disableOriginalConstructor()
81+
->getMock();
82+
83+
$this->fetchStrategyMock = $this->getMockBuilder(FetchStrategyInterface::class)
84+
->disableOriginalConstructor()
85+
->getMock();
86+
87+
$this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
88+
->disableOriginalConstructor()
89+
->getMock();
90+
91+
$this->eavConfigMock = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
92+
->disableOriginalConstructor()
93+
->getMock();
94+
95+
$this->connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\Pdo\Mysql::class)
96+
->disableOriginalConstructor()
97+
->getMock();
98+
99+
$this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
100+
->disableOriginalConstructor()
101+
->getMock();
102+
103+
$this->resourceMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class)
104+
->setMethods(['__wakeup', 'getConnection', 'getMainTable', 'getTable'])
105+
->disableOriginalConstructor()
106+
->getMockForAbstractClass();
107+
108+
$this->resourceMock->expects($this->any())->method('getConnection')->willReturn($this->connectionMock);
109+
$this->resourceMock->expects($this->any())->method('getMainTable')->willReturn('eav_entity_attribute');
110+
111+
$this->selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
112+
->disableOriginalConstructor()
113+
->getMock();
114+
115+
$this->connectionMock->expects($this->any())->method('select')->willReturn($this->selectMock);
116+
117+
$objectManager = new ObjectManager($this);
118+
$this->model = $objectManager->getObject(
119+
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class,
120+
[
121+
'entityFactory' => $this->entityFactoryMock,
122+
'logger' => $this->loggerMock,
123+
'fetchStrategy' => $this->fetchStrategyMock,
124+
'eventManager' => $this->eventManagerMock,
125+
'eavConfig' => $this->eavConfigMock,
126+
'connection' => $this->connectionMock,
127+
'resource' => $this->resourceMock,
128+
]
129+
);
130+
}
131+
132+
/**
133+
* Test method \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::setInAllAttributeSetsFilter
134+
*
135+
* @return void
136+
*/
137+
public function testSetInAllAttributeSetsFilter()
138+
{
139+
$setIds = [1, 2, 3];
140+
141+
$this->selectMock->expects($this->atLeastOnce())
142+
->method('where')
143+
->with('entity_attribute.attribute_set_id IN (?)', $setIds)
144+
->willReturnSelf();
145+
$this->selectMock->expects($this->atLeastOnce())->method('join')->with(
146+
['entity_attribute' => $this->model->getTable('eav_entity_attribute')],
147+
'entity_attribute.attribute_id = main_table.attribute_id',
148+
['count' => new \Zend_Db_Expr('COUNT(*)')]
149+
)->willReturnSelf();
150+
151+
$this->selectMock->expects($this->atLeastOnce())->method('group')->with('entity_attribute.attribute_id')
152+
->willReturnSelf();
153+
154+
$this->selectMock->expects($this->atLeastOnce())->method('having')->with('count = ' . count($setIds))
155+
->willReturnSelf();
156+
157+
$this->model->setInAllAttributeSetsFilter($setIds);
158+
}
159+
}

app/code/Magento/Sales/Model/Order/Shipment.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,6 @@ public function register()
257257
if (!$item->getOrderItem()->isDummy(true)) {
258258
$totalQty += $item->getQty();
259259
}
260-
} else {
261-
$item->isDeleted(true);
262260
}
263261
}
264262

app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Sales\Api\Data\OrderInterface;
1515
use Magento\Sales\Api\Data\ShipmentCommentCreationInterface;
1616
use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface;
17+
use Magento\Sales\Api\Data\OrderItemInterface;
1718

1819
/**
1920
* Class ShipmentDocumentFactory
@@ -75,13 +76,23 @@ public function create(
7576
array $packages = [],
7677
ShipmentCreationArgumentsInterface $arguments = null
7778
) {
78-
$shipmentItems = $this->itemsToArray($items);
79+
$shipmentItems = empty($items)
80+
? $this->getQuantitiesFromOrderItems($order->getItems())
81+
: $this->getQuantitiesFromShipmentItems($items);
82+
7983
/** @var Shipment $shipment */
8084
$shipment = $this->shipmentFactory->create(
8185
$order,
8286
$shipmentItems
8387
);
84-
$this->prepareTracks($shipment, $tracks);
88+
89+
foreach ($tracks as $track) {
90+
$hydrator = $this->hydratorPool->getHydrator(
91+
\Magento\Sales\Api\Data\ShipmentTrackCreationInterface::class
92+
);
93+
$shipment->addTrack($this->trackFactory->create(['data' => $hydrator->extract($track)]));
94+
}
95+
8596
if ($comment) {
8697
$shipment->addComment(
8798
$comment->getComment(),
@@ -99,35 +110,34 @@ public function create(
99110
}
100111

101112
/**
102-
* Adds tracks to the shipment.
113+
* Translate OrderItemInterface array to product id => product quantity array.
103114
*
104-
* @param ShipmentInterface $shipment
105-
* @param ShipmentTrackCreationInterface[] $tracks
106-
* @return ShipmentInterface
115+
* @param OrderItemInterface[] $items
116+
* @return int[]
107117
*/
108-
private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipment, array $tracks)
118+
private function getQuantitiesFromOrderItems(array $items)
109119
{
110-
foreach ($tracks as $track) {
111-
$hydrator = $this->hydratorPool->getHydrator(
112-
\Magento\Sales\Api\Data\ShipmentTrackCreationInterface::class
113-
);
114-
$shipment->addTrack($this->trackFactory->create(['data' => $hydrator->extract($track)]));
120+
$shipmentItems = [];
121+
foreach ($items as $item) {
122+
if (!$item->getIsVirtual() && !$item->getParentItem()) {
123+
$shipmentItems[$item->getItemId()] = $item->getQtyOrdered();
124+
}
115125
}
116-
return $shipment;
126+
return $shipmentItems;
117127
}
118128

119129
/**
120-
* Convert items to array
130+
* Translate ShipmentItemCreationInterface array to product id => product quantity array.
121131
*
122132
* @param ShipmentItemCreationInterface[] $items
123-
* @return array
133+
* @return int[]
124134
*/
125-
private function itemsToArray(array $items = [])
135+
private function getQuantitiesFromShipmentItems(array $items)
126136
{
127137
$shipmentItems = [];
128138
foreach ($items as $item) {
129139
$shipmentItems[$item->getOrderItemId()] = $item->getQty();
130140
}
131-
return $shipmentItems;
141+
return $shipmentItems;
132142
}
133143
}

app/code/Magento/Sales/Model/Order/ShipmentFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ protected function prepareItems(
9292

9393
/** @var \Magento\Sales\Model\Order\Shipment\Item $item */
9494
$item = $this->converter->itemToShipmentItem($orderItem);
95-
9695
if ($orderItem->isDummy(true)) {
9796
$qty = 0;
9897

@@ -128,6 +127,9 @@ protected function prepareItems(
128127
continue;
129128
}
130129
}
130+
if ($orderItem->getIsVirtual() || $orderItem->getParentItemId()) {
131+
$item->isDeleted(true);
132+
}
131133

132134
$totalQty += $qty;
133135

app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Magento\Framework\EntityManager\HydratorInterface;
1919

2020
/**
21-
* Class ShipmentDocumentFactoryTest
21+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2222
*/
2323
class ShipmentDocumentFactoryTest extends \PHPUnit_Framework_TestCase
2424
{
@@ -128,14 +128,8 @@ public function testCreate()
128128
$packages = [];
129129
$items = [1 => 10];
130130

131-
$this->itemMock->expects($this->once())
132-
->method('getOrderItemId')
133-
->willReturn(1);
134-
135-
$this->itemMock->expects($this->once())
136-
->method('getQty')
137-
->willReturn(10);
138-
131+
$this->itemMock->expects($this->once())->method('getOrderItemId')->willReturn(1);
132+
$this->itemMock->expects($this->once())->method('getQty')->willReturn(10);
139133
$this->shipmentFactoryMock->expects($this->once())
140134
->method('create')
141135
->with(

app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testCreate($tracks)
8585
{
8686
$orderItem = $this->getMock(
8787
'Magento\Sales\Model\Order\Item',
88-
['getId', 'getQtyOrdered'],
88+
['getId', 'getQtyOrdered', 'getParentItemId', 'getIsVirtual'],
8989
[],
9090
'',
9191
false
@@ -97,6 +97,9 @@ public function testCreate($tracks)
9797
->method('getQtyOrdered')
9898
->willReturn(5);
9999

100+
$orderItem->expects($this->any())->method('getParentItemId')->willReturn(false);
101+
$orderItem->expects($this->any())->method('getIsVirtual')->willReturn(false);
102+
100103
$shipmentItem = $this->getMock(
101104
'Magento\Sales\Model\Order\Shipment\Item',
102105
['setQty'],

0 commit comments

Comments
 (0)