Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GenericMapperTest.php
Go to the documentation of this file.
1 <?php
7 
11 class GenericMapperTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $selectMock;
17 
21  protected $mapperFactoryMock;
22 
26  protected $geneticMapper;
27 
33  protected function setUp()
34  {
35  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
36 
37  $this->selectMock = $this->createPartialMock(
38  \Magento\Framework\DB\Select::class,
39  ['orWhere', 'where', 'setPart', 'getPart']
40  );
41  $this->mapperFactoryMock = $this->createPartialMock(\Magento\Framework\DB\MapperFactory::class, ['create']);
42 
43  $this->geneticMapper = $objectManager->getObject(
44  \Magento\Framework\DB\GenericMapper::class,
45  [
46  'select' => $this->selectMock,
47  'mapperFactory' => $this->mapperFactoryMock,
48  ]
49  );
50  }
51 
57  public function testMapCriteriaList()
58  {
59  $criteriaMock = $this->getMockForAbstractClass(
60  \Magento\Framework\Api\CriteriaInterface::class,
61  [],
62  '',
63  false,
64  true,
65  true,
66  ['getMapperInterfaceName']
67  );
68  $mapperInstanceMock = $this->getMockForAbstractClass(
69  \Magento\Framework\DB\MapperInterface::class,
70  [],
71  '',
72  false,
73  true,
74  true,
75  ['map']
76  );
77 
78  $criteriaMock->expects($this->any())
79  ->method('getMapperInterfaceName')
80  ->will($this->returnValue('mapper-name'));
81  $this->mapperFactoryMock->expects($this->exactly(4))
82  ->method('create')
83  ->with('mapper-name', ['select' => $this->selectMock])
84  ->will($this->returnValue($mapperInstanceMock));
85  $mapperInstanceMock->expects($this->exactly(4))
86  ->method('map')
87  ->will($this->returnValue($this->selectMock));
88 
89  $this->geneticMapper->mapCriteriaList(array_fill(0, 4, $criteriaMock));
90  }
91 
97  public function testMapFilters()
98  {
99  $filters = [
100  [
101  'type' => 'or',
102  'field' => 'test-field',
103  'condition' => 'test-condition',
104  ],
105  [
106  'type' => 'string',
107  'field' => 'test-field',
108  'condition' => 'test-condition'
109  ],
110  [
111  'type' => 'public',
112  'field' => 'test-field',
113  'condition' => 'test-condition'
114  ],
115  [
116  'type' => 'default',
117  'field' => 'test-field',
118  'condition' => 'test-condition'
119  ],
120  ];
121 
122  $connectionMock = $this->getMockForAbstractClass(
123  \Magento\Framework\DB\Adapter\AdapterInterface::class,
124  [],
125  '',
126  false,
127  true,
128  true,
129  ['quoteInto', 'prepareSqlCondition']
130  );
131 
134  $this->createPartialMock(\Magento\Framework\DB\GenericMapper::class, ['getConnection', 'getSelect']);
135 
136  $geneticMapper->expects($this->any())
137  ->method('getConnection')
138  ->will($this->returnValue($connectionMock));
139  $geneticMapper->expects($this->exactly(4))
140  ->method('getSelect')
141  ->will($this->returnValue($this->selectMock));
142  $connectionMock->expects($this->exactly(2))
143  ->method('quoteInto')
144  ->with('test-field=?', 'test-condition')
145  ->will($this->returnValue('test-condition'));
146  $this->selectMock->expects($this->once())
147  ->method('orWhere')
148  ->with('test-condition');
149  $this->selectMock->expects($this->exactly(3))
150  ->method('where')
151  ->with('test-condition');
152  $connectionMock->expects($this->any())
153  ->method('prepareSqlCondition')
154  ->with('test-field', 'test-condition')
155  ->will($this->returnValue('test-condition'));
156 
157  $geneticMapper->mapFilters($filters);
158  }
159 
165  public function testMapFields()
166  {
167  $fields = [
168  [
169  'test-correlation-name',
170  'test-field',
171  'test-alias',
172  ],
173  [
174  'test-correlation-name',
175  'test-field',
176  null
177  ],
178  [
179  'test-correlation-name',
180  'test-field',
181  'test-alias-unique'
182  ],
183  ];
184 
186  $geneticMapper = $this->createPartialMock(\Magento\Framework\DB\GenericMapper::class, ['getSelect']);
187 
188  $geneticMapper->expects($this->any())
189  ->method('getSelect')
190  ->will($this->returnValue($this->selectMock));
191  $this->selectMock->expects($this->once())
192  ->method('getPart')
193  ->with(\Magento\Framework\DB\Select::COLUMNS)
194  ->willReturn([]);
195  $this->selectMock->expects($this->once())
196  ->method('setPart')
197  ->with(\Magento\Framework\DB\Select::COLUMNS, $this->equalTo($fields));
198 
199  $geneticMapper->mapFields($fields);
200  }
201 }
$objectManager
Definition: bootstrap.php:17
$fields
Definition: details.phtml:14
const COLUMNS
Definition: Select.php:48
$filters
Definition: uploader.phtml:11