Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CompositeProductRelationsCalculatorTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class CompositeProductRelationsCalculatorTest extends \PHPUnit\Framework\TestCase
13 {
17  private $defaultPriceMock;
18 
22  private $model;
23 
24  protected function setUp()
25  {
26  $this->defaultPriceMock = $this->getMockBuilder(DefaultPrice::class)->disableOriginalConstructor()->getMock();
27  $this->model = new CompositeProductRelationsCalculator($this->defaultPriceMock);
28  }
29 
30  public function testGetMaxRelationsCount()
31  {
32  $tableName = 'catalog_product_relation';
33  $maxRelatedProductCount = 200;
34 
35  $connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)->getMock();
36  $this->defaultPriceMock->expects($this->once())->method('getConnection')->willReturn($connectionMock);
37  $this->defaultPriceMock->expects($this->once())->method('getTable')->with($tableName)->willReturn($tableName);
38 
39  $relationSelectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
40  ->disableOriginalConstructor()
41  ->getMock();
42  $relationSelectMock->expects($this->once())
43  ->method('from')
44  ->with(
45  ['relation' => $tableName],
46  ['count' => 'count(relation.child_id)']
47  )
48  ->willReturnSelf();
49  $relationSelectMock->expects($this->once())->method('group')->with('parent_id')->willReturnSelf();
50  $connectionMock->expects($this->at(0))->method('select')->willReturn($relationSelectMock);
51 
52  $maxSelectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
53  ->disableOriginalConstructor()
54  ->getMock();
55  $maxSelectMock->expects($this->once())
56  ->method('from')
57  ->with(
58  ['max_value' => $relationSelectMock],
59  ['count' => 'MAX(count)']
60  )
61  ->willReturnSelf();
62  $connectionMock->expects($this->at(1))->method('select')->willReturn($maxSelectMock);
63 
64  $connectionMock->expects($this->at(2))
65  ->method('fetchOne')
66  ->with($maxSelectMock)
67  ->willReturn($maxRelatedProductCount);
68 
69  $this->assertEquals($maxRelatedProductCount, $this->model->getMaxRelationsCount());
70  }
71 }
$tableName
Definition: trigger.php:13