UpdateRowTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\EntityManager\Test\Unit\Db;
  7. use Magento\Framework\EntityManager\Db\UpdateRow;
  8. use Magento\Framework\EntityManager\MetadataPool;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. use Magento\Framework\DB\Adapter\AdapterInterface;
  12. use Magento\Framework\EntityManager\EntityMetadataInterface;
  13. /**
  14. * Class UpdateRowTest
  15. */
  16. class UpdateRowTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var UpdateRow
  20. */
  21. protected $model;
  22. /**
  23. * @var MetadataPool|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $metadataPoolMock;
  26. /**
  27. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $resourceConnectionMock;
  30. /**
  31. * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $connectionMock;
  34. /**
  35. * @var EntityMetadataInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $metadataMock;
  38. protected function setUp()
  39. {
  40. $this->metadataPoolMock = $this->getMockBuilder(MetadataPool::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->metadataMock = $this->getMockBuilder(EntityMetadataInterface::class)
  47. ->getMockForAbstractClass();
  48. $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
  49. ->getMockForAbstractClass();
  50. $this->metadataMock = $this->getMockBuilder(EntityMetadataInterface::class)
  51. ->getMockForAbstractClass();
  52. $this->model = (new ObjectManager($this))->getObject(UpdateRow::class, [
  53. 'metadataPool' => $this->metadataPoolMock,
  54. 'resourceConnection' => $this->resourceConnectionMock,
  55. ]);
  56. }
  57. /**
  58. * @dataProvider columnsDataProvider
  59. * @param array $data
  60. * @param array $columns
  61. * @param array $preparedColumns
  62. */
  63. public function testExecute(array $data, array $columns, array $preparedColumns)
  64. {
  65. $primaryKeyName = 'entity_id';
  66. $this->metadataPoolMock->expects($this->once())
  67. ->method('getMetadata')
  68. ->with('test')
  69. ->willReturn($this->metadataMock);
  70. $this->resourceConnectionMock->expects($this->once())
  71. ->method('getConnectionByName')
  72. ->willReturn($this->connectionMock);
  73. $this->metadataMock->expects($this->once())
  74. ->method('getEntityConnectionName')
  75. ->willReturn('test_connection_name');
  76. $this->metadataMock->expects($this->atLeastOnce())
  77. ->method('getEntityTable')
  78. ->willReturn('test_entity_table');
  79. $this->connectionMock->expects($this->once())
  80. ->method('update')
  81. ->with('test_entity_table', $preparedColumns, ['test_link_field' . ' = ?' => $data['test_link_field']]);
  82. $this->connectionMock->expects($this->once())->method('getIndexList')
  83. ->willReturn([$primaryKeyName => ['COLUMNS_LIST' => ['test_link_field']]]);
  84. $this->connectionMock->expects($this->once())->method('getPrimaryKeyName')
  85. ->willReturn($primaryKeyName);
  86. $this->connectionMock->expects($this->once())
  87. ->method('describeTable')
  88. ->willReturn($columns);
  89. $this->metadataMock->expects($this->exactly(2))
  90. ->method('getIdentifierField')
  91. ->willReturn('test_identified_field');
  92. if (empty($data['updated_at'])) {
  93. unset($data['updated_at']);
  94. }
  95. $this->assertSame($data, $this->model->execute('test', $data));
  96. }
  97. /**
  98. * @return array
  99. */
  100. public function columnsDataProvider()
  101. {
  102. $data = [
  103. 'test_link_field' => 1,
  104. 'identified_field' => 'test_identified_field',
  105. 'test_simple' => 'test_value',
  106. ];
  107. $columns = [
  108. 'test_nullable' => [
  109. 'NULLABLE' => true,
  110. 'DEFAULT' => false,
  111. 'IDENTITY' => false,
  112. 'COLUMN_NAME' => 'test_nullable',
  113. ],
  114. 'test_simple' => [
  115. 'NULLABLE' => true,
  116. 'DEFAULT' => false,
  117. 'IDENTITY' => false,
  118. 'COLUMN_NAME' => 'test_simple',
  119. ],
  120. ];
  121. $preparedColumns = [
  122. 'test_identified_field' => null,
  123. 'test_nullable' => null,
  124. 'test_simple' => 'test_value',
  125. ];
  126. return [
  127. 'default' => [
  128. 'data' => $data,
  129. 'columns' => $columns,
  130. 'preparedColumns' => $preparedColumns,
  131. ],
  132. 'empty timestamp field' => [
  133. 'data' => array_merge($data, ['updated_at' => '']),
  134. 'columns' => array_merge(
  135. $columns,
  136. [
  137. 'updated_at' => [
  138. 'NULLABLE' => false,
  139. 'DEFAULT' => 'CURRENT_TIMESTAMP',
  140. 'IDENTITY' => false,
  141. 'COLUMN_NAME' => 'updated_at',
  142. ],
  143. ]
  144. ),
  145. 'preparedColumns' => $preparedColumns,
  146. ],
  147. 'filled timestamp field' => [
  148. 'data' => array_merge($data, ['updated_at' => '2016-01-01 00:00:00']),
  149. 'columns' => array_merge(
  150. $columns,
  151. [
  152. 'updated_at' => [
  153. 'NULLABLE' => false,
  154. 'DEFAULT' => 'CURRENT_TIMESTAMP',
  155. 'IDENTITY' => false,
  156. 'COLUMN_NAME' => 'updated_at',
  157. ],
  158. ]
  159. ),
  160. 'preparedColumns' => array_merge($preparedColumns, ['updated_at' => '2016-01-01 00:00:00']),
  161. ],
  162. ];
  163. }
  164. }