ManagerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Migration\Handler;
  7. class ManagerTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $objectManager;
  13. /**
  14. * @var Manager
  15. */
  16. protected $manager;
  17. /**
  18. * @return void
  19. */
  20. protected function setUp()
  21. {
  22. $this->objectManager = $this->createPartialMock(
  23. \Magento\Framework\ObjectManager\ObjectManager::class,
  24. ['create']
  25. );
  26. $this->manager = new Manager($this->objectManager);
  27. }
  28. /**
  29. * @covers \Migration\Handler\Manager::initHandler
  30. * @covers \Migration\Handler\Manager::getHandler
  31. * @return void
  32. */
  33. public function testGetHandlerCorrect()
  34. {
  35. $field = 'someField';
  36. $handlerConfig = ['class' => \Migration\Handler\SetValue::class, 'params' => ['value' => '12']];
  37. $handler = $this->createPartialMock(
  38. \Migration\Handler\SetValue::class,
  39. ['setField']
  40. );
  41. $this->objectManager->expects($this->any())->method('create')->will($this->returnValue($handler));
  42. $handler->expects($this->once())->method('setField')->with($field);
  43. $this->manager->initHandler($field, $handlerConfig);
  44. $this->assertEquals($handler, $this->manager->getHandler($field));
  45. $this->assertEquals([$field => $handler], $this->manager->getHandlers());
  46. }
  47. /**
  48. * @throws \Migration\Exception
  49. * @return void
  50. */
  51. public function testGetHandlerWithHandlerKey()
  52. {
  53. $field = 'someField';
  54. $handlerKey = 'someKey';
  55. $handlerConfig = ['class' => \Migration\Handler\SetValue::class, 'params' => ['value' => '12']];
  56. $handler = $this->createPartialMock(
  57. \Migration\Handler\SetValue::class,
  58. ['setField']
  59. );
  60. $this->objectManager->expects($this->any())->method('create')->will($this->returnValue($handler));
  61. $handler->expects($this->once())->method('setField')->with($field);
  62. $this->manager->initHandler($field, $handlerConfig, $handlerKey);
  63. $this->assertEquals($handler, $this->manager->getHandler($handlerKey));
  64. $this->assertEquals([$handlerKey => $handler], $this->manager->getHandlers());
  65. }
  66. /**
  67. * @covers \Migration\Handler\Manager::initHandler
  68. * @covers \Migration\Handler\Manager::getHandler
  69. * @return void
  70. */
  71. public function testGetHandlerEmpty()
  72. {
  73. $field = 'someField';
  74. $handlerConfig = ['class' => \Migration\Handler\SetValue::class, 'params' => ['value' => '12']];
  75. $handler = $this->createPartialMock(
  76. \Migration\Handler\SetValue::class,
  77. ['setField']
  78. );
  79. $this->objectManager->expects($this->once())->method('create')->will($this->returnValue($handler));
  80. $handler->expects($this->once())->method('setField')->with($field);
  81. $this->manager->initHandler($field, $handlerConfig);
  82. $this->assertEquals(null, $this->manager->getHandler('non_existant_field'));
  83. }
  84. /**
  85. * @throws \Migration\Exception
  86. * @return void
  87. */
  88. public function testInitHandlerEmptyConfig()
  89. {
  90. $this->objectManager->expects($this->never())->method('create');
  91. $this->manager->initHandler('anyfield');
  92. }
  93. /**
  94. * @throws \Migration\Exception
  95. * @return void
  96. */
  97. public function testInitHandlerEmptyClass()
  98. {
  99. $this->expectException('Exception');
  100. $this->expectExceptionMessage('Handler class name not specified.');
  101. $config = ['class' => '', 'params' => ['value' => '12']];
  102. $this->manager->initHandler('anyfield', $config);
  103. }
  104. /**
  105. * @throws \Migration\Exception
  106. * @return void
  107. */
  108. public function testInitInvalidHandler()
  109. {
  110. $handlerConfig = ['class' => "Migration\\Migration", 'params' => ['value' => '12']];
  111. $invalidHandler = $this->getMockBuilder(\Migration\Migration::class)
  112. ->disableOriginalConstructor()
  113. ->getMock();
  114. $this->objectManager->expects($this->once())->method('create')->will($this->returnValue($invalidHandler));
  115. $this->expectException('\Exception');
  116. $this->expectExceptionMessage("'Migration\\Migration' is not correct handler.");
  117. $this->manager->initHandler('somefield', $handlerConfig);
  118. }
  119. }