Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractMapperTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class AbstractMapperTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $resourceMock;
20 
24  protected $connectionMock;
25 
29  protected $selectMock;
30 
34  protected $loggerMock;
35 
39  protected $fetchStrategyMock;
40 
44  protected $objectFactoryMock;
45 
49  protected $mapperFactoryMock;
50 
54  protected $mapper;
55 
61  protected function setUp()
62  {
63  $this->resourceMock = $this->getMockForAbstractClass(
64  \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
65  [],
66  '',
67  false,
68  true,
69  true,
70  []
71  );
72  $this->connectionMock = $this->getMockForAbstractClass(
73  \Magento\Framework\DB\Adapter\AdapterInterface::class,
74  [],
75  '',
76  false,
77  true,
78  true,
79  []
80  );
81  $this->selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
82  $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
83  $this->fetchStrategyMock = $this->getMockForAbstractClass(
84  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface::class,
85  [],
86  '',
87  false,
88  true,
89  true,
90  []
91  );
92  $this->objectFactoryMock = $this->createMock(\Magento\Framework\Data\ObjectFactory::class);
93  $this->mapperFactoryMock = $this->createMock(\Magento\Framework\DB\MapperFactory::class);
94  }
95 
105  public function testMap(array $mapperMethods, array $criteriaParts)
106  {
108  $mapper = $this->getMockForAbstractClass(
109  \Magento\Framework\DB\AbstractMapper::class,
110  [
111  'logger' => $this->loggerMock,
112  'fetchStrategy' => $this->fetchStrategyMock,
113  'objectFactory' => $this->objectFactoryMock,
114  'mapperFactory' => $this->mapperFactoryMock,
115  'select' => $this->selectMock
116  ],
117  '',
118  true,
119  true,
120  true,
121  $mapperMethods
122  );
123  $criteriaMock = $this->getMockForAbstractClass(
124  \Magento\Framework\Api\CriteriaInterface::class,
125  [],
126  '',
127  false,
128  true,
129  true,
130  ['toArray']
131  );
132  $criteriaMock->expects($this->once())
133  ->method('toArray')
134  ->will($this->returnValue($criteriaParts));
135  foreach ($mapperMethods as $value => $method) {
136  $mapper->expects($this->once())
137  ->method($method)
138  ->with($value);
139  }
140 
141  $this->assertEquals($this->selectMock, $mapper->map($criteriaMock));
142  }
143 
144  public function testMapException()
145  {
146  $mapperMethods = [
147  'my-test-value1' => 'mapMyMapperMethodOne'
148  ];
149 
150  $criteriaParts = [
151  'my_mapper_method_one' => 'my-test-value1'
152  ];
154  $mapper = $this->getMockForAbstractClass(
155  \Magento\Framework\DB\AbstractMapper::class,
156  [
157  'logger' => $this->loggerMock,
158  'fetchStrategy' => $this->fetchStrategyMock,
159  'objectFactory' => $this->objectFactoryMock,
160  'mapperFactory' => $this->mapperFactoryMock,
161  'select' => $this->selectMock
162  ],
163  '',
164  true,
165  true,
166  true,
167  $mapperMethods
168  );
169  $criteriaMock = $this->getMockForAbstractClass(
170  \Magento\Framework\Api\CriteriaInterface::class,
171  [],
172  '',
173  false,
174  true,
175  true,
176  ['toArray']
177  );
178  $criteriaMock->expects($this->once())
179  ->method('toArray')
180  ->will($this->returnValue($criteriaParts));
181  $this->expectException(\InvalidArgumentException::class);
182  $mapper->map($criteriaMock);
183  }
184 
190  public function testAddExpressionFieldToSelect()
191  {
192  $fields = [
193  'key-attribute' => 'value-attribute',
194  ];
196  $mapper = $this->getMockForAbstractClass(
197  \Magento\Framework\DB\AbstractMapper::class,
198  [
199  'logger' => $this->loggerMock,
200  'fetchStrategy' => $this->fetchStrategyMock,
201  'objectFactory' => $this->objectFactoryMock,
202  'mapperFactory' => $this->mapperFactoryMock,
203  'select' => $this->selectMock
204  ],
205  '',
206  true,
207  true,
208  true,
209  []
210  );
211 
212  $this->selectMock->expects($this->once())
213  ->method('columns')
214  ->with(['my-alias' => "('sub_total', 'SUM(value-attribute)', 'revenue')"]);
215 
216  $mapper->addExpressionFieldToSelect('my-alias', "('sub_total', 'SUM({{key-attribute}})', 'revenue')", $fields);
217  }
218 
228  public function testAddFieldToFilter($field, $condition)
229  {
230  $resultCondition = 'sql-condition-value';
231 
233  $mapper = $this->getMockForAbstractClass(
234  \Magento\Framework\DB\AbstractMapper::class,
235  [
236  'logger' => $this->loggerMock,
237  'fetchStrategy' => $this->fetchStrategyMock,
238  'objectFactory' => $this->objectFactoryMock,
239  'mapperFactory' => $this->mapperFactoryMock,
240  'select' => $this->selectMock
241  ],
242  '',
243  true,
244  true,
245  true,
246  ['getConnection']
247  );
248  $connectionMock = $this->getMockForAbstractClass(
249  \Magento\Framework\DB\Adapter\AdapterInterface::class,
250  [],
251  '',
252  true,
253  true,
254  true,
255  ['quoteIdentifier', 'prepareSqlCondition']
256  );
257 
258  $mapper->expects($this->any())
259  ->method('getConnection')
260  ->will($this->returnValue($connectionMock));
261  $connectionMock->expects($this->any())
262  ->method('quoteIdentifier')
263  ->with('my-field')
264  ->will($this->returnValue('quote-field'));
265  $connectionMock->expects($this->any())
266  ->method('prepareSqlCondition')
267  ->with('quote-field', $condition)
268  ->will($this->returnValue($resultCondition));
269 
270  if (is_array($field)) {
271  $resultCondition = '(' . implode(
272  ') ' . \Magento\Framework\DB\Select::SQL_OR . ' (',
273  array_fill(0, count($field), $resultCondition)
274  ) . ')';
275  }
276 
277  $this->selectMock->expects($this->once())
278  ->method('where')
279  ->with($resultCondition, null, Select::TYPE_CONDITION);
280 
281  $mapper->addFieldToFilter($field, $condition);
282  }
283 
289  public function dataProviderMap()
290  {
291  return [
292  [
293  'mapperMethods' => [
294  'my-test-value1' => 'mapMyMapperMethodOne',
295  'my-test-value2' => 'mapMyMapperMethodTwo',
296  ],
297  'criteriaParts' => [
298  'my_mapper_method_one' => ['my-test-value1'],
299  'my_mapper_method_two' => ['my-test-value2'],
300  ],
301  ]
302  ];
303  }
304 
311  {
312  return [
313  [
314  'field' => 'my-field',
315  'condition' => ['condition'],
316  ],
317  [
318  'field' => ['my-field', 'my-field'],
319  'condition' => null
320  ],
321  ];
322  }
323 }
$fields
Definition: details.phtml:14
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
const SQL_OR
Definition: Select.php:79