SequenceTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Migration\Handler;
  7. class SequenceTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @param string $table
  11. * @param int $id
  12. *
  13. * @return void
  14. * @dataProvider dataProviderTables
  15. */
  16. public function testHandle($table, $id)
  17. {
  18. $fieldName = 'fieldname';
  19. $createdVersionField = 'fieldname_version';
  20. $version = 1;
  21. /** @var \Migration\ResourceModel\Structure|\PHPUnit_Framework_MockObject_MockObject $record */
  22. $structure = $this->getMockBuilder(\Migration\ResourceModel\Structure::class)
  23. ->setMethods(['getFields'])
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $structure->expects($this->any())->method('getFields')->willReturn([$fieldName]);
  27. /** @var \Migration\ResourceModel\Record|\PHPUnit_Framework_MockObject_MockObject $record */
  28. $record = $this->getMockBuilder(\Migration\ResourceModel\Record::class)
  29. ->setMethods(['getValue', 'setValue', 'getFields', 'getStructure'])
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $record->expects($this->any())->method('getStructure')->willReturn($structure);
  33. $record->expects($this->any())->method('getFields')->willReturn([$fieldName]);
  34. $record->expects($this->any())->method('getValue')->with($fieldName)->willReturn($id);
  35. $record->expects($this->any())->method('setValue')->with($fieldName, $id);
  36. /** @var \Migration\ResourceModel\Record|\PHPUnit_Framework_MockObject_MockObject $record2 */
  37. $record2 = $this->getMockBuilder(\Migration\ResourceModel\Record::class)
  38. ->setMethods(['getValue', 'setValue', 'getFields', 'getStructure'])
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $record2->expects($this->any())->method('setValue')->willReturn($createdVersionField, $version);
  42. $record2->expects($this->any())->method('getFields')->willReturn([$fieldName]);
  43. $record2->expects($this->any())->method('getStructure')->willReturn($structure);
  44. /** @var \Migration\ResourceModel\Destination|\PHPUnit_Framework_MockObject_MockObject $destination */
  45. $destination = $this->getMockBuilder(\Migration\ResourceModel\Destination::class)
  46. ->setMethods(['clearDocument', 'saveRecords'])
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $destination->expects($this->any())->method('clearDocument')->with($table);
  50. $destination->expects($this->any())->method('saveRecords')->with($table, [['sequence_value' => $id]])
  51. ->willReturnSelf();
  52. $handler = new Sequence($table, $destination);
  53. $handler->setField($fieldName);
  54. $this->assertNull($handler->handle($record, $record2));
  55. }
  56. /**
  57. * @return array
  58. */
  59. public function dataProviderTables()
  60. {
  61. return [
  62. ['table' => 'tablename', 'id' => 1],
  63. ['table' => '', 'id' => 1],
  64. ['table' => 'tablename', 'id' => 0],
  65. ['table' => '', 'id' => 0],
  66. ];
  67. }
  68. }