Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductIdLocatorTest.php
Go to the documentation of this file.
1 <?php
8 
12 class ProductIdLocatorTest extends \PHPUnit\Framework\TestCase
13 {
17  private $metadataPool;
18 
22  private $collectionFactory;
23 
27  private $model;
28 
34  protected function setUp()
35  {
36  $this->metadataPool = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
37  ->setMethods(['getMetadata'])
38  ->disableOriginalConstructor()->getMock();
39  $this->collectionFactory = $this
40  ->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class)
41  ->setMethods(['create'])
42  ->disableOriginalConstructor()->getMock();
43 
44  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
45  $this->model = $objectManager->getObject(
46  \Magento\Catalog\Model\ProductIdLocator::class,
47  [
48  'metadataPool' => $this->metadataPool,
49  'collectionFactory' => $this->collectionFactory,
50  ]
51  );
52  }
53 
57  public function testRetrieveProductIdsBySkus()
58  {
59  $skus = ['sku_1', 'sku_2'];
60  $collection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
61  ->setMethods(['getIterator', 'addFieldToFilter'])
62  ->disableOriginalConstructor()->getMock();
63  $product = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
64  ->setMethods(['getSku', 'getData', 'getTypeId'])
65  ->disableOriginalConstructor()->getMockForAbstractClass();
66  $metaDataInterface = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
67  ->setMethods(['getLinkField'])
68  ->disableOriginalConstructor()->getMockForAbstractClass();
69  $this->collectionFactory->expects($this->once())->method('create')->willReturn($collection);
70  $collection->expects($this->once())->method('addFieldToFilter')
71  ->with(\Magento\Catalog\Api\Data\ProductInterface::SKU, ['in' => $skus])->willReturnSelf();
72  $collection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$product]));
73  $this->metadataPool
74  ->expects($this->once())
75  ->method('getMetadata')
76  ->with(\Magento\Catalog\Api\Data\ProductInterface::class)
77  ->willReturn($metaDataInterface);
78  $metaDataInterface->expects($this->once())->method('getLinkField')->willReturn('entity_id');
79  $product->expects($this->once())->method('getSku')->willReturn('sku_1');
80  $product->expects($this->once())->method('getData')->with('entity_id')->willReturn(1);
81  $product->expects($this->once())->method('getTypeId')->willReturn('simple');
82  $this->assertEquals(
83  ['sku_1' => [1 => 'simple']],
84  $this->model->retrieveProductIdsBySkus($skus)
85  );
86  }
87 }
$objectManager
Definition: bootstrap.php:17
foreach($websiteCodes as $websiteCode) $skus