Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FieldDataConverterTest.php
Go to the documentation of this file.
1 <?php
7 
16 
17 class FieldDataConverterTest extends \PHPUnit\Framework\TestCase
18 {
22  private $connectionMock;
23 
27  private $queryGeneratorMock;
28 
32  private $dataConverterMock;
33 
37  private $selectMock;
38 
42  private $queryModifierMock;
43 
47  private $selectFactoryMock;
48 
52  private $fieldDataConverter;
53 
57  private $objectManager;
58 
59  protected function setUp()
60  {
61  $this->objectManager = new ObjectManager($this);
62  $this->connectionMock = $this->createMock(AdapterInterface::class);
63  $this->queryGeneratorMock = $this->createMock(Generator::class);
64  $this->dataConverterMock = $this->createMock(DataConverterInterface::class);
65  $this->selectMock = $this->createMock(Select::class);
66  $this->queryModifierMock = $this->createMock(QueryModifierInterface::class);
67  $this->selectFactoryMock = $this->getMockBuilder(SelectFactory::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70  $this->fieldDataConverter = $this->objectManager->getObject(
71  FieldDataConverter::class,
72  [
73  'queryGenerator' => $this->queryGeneratorMock,
74  'dataConverter' => $this->dataConverterMock,
75  'selectFactory' => $this->selectFactoryMock,
76  ]
77  );
78  }
79 
85  public function testConvert($useQueryModifier, $numQueryModifierCalls)
86  {
87  $table = 'table';
88  $identifier = 'id';
89  $field = 'field';
90  $where = $field . ' IS NOT NULL';
91  $iterator = ['query 1'];
92  $rows = [1 => 'value'];
93  $convertedValue = 'converted value';
94  $this->selectFactoryMock->expects($this->once())
95  ->method('create')
96  ->with($this->connectionMock)
97  ->willReturn($this->selectMock);
98  $this->selectMock->expects($this->once())
99  ->method('from')
100  ->with(
101  $table,
102  [$identifier, $field]
103  )
104  ->willReturnSelf();
105  $this->selectMock->expects($this->once())
106  ->method('where')
107  ->with($where)
108  ->willReturnSelf();
109  $this->queryModifierMock->expects($this->exactly($numQueryModifierCalls))
110  ->method('modify')
111  ->with($this->selectMock);
112  $this->queryGeneratorMock->expects($this->once())
113  ->method('generate')
114  ->with($identifier, $this->selectMock)
115  ->willReturn($iterator);
116  $this->connectionMock->expects($this->once())
117  ->method('fetchPairs')
118  ->with($iterator[0])
119  ->willReturn($rows);
120  $this->dataConverterMock->expects($this->once())
121  ->method('convert')
122  ->with($rows[1])
123  ->willReturn($convertedValue);
124  $this->connectionMock->expects($this->once())
125  ->method('update')
126  ->with(
127  $table,
128  [$field => $convertedValue],
129  [$identifier . ' IN (?)' => [1]]
130  );
131  $this->fieldDataConverter->convert(
132  $this->connectionMock,
133  $table,
134  $identifier,
135  $field,
136  $useQueryModifier ? $this->queryModifierMock : null
137  );
138  }
139 
143  public function convertDataProvider()
144  {
145  return [
146  [false, 0, ],
147  [true, 1]
148  ];
149  }
150 
155  public function testConvertBatchSizeFromEnv($envBatchSize, $usedBatchSize)
156  {
157  $table = 'table';
158  $identifier = 'id';
159  $field = 'field';
160  $where = $field . ' IS NOT NULL';
161  $this->selectFactoryMock->expects($this->once())
162  ->method('create')
163  ->with($this->connectionMock)
164  ->willReturn($this->selectMock);
165  $this->selectMock->expects($this->once())
166  ->method('from')
167  ->with(
168  $table,
169  [$identifier, $field]
170  )
171  ->willReturnSelf();
172  $this->selectMock->expects($this->once())
173  ->method('where')
174  ->with($where)
175  ->willReturnSelf();
176  $this->queryGeneratorMock->expects($this->once())
177  ->method('generate')
178  ->with($identifier, $this->selectMock, $usedBatchSize)
179  ->willReturn([]);
180  $fieldDataConverter = $this->objectManager->getObject(
181  FieldDataConverter::class,
182  [
183  'queryGenerator' => $this->queryGeneratorMock,
184  'dataConverter' => $this->dataConverterMock,
185  'selectFactory' => $this->selectFactoryMock,
186  'envBatchSize' => $envBatchSize
187  ]
188  );
189  $fieldDataConverter->convert(
190  $this->connectionMock,
191  $table,
192  $identifier,
193  $field
194  );
195  }
196 
201  {
202  return [
204  [100000, 100000],
205  ];
206  }
207 
216  public function testConvertBatchSizeFromEnvInvalid($batchSize)
217  {
218  $table = 'table';
219  $identifier = 'id';
220  $field = 'field';
221  $where = $field . ' IS NOT NULL';
222  $this->selectFactoryMock->expects($this->once())
223  ->method('create')
224  ->with($this->connectionMock)
225  ->willReturn($this->selectMock);
226  $this->selectMock->expects($this->once())
227  ->method('from')
228  ->with(
229  $table,
230  [$identifier, $field]
231  )
232  ->willReturnSelf();
233  $this->selectMock->expects($this->once())
234  ->method('where')
235  ->with($where)
236  ->willReturnSelf();
237  $fieldDataConverter = $this->objectManager->getObject(
238  FieldDataConverter::class,
239  [
240  'queryGenerator' => $this->queryGeneratorMock,
241  'dataConverter' => $this->dataConverterMock,
242  'selectFactory' => $this->selectFactoryMock,
243  'envBatchSize' => $batchSize
244  ]
245  );
246  $fieldDataConverter->convert(
247  $this->connectionMock,
248  $table,
249  $identifier,
250  $field
251  );
252  }
253 
258  {
259  return [
260  ['value'],
261  [bcadd(PHP_INT_MAX, 1)],
262  ];
263  }
264 }
testConvert($useQueryModifier, $numQueryModifierCalls)
$table
Definition: trigger.php:14