GPMTest.php 13 KB

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