ArrayLoaderTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Package\Loader;
  12. use Composer\Package\Loader\ArrayLoader;
  13. use Composer\Package\Dumper\ArrayDumper;
  14. use Composer\Test\TestCase;
  15. class ArrayLoaderTest extends TestCase
  16. {
  17. /**
  18. * @var ArrayLoader
  19. */
  20. private $loader;
  21. public function setUp()
  22. {
  23. $this->loader = new ArrayLoader(null);
  24. }
  25. public function testSelfVersion()
  26. {
  27. $config = array(
  28. 'name' => 'A',
  29. 'version' => '1.2.3.4',
  30. 'replace' => array(
  31. 'foo' => 'self.version',
  32. ),
  33. );
  34. $package = $this->loader->load($config);
  35. $replaces = $package->getReplaces();
  36. $this->assertEquals('== 1.2.3.4', (string) $replaces['foo']->getConstraint());
  37. }
  38. public function testTypeDefault()
  39. {
  40. $config = array(
  41. 'name' => 'A',
  42. 'version' => '1.0',
  43. );
  44. $package = $this->loader->load($config);
  45. $this->assertEquals('library', $package->getType());
  46. $config = array(
  47. 'name' => 'A',
  48. 'version' => '1.0',
  49. 'type' => 'foo',
  50. );
  51. $package = $this->loader->load($config);
  52. $this->assertEquals('foo', $package->getType());
  53. }
  54. public function testNormalizedVersionOptimization()
  55. {
  56. $config = array(
  57. 'name' => 'A',
  58. 'version' => '1.2.3',
  59. );
  60. $package = $this->loader->load($config);
  61. $this->assertEquals('1.2.3.0', $package->getVersion());
  62. $config = array(
  63. 'name' => 'A',
  64. 'version' => '1.2.3',
  65. 'version_normalized' => '1.2.3.4',
  66. );
  67. $package = $this->loader->load($config);
  68. $this->assertEquals('1.2.3.4', $package->getVersion());
  69. }
  70. public function parseDumpProvider()
  71. {
  72. $validConfig = array(
  73. 'name' => 'A/B',
  74. 'version' => '1.2.3',
  75. 'version_normalized' => '1.2.3.0',
  76. 'description' => 'Foo bar',
  77. 'type' => 'library',
  78. 'keywords' => array('a', 'b', 'c'),
  79. 'homepage' => 'http://example.com',
  80. 'license' => array('MIT', 'GPLv3'),
  81. 'authors' => array(
  82. array('name' => 'Bob', 'email' => 'bob@example.org', 'homepage' => 'example.org', 'role' => 'Developer'),
  83. ),
  84. 'funding' => array(
  85. array('type' => 'example', 'url' => 'https://example.org/fund'),
  86. ),
  87. 'require' => array(
  88. 'foo/bar' => '1.0',
  89. ),
  90. 'require-dev' => array(
  91. 'foo/baz' => '1.0',
  92. ),
  93. 'replace' => array(
  94. 'foo/qux' => '1.0',
  95. ),
  96. 'conflict' => array(
  97. 'foo/quux' => '1.0',
  98. ),
  99. 'provide' => array(
  100. 'foo/quuux' => '1.0',
  101. ),
  102. 'autoload' => array(
  103. 'psr-0' => array('Ns\Prefix' => 'path'),
  104. 'classmap' => array('path', 'path2'),
  105. ),
  106. 'include-path' => array('path3', 'path4'),
  107. 'target-dir' => 'some/prefix',
  108. 'extra' => array('random' => array('things' => 'of', 'any' => 'shape')),
  109. 'bin' => array('bin1', 'bin/foo'),
  110. 'archive' => array(
  111. 'exclude' => array('/foo/bar', 'baz', '!/foo/bar/baz'),
  112. ),
  113. 'transport-options' => array('ssl' => array('local_cert' => '/opt/certs/test.pem')),
  114. 'abandoned' => 'foo/bar',
  115. );
  116. return array(array($validConfig));
  117. }
  118. protected function fixConfigWhenLoadConfigIsFalse($config)
  119. {
  120. $expectedConfig = $config;
  121. unset($expectedConfig['transport-options']);
  122. return $expectedConfig;
  123. }
  124. /**
  125. * The default parser should default to loading the config as this
  126. * allows require-dev libraries to have transport options included.
  127. *
  128. * @dataProvider parseDumpProvider
  129. */
  130. public function testParseDumpDefaultLoadConfig($config)
  131. {
  132. $package = $this->loader->load($config);
  133. $dumper = new ArrayDumper;
  134. $expectedConfig = $this->fixConfigWhenLoadConfigIsFalse($config);
  135. $this->assertEquals($expectedConfig, $dumper->dump($package));
  136. }
  137. /**
  138. * @dataProvider parseDumpProvider
  139. */
  140. public function testParseDumpTrueLoadConfig($config)
  141. {
  142. $loader = new ArrayLoader(null, true);
  143. $package = $loader->load($config);
  144. $dumper = new ArrayDumper;
  145. $expectedConfig = $config;
  146. $this->assertEquals($expectedConfig, $dumper->dump($package));
  147. }
  148. /**
  149. * @dataProvider parseDumpProvider
  150. */
  151. public function testParseDumpFalseLoadConfig($config)
  152. {
  153. $loader = new ArrayLoader(null, false);
  154. $package = $loader->load($config);
  155. $dumper = new ArrayDumper;
  156. $expectedConfig = $this->fixConfigWhenLoadConfigIsFalse($config);
  157. $this->assertEquals($expectedConfig, $dumper->dump($package));
  158. }
  159. public function testPackageWithBranchAlias()
  160. {
  161. $config = array(
  162. 'name' => 'A',
  163. 'version' => 'dev-master',
  164. 'extra' => array('branch-alias' => array('dev-master' => '1.0.x-dev')),
  165. );
  166. $package = $this->loader->load($config);
  167. $this->assertInstanceOf('Composer\Package\AliasPackage', $package);
  168. $this->assertEquals('1.0.x-dev', $package->getPrettyVersion());
  169. $config = array(
  170. 'name' => 'A',
  171. 'version' => 'dev-master',
  172. 'extra' => array('branch-alias' => array('dev-master' => '1.0-dev')),
  173. );
  174. $package = $this->loader->load($config);
  175. $this->assertInstanceOf('Composer\Package\AliasPackage', $package);
  176. $this->assertEquals('1.0.x-dev', $package->getPrettyVersion());
  177. $config = array(
  178. 'name' => 'B',
  179. 'version' => '4.x-dev',
  180. 'extra' => array('branch-alias' => array('4.x-dev' => '4.0.x-dev')),
  181. );
  182. $package = $this->loader->load($config);
  183. $this->assertInstanceOf('Composer\Package\AliasPackage', $package);
  184. $this->assertEquals('4.0.x-dev', $package->getPrettyVersion());
  185. $config = array(
  186. 'name' => 'B',
  187. 'version' => '4.x-dev',
  188. 'extra' => array('branch-alias' => array('4.x-dev' => '4.0-dev')),
  189. );
  190. $package = $this->loader->load($config);
  191. $this->assertInstanceOf('Composer\Package\AliasPackage', $package);
  192. $this->assertEquals('4.0.x-dev', $package->getPrettyVersion());
  193. $config = array(
  194. 'name' => 'C',
  195. 'version' => '4.x-dev',
  196. 'extra' => array('branch-alias' => array('4.x-dev' => '3.4.x-dev')),
  197. );
  198. $package = $this->loader->load($config);
  199. $this->assertInstanceOf('Composer\Package\CompletePackage', $package);
  200. $this->assertEquals('4.x-dev', $package->getPrettyVersion());
  201. }
  202. public function testAbandoned()
  203. {
  204. $config = array(
  205. 'name' => 'A',
  206. 'version' => '1.2.3.4',
  207. 'abandoned' => 'foo/bar',
  208. );
  209. $package = $this->loader->load($config);
  210. $this->assertTrue($package->isAbandoned());
  211. $this->assertEquals('foo/bar', $package->getReplacementPackage());
  212. }
  213. public function testNotAbandoned()
  214. {
  215. $config = array(
  216. 'name' => 'A',
  217. 'version' => '1.2.3.4',
  218. );
  219. $package = $this->loader->load($config);
  220. $this->assertFalse($package->isAbandoned());
  221. }
  222. public function pluginApiVersions()
  223. {
  224. return array(
  225. array('1.0'),
  226. array('1.0.0'),
  227. array('1.0.0.0'),
  228. array('1'),
  229. array('=1.0.0'),
  230. array('==1.0'),
  231. array('~1.0.0'),
  232. array('*'),
  233. array('3.0.*'),
  234. array('@stable'),
  235. array('1.0.0@stable'),
  236. array('^5.1'),
  237. array('>=1.0.0 <2.5'),
  238. array('x'),
  239. array('1.0.0-dev'),
  240. );
  241. }
  242. /**
  243. * @dataProvider pluginApiVersions
  244. */
  245. public function testPluginApiVersionAreKeptAsDeclared($apiVersion)
  246. {
  247. $links = $this->loader->parseLinks('Plugin', '9.9.9', '', array('composer-plugin-api' => $apiVersion));
  248. $this->assertArrayHasKey('composer-plugin-api', $links);
  249. $this->assertSame($apiVersion, $links['composer-plugin-api']->getConstraint()->getPrettyString());
  250. }
  251. public function testPluginApiVersionDoesSupportSelfVersion()
  252. {
  253. $links = $this->loader->parseLinks('Plugin', '6.6.6', '', array('composer-plugin-api' => 'self.version'));
  254. $this->assertArrayHasKey('composer-plugin-api', $links);
  255. $this->assertSame('6.6.6', $links['composer-plugin-api']->getConstraint()->getPrettyString());
  256. }
  257. }