UpgradeNotesTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace tests;
  3. use Composer\Config;
  4. use Composer\IO\IOInterface;
  5. use yii\composer\Plugin;
  6. /**
  7. *
  8. */
  9. class UpgradeNotesTest extends TestCase
  10. {
  11. private function getPlugin()
  12. {
  13. $plugin = new Plugin();
  14. $reflection = new \ReflectionObject($plugin);
  15. $property = $reflection->getProperty('_vendorDir');
  16. $property->setAccessible(true);
  17. $property->setValue($plugin,__DIR__ . '/data');
  18. return $plugin;
  19. }
  20. public function introProvider()
  21. {
  22. return [
  23. ['up', '2.0.12', '2.0.13', 'from version 2.0.12 to 2.0.13'],
  24. ['up', '2.0.12', 'dev-master', 'from version 2.0.12 to dev-master'],
  25. ['down', '2.0.14.1', '2.0.13', 'from version 2.0.14.1 to 2.0.13'],
  26. ['down', 'dev-master', '2.0.13', 'from version dev-master to 2.0.13'],
  27. ];
  28. }
  29. /**
  30. * @dataProvider introProvider
  31. */
  32. public function testPrintUpgradeIntro($direction, $from, $to, $expected)
  33. {
  34. $io = new MockIO();
  35. $this->invokeMethod($this->getPlugin(), 'printUpgradeIntro', [$io, [
  36. 'direction' => $direction,
  37. 'fromPretty' => $from,
  38. 'toPretty' => $to,
  39. ]]);
  40. $this->assertCount(3, $io->messages);
  41. if ($direction === 'up') {
  42. $this->assertContains('upgraded', implode("\n", $io->messages));
  43. } else {
  44. $this->assertContains('downgraded', implode("\n", $io->messages));
  45. }
  46. $this->assertContains($expected, implode("\n", $io->messages));
  47. $this->assertContains('check the upgrade notes for possible incompatible changes', implode("\n", $io->messages));
  48. }
  49. /**
  50. * @dataProvider introProvider
  51. */
  52. public function testPrintUpgradeLink($direction, $from, $to)
  53. {
  54. $io = new MockIO();
  55. $this->invokeMethod($this->getPlugin(), 'printUpgradeLink', [$io, [
  56. 'direction' => $direction,
  57. 'fromPretty' => $from,
  58. 'toPretty' => $to,
  59. ]]);
  60. $this->assertCount(1, $io->messages);
  61. if ($direction === 'up') {
  62. if ($to === 'dev-master') {
  63. $to = 'master';
  64. }
  65. $this->assertContains("https://github.com/yiisoft/yii2/blob/$to/framework/UPGRADE.md", implode("\n", $io->messages));
  66. } else {
  67. if ($from === 'dev-master') {
  68. $from = 'master';
  69. }
  70. $this->assertContains("https://github.com/yiisoft/yii2/blob/$from/framework/UPGRADE.md", implode("\n", $io->messages));
  71. }
  72. }
  73. public function testUpgradeNotes_brokenFile()
  74. {
  75. $notes = $this->invokeMethod($this->getPlugin(), 'findUpgradeNotes', ['yiisoft/yii3', 'dev-master']);
  76. $this->assertFalse($notes);
  77. }
  78. public function testUpgradeNotes_alpha()
  79. {
  80. $notes = $this->invokeMethod($this->getPlugin(), 'findUpgradeNotes', ['yiisoft/yii2', '2.0.0-alpha']);
  81. $this->assertEquals(<<<STRING
  82. Upgrade from Yii 2.0.14
  83. -----------------------
  84. These are the upgrade notes from 2.0.14.
  85. Upgrade from Yii 2.0.13.1
  86. -------------------------
  87. These are the upgrade notes from 2.0.13.1.
  88. Upgrade from Yii 2.0.13
  89. -----------------------
  90. These are the upgrade notes from 2.0.13.
  91. Upgrade from Yii 2.0.12
  92. -----------------------
  93. These are the upgrade notes from 2.0.12.
  94. Upgrade from Yii 2.0.0
  95. ----------------------
  96. These are the upgrade notes from 2.0.0.
  97. Upgrade from Yii 2.0.0-alpha
  98. ----------------------------
  99. These are the upgrade notes from 2.0.0-alpha.
  100. STRING
  101. , implode("\n", $notes));
  102. }
  103. public function testUpgradeNotes_fromMajor()
  104. {
  105. $notes = $this->invokeMethod($this->getPlugin(), 'findUpgradeNotes', ['yiisoft/yii2', '2.0.10']);
  106. $this->assertEquals(<<<STRING
  107. Upgrade from Yii 2.0.14
  108. -----------------------
  109. These are the upgrade notes from 2.0.14.
  110. Upgrade from Yii 2.0.13.1
  111. -------------------------
  112. These are the upgrade notes from 2.0.13.1.
  113. Upgrade from Yii 2.0.13
  114. -----------------------
  115. These are the upgrade notes from 2.0.13.
  116. Upgrade from Yii 2.0.12
  117. -----------------------
  118. These are the upgrade notes from 2.0.12.
  119. STRING
  120. , implode("\n", $notes));
  121. }
  122. public function testUpgradeNotes_fromMinorWithMinorNotes()
  123. {
  124. $notes = $this->invokeMethod($this->getPlugin(), 'findUpgradeNotes', ['yiisoft/yii2', '2.0.13.1']);
  125. $this->assertEquals(<<<STRING
  126. Upgrade from Yii 2.0.14
  127. -----------------------
  128. These are the upgrade notes from 2.0.14.
  129. Upgrade from Yii 2.0.13.1
  130. -------------------------
  131. These are the upgrade notes from 2.0.13.1.
  132. STRING
  133. , implode("\n", $notes));
  134. }
  135. public function testUpgradeNotes_fromMinorWithoutMinorNotes()
  136. {
  137. $notes = $this->invokeMethod($this->getPlugin(), 'findUpgradeNotes', ['yiisoft/yii2', '2.0.12.1']);
  138. $this->assertEquals(<<<STRING
  139. Upgrade from Yii 2.0.14
  140. -----------------------
  141. These are the upgrade notes from 2.0.14.
  142. Upgrade from Yii 2.0.13.1
  143. -------------------------
  144. These are the upgrade notes from 2.0.13.1.
  145. Upgrade from Yii 2.0.13
  146. -----------------------
  147. These are the upgrade notes from 2.0.13.
  148. Upgrade from Yii 2.0.12
  149. -----------------------
  150. These are the upgrade notes from 2.0.12.
  151. STRING
  152. , implode("\n", $notes));
  153. }
  154. public function testIsNumericVersion()
  155. {
  156. $plugin = new Plugin();
  157. $this->assertTrue($this->invokeMethod($plugin, 'isNumericVersion', ['2.0.10']));
  158. $this->assertTrue($this->invokeMethod($plugin, 'isNumericVersion', ['2.0.0']));
  159. $this->assertTrue($this->invokeMethod($plugin, 'isNumericVersion', ['2.0.0-alpha']));
  160. $this->assertTrue($this->invokeMethod($plugin, 'isNumericVersion', ['2.0.13.0']));
  161. $this->assertTrue($this->invokeMethod($plugin, 'isNumericVersion', ['2.0.13.1']));
  162. $this->assertTrue($this->invokeMethod($plugin, 'isNumericVersion', ['2.1']));
  163. $this->assertTrue($this->invokeMethod($plugin, 'isNumericVersion', ['2.0']));
  164. $this->assertFalse($this->invokeMethod($plugin, 'isNumericVersion', ['dev-master']));
  165. $this->assertFalse($this->invokeMethod($plugin, 'isNumericVersion', ['dev-something']));
  166. }
  167. }
  168. class MockIO
  169. {
  170. public $messages = [];
  171. public function write($messages, $newline = true, $verbosity = 'normal')
  172. {
  173. $this->messages[] = $messages;
  174. }
  175. public $errorMessages = [];
  176. public function writeError($messages, $newline = true, $verbosity = self::NORMAL)
  177. {
  178. $this->errorMessages[] = $messages;
  179. }
  180. }