Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataCategoryHashMapTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Catalog\Model\ResourceModel\CategoryFactory;
16 
20 class DataCategoryHashMapTest extends \PHPUnit\Framework\TestCase
21 {
23  private $categoryRepository;
24 
26  private $categoryResourceFactory;
27 
29  private $categoryResource;
30 
32  private $model;
33 
34  protected function setUp()
35  {
36  $this->categoryRepository = $this->createMock(CategoryRepository::class);
37  $this->categoryResourceFactory = $this->createPartialMock(CategoryFactory::class, ['create']);
38  $this->categoryResource = $this->createPartialMock(Category::class, ['getConnection', 'getEntityTable']);
39 
40  $this->categoryResourceFactory->expects($this->any())
41  ->method('create')
42  ->willReturn($this->categoryResource);
43 
44  $this->model = (new ObjectManager($this))->getObject(
45  DataCategoryHashMap::class,
46  [
47  'categoryRepository' => $this->categoryRepository,
48  'categoryResourceFactory' => $this->categoryResourceFactory
49  ]
50  );
51  }
52 
56  public function testGetAllData()
57  {
58  $categoryIds = ['1' => [1, 2, 3], '2' => [2, 3], '3' => 3];
59  $categoryIdsOther = ['2' => [2, 3, 4]];
60 
61  $categoryMock = $this->getMockBuilder(CategoryInterface::class)
62  ->setMethods(['getResource'])
63  ->getMockForAbstractClass();
64  $connectionAdapterMock = $this->createMock(AdapterInterface::class);
65  $selectMock = $this->createMock(Select::class);
66 
67  $this->categoryRepository->expects($this->any())
68  ->method('get')
69  ->willReturn($categoryMock);
70  $categoryMock->expects($this->any())
71  ->method('getResource')
72  ->willReturn($this->categoryResource);
73  $this->categoryResource->expects($this->any())
74  ->method('getConnection')
75  ->willReturn($connectionAdapterMock);
76  $this->categoryResource->expects($this->any())
77  ->method('getEntityTable')
78  ->willReturn('category_entity');
79  $connectionAdapterMock->expects($this->any())
80  ->method('select')
81  ->willReturn($selectMock);
82  $selectMock->expects($this->any())
83  ->method('from')
84  ->willReturnSelf();
85  $selectMock->expects($this->any())
86  ->method('where')
87  ->willReturnSelf();
88  $connectionAdapterMock->expects($this->any())
89  ->method('fetchCol')
90  ->willReturnOnConsecutiveCalls($categoryIds, $categoryIdsOther, $categoryIds);
91 
92  $this->assertEquals($categoryIds, $this->model->getAllData(1));
93  $this->assertEquals($categoryIds[2], $this->model->getData(1, 2));
94  $this->assertEquals($categoryIdsOther, $this->model->getAllData(2));
95  $this->assertEquals($categoryIdsOther[2], $this->model->getData(2, 2));
96  $this->model->resetData(1);
97  $this->assertEquals($categoryIds[2], $this->model->getData(1, 2));
98  $this->assertEquals($categoryIds, $this->model->getAllData(1));
99  }
100 }