FieldDataConverterTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB\Test\Unit;
  7. use Magento\Framework\DB\SelectFactory;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Framework\DB\Query\Generator;
  10. use Magento\Framework\DB\Adapter\AdapterInterface;
  11. use Magento\Framework\DB\FieldDataConverter;
  12. use Magento\Framework\DB\DataConverter\DataConverterInterface;
  13. use Magento\Framework\DB\Select;
  14. use Magento\Framework\DB\Select\QueryModifierInterface;
  15. class FieldDataConverterTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $connectionMock;
  21. /**
  22. * @var Generator|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $queryGeneratorMock;
  25. /**
  26. * @var DataConverterInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $dataConverterMock;
  29. /**
  30. * @var Select|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $selectMock;
  33. /**
  34. * @var QueryModifierInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $queryModifierMock;
  37. /**
  38. * @var SelectFactory|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $selectFactoryMock;
  41. /**
  42. * @var FieldDataConverter
  43. */
  44. private $fieldDataConverter;
  45. /**
  46. * @var ObjectManager
  47. */
  48. private $objectManager;
  49. protected function setUp()
  50. {
  51. $this->objectManager = new ObjectManager($this);
  52. $this->connectionMock = $this->createMock(AdapterInterface::class);
  53. $this->queryGeneratorMock = $this->createMock(Generator::class);
  54. $this->dataConverterMock = $this->createMock(DataConverterInterface::class);
  55. $this->selectMock = $this->createMock(Select::class);
  56. $this->queryModifierMock = $this->createMock(QueryModifierInterface::class);
  57. $this->selectFactoryMock = $this->getMockBuilder(SelectFactory::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->fieldDataConverter = $this->objectManager->getObject(
  61. FieldDataConverter::class,
  62. [
  63. 'queryGenerator' => $this->queryGeneratorMock,
  64. 'dataConverter' => $this->dataConverterMock,
  65. 'selectFactory' => $this->selectFactoryMock,
  66. ]
  67. );
  68. }
  69. /**
  70. * @param boolean $useQueryModifier
  71. * @param int $numQueryModifierCalls
  72. * @dataProvider convertDataProvider
  73. */
  74. public function testConvert($useQueryModifier, $numQueryModifierCalls)
  75. {
  76. $table = 'table';
  77. $identifier = 'id';
  78. $field = 'field';
  79. $where = $field . ' IS NOT NULL';
  80. $iterator = ['query 1'];
  81. $rows = [1 => 'value'];
  82. $convertedValue = 'converted value';
  83. $this->selectFactoryMock->expects($this->once())
  84. ->method('create')
  85. ->with($this->connectionMock)
  86. ->willReturn($this->selectMock);
  87. $this->selectMock->expects($this->once())
  88. ->method('from')
  89. ->with(
  90. $table,
  91. [$identifier, $field]
  92. )
  93. ->willReturnSelf();
  94. $this->selectMock->expects($this->once())
  95. ->method('where')
  96. ->with($where)
  97. ->willReturnSelf();
  98. $this->queryModifierMock->expects($this->exactly($numQueryModifierCalls))
  99. ->method('modify')
  100. ->with($this->selectMock);
  101. $this->queryGeneratorMock->expects($this->once())
  102. ->method('generate')
  103. ->with($identifier, $this->selectMock)
  104. ->willReturn($iterator);
  105. $this->connectionMock->expects($this->once())
  106. ->method('fetchPairs')
  107. ->with($iterator[0])
  108. ->willReturn($rows);
  109. $this->dataConverterMock->expects($this->once())
  110. ->method('convert')
  111. ->with($rows[1])
  112. ->willReturn($convertedValue);
  113. $this->connectionMock->expects($this->once())
  114. ->method('update')
  115. ->with(
  116. $table,
  117. [$field => $convertedValue],
  118. [$identifier . ' IN (?)' => [1]]
  119. );
  120. $this->fieldDataConverter->convert(
  121. $this->connectionMock,
  122. $table,
  123. $identifier,
  124. $field,
  125. $useQueryModifier ? $this->queryModifierMock : null
  126. );
  127. }
  128. /**
  129. * @return array
  130. */
  131. public function convertDataProvider()
  132. {
  133. return [
  134. [false, 0, ],
  135. [true, 1]
  136. ];
  137. }
  138. /**
  139. * @param null|int $envBatchSize
  140. * @dataProvider convertBatchSizeFromEnvDataProvider
  141. */
  142. public function testConvertBatchSizeFromEnv($envBatchSize, $usedBatchSize)
  143. {
  144. $table = 'table';
  145. $identifier = 'id';
  146. $field = 'field';
  147. $where = $field . ' IS NOT NULL';
  148. $this->selectFactoryMock->expects($this->once())
  149. ->method('create')
  150. ->with($this->connectionMock)
  151. ->willReturn($this->selectMock);
  152. $this->selectMock->expects($this->once())
  153. ->method('from')
  154. ->with(
  155. $table,
  156. [$identifier, $field]
  157. )
  158. ->willReturnSelf();
  159. $this->selectMock->expects($this->once())
  160. ->method('where')
  161. ->with($where)
  162. ->willReturnSelf();
  163. $this->queryGeneratorMock->expects($this->once())
  164. ->method('generate')
  165. ->with($identifier, $this->selectMock, $usedBatchSize)
  166. ->willReturn([]);
  167. $fieldDataConverter = $this->objectManager->getObject(
  168. FieldDataConverter::class,
  169. [
  170. 'queryGenerator' => $this->queryGeneratorMock,
  171. 'dataConverter' => $this->dataConverterMock,
  172. 'selectFactory' => $this->selectFactoryMock,
  173. 'envBatchSize' => $envBatchSize
  174. ]
  175. );
  176. $fieldDataConverter->convert(
  177. $this->connectionMock,
  178. $table,
  179. $identifier,
  180. $field
  181. );
  182. }
  183. /**
  184. * @return array
  185. */
  186. public function convertBatchSizeFromEnvDataProvider()
  187. {
  188. return [
  189. [null, FieldDataConverter::DEFAULT_BATCH_SIZE],
  190. [100000, 100000],
  191. ];
  192. }
  193. /**
  194. * @param string|int $batchSize
  195. * @expectedException \InvalidArgumentException
  196. * @codingStandardsIgnoreStart
  197. * @expectedExceptionMessage Invalid value for environment variable DATA_CONVERTER_BATCH_SIZE. Should be integer, >= 1 and < value of PHP_INT_MAX
  198. * @codingStandardsIgnoreEnd
  199. * @dataProvider convertBatchSizeFromEnvInvalidDataProvider
  200. */
  201. public function testConvertBatchSizeFromEnvInvalid($batchSize)
  202. {
  203. $table = 'table';
  204. $identifier = 'id';
  205. $field = 'field';
  206. $where = $field . ' IS NOT NULL';
  207. $this->selectFactoryMock->expects($this->once())
  208. ->method('create')
  209. ->with($this->connectionMock)
  210. ->willReturn($this->selectMock);
  211. $this->selectMock->expects($this->once())
  212. ->method('from')
  213. ->with(
  214. $table,
  215. [$identifier, $field]
  216. )
  217. ->willReturnSelf();
  218. $this->selectMock->expects($this->once())
  219. ->method('where')
  220. ->with($where)
  221. ->willReturnSelf();
  222. $fieldDataConverter = $this->objectManager->getObject(
  223. FieldDataConverter::class,
  224. [
  225. 'queryGenerator' => $this->queryGeneratorMock,
  226. 'dataConverter' => $this->dataConverterMock,
  227. 'selectFactory' => $this->selectFactoryMock,
  228. 'envBatchSize' => $batchSize
  229. ]
  230. );
  231. $fieldDataConverter->convert(
  232. $this->connectionMock,
  233. $table,
  234. $identifier,
  235. $field
  236. );
  237. }
  238. /**
  239. * @return array
  240. */
  241. public function convertBatchSizeFromEnvInvalidDataProvider()
  242. {
  243. return [
  244. ['value'],
  245. [bcadd(PHP_INT_MAX, 1)],
  246. ];
  247. }
  248. }