DbVersionInfoTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Module\Test\Unit;
  7. use Magento\Framework\Module\DbVersionInfo;
  8. class DbVersionInfoTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var DbVersionInfo
  12. */
  13. private $dbVersionInfo;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $moduleList;
  18. /**
  19. * @var \Magento\Framework\Module\ResourceInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $moduleResource;
  22. /**
  23. * @var \Magento\Framework\Module\Output\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $_outputConfig;
  26. protected function setUp()
  27. {
  28. $this->moduleList = $this->getMockForAbstractClass(\Magento\Framework\Module\ModuleListInterface::class);
  29. $this->moduleList->expects($this->any())
  30. ->method('getOne')
  31. ->will($this->returnValueMap([
  32. ['Module_One', ['name' => 'Module_One', 'setup_version' => '1']],
  33. ['Module_Two', ['name' => 'Module_Two', 'setup_version' => '2']],
  34. ['Module_No_Schema', []],
  35. ]));
  36. $this->moduleList->expects($this->any())
  37. ->method('getNames')
  38. ->will($this->returnValue(['Module_One', 'Module_Two']));
  39. $this->_outputConfig = $this->getMockForAbstractClass(\Magento\Framework\Module\Output\ConfigInterface::class);
  40. $this->moduleResource = $this->getMockForAbstractClass(\Magento\Framework\Module\ResourceInterface::class);
  41. $this->dbVersionInfo = new DbVersionInfo(
  42. $this->moduleList,
  43. $this->moduleResource
  44. );
  45. }
  46. /**
  47. * @param string $moduleName
  48. * @param string|bool $dbVersion
  49. * @param bool $expectedResult
  50. *
  51. * @dataProvider isDbUpToDateDataProvider
  52. */
  53. public function testIsDbSchemaUpToDate($moduleName, $dbVersion, $expectedResult)
  54. {
  55. $this->moduleResource->expects($this->once())
  56. ->method('getDbVersion')
  57. ->with($moduleName)
  58. ->will($this->returnValue($dbVersion));
  59. $this->moduleList->expects(self::once())
  60. ->method('getOne')
  61. ->with($moduleName)
  62. ->willReturn(
  63. ['setup_version' => $dbVersion]
  64. );
  65. $this->assertEquals(
  66. $expectedResult,
  67. $this->dbVersionInfo->isSchemaUpToDate($moduleName)
  68. );
  69. }
  70. /**
  71. * @param string $moduleName
  72. * @param string|bool $dbVersion
  73. * @param bool $expectedResult
  74. *
  75. * @dataProvider isDbUpToDateDataProvider
  76. */
  77. public function testIsDbDataUpToDate($moduleName, $dbVersion, $expectedResult)
  78. {
  79. $this->moduleResource->expects($this->once())
  80. ->method('getDataVersion')
  81. ->with($moduleName)
  82. ->will($this->returnValue($dbVersion));
  83. $this->moduleList->expects(self::once())
  84. ->method('getOne')
  85. ->with($moduleName)
  86. ->willReturn(
  87. ['setup_version' => $dbVersion]
  88. );
  89. $this->assertEquals(
  90. $expectedResult,
  91. $this->dbVersionInfo->isDataUpToDate($moduleName)
  92. );
  93. }
  94. /**
  95. * @return array
  96. */
  97. public function isDbUpToDateDataProvider()
  98. {
  99. return [
  100. 'version in config == version in db' => ['Module_One', '1', true],
  101. 'version in config < version in db' => [
  102. 'Module_One',
  103. '2',
  104. false
  105. ],
  106. 'version in config > version in db' => [
  107. 'Module_Two',
  108. '1',
  109. false
  110. ],
  111. 'no version in db' => [
  112. 'Module_One',
  113. false,
  114. false
  115. ],
  116. ];
  117. }
  118. public function testGetDbVersionErrors()
  119. {
  120. $this->moduleResource->expects($this->any())
  121. ->method('getDataVersion')
  122. ->will($this->returnValue(2));
  123. $this->moduleResource->expects($this->any())
  124. ->method('getDbVersion')
  125. ->will($this->returnValue(2));
  126. $expectedErrors = [
  127. [
  128. DbVersionInfo::KEY_MODULE => 'Module_One',
  129. DbVersionInfo::KEY_CURRENT => '2',
  130. DbVersionInfo::KEY_REQUIRED => '1',
  131. DbVersionInfo::KEY_TYPE => 'schema',
  132. ],
  133. [
  134. DbVersionInfo::KEY_MODULE => 'Module_One',
  135. DbVersionInfo::KEY_CURRENT => '2',
  136. DbVersionInfo::KEY_REQUIRED => '1',
  137. DbVersionInfo::KEY_TYPE => 'data',
  138. ]
  139. ];
  140. $this->assertEquals($expectedErrors, $this->dbVersionInfo->getDbVersionErrors());
  141. }
  142. /**
  143. * Test is DB schema up to date for module with no schema
  144. */
  145. public function testIsDbSchemaUpToDateException()
  146. {
  147. $this->assertTrue($this->dbVersionInfo->isSchemaUpToDate('Module_No_Schema'));
  148. }
  149. /**
  150. * Test is DB Data up to date for module with no schema
  151. */
  152. public function testIsDbDataUpToDateException()
  153. {
  154. $this->assertTrue($this->dbVersionInfo->isDataUpToDate('Module_No_Schema'));
  155. }
  156. }