VersionTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace vierbergenlars\SemVer\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. use vierbergenlars\SemVer;
  5. class VersionTest extends TestCase
  6. {
  7. public function testKeepSimpleversion()
  8. {
  9. $t = array(
  10. '1.0.0',
  11. '2.0.0',
  12. '1.0.1',
  13. '1.3.2',
  14. '1.02.0' => '1.2.0',
  15. '0.2.5',
  16. '01.2.6' => '1.2.6',
  17. '2.0.03' => '2.0.3',
  18. '0.0.0',
  19. );
  20. foreach ($t as $original => $result) {
  21. if ( ! is_string($original)) {
  22. $original = $result;
  23. }
  24. $v = new SemVer\version($original, true);
  25. $this->assertEquals($v->__toString(), $result, '[' . $original . '] %s');
  26. }
  27. }
  28. public function testKeepSimpleversionComparator()
  29. {
  30. $t = array(
  31. '>1.0.0',
  32. '>1.1.0',
  33. '>1.2.3',
  34. '>0.5.6',
  35. '>0.025.6' => '>0.25.6',
  36. '>=1.0.0',
  37. '>=1.2.0',
  38. '>=1.4.5',
  39. '>=0.9.3',
  40. '>=05.3.6' => '>=5.3.6',
  41. '<2.0.0-0',
  42. '<5.6.0-0',
  43. '<2.3.5-0',
  44. '<0.2.3-0',
  45. '<0.2.05' => '<0.2.5-0',
  46. '<=7.0.0',
  47. '<=1.3.0',
  48. '<=1.4.3',
  49. '<=0.2.6',
  50. '<=00.05.6' => '<=0.5.6',
  51. );
  52. foreach ($t as $original => $result) {
  53. if ( ! is_string($original)) {
  54. $original = $result;
  55. }
  56. $v = new SemVer\expression($original, true);
  57. $this->assertEquals($v->getString(), $result, '[' . $original . '] %s');
  58. }
  59. }
  60. public function testShortSimpleversion()
  61. {
  62. $t = array(
  63. '1' => '>=1.0.0-0 <2.0.0-0',
  64. '1.2' => '>=1.2.0-0 <1.3.0-0',
  65. '1.0' => '>=1.0.0-0 <1.1.0-0',
  66. '501' => '>=501.0.0-0 <502.0.0-0',
  67. );
  68. foreach ($t as $original => $result) {
  69. $v = new SemVer\expression($original, true);
  70. $this->assertEquals($v->getString(), $result, '[' . $original . '] %s');
  71. }
  72. }
  73. public function testShortSimpleversionComparator()
  74. {
  75. $t = array(
  76. '>1.0.0' => '>1.0.0',
  77. '<2.0.0' => '<2.0.0-0',
  78. '<=5.2' => '<=5.2.0-0',
  79. '>=3' => '>=3.0.0-0',
  80. );
  81. foreach ($t as $original => $result) {
  82. $v = new SemVer\expression($original, true);
  83. $this->assertEquals($v->getString(), $result, '[' . $original . '] %s');
  84. }
  85. }
  86. public function testSimpleversionWildcard()
  87. {
  88. $t = array(
  89. '1.x.x' => '>=1.0.0-0 <2.0.0-0',
  90. '1.x' => '>=1.0.0-0 <2.0.0-0',
  91. '1.x.5' => '>=1.0.0-0 <2.0.0-0',
  92. '3.x' => '>=3.0.0-0 <4.0.0-0',
  93. '1.X.X' => '>=1.0.0-0 <2.0.0-0',
  94. '1.*.*' => '>=1.0.0-0 <2.0.0-0',
  95. '2.X.x' => '>=2.0.0-0 <3.0.0-0',
  96. '5.*.x' => '>=5.0.0-0 <6.0.0-0',
  97. 'x' => '',
  98. );
  99. foreach ($t as $original => $result) {
  100. $v = new SemVer\expression($original, true);
  101. $this->assertEquals($v->getString(), $result, '[' . $original . '] %s');
  102. }
  103. }
  104. public function testSimpleversionRange()
  105. {
  106. $t = array(
  107. '1.0.0 - 2.0.0' => '>=1.0.0 <=2.0.0',
  108. '1.2.3 - 1.3.0' => '>=1.2.3 <=1.3.0',
  109. '4.3.0 - 4.3.1' => '>=4.3.0 <=4.3.1',
  110. );
  111. foreach ($t as $original => $result) {
  112. $v = new SemVer\expression($original, true);
  113. $this->assertEquals($v->getString(), $result, '[' . $original . '] %s');
  114. }
  115. }
  116. public function testShortversionRange()
  117. {
  118. $t = array(
  119. '1 - 2' => '>=1.0.0-0 <3.0.0-0',
  120. '1.2 - 2.1' => '>=1.2.0-0 <2.2.0-0',
  121. );
  122. foreach ($t as $original => $result) {
  123. $v = new SemVer\expression($original, true);
  124. $this->assertEquals($v->getString(), $result, '[' . $original . '] %s');
  125. }
  126. }
  127. public function testSpermies()
  128. {
  129. $t = array(
  130. '~1' => '>=1.0.0-0 <2.0.0-0',
  131. '~2.3' => '>=2.3.0-0 <2.4.0-0',
  132. '~3.7.2' => '>=3.7.2-0 <3.8.0-0',
  133. '~1.x' => '>=1.0.0-0 <2.0.0-0',
  134. '~1.2.x' => '>=1.2.0-0 <1.3.0-0',
  135. );
  136. foreach ($t as $original => $result) {
  137. $v = new SemVer\expression($original, true);
  138. $this->assertEquals($v->getString(), $result, '[' . $original . '] %s');
  139. }
  140. }
  141. public function testInvalidVersion()
  142. {
  143. $t = array(
  144. '3.x2',
  145. 'xx',
  146. '2.xx',
  147. '**.2',
  148. 'Xx*',
  149. '.2.2',
  150. '1..2',
  151. '1.5.6.x',
  152. '1.5.6.7',
  153. );
  154. foreach ($t as $original) {
  155. $ex = false;
  156. try {
  157. $v = new SemVer\expression($original);
  158. } catch (SemVer\SemVerException $e) {
  159. $ex = true;
  160. }
  161. $this->assertTrue($ex);
  162. }
  163. }
  164. public function testComplexExpessions()
  165. {
  166. $t = array(
  167. '1.x || 2.0 - 2.3 || >4.x.x' => '>=1.0.0-0 <2.0.0-0||>=2.0.0-0 <2.4.0-0||>=5.0.0-0',
  168. '2.0.x || 2.1 - 4 || 4 - 4.5' => '>=2.0.0-0 <2.1.0-0||>=2.1.0-0 <5.0.0-0||>=4.0.0-0 <4.6.0-0',
  169. );
  170. foreach ($t as $original => $result) {
  171. $v = new SemVer\expression($original, true);
  172. $this->assertEquals($v->getString(), $result, '[' . $original . '] %s');
  173. }
  174. }
  175. public function testSatisfiedBy()
  176. {
  177. $t = array(
  178. '1.0.0' => '1.0.0',
  179. '1.2.3' => '1.2.3',
  180. '>=1.0.1' => array('1.0.1', '1.0.2', '1.2.0', '2.0.0'),
  181. '>=2' => array('2.0.0', '2.0.1', '2.1.5', '3.0.0'),
  182. '<=2.4' => array('1.2.0', '2.0.0', '2.3.999999999999'),
  183. '3.x' => array('3.0.1', '3.2.0', '3.1.5'),
  184. '1.5.6 - 2.3.4' => array('1.5.6', '1.5.7', '1.6.0', '2.1.0', '2.3.0', '2.3.3', '2.3.4'),
  185. '1 - 2 || >=2.0.5' => array('1.0.0', '2.1.0', '1.4.0', '1.0.2', '3.0.4', '2.0.6', '3.0.0'),
  186. '>4.0.0 <=4.2.3' => array('4.0.1', '4.1.2', '4.2.3', '4.1.0'),
  187. );
  188. foreach ($t as $range => $satisfies) {
  189. $e = new SemVer\expression($range, true);
  190. if ( ! is_array($satisfies)) {
  191. $satisfies = array($satisfies);
  192. }
  193. foreach ($satisfies as $version) {
  194. $v = new SemVer\version($version, true);
  195. $this->assertTrue($e->satisfiedBy($v), '[' . $range . ' :: ' . $version . '] %s');
  196. $this->assertTrue($v->satisfies($e), '[' . $range . ' :: ' . $version . '] %s');
  197. }
  198. }
  199. }
  200. public function testNotSatisfiedBy()
  201. {
  202. $t = array(
  203. '1.0.0' => '1.0.1',
  204. '1.2.3' => '2.0.0',
  205. '<1.0.1' => array('1.0.1', '1.0.2', '1.2.0', '2.0.0'),
  206. '<=2' => array('2.0.1', '2.1.5', '3.0.0'),
  207. '>=2.4' => array('1.2.0', '2.0.0'),
  208. '3.x' => array('1.0.0', '1.9.9', '2.999.9999', '4.0.0'),
  209. '<1.5.6 || >=2.3.4 <3.0.0' => array('1.5.6', '1.5.7', '1.6.0', '2.1.0', '2.3.0', '2.3.3', '3.0.0', '3.2.1'),
  210. '1.2.0 - 2.1.2' => array('1.1.2', '2.2.0'),
  211. '>4.0.0 <=4.2.3' => array('4.0.0', '4.2.4', '4.5.0', '3.2.2'),
  212. );
  213. foreach ($t as $range => $satisfies) {
  214. $e = new SemVer\expression($range, true);
  215. if ( ! is_array($satisfies)) {
  216. $satisfies = array($satisfies);
  217. }
  218. foreach ($satisfies as $version) {
  219. $v = new SemVer\version($version, true);
  220. $this->assertFalse($e->satisfiedBy($v), '[' . $range . ' :: ' . $version . '] %s');
  221. $this->assertFalse($v->satisfies($e), '[' . $range . ' :: ' . $version . '] %s');
  222. }
  223. }
  224. }
  225. /**
  226. * @dataProvider invalidSemanticVersionProvider
  227. */
  228. public function testInvalidExpressionString($version)
  229. {
  230. $this->expectException(SemVer\SemVerException::class);
  231. new SemVer\expression($version);
  232. }
  233. /**
  234. * @dataProvider invalidSemanticVersionProvider
  235. */
  236. public function testInvalidVersionString($version)
  237. {
  238. $this->expectException(SemVer\SemVerException::class);
  239. new SemVer\version($version);
  240. }
  241. public function invalidSemanticVersionProvider()
  242. {
  243. return array(
  244. array('ce16575adbf06e5771b4bb4d5ac9609a685c1504'),
  245. array('faeih178.58498uinv-dibqo'),
  246. array('1.2.vqosbie'),
  247. array('vce16575adbf06e5771b4bb4d5ac9609a685.c1504'),
  248. );
  249. }
  250. function testVersionFunctions()
  251. {
  252. $t = array(
  253. '1.0.0-alpha' => array('M' => 1, 'm' => 0, 'p' => 0, 'pr' => array('alpha'), 'b' => array()),
  254. '1.0.0-alpha.1' => array('M' => 1, 'm' => 0, 'p' => 0, 'pr' => array('alpha', 1), 'b' => array()),
  255. '1.0.0-0.3.7' => array('M' => 1, 'm' => 0, 'p' => 0, 'pr' => array(0, 3, 7), 'b' => array()),
  256. '1.0.0-x.7.z.92' => array('M' => 1, 'm' => 0, 'p' => 0, 'pr' => array('x', 7, 'z', 92), 'b' => array()),
  257. '1.0.0-alpha+001' => array('M' => 1, 'm' => 0, 'p' => 0, 'pr' => array('alpha'), 'b' => array('001')),
  258. '1.2.3-alpha.2+02' => array('M' => 1, 'm' => 2, 'p' => 3, 'pr' => array('alpha', 2), 'b' => array('02')),
  259. '1.2.3-a.3+02.5.a' => array(
  260. 'M' => 1,
  261. 'm' => 2,
  262. 'p' => 3,
  263. 'pr' => array('a', 3),
  264. 'b' => array('02', 5, 'a'),
  265. ),
  266. );
  267. foreach ($t as $version => $parts) {
  268. $v = new SemVer\version($version);
  269. $this->assertEquals($v->getMajor(), $parts['M']);
  270. $this->assertEquals($v->getMinor(), $parts['m']);
  271. $this->assertEquals($v->getPatch(), $parts['p']);
  272. $this->assertEquals($v->getPrerelease(), $parts['pr']);
  273. $this->assertEquals($v->getBuild(), $parts['b']);
  274. }
  275. }
  276. }