Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataProductHashMapTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
16 
20 class DataProductHashMapTest extends \PHPUnit\Framework\TestCase
21 {
23  private $hashMapPoolMock;
24 
26  private $dataCategoryMapMock;
27 
31  private $collectionFactoryMock;
32 
36  private $productCollectionMock;
37 
39  private $model;
40 
41  protected function setUp()
42  {
43  $this->hashMapPoolMock = $this->createMock(HashMapPool::class);
44  $this->dataCategoryMapMock = $this->createMock(DataCategoryHashMap::class);
45  $this->collectionFactoryMock = $this->createPartialMock(CollectionFactory::class, ['create']);
46  $this->productCollectionMock = $this->createPartialMock(
47  ProductCollection::class,
48  ['getSelect', 'getConnection', 'getAllIds']
49  );
50 
51  $this->collectionFactoryMock->expects($this->any())
52  ->method('create')
53  ->willReturn($this->productCollectionMock);
54 
55  $this->hashMapPoolMock->expects($this->any())
56  ->method('getDataMap')
57  ->willReturn($this->dataCategoryMapMock);
58 
59  $this->model = (new ObjectManager($this))->getObject(
60  DataProductHashMap::class,
61  [
62  'collectionFactory' => $this->collectionFactoryMock,
63  'hashMapPool' => $this->hashMapPoolMock
64  ]
65  );
66  }
67 
71  public function testGetAllData()
72  {
73  $productIds = ['1' => [1, 2, 3], '2' => [2, 3], '3' => 3];
74  $productIdsOther = ['2' => [2, 3, 4]];
75 
76  $connectionMock = $this->createMock(AdapterInterface::class);
77  $selectMock = $this->createMock(Select::class);
78 
79  $this->productCollectionMock->expects($this->exactly(3))
80  ->method('getAllIds')
81  ->willReturnOnConsecutiveCalls($productIds, $productIdsOther, $productIds);
82  $this->productCollectionMock->expects($this->any())
83  ->method('getConnection')
84  ->willReturn($connectionMock);
85  $connectionMock->expects($this->any())
86  ->method('getTableName')
87  ->willReturn($this->returnValue($this->returnArgument(0)));
88  $this->productCollectionMock->expects($this->any())
89  ->method('getSelect')
90  ->willReturn($selectMock);
91  $selectMock->expects($this->any())
92  ->method('from')
93  ->willReturnSelf();
94  $selectMock->expects($this->any())
95  ->method('joinInner')
96  ->willReturnSelf();
97  $selectMock->expects($this->any())
98  ->method('where')
99  ->willReturnSelf();
100  $this->dataCategoryMapMock->expects($this->any())
101  ->method('getAllData')
102  ->willReturn([]);
103  $this->hashMapPoolMock->expects($this->any())
104  ->method('resetMap')
105  ->with(DataCategoryHashMap::class, 1);
106  $this->assertEquals($productIds, $this->model->getAllData(1));
107  $this->assertEquals($productIds[2], $this->model->getData(1, 2));
108  $this->assertEquals($productIdsOther, $this->model->getAllData(2));
109  $this->assertEquals($productIdsOther[2], $this->model->getData(2, 2));
110  $this->model->resetData(1);
111  $this->assertEquals($productIds[2], $this->model->getData(1, 2));
112  $this->assertEquals($productIds, $this->model->getAllData(1));
113  }
114 }