Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FromRendererTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class FromRendererTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $model;
20 
24  protected $quoteMock;
25 
29  protected $selectMock;
30 
36  protected function setUp()
37  {
38  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
39  $this->quoteMock =
40  $this->createPartialMock(\Magento\Framework\DB\Platform\Quote::class, ['quoteTableAs', 'quoteIdentifier']);
41  $this->selectMock = $this->createPartialMock(\Magento\Framework\DB\Select::class, ['getPart']);
42  $this->model = $objectManager->getObject(
43  \Magento\Framework\DB\Select\FromRenderer::class,
44  ['quote' => $this->quoteMock]
45  );
46  }
47 
48  public function testRenderNoPart()
49  {
50  $sql = 'SELECT';
51  $this->selectMock->expects($this->once())
52  ->method('getPart')
53  ->with(Select::FROM)
54  ->willReturn([]);
55  $this->assertEquals($sql, $this->model->render($this->selectMock, $sql));
56  }
57 
64  public function testRender($from, $sql, $expectedResult)
65  {
66  $this->quoteMock->expects($this->any())
67  ->method('quoteIdentifier')
68  ->willReturnArgument(0);
69  $this->quoteMock->expects($this->any())
70  ->method('quoteTableAs')
71  ->willReturnCallback(
72  function ($tableName, $correlationName) {
73  return $tableName.' AS '.$correlationName;
74  }
75  );
76  $this->selectMock->expects($this->once())
77  ->method('getPart')
78  ->with(Select::FROM)
79  ->willReturn($from);
80  $this->assertEquals($expectedResult, $this->model->render($this->selectMock, $sql));
81  }
82 
87  public function renderDataProvider()
88  {
89  return [
90  [
91  [['joinType' => Select::FROM, 'schema' => null, 'tableName' => 't1', 'joinCondition' => null]],
92  'SELECT *',
93  'SELECT * FROM t1 AS 0'
94  ],
95  [
96  [
97  'a' => ['joinType' => Select::FROM, 'schema' => null, 'tableName' => 't1', 'joinCondition' => null],
98  'b' => ['joinType' => Select::FROM, 'schema' => null, 'tableName' => 't2', 'joinCondition' => null]
99  ],
100  'SELECT a.*',
101  'SELECT a.* FROM t1 AS a' . "\n" . ' INNER JOIN t2 AS b'
102  ],
103  [
104  [
105  'a' => ['joinType' => Select::FROM, 'schema' => null, 'tableName' => 't1', 'joinCondition' => null],
106  'b' => [
107  'joinType' => Select::LEFT_JOIN,
108  'schema' => 'db',
109  'tableName' => 't2',
110  'joinCondition' => 't1.f1 = t2.f2'
111  ]
112  ],
113  'SELECT b.f2',
114  'SELECT b.f2 FROM t1 AS a' . "\n" . ' LEFT JOIN db.t2 AS b ON t1.f1 = t2.f2'
115  ]
116  ];
117  }
118 }
$tableName
Definition: trigger.php:13
$objectManager
Definition: bootstrap.php:17
const FROM
Definition: Select.php:49
const LEFT_JOIN
Definition: Select.php:60