Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QueryBuilderTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\CatalogInventory\Model\Configuration as CatalogInventoryConfiguration;
18 
26 class QueryBuilderTest extends \PHPUnit\Framework\TestCase
27 {
31  private $model;
32 
36  private $resourceConnectionMock;
37 
41  private $scopeResolverMock;
42 
46  private $adapterMock;
47 
51  private $inventoryConfigMock;
52 
53  protected function setUp()
54  {
55  $this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
56  $this->scopeResolverMock = $this->createMock(ScopeResolverInterface::class);
57  $this->adapterMock = $this->createMock(AdapterInterface::class);
58  $this->inventoryConfigMock = $this->createMock(CatalogInventoryConfiguration::class);
59 
60  $this->resourceConnectionMock->expects($this->atLeastOnce())
61  ->method('getConnection')
62  ->willReturn($this->adapterMock);
63 
64  $this->indexScopeResolverMock = $this->createMock(
65  \Magento\Framework\Search\Request\IndexScopeResolverInterface::class
66  );
67  $this->dimensionMock = $this->createMock(\Magento\Framework\Indexer\Dimension::class);
68  $this->dimensionFactoryMock = $this->createMock(\Magento\Framework\Indexer\DimensionFactory::class);
69  $this->dimensionFactoryMock->method('create')->willReturn($this->dimensionMock);
70  $storeMock = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class);
71  $storeMock->method('getId')->willReturn(1);
72  $storeMock->method('getWebsiteId')->willReturn(1);
73  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
74  $this->storeManagerMock->method('getStore')->willReturn($storeMock);
75  $this->indexScopeResolverMock->method('resolve')->willReturn('catalog_product_index_price');
76 
77  $this->model = new QueryBuilder(
78  $this->resourceConnectionMock,
79  $this->scopeResolverMock,
80  $this->inventoryConfigMock,
81  $this->indexScopeResolverMock,
82  $this->dimensionFactoryMock
83  );
84  }
85 
87  {
88  $tableName = 'test_table';
89  $scope = 1;
90  $selectMock = $this->createMock(Select::class);
91  $attributeMock = $this->createMock(AbstractAttribute::class);
92  $storeMock = $this->createMock(Store::class);
93 
94  $this->adapterMock->expects($this->atLeastOnce())->method('select')
95  ->willReturn($selectMock);
96  $selectMock->expects($this->once())->method('joinInner')
97  ->with(['entities' => $tableName], 'main_table.entity_id = entities.entity_id', []);
98  $attributeMock->expects($this->once())->method('getAttributeCode')
99  ->willReturn('price');
100  $this->scopeResolverMock->expects($this->once())->method('getScope')
101  ->with($scope)->willReturn($storeMock);
102  $storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1);
103  $selectMock->expects($this->once())->method('from')
104  ->with(['main_table' => 'catalog_product_index_price'], null)
105  ->willReturn($selectMock);
106  $selectMock->expects($this->once())->method('columns')
107  ->with(['value' => 'main_table.min_price'])
108  ->willReturn($selectMock);
109  $selectMock->expects($this->exactly(2))->method('where')
110  ->withConsecutive(
111  ['main_table.customer_group_id = ?', 1],
112  ['main_table.website_id = ?', 1]
113  )->willReturn($selectMock);
114 
115  $this->model->build($attributeMock, $tableName, $scope, 1);
116  }
117 
119  {
120  $tableName = 'test_table';
121  $scope = 1;
122  $selectMock = $this->createMock(Select::class);
123  $attributeMock = $this->createMock(AbstractAttribute::class);
124  $storeMock = $this->createMock(Store::class);
125 
126  $this->adapterMock->expects($this->atLeastOnce())->method('select')
127  ->willReturn($selectMock);
128  $selectMock->expects($this->once())->method('joinInner')
129  ->with(['entities' => $tableName], 'main_table.entity_id = entities.entity_id', []);
130  $attributeMock->expects($this->once())->method('getBackendType')
131  ->willReturn('decimal');
132  $this->scopeResolverMock->expects($this->once())->method('getScope')
133  ->with($scope)->willReturn($storeMock);
134  $storeMock->expects($this->once())->method('getId')->willReturn(1);
135  $this->resourceConnectionMock->expects($this->exactly(2))->method('getTableName')
136  ->withConsecutive(
137  ['catalog_product_index_eav_decimal'],
138  ['cataloginventory_stock_status']
139  )->willReturnOnConsecutiveCalls(
140  'catalog_product_index_eav_decimal',
141  'cataloginventory_stock_status'
142  );
143 
144  $selectMock->expects($this->exactly(2))->method('from')
145  ->withConsecutive(
146  [
147  ['main_table' => 'catalog_product_index_eav_decimal'],
148  ['main_table.entity_id', 'main_table.value']
149  ],
150  [['main_table' => $selectMock], ['main_table.value']]
151  )
152  ->willReturn($selectMock);
153  $selectMock->expects($this->once())->method('distinct')->willReturn($selectMock);
154  $selectMock->expects($this->once())->method('joinLeft')
155  ->with(
156  ['stock_index' => 'cataloginventory_stock_status'],
157  'main_table.source_id = stock_index.product_id',
158  []
159  )->willReturn($selectMock);
160  $attributeMock->expects($this->once())->method('getAttributeId')->willReturn(3);
161  $selectMock->expects($this->exactly(3))->method('where')
162  ->withConsecutive(
163  ['main_table.attribute_id = ?', 3],
164  ['main_table.store_id = ? ', 1],
165  ['stock_index.stock_status = ?', Stock::STOCK_IN_STOCK]
166  )->willReturn($selectMock);
167  $this->inventoryConfigMock->expects($this->once())->method('isShowOutOfStock')->with(1)->willReturn(false);
168 
169  $this->model->build($attributeMock, $tableName, $scope, 1);
170  }
171 }
$tableName
Definition: trigger.php:13