|
13 | 13 | use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
|
14 | 14 | use Magento\Framework\EntityManager\EntityMetadataInterface;
|
15 | 15 | use Magento\Framework\EntityManager\MetadataPool;
|
16 |
| -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
17 | 16 | use PHPUnit\Framework\MockObject\MockObject;
|
18 | 17 | use PHPUnit\Framework\TestCase;
|
19 | 18 |
|
|
23 | 22 | class ProductIdLocatorTest extends TestCase
|
24 | 23 | {
|
25 | 24 | /**
|
26 |
| - * @var MetadataPool|MockObject |
| 25 | + * @var int |
27 | 26 | */
|
28 |
| - private $metadataPool; |
| 27 | + private $idsLimit; |
29 | 28 |
|
30 | 29 | /**
|
31 |
| - * @var CollectionFactory|MockObject |
| 30 | + * @var string |
32 | 31 | */
|
33 |
| - private $collectionFactory; |
| 32 | + private $linkField; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var Collection|MockObject |
| 36 | + */ |
| 37 | + private $collection; |
34 | 38 |
|
35 | 39 | /**
|
36 | 40 | * @var ProductIdLocator
|
37 | 41 | */
|
38 | 42 | private $model;
|
39 | 43 |
|
40 | 44 | /**
|
41 |
| - * Set up. |
42 |
| - * |
43 |
| - * @return void |
| 45 | + * @inheritDoc |
44 | 46 | */
|
45 | 47 | protected function setUp(): void
|
46 | 48 | {
|
47 |
| - $this->metadataPool = $this->getMockBuilder(MetadataPool::class) |
48 |
| - ->setMethods(['getMetadata']) |
49 |
| - ->disableOriginalConstructor() |
50 |
| - ->getMock(); |
51 |
| - $this->collectionFactory = $this |
52 |
| - ->getMockBuilder(CollectionFactory::class) |
| 49 | + $metadataPool = $this->createMock(MetadataPool::class); |
| 50 | + $collectionFactory = $this->getMockBuilder(CollectionFactory::class) |
53 | 51 | ->setMethods(['create'])
|
54 | 52 | ->disableOriginalConstructor()
|
55 | 53 | ->getMock();
|
| 54 | + $this->idsLimit = 4; |
56 | 55 |
|
57 |
| - $objectManager = new ObjectManager($this); |
58 |
| - $this->model = $objectManager->getObject( |
59 |
| - ProductIdLocator::class, |
60 |
| - [ |
61 |
| - 'metadataPool' => $this->metadataPool, |
62 |
| - 'collectionFactory' => $this->collectionFactory, |
63 |
| - ] |
64 |
| - ); |
| 56 | + $this->linkField = 'entity_id'; |
| 57 | + $metaDataInterface = $this->createMock(EntityMetadataInterface::class); |
| 58 | + $metaDataInterface->method('getLinkField') |
| 59 | + ->willReturn($this->linkField); |
| 60 | + $metadataPool->method('getMetadata') |
| 61 | + ->with(ProductInterface::class) |
| 62 | + ->willReturn($metaDataInterface); |
| 63 | + |
| 64 | + $this->collection = $this->createMock(Collection::class); |
| 65 | + $collectionFactory->method('create') |
| 66 | + ->willReturn($this->collection); |
| 67 | + |
| 68 | + $this->model = new ProductIdLocator($metadataPool, $collectionFactory, $this->idsLimit); |
65 | 69 | }
|
66 | 70 |
|
67 |
| - /** |
68 |
| - * Test retrieve |
69 |
| - */ |
70 | 71 | public function testRetrieveProductIdsBySkus()
|
71 | 72 | {
|
72 | 73 | $skus = ['sku_1', 'sku_2'];
|
73 |
| - $collection = $this->getMockBuilder(Collection::class) |
74 |
| - ->setMethods( |
75 |
| - [ |
76 |
| - 'getItems', |
77 |
| - 'addFieldToFilter', |
78 |
| - 'setPageSize', |
79 |
| - 'getLastPageNumber', |
80 |
| - 'setCurPage', |
81 |
| - 'clear' |
82 |
| - ] |
83 |
| - ) |
84 |
| - ->disableOriginalConstructor() |
85 |
| - ->getMock(); |
| 74 | + |
86 | 75 | $product = $this->getMockBuilder(ProductInterface::class)
|
87 | 76 | ->setMethods(['getSku', 'getData', 'getTypeId'])
|
88 | 77 | ->disableOriginalConstructor()
|
89 | 78 | ->getMockForAbstractClass();
|
90 |
| - $metaDataInterface = $this->getMockBuilder(EntityMetadataInterface::class) |
91 |
| - ->setMethods(['getLinkField']) |
92 |
| - ->disableOriginalConstructor() |
93 |
| - ->getMockForAbstractClass(); |
94 |
| - $this->collectionFactory->expects($this->once())->method('create')->willReturn($collection); |
95 |
| - $collection->expects($this->once())->method('addFieldToFilter') |
96 |
| - ->with(ProductInterface::SKU, ['in' => $skus])->willReturnSelf(); |
97 |
| - $collection->expects($this->atLeastOnce())->method('getItems')->willReturn([$product]); |
98 |
| - $collection->expects($this->atLeastOnce())->method('setPageSize')->willReturnSelf(); |
99 |
| - $collection->expects($this->atLeastOnce())->method('getLastPageNumber')->willReturn(1); |
100 |
| - $collection->expects($this->atLeastOnce())->method('setCurPage')->with(1)->willReturnSelf(); |
101 |
| - $collection->expects($this->atLeastOnce())->method('clear')->willReturnSelf(); |
102 |
| - $this->metadataPool |
103 |
| - ->expects($this->once()) |
104 |
| - ->method('getMetadata') |
105 |
| - ->with(ProductInterface::class) |
106 |
| - ->willReturn($metaDataInterface); |
107 |
| - $metaDataInterface->expects($this->once())->method('getLinkField')->willReturn('entity_id'); |
108 |
| - $product->expects($this->once())->method('getSku')->willReturn('sku_1'); |
109 |
| - $product->expects($this->once())->method('getData')->with('entity_id')->willReturn(1); |
110 |
| - $product->expects($this->once())->method('getTypeId')->willReturn('simple'); |
| 79 | + $product->method('getSku') |
| 80 | + ->willReturn('sku_1'); |
| 81 | + $product->method('getData') |
| 82 | + ->with($this->linkField) |
| 83 | + ->willReturn(1); |
| 84 | + $product->method('getTypeId') |
| 85 | + ->willReturn('simple'); |
| 86 | + |
| 87 | + $this->collection->expects($this->once()) |
| 88 | + ->method('addFieldToFilter') |
| 89 | + ->with(ProductInterface::SKU, ['in' => $skus]) |
| 90 | + ->willReturnSelf(); |
| 91 | + $this->collection->expects($this->atLeastOnce()) |
| 92 | + ->method('getItems') |
| 93 | + ->willReturn([$product]); |
| 94 | + $this->collection->expects($this->atLeastOnce()) |
| 95 | + ->method('setPageSize') |
| 96 | + ->willReturnSelf(); |
| 97 | + $this->collection->expects($this->atLeastOnce()) |
| 98 | + ->method('getLastPageNumber') |
| 99 | + ->willReturn(1); |
| 100 | + $this->collection->expects($this->atLeastOnce()) |
| 101 | + ->method('setCurPage') |
| 102 | + ->with(1) |
| 103 | + ->willReturnSelf(); |
| 104 | + $this->collection->expects($this->atLeastOnce()) |
| 105 | + ->method('clear') |
| 106 | + ->willReturnSelf(); |
| 107 | + |
111 | 108 | $this->assertEquals(
|
112 | 109 | ['sku_1' => [1 => 'simple']],
|
113 | 110 | $this->model->retrieveProductIdsBySkus($skus)
|
114 | 111 | );
|
115 | 112 | }
|
| 113 | + |
| 114 | + public function testRetrieveProductIdsWithNumericSkus() |
| 115 | + { |
| 116 | + $skus = ['111', '222', '333', '444', '555']; |
| 117 | + $products = []; |
| 118 | + foreach ($skus as $sku) { |
| 119 | + $product = $this->getMockBuilder(ProductInterface::class) |
| 120 | + ->setMethods(['getSku', 'getData', 'getTypeId']) |
| 121 | + ->disableOriginalConstructor() |
| 122 | + ->getMockForAbstractClass(); |
| 123 | + $product->method('getSku') |
| 124 | + ->willReturn($sku); |
| 125 | + $product->method('getData') |
| 126 | + ->with($this->linkField) |
| 127 | + ->willReturn((int) $sku); |
| 128 | + $product->method('getTypeId') |
| 129 | + ->willReturn('simple'); |
| 130 | + $products[] = $product; |
| 131 | + } |
| 132 | + |
| 133 | + $this->collection->expects($this->atLeastOnce()) |
| 134 | + ->method('addFieldToFilter') |
| 135 | + ->withConsecutive([ProductInterface::SKU, ['in' => $skus]], [ProductInterface::SKU, ['in' => ['1']]]) |
| 136 | + ->willReturnSelf(); |
| 137 | + $this->collection->expects($this->atLeastOnce()) |
| 138 | + ->method('getItems') |
| 139 | + ->willReturnOnConsecutiveCalls($products, []); |
| 140 | + $this->collection->expects($this->atLeastOnce()) |
| 141 | + ->method('setPageSize') |
| 142 | + ->willReturnSelf(); |
| 143 | + $this->collection->expects($this->atLeastOnce()) |
| 144 | + ->method('getLastPageNumber') |
| 145 | + ->willReturn(1); |
| 146 | + $this->collection->expects($this->atLeastOnce()) |
| 147 | + ->method('setCurPage') |
| 148 | + ->with(1) |
| 149 | + ->willReturnSelf(); |
| 150 | + $this->collection->expects($this->atLeastOnce()) |
| 151 | + ->method('clear') |
| 152 | + ->willReturnSelf(); |
| 153 | + |
| 154 | + $this->assertEquals( |
| 155 | + [ |
| 156 | + '111' => [111 => 'simple'], |
| 157 | + '222' => [222 => 'simple'], |
| 158 | + '333' => [333 => 'simple'], |
| 159 | + '444' => [444 => 'simple'], |
| 160 | + '555' => [555 => 'simple'], |
| 161 | + ], |
| 162 | + $this->model->retrieveProductIdsBySkus($skus) |
| 163 | + ); |
| 164 | + $this->assertEmpty($this->model->retrieveProductIdsBySkus(['1'])); |
| 165 | + } |
116 | 166 | }
|
0 commit comments