Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SelectHydratorTest.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Framework\DB\Sql\JsonSerializableExpression;
15 
16 class SelectHydratorTest extends \PHPUnit\Framework\TestCase
17 {
21  private $selectHydrator;
22 
26  private $resourceConnectionMock;
27 
31  private $connectionMock;
32 
36  private $selectMock;
37 
41  private $objectManagerMock;
42 
46  private $objectManagerHelper;
47 
51  protected function setUp()
52  {
53  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56 
57  $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60 
61  $this->selectMock = $this->getMockBuilder(Select::class)
62  ->disableOriginalConstructor()
63  ->getMock();
64 
65  $this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68 
69  $this->objectManagerHelper = new ObjectManagerHelper($this);
70 
71  $this->selectHydrator = $this->objectManagerHelper->getObject(
72  SelectHydrator::class,
73  [
74  'resourceConnection' => $this->resourceConnectionMock,
75  'objectManager' => $this->objectManagerMock,
76  ]
77  );
78  }
79 
80  public function testExtract()
81  {
82  $selectParts =
83  [
95  ];
96 
97  $result = [];
98  foreach ($selectParts as $part) {
99  $result[$part] = "Part";
100  }
101  $this->selectMock->expects($this->any())
102  ->method('getPart')
103  ->willReturn("Part");
104  $this->assertEquals($this->selectHydrator->extract($this->selectMock), $result);
105  }
106 
113  public function testRecreateWithoutExpression($selectParts, $parts, $partValues)
114  {
115  $this->resourceConnectionMock->expects($this->once())
116  ->method('getConnection')
117  ->willReturn($this->connectionMock);
118  $this->connectionMock->expects($this->once())
119  ->method('select')
120  ->willReturn($this->selectMock);
121  foreach ($parts as $key => $part) {
122  $this->selectMock->expects($this->at($key))
123  ->method('setPart')
124  ->with($part, $partValues[$key]);
125  }
126 
127  $this->assertSame($this->selectMock, $this->selectHydrator->recreate($selectParts));
128  }
129 
134  {
135  return [
136  'Select without expressions' => [
137  [
138  Select::COLUMNS => [
139  [
140  'table_name',
141  'field_name',
142  'alias',
143  ],
144  [
145  'table_name',
146  'field_name_2',
147  'alias_2',
148  ],
149  ]
150  ],
151  [Select::COLUMNS],
152  [[
153  [
154  'table_name',
155  'field_name',
156  'alias',
157  ],
158  [
159  'table_name',
160  'field_name_2',
161  'alias_2',
162  ],
163  ]],
164  ],
165  ];
166  }
167 
174  public function testRecreateWithExpression(
175  array $selectParts,
176  array $expectedParts,
177  array $expressionMocks
178  ) {
179  $this->objectManagerMock
180  ->expects($this->exactly(count($expressionMocks)))
181  ->method('create')
182  ->with($this->isType('string'), $this->isType('array'))
183  ->willReturnOnConsecutiveCalls(...$expressionMocks);
184  $this->resourceConnectionMock
185  ->expects($this->once())
186  ->method('getConnection')
187  ->with()
188  ->willReturn($this->connectionMock);
189  $this->connectionMock
190  ->expects($this->once())
191  ->method('select')
192  ->with()
193  ->willReturn($this->selectMock);
194  foreach (array_keys($selectParts) as $key => $partName) {
195  $this->selectMock
196  ->expects($this->at($key))
197  ->method('setPart')
198  ->with($partName, $expectedParts[$partName]);
199  }
200 
201  $this->assertSame($this->selectMock, $this->selectHydrator->recreate($selectParts));
202  }
203 
208  {
209  $expressionMock = $this->getMockBuilder(JsonSerializableExpression::class)
210  ->disableOriginalConstructor()
211  ->getMock();
212 
213  return [
214  'Select without expressions' => [
215  'Parts' => [
216  Select::COLUMNS => [
217  [
218  'table_name',
219  'field_name',
220  'alias',
221  ],
222  [
223  'table_name',
224  [
225  'class' => 'Some_class',
226  'arguments' => [
227  'expression' => ['some(expression)']
228  ]
229  ],
230  'alias_2',
231  ],
232  ]
233  ],
234  'expectedParts' => [
235  Select::COLUMNS => [
236  [
237  'table_name',
238  'field_name',
239  'alias',
240  ],
241  [
242  'table_name',
243  $expressionMock,
244  'alias_2',
245  ],
246  ]
247  ],
248  'expectedExpressions' => [
249  $expressionMock
250  ]
251  ],
252  ];
253  }
254 }
const FOR_UPDATE
Definition: Select.php:57
testRecreateWithExpression(array $selectParts, array $expectedParts, array $expressionMocks)
const HAVING
Definition: Select.php:53
const ORDER
Definition: Select.php:54
testRecreateWithoutExpression($selectParts, $parts, $partValues)
const FROM
Definition: Select.php:49
const DISTINCT
Definition: Select.php:47
const COLUMNS
Definition: Select.php:48
const LIMIT_OFFSET
Definition: Select.php:56
const GROUP
Definition: Select.php:52
const WHERE
Definition: Select.php:51
const UNION
Definition: Select.php:50
const LIMIT_COUNT
Definition: Select.php:55