GPMTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. use Codeception\Util\Fixtures;
  3. use Grav\Common\Grav;
  4. use Grav\Common\GPM\GPM;
  5. define('EXCEPTION_BAD_FORMAT', 1);
  6. define('EXCEPTION_INCOMPATIBLE_VERSIONS', 2);
  7. class GpmStub extends GPM
  8. {
  9. public $data;
  10. public function findPackage($packageName, $ignore_exception = false)
  11. {
  12. if (isset($this->data[$packageName])) {
  13. return $this->data[$packageName];
  14. }
  15. }
  16. public function findPackages($searches = [])
  17. {
  18. return $this->data;
  19. }
  20. }
  21. /**
  22. * Class InstallCommandTest
  23. */
  24. class GpmTest extends \Codeception\TestCase\Test
  25. {
  26. /** @var Grav $grav */
  27. protected $grav;
  28. /** @var GpmStub */
  29. protected $gpm;
  30. protected function _before()
  31. {
  32. $this->grav = Fixtures::get('grav');
  33. $this->gpm = new GpmStub();
  34. }
  35. protected function _after()
  36. {
  37. }
  38. public function testCalculateMergedDependenciesOfPackages()
  39. {
  40. //////////////////////////////////////////////////////////////////////////////////////////
  41. // First working example
  42. //////////////////////////////////////////////////////////////////////////////////////////
  43. $this->gpm->data = [
  44. 'admin' => (object)[
  45. 'dependencies' => [
  46. ["name" => "grav", "version" => ">=1.0.10"],
  47. ["name" => "form", "version" => "~2.0"],
  48. ["name" => "login", "version" => ">=2.0"],
  49. ["name" => "errors", "version" => "*"],
  50. ["name" => "problems"],
  51. ]
  52. ],
  53. 'test' => (object)[
  54. 'dependencies' => [
  55. ["name" => "errors", "version" => ">=1.0"]
  56. ]
  57. ],
  58. 'grav',
  59. 'form' => (object)[
  60. 'dependencies' => [
  61. ["name" => "errors", "version" => ">=3.2"]
  62. ]
  63. ]
  64. ];
  65. $packages = ['admin', 'test'];
  66. $dependencies = $this->gpm->calculateMergedDependenciesOfPackages($packages);
  67. $this->assertInternalType('array', $dependencies);
  68. $this->assertCount(5, $dependencies);
  69. $this->assertSame('>=1.0.10', $dependencies['grav']);
  70. $this->assertArrayHasKey('errors', $dependencies);
  71. $this->assertArrayHasKey('problems', $dependencies);
  72. //////////////////////////////////////////////////////////////////////////////////////////
  73. // Second working example
  74. //////////////////////////////////////////////////////////////////////////////////////////
  75. $packages = ['admin', 'form'];
  76. $dependencies = $this->gpm->calculateMergedDependenciesOfPackages($packages);
  77. $this->assertInternalType('array', $dependencies);
  78. $this->assertCount(5, $dependencies);
  79. $this->assertSame('>=3.2', $dependencies['errors']);
  80. //////////////////////////////////////////////////////////////////////////////////////////
  81. // Third working example
  82. //////////////////////////////////////////////////////////////////////////////////////////
  83. $this->gpm->data = [
  84. 'admin' => (object)[
  85. 'dependencies' => [
  86. ["name" => "errors", "version" => ">=4.0"],
  87. ]
  88. ],
  89. 'test' => (object)[
  90. 'dependencies' => [
  91. ["name" => "errors", "version" => ">=1.0"]
  92. ]
  93. ],
  94. 'another' => (object)[
  95. 'dependencies' => [
  96. ["name" => "errors", "version" => ">=3.2"]
  97. ]
  98. ]
  99. ];
  100. $packages = ['admin', 'test', 'another'];
  101. $dependencies = $this->gpm->calculateMergedDependenciesOfPackages($packages);
  102. $this->assertInternalType('array', $dependencies);
  103. $this->assertCount(1, $dependencies);
  104. $this->assertSame('>=4.0', $dependencies['errors']);
  105. //////////////////////////////////////////////////////////////////////////////////////////
  106. // Test alpha / beta / rc
  107. //////////////////////////////////////////////////////////////////////////////////////////
  108. $this->gpm->data = [
  109. 'admin' => (object)[
  110. 'dependencies' => [
  111. ["name" => "package1", "version" => ">=4.0.0-rc1"],
  112. ["name" => "package4", "version" => ">=3.2.0"],
  113. ]
  114. ],
  115. 'test' => (object)[
  116. 'dependencies' => [
  117. ["name" => "package1", "version" => ">=4.0.0-rc2"],
  118. ["name" => "package2", "version" => ">=3.2.0-alpha"],
  119. ["name" => "package3", "version" => ">=3.2.0-alpha.2"],
  120. ["name" => "package4", "version" => ">=3.2.0-alpha"],
  121. ]
  122. ],
  123. 'another' => (object)[
  124. 'dependencies' => [
  125. ["name" => "package2", "version" => ">=3.2.0-beta.11"],
  126. ["name" => "package3", "version" => ">=3.2.0-alpha.1"],
  127. ["name" => "package4", "version" => ">=3.2.0-beta"],
  128. ]
  129. ]
  130. ];
  131. $packages = ['admin', 'test', 'another'];
  132. $dependencies = $this->gpm->calculateMergedDependenciesOfPackages($packages);
  133. $this->assertSame('>=4.0.0-rc2', $dependencies['package1']);
  134. $this->assertSame('>=3.2.0-beta.11', $dependencies['package2']);
  135. $this->assertSame('>=3.2.0-alpha.2', $dependencies['package3']);
  136. $this->assertSame('>=3.2.0', $dependencies['package4']);
  137. //////////////////////////////////////////////////////////////////////////////////////////
  138. // Raise exception if no version is specified
  139. //////////////////////////////////////////////////////////////////////////////////////////
  140. $this->gpm->data = [
  141. 'admin' => (object)[
  142. 'dependencies' => [
  143. ["name" => "errors", "version" => ">=4.0"],
  144. ]
  145. ],
  146. 'test' => (object)[
  147. 'dependencies' => [
  148. ["name" => "errors", "version" => ">="]
  149. ]
  150. ],
  151. ];
  152. $packages = ['admin', 'test'];
  153. try {
  154. $this->gpm->calculateMergedDependenciesOfPackages($packages);
  155. $this->fail("Expected Exception not thrown");
  156. } catch (Exception $e) {
  157. $this->assertEquals(EXCEPTION_BAD_FORMAT, $e->getCode());
  158. $this->assertStringStartsWith("Bad format for version of dependency", $e->getMessage());
  159. }
  160. //////////////////////////////////////////////////////////////////////////////////////////
  161. // Raise exception if incompatible versions are specified
  162. //////////////////////////////////////////////////////////////////////////////////////////
  163. $this->gpm->data = [
  164. 'admin' => (object)[
  165. 'dependencies' => [
  166. ["name" => "errors", "version" => "~4.0"],
  167. ]
  168. ],
  169. 'test' => (object)[
  170. 'dependencies' => [
  171. ["name" => "errors", "version" => "~3.0"]
  172. ]
  173. ],
  174. ];
  175. $packages = ['admin', 'test'];
  176. try {
  177. $this->gpm->calculateMergedDependenciesOfPackages($packages);
  178. $this->fail("Expected Exception not thrown");
  179. } catch (Exception $e) {
  180. $this->assertEquals(EXCEPTION_INCOMPATIBLE_VERSIONS, $e->getCode());
  181. $this->assertStringEndsWith("required in two incompatible versions", $e->getMessage());
  182. }
  183. //////////////////////////////////////////////////////////////////////////////////////////
  184. // Test dependencies of dependencies
  185. //////////////////////////////////////////////////////////////////////////////////////////
  186. $this->gpm->data = [
  187. 'admin' => (object)[
  188. 'dependencies' => [
  189. ["name" => "grav", "version" => ">=1.0.10"],
  190. ["name" => "form", "version" => "~2.0"],
  191. ["name" => "login", "version" => ">=2.0"],
  192. ["name" => "errors", "version" => "*"],
  193. ["name" => "problems"],
  194. ]
  195. ],
  196. 'login' => (object)[
  197. 'dependencies' => [
  198. ["name" => "antimatter", "version" => ">=1.0"]
  199. ]
  200. ],
  201. 'grav',
  202. 'antimatter' => (object)[
  203. 'dependencies' => [
  204. ["name" => "something", "version" => ">=3.2"]
  205. ]
  206. ]
  207. ];
  208. $packages = ['admin'];
  209. $dependencies = $this->gpm->calculateMergedDependenciesOfPackages($packages);
  210. $this->assertInternalType('array', $dependencies);
  211. $this->assertCount(7, $dependencies);
  212. $this->assertSame('>=1.0.10', $dependencies['grav']);
  213. $this->assertArrayHasKey('errors', $dependencies);
  214. $this->assertArrayHasKey('problems', $dependencies);
  215. $this->assertArrayHasKey('antimatter', $dependencies);
  216. $this->assertArrayHasKey('something', $dependencies);
  217. $this->assertSame('>=3.2', $dependencies['something']);
  218. }
  219. public function testVersionFormatIsNextSignificantRelease()
  220. {
  221. $this->assertFalse($this->gpm->versionFormatIsNextSignificantRelease('>=1.0'));
  222. $this->assertFalse($this->gpm->versionFormatIsNextSignificantRelease('>=2.3.4'));
  223. $this->assertFalse($this->gpm->versionFormatIsNextSignificantRelease('>=2.3.x'));
  224. $this->assertFalse($this->gpm->versionFormatIsNextSignificantRelease('1.0'));
  225. $this->assertTrue($this->gpm->versionFormatIsNextSignificantRelease('~2.3.x'));
  226. $this->assertTrue($this->gpm->versionFormatIsNextSignificantRelease('~2.0'));
  227. }
  228. public function testVersionFormatIsEqualOrHigher()
  229. {
  230. $this->assertTrue($this->gpm->versionFormatIsEqualOrHigher('>=1.0'));
  231. $this->assertTrue($this->gpm->versionFormatIsEqualOrHigher('>=2.3.4'));
  232. $this->assertTrue($this->gpm->versionFormatIsEqualOrHigher('>=2.3.x'));
  233. $this->assertFalse($this->gpm->versionFormatIsEqualOrHigher('~2.3.x'));
  234. $this->assertFalse($this->gpm->versionFormatIsEqualOrHigher('1.0'));
  235. }
  236. public function testCheckNextSignificantReleasesAreCompatible()
  237. {
  238. /*
  239. * ~1.0 is equivalent to >=1.0 < 2.0.0
  240. * ~1.2 is equivalent to >=1.2 <2.0.0
  241. * ~1.2.3 is equivalent to >=1.2.3 <1.3.0
  242. */
  243. $this->assertTrue($this->gpm->checkNextSignificantReleasesAreCompatible('1.0', '1.2'));
  244. $this->assertTrue($this->gpm->checkNextSignificantReleasesAreCompatible('1.2', '1.0'));
  245. $this->assertTrue($this->gpm->checkNextSignificantReleasesAreCompatible('1.0', '1.0.10'));
  246. $this->assertTrue($this->gpm->checkNextSignificantReleasesAreCompatible('1.1', '1.1.10'));
  247. $this->assertTrue($this->gpm->checkNextSignificantReleasesAreCompatible('30.0', '30.10'));
  248. $this->assertTrue($this->gpm->checkNextSignificantReleasesAreCompatible('1.0', '1.1.10'));
  249. $this->assertTrue($this->gpm->checkNextSignificantReleasesAreCompatible('1.0', '1.8'));
  250. $this->assertTrue($this->gpm->checkNextSignificantReleasesAreCompatible('1.0.1', '1.1'));
  251. $this->assertTrue($this->gpm->checkNextSignificantReleasesAreCompatible('2.0.0-beta', '2.0'));
  252. $this->assertTrue($this->gpm->checkNextSignificantReleasesAreCompatible('2.0.0-rc.1', '2.0'));
  253. $this->assertTrue($this->gpm->checkNextSignificantReleasesAreCompatible('2.0', '2.0.0-alpha'));
  254. $this->assertFalse($this->gpm->checkNextSignificantReleasesAreCompatible('1.0', '2.2'));
  255. $this->assertFalse($this->gpm->checkNextSignificantReleasesAreCompatible('1.0.0-beta.1', '2.0'));
  256. $this->assertFalse($this->gpm->checkNextSignificantReleasesAreCompatible('0.9.99', '1.0.0'));
  257. $this->assertFalse($this->gpm->checkNextSignificantReleasesAreCompatible('0.9.99', '1.0.10'));
  258. $this->assertFalse($this->gpm->checkNextSignificantReleasesAreCompatible('0.9.99', '1.0.10.2'));
  259. }
  260. public function testCalculateVersionNumberFromDependencyVersion()
  261. {
  262. $this->assertSame('2.0', $this->gpm->calculateVersionNumberFromDependencyVersion('>=2.0'));
  263. $this->assertSame('2.0.2', $this->gpm->calculateVersionNumberFromDependencyVersion('>=2.0.2'));
  264. $this->assertSame('2.0.2', $this->gpm->calculateVersionNumberFromDependencyVersion('~2.0.2'));
  265. $this->assertSame('1', $this->gpm->calculateVersionNumberFromDependencyVersion('~1'));
  266. $this->assertNull($this->gpm->calculateVersionNumberFromDependencyVersion(''));
  267. $this->assertNull($this->gpm->calculateVersionNumberFromDependencyVersion('*'));
  268. $this->assertSame('2.0.2', $this->gpm->calculateVersionNumberFromDependencyVersion('2.0.2'));
  269. }
  270. }