8
8
use Magento \Framework \DB \Adapter \Pdo \Mysql ;
9
9
use Magento \Framework \DB \FieldDataConverter ;
10
10
use Magento \Framework \DB \Select ;
11
+ use Magento \Framework \DB \Select \QueryModifierInterface ;
11
12
use Magento \Framework \DB \Select \InQueryModifier ;
12
13
use Magento \Framework \Serialize \Serializer \Serialize ;
13
14
use Magento \TestFramework \Helper \Bootstrap ;
15
+ use Magento \Framework \DB \Query \Generator ;
16
+ use Magento \Framework \DB \Query \BatchIterator ;
17
+ use Magento \Framework \ObjectManagerInterface ;
14
18
15
19
class DataConverterTest extends \PHPUnit_Framework_TestCase
16
20
{
17
- /**
18
- * @var \Magento\Framework\ObjectManagerInterface
19
- */
20
- private $ objectManager ;
21
-
22
21
/**
23
22
* @var InQueryModifier|\PHPUnit_Framework_MockObject_MockObject
24
23
*/
@@ -30,12 +29,12 @@ class DataConverterTest extends \PHPUnit_Framework_TestCase
30
29
private $ dataConverter ;
31
30
32
31
/**
33
- * @var \Magento\Framework\DB\Query\ BatchIterator|\PHPUnit_Framework_MockObject_MockObject
32
+ * @var BatchIterator|\PHPUnit_Framework_MockObject_MockObject
34
33
*/
35
34
private $ iteratorMock ;
36
35
37
36
/**
38
- * @var \Magento\Framework\DB\Query\ Generator|\PHPUnit_Framework_MockObject_MockObject
37
+ * @var Generator|\PHPUnit_Framework_MockObject_MockObject
39
38
*/
40
39
private $ queryGeneratorMock ;
41
40
@@ -54,6 +53,11 @@ class DataConverterTest extends \PHPUnit_Framework_TestCase
54
53
*/
55
54
private $ fieldDataConverter ;
56
55
56
+ /**
57
+ * @var ObjectManagerInterface
58
+ */
59
+ private $ objectManager ;
60
+
57
61
/**
58
62
* Set up before test
59
63
*/
@@ -62,23 +66,24 @@ protected function setUp()
62
66
$ this ->objectManager = Bootstrap::getObjectManager ();
63
67
64
68
/** @var InQueryModifier $queryModifier */
65
- $ this ->queryModifierMock = $ this ->getMockBuilder (Select \ QueryModifierInterface::class)
69
+ $ this ->queryModifierMock = $ this ->getMockBuilder (QueryModifierInterface::class)
66
70
->disableOriginalConstructor ()
67
71
->setMethods (['modify ' ])
68
72
->getMock ();
69
73
70
74
$ this ->dataConverter = $ this ->objectManager ->get (SerializedToJson::class);
71
75
72
- $ this ->iteratorMock = $ this ->getMockBuilder (\ Magento \ Framework \ DB \ Query \ BatchIterator::class)
76
+ $ this ->iteratorMock = $ this ->getMockBuilder (BatchIterator::class)
73
77
->disableOriginalConstructor ()
74
78
->setMethods (['current ' , 'valid ' , 'next ' ])
75
79
->getMock ();
76
80
77
81
$ iterationComplete = false ;
78
82
79
83
// mock valid() call so iterator passes only current() result in foreach invocation
80
- $ this ->iteratorMock ->expects ($ this ->any ())->method ('valid ' )->will (
81
- $ this ->returnCallback (
84
+ $ this ->iteratorMock ->expects ($ this ->any ())
85
+ ->method ('valid ' )
86
+ ->willReturnCallback (
82
87
function () use (&$ iterationComplete ) {
83
88
if (!$ iterationComplete ) {
84
89
$ iterationComplete = true ;
@@ -87,10 +92,9 @@ function () use (&$iterationComplete) {
87
92
return false ;
88
93
}
89
94
}
90
- )
91
- );
95
+ );
92
96
93
- $ this ->queryGeneratorMock = $ this ->getMockBuilder (\ Magento \ Framework \ DB \ Query \ Generator::class)
97
+ $ this ->queryGeneratorMock = $ this ->getMockBuilder (Generator::class)
94
98
->disableOriginalConstructor ()
95
99
->setMethods (['generate ' ])
96
100
->getMock ();
@@ -111,7 +115,7 @@ function () use (&$iterationComplete) {
111
115
112
116
$ this ->adapterMock = $ this ->getMockBuilder (Mysql::class)
113
117
->disableOriginalConstructor ()
114
- ->setMethods (['fetchAll ' , 'quoteInto ' , 'update ' ])
118
+ ->setMethods (['fetchPairs ' , 'quoteInto ' , 'update ' ])
115
119
->getMock ();
116
120
117
121
$ this ->adapterMock ->expects ($ this ->any ())
@@ -129,6 +133,7 @@ function () use (&$iterationComplete) {
129
133
130
134
/**
131
135
* Test that exception with valid text is thrown when data is corrupted
136
+ *
132
137
* @expectedException \Magento\Framework\DB\FieldDataConversionException
133
138
* @expectedExceptionMessage Error converting field `value` in table `table` where `id`=2 using
134
139
*/
@@ -140,18 +145,18 @@ public function testDataConvertErrorReporting()
140
145
$ serializedDataLength = strlen ($ serializedData );
141
146
$ brokenSerializedData = substr ($ serializedData , 0 , $ serializedDataLength - 6 );
142
147
$ rows = [
143
- [ ' id ' => 1 , ' value ' => 'N; ' ] ,
144
- [ ' id ' => 2 , ' value ' => $ brokenSerializedData] ,
148
+ 1 => 'N; ' ,
149
+ 2 => $ brokenSerializedData ,
145
150
];
146
151
147
152
$ this ->adapterMock ->expects ($ this ->any ())
148
- ->method ('fetchAll ' )
153
+ ->method ('fetchPairs ' )
149
154
->with ($ this ->selectByRangeMock )
150
155
->will ($ this ->returnValue ($ rows ));
151
156
152
157
$ this ->adapterMock ->expects ($ this ->once ())
153
158
->method ('update ' )
154
- ->with ('table ' , ['value ' => 'null ' ], ['id = ? ' => 1 ]);
159
+ ->with ('table ' , ['value ' => 'null ' ], ['id IN (?) ' => [ 1 ] ]);
155
160
156
161
$ this ->fieldDataConverter ->convert ($ this ->adapterMock , 'table ' , 'id ' , 'value ' , $ this ->queryModifierMock );
157
162
}
@@ -161,23 +166,23 @@ public function testDataConvertErrorReporting()
161
166
public function testAlreadyConvertedDataSkipped ()
162
167
{
163
168
$ rows = [
164
- [ ' id ' => 2 , ' value ' => '[] ' ] ,
165
- [ ' id ' => 3 , ' value ' => '{} ' ] ,
166
- [ ' id ' => 4 , ' value ' => 'null ' ] ,
167
- [ ' id ' => 5 , ' value ' => '"" ' ] ,
168
- [ ' id ' => 6 , ' value ' => '0 ' ] ,
169
- [ ' id ' => 7 , ' value ' => 'N; ' ] ,
170
- [ ' id ' => 8 , ' value ' => '{"valid": "json value"} ' ] ,
169
+ 2 => '[] ' ,
170
+ 3 => '{} ' ,
171
+ 4 => 'null ' ,
172
+ 5 => '"" ' ,
173
+ 6 => '0 ' ,
174
+ 7 => 'N; ' ,
175
+ 8 => '{"valid": "json value"} ' ,
171
176
];
172
177
173
178
$ this ->adapterMock ->expects ($ this ->any ())
174
- ->method ('fetchAll ' )
179
+ ->method ('fetchPairs ' )
175
180
->with ($ this ->selectByRangeMock )
176
181
->will ($ this ->returnValue ($ rows ));
177
182
178
183
$ this ->adapterMock ->expects ($ this ->once ())
179
184
->method ('update ' )
180
- ->with ('table ' , ['value ' => 'null ' ], ['id = ? ' => 7 ]);
185
+ ->with ('table ' , ['value ' => 'null ' ], ['id IN (?) ' => [ 7 ] ]);
181
186
182
187
$ this ->fieldDataConverter ->convert ($ this ->adapterMock , 'table ' , 'id ' , 'value ' , $ this ->queryModifierMock );
183
188
}
0 commit comments