Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataConverterTest.php
Go to the documentation of this file.
1 <?php
7 
17 
18 class DataConverterTest extends \PHPUnit\Framework\TestCase
19 {
23  private $queryModifierMock;
24 
28  private $dataConverter;
29 
33  private $iteratorMock;
34 
38  private $queryGeneratorMock;
39 
43  private $selectByRangeMock;
44 
48  private $adapterMock;
49 
53  private $fieldDataConverter;
54 
58  private $objectManager;
59 
63  protected function setUp()
64  {
65  $this->objectManager = Bootstrap::getObjectManager();
66 
68  $this->queryModifierMock = $this->getMockBuilder(QueryModifierInterface::class)
69  ->disableOriginalConstructor()
70  ->setMethods(['modify'])
71  ->getMock();
72 
73  $this->dataConverter = $this->objectManager->get(SerializedToJson::class);
74 
75  $this->iteratorMock = $this->getMockBuilder(BatchIterator::class)
76  ->disableOriginalConstructor()
77  ->setMethods(['current', 'valid', 'next'])
78  ->getMock();
79 
80  $iterationComplete = false;
81 
82  // mock valid() call so iterator passes only current() result in foreach invocation
83  $this->iteratorMock->expects($this->any())
84  ->method('valid')
85  ->willReturnCallback(
86  function () use (&$iterationComplete) {
87  if (!$iterationComplete) {
88  $iterationComplete = true;
89  return true;
90  } else {
91  return false;
92  }
93  }
94  );
95 
96  $this->queryGeneratorMock = $this->getMockBuilder(Generator::class)
97  ->disableOriginalConstructor()
98  ->setMethods(['generate'])
99  ->getMock();
100 
101  $this->selectByRangeMock = $this->getMockBuilder(Select::class)
102  ->disableOriginalConstructor()
103  ->setMethods([])
104  ->getMock();
105 
106  $this->queryGeneratorMock->expects($this->any())
107  ->method('generate')
108  ->will($this->returnValue($this->iteratorMock));
109 
110  // mocking only current as next() is not supposed to be called
111  $this->iteratorMock->expects($this->any())
112  ->method('current')
113  ->will($this->returnValue($this->selectByRangeMock));
114 
115  $this->adapterMock = $this->getMockBuilder(Mysql::class)
116  ->disableOriginalConstructor()
117  ->setMethods(['fetchPairs', 'quoteInto', 'update'])
118  ->getMock();
119 
120  $this->adapterMock->expects($this->any())
121  ->method('quoteInto')
122  ->will($this->returnValue('field=value'));
123 
124  $this->fieldDataConverter = $this->objectManager->create(
125  FieldDataConverter::class,
126  [
127  'queryGenerator' => $this->queryGeneratorMock,
128  'dataConverter' => $this->dataConverter
129  ]
130  );
131  }
132 
140  {
141  $rows = [
142  1 => 'N;',
143  2 => 'a:2:{s:3:"foo";s:3:"bar";s:3:"bar";s:',
144  ];
145 
146  $this->adapterMock->expects($this->any())
147  ->method('fetchPairs')
148  ->with($this->selectByRangeMock)
149  ->will($this->returnValue($rows));
150 
151  $this->adapterMock->expects($this->once())
152  ->method('update')
153  ->with('table', ['value' => 'null'], ['id IN (?)' => [1]]);
154 
155  $this->fieldDataConverter->convert($this->adapterMock, 'table', 'id', 'value', $this->queryModifierMock);
156  }
157 
159  {
160  $rows = [
161  2 => '[]',
162  3 => '{}',
163  4 => 'null',
164  5 => '""',
165  6 => '0',
166  7 => 'N;',
167  8 => '{"valid": "json value"}',
168  ];
169 
170  $this->adapterMock->expects($this->any())
171  ->method('fetchPairs')
172  ->with($this->selectByRangeMock)
173  ->will($this->returnValue($rows));
174 
175  $this->adapterMock->expects($this->once())
176  ->method('update')
177  ->with('table', ['value' => 'null'], ['id IN (?)' => [7]]);
178 
179  $this->fieldDataConverter->convert($this->adapterMock, 'table', 'id', 'value', $this->queryModifierMock);
180  }
181 }