32 private $storeManagerMock;
37 private $resourceMock;
42 private $activeTableSwitcherMock;
47 private $eavConfigMock;
52 private $metadataPoolMock;
57 private $tableSwapperMock;
61 $this->storeManagerMock = $this->getMockBuilder(\
Magento\
Store\Model\StoreManagerInterface::class)
62 ->getMockForAbstractClass();
63 $this->resourceMock = $this->getMockBuilder(\
Magento\Framework\
App\ResourceConnection::class)
64 ->disableOriginalConstructor()
66 $this->activeTableSwitcherMock = $this->getMockBuilder(ActiveTableSwitcher::class)
67 ->disableOriginalConstructor()
69 $this->eavConfigMock = $this->getMockBuilder(\
Magento\Eav\Model\Config::class)
70 ->disableOriginalConstructor()
72 $this->metadataPoolMock = $this->getMockBuilder(\
Magento\Framework\EntityManager\MetadataPool::class)
73 ->disableOriginalConstructor()
75 $this->tableSwapperMock = $this->getMockForAbstractClass(
76 IndexerTableSwapperInterface::class
79 $this->model = new \Magento\CatalogRule\Model\Indexer\RuleProductsSelectBuilder(
82 $this->storeManagerMock,
83 $this->metadataPoolMock,
84 $this->activeTableSwitcherMock,
85 $this->tableSwapperMock
95 $ruleTable =
'catalogrule_product';
96 $rplTable =
'catalogrule_product_replica';
97 $prTable =
'catalog_product_entity';
98 $wsTable =
'catalog_product_website';
99 $productMock = $this->getMockBuilder(Product::class)->disableOriginalConstructor()->getMock();
100 $productMock->expects($this->exactly(2))->method(
'getEntityId')->willReturn(500);
102 $connectionMock = $this->getMockBuilder(AdapterInterface::class)->disableOriginalConstructor()->getMock();
103 $this->resourceMock->expects($this->at(0))->method(
'getConnection')->willReturn($connectionMock);
105 $this->tableSwapperMock->expects($this->once())
106 ->method(
'getWorkingTableName')
108 ->willReturn($rplTable);
110 $this->resourceMock->expects($this->at(1))->method(
'getTableName')->with($ruleTable)->willReturn($ruleTable);
111 $this->resourceMock->expects($this->at(2))->method(
'getTableName')->with($rplTable)->willReturn($rplTable);
112 $this->resourceMock->expects($this->at(3))->method(
'getTableName')->with($prTable)->willReturn($prTable);
113 $this->resourceMock->expects($this->at(4))->method(
'getTableName')->with($wsTable)->willReturn($wsTable);
115 $selectMock = $this->getMockBuilder(Select::class)->disableOriginalConstructor()->getMock();
116 $connectionMock->expects($this->once())->method(
'select')->willReturn($selectMock);
117 $selectMock->expects($this->at(0))->method(
'from')->with([
'rp' => $rplTable])->willReturnSelf();
118 $selectMock->expects($this->at(1))
120 ->with([
'rp.website_id',
'rp.customer_group_id',
'rp.product_id',
'rp.sort_order',
'rp.rule_id'])
122 $selectMock->expects($this->at(2))->method(
'where')->with(
'rp.product_id=?', 500)->willReturnSelf();
124 $attributeMock = $this->getMockBuilder(AbstractAttribute::class)->disableOriginalConstructor()->getMock();
125 $this->eavConfigMock->expects($this->once())
126 ->method(
'getAttribute')
128 ->willReturn($attributeMock);
129 $backendMock = $this->getMockBuilder(AbstractBackend::class)->disableOriginalConstructor()->getMock();
130 $backendMock->expects($this->once())->method(
'getTable')->willReturn(
'price_table');
131 $attributeMock->expects($this->once())->method(
'getBackend')->willReturn($backendMock);
132 $attributeMock->expects($this->once())->method(
'getId')->willReturn(200);
134 $metadataMock = $this->getMockBuilder(EntityMetadataInterface::class)->disableOriginalConstructor()->getMock();
135 $this->metadataPoolMock->expects($this->once())
136 ->method(
'getMetadata')
137 ->with(\
Magento\Catalog\Api\Data\ProductInterface::class)
138 ->willReturn($metadataMock);
139 $metadataMock->expects($this->once())->method(
'getLinkField')->willReturn(
'link_field');
141 $selectMock->expects($this->at(3))
143 ->with([
'e' => $prTable],
'e.entity_id = rp.product_id', [])
145 $selectMock->expects($this->at(4))
148 [
'pp_default' =>
'price_table'],
149 'pp_default.link_field=e.link_field AND (pp_default.attribute_id=200) and pp_default.store_id=0',
152 $websiteMock = $this->getMockBuilder(WebsiteInterface::class)
153 ->setMethods([
'getDefaultGroup'])
154 ->getMockForAbstractClass();
155 $this->storeManagerMock->expects($this->once())
156 ->method(
'getWebsite')
158 ->willReturn($websiteMock);
160 $groupMock = $this->getMockBuilder(\
Magento\
Store\Model\Group::class)
161 ->setMethods([
'getDefaultStoreId'])
162 ->disableOriginalConstructor()
164 $websiteMock->expects($this->once())->method(
'getDefaultGroup')->willReturn($groupMock);
165 $groupMock->expects($this->once())->method(
'getDefaultStoreId')->willReturn(700);
167 $selectMock->expects($this->at(5))
168 ->method(
'joinInner')
170 [
'product_website' => $wsTable],
171 'product_website.product_id=rp.product_id ' 172 .
'AND product_website.website_id = rp.website_id ' 173 .
'AND product_website.website_id=' 177 $selectMock->expects($this->at(6))
181 'pp55.link_field=e.link_field AND (pp55.attribute_id=200) and pp55.store_id=700',
185 $connectionMock->expects($this->once())
186 ->method(
'getIfNullSql')
187 ->with(
'pp55.value',
'pp_default.value')
188 ->willReturn(
'IF NULL SQL');
189 $selectMock->expects($this->at(7))
191 ->with([
'default_price' =>
'IF NULL SQL'])
193 $statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)
194 ->disableOriginalConstructor()
196 $connectionMock->expects($this->once())->method(
'query')->with($selectMock)->willReturn($statementMock);
198 $this->assertEquals($statementMock, $this->model->build(
$websiteId, $productMock,
true));