Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RuleProductsSelectBuilderTest.php
Go to the documentation of this file.
1 <?php
8 
18 
22 class RuleProductsSelectBuilderTest extends \PHPUnit\Framework\TestCase
23 {
27  private $model;
28 
32  private $storeManagerMock;
33 
37  private $resourceMock;
38 
42  private $activeTableSwitcherMock;
43 
47  private $eavConfigMock;
48 
52  private $metadataPoolMock;
53 
57  private $tableSwapperMock;
58 
59  protected function setUp()
60  {
61  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
62  ->getMockForAbstractClass();
63  $this->resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66  $this->activeTableSwitcherMock = $this->getMockBuilder(ActiveTableSwitcher::class)
67  ->disableOriginalConstructor()
68  ->getMock();
69  $this->eavConfigMock = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
70  ->disableOriginalConstructor()
71  ->getMock();
72  $this->metadataPoolMock = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
73  ->disableOriginalConstructor()
74  ->getMock();
75  $this->tableSwapperMock = $this->getMockForAbstractClass(
76  IndexerTableSwapperInterface::class
77  );
78 
79  $this->model = new \Magento\CatalogRule\Model\Indexer\RuleProductsSelectBuilder(
80  $this->resourceMock,
81  $this->eavConfigMock,
82  $this->storeManagerMock,
83  $this->metadataPoolMock,
84  $this->activeTableSwitcherMock,
85  $this->tableSwapperMock
86  );
87  }
88 
92  public function testBuild()
93  {
94  $websiteId = 55;
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);
101 
102  $connectionMock = $this->getMockBuilder(AdapterInterface::class)->disableOriginalConstructor()->getMock();
103  $this->resourceMock->expects($this->at(0))->method('getConnection')->willReturn($connectionMock);
104 
105  $this->tableSwapperMock->expects($this->once())
106  ->method('getWorkingTableName')
107  ->with($ruleTable)
108  ->willReturn($rplTable);
109 
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);
114 
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))
119  ->method('order')
120  ->with(['rp.website_id', 'rp.customer_group_id', 'rp.product_id', 'rp.sort_order', 'rp.rule_id'])
121  ->willReturnSelf();
122  $selectMock->expects($this->at(2))->method('where')->with('rp.product_id=?', 500)->willReturnSelf();
123 
124  $attributeMock = $this->getMockBuilder(AbstractAttribute::class)->disableOriginalConstructor()->getMock();
125  $this->eavConfigMock->expects($this->once())
126  ->method('getAttribute')
127  ->with(Product::ENTITY, 'price')
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);
133 
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');
140 
141  $selectMock->expects($this->at(3))
142  ->method('join')
143  ->with(['e' => $prTable], 'e.entity_id = rp.product_id', [])
144  ->willReturnSelf();
145  $selectMock->expects($this->at(4))
146  ->method('join')
147  ->with(
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',
150  []
151  )->willReturnSelf();
152  $websiteMock = $this->getMockBuilder(WebsiteInterface::class)
153  ->setMethods(['getDefaultGroup'])
154  ->getMockForAbstractClass();
155  $this->storeManagerMock->expects($this->once())
156  ->method('getWebsite')
157  ->with($websiteId)
158  ->willReturn($websiteMock);
159 
160  $groupMock = $this->getMockBuilder(\Magento\Store\Model\Group::class)
161  ->setMethods(['getDefaultStoreId'])
162  ->disableOriginalConstructor()
163  ->getMock();
164  $websiteMock->expects($this->once())->method('getDefaultGroup')->willReturn($groupMock);
165  $groupMock->expects($this->once())->method('getDefaultStoreId')->willReturn(700);
166 
167  $selectMock->expects($this->at(5))
168  ->method('joinInner')
169  ->with(
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='
174  . $websiteId,
175  []
176  )->willReturnSelf();
177  $selectMock->expects($this->at(6))
178  ->method('joinLeft')
179  ->with(
180  ['pp' . $websiteId => 'price_table'],
181  'pp55.link_field=e.link_field AND (pp55.attribute_id=200) and pp55.store_id=700',
182  []
183  )->willReturnSelf();
184 
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))
190  ->method('columns')
191  ->with(['default_price' => 'IF NULL SQL'])
192  ->willReturnSelf();
193  $statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)
194  ->disableOriginalConstructor()
195  ->getMock();
196  $connectionMock->expects($this->once())->method('query')->with($selectMock)->willReturn($statementMock);
197 
198  $this->assertEquals($statementMock, $this->model->build($websiteId, $productMock, true));
199  }
200 }