PatchRegirtryTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Test\Unit\Patch;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Framework\Setup\Patch\PatchFactory;
  9. use Magento\Framework\Setup\Patch\PatchHistory;
  10. use Magento\Framework\Setup\Patch\PatchRegistry;
  11. /**
  12. * Class PatchRegirtryTest
  13. * @package Magento\Framework\Setup\Test\Unit\Patch
  14. */
  15. class PatchRegirtryTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var PatchRegistry
  19. */
  20. private $patchRegistry;
  21. /**
  22. * @var PatchFactory|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $patchFactoryMock;
  25. /**
  26. * @var PatchHistory|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $patchHistoryMock;
  29. protected function setUp()
  30. {
  31. $objectManager = new ObjectManager($this);
  32. $this->patchFactoryMock = $this->getMockBuilder(PatchFactory::class)
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $this->patchHistoryMock = $this->getMockBuilder(PatchHistory::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->patchRegistry = $objectManager->getObject(
  39. PatchRegistry::class,
  40. [
  41. 'patchHistory' => $this->patchHistoryMock,
  42. 'patchFactory' => $this->patchFactoryMock,
  43. ]
  44. );
  45. require_once __DIR__ . '/../_files/data_patch_classes.php';
  46. }
  47. public function testRegisterAppliedPatch()
  48. {
  49. $this->patchHistoryMock->expects($this->once())
  50. ->method('isApplied')
  51. ->with(\SomeDataPatch::class)
  52. ->willReturn(false);
  53. $this->assertEquals(\SomeDataPatch::class, $this->patchRegistry->registerPatch(\SomeDataPatch::class));
  54. }
  55. public function testRegisterNonAplliedPatch()
  56. {
  57. $this->patchHistoryMock->expects($this->once())
  58. ->method('isApplied')
  59. ->with(\SomeDataPatch::class)
  60. ->willReturn(true);
  61. $this->assertEquals(false, $this->patchRegistry->registerPatch(\SomeDataPatch::class));
  62. }
  63. public function testGetIterator()
  64. {
  65. $this->patchHistoryMock->expects($this->any())
  66. ->method('isApplied')
  67. ->willReturnMap(
  68. [
  69. [\SomeDataPatch::class, false],
  70. [\OtherDataPatch::class, false]
  71. ]
  72. );
  73. $this->assertEquals(\SomeDataPatch::class, $this->patchRegistry->registerPatch(\SomeDataPatch::class));
  74. $actualPatches = [];
  75. foreach ($this->patchRegistry->getIterator() as $patch) {
  76. $actualPatches[] = $patch;
  77. }
  78. // assert that all dependencies are present and placed in valid sequence
  79. $this->assertEquals(
  80. [\OtherDataPatch::class, \SomeDataPatch::class],
  81. $actualPatches,
  82. 'Failed to assert that actual non-apllied patches sequence is valid.'
  83. );
  84. }
  85. }