ValidatingArrayLoaderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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\ValidatingArrayLoader;
  13. use Composer\Package\Loader\InvalidPackageException;
  14. class ValidatingArrayLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @dataProvider successProvider
  18. */
  19. public function testLoadSuccess($config)
  20. {
  21. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  22. $internalLoader
  23. ->expects($this->once())
  24. ->method('load')
  25. ->with($config);
  26. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  27. $loader->load($config);
  28. }
  29. public function successProvider()
  30. {
  31. return array(
  32. array( // minimal
  33. array(
  34. 'name' => 'foo/bar',
  35. ),
  36. ),
  37. array( // complete
  38. array(
  39. 'name' => 'foo/bar',
  40. 'description' => 'Foo bar',
  41. 'version' => '1.0.0',
  42. 'type' => 'library',
  43. 'keywords' => array('a', 'b_c', 'D E', 'éîüø', '微信'),
  44. 'homepage' => 'https://foo.com',
  45. 'time' => '2010-10-10T10:10:10+00:00',
  46. 'license' => 'MIT',
  47. 'authors' => array(
  48. array(
  49. 'name' => 'Alice',
  50. 'email' => 'alice@example.org',
  51. 'role' => 'Lead',
  52. 'homepage' => 'http://example.org',
  53. ),
  54. array(
  55. 'name' => 'Bob',
  56. 'homepage' => '',
  57. ),
  58. ),
  59. 'support' => array(
  60. 'email' => 'mail@example.org',
  61. 'issues' => 'http://example.org/',
  62. 'forum' => 'http://example.org/',
  63. 'wiki' => 'http://example.org/',
  64. 'source' => 'http://example.org/',
  65. 'irc' => 'irc://example.org/example',
  66. ),
  67. 'require' => array(
  68. 'a/b' => '1.*',
  69. 'b/c' => '~2',
  70. 'example' => '>2.0-dev,<2.4-dev',
  71. ),
  72. 'require-dev' => array(
  73. 'a/b' => '1.*',
  74. 'b/c' => '*',
  75. 'example' => '>2.0-dev,<2.4-dev',
  76. ),
  77. 'conflict' => array(
  78. 'a/b' => '1.*',
  79. 'b/c' => '>2.7',
  80. 'example' => '>2.0-dev,<2.4-dev',
  81. ),
  82. 'replace' => array(
  83. 'a/b' => '1.*',
  84. 'example' => '>2.0-dev,<2.4-dev',
  85. ),
  86. 'provide' => array(
  87. 'a/b' => '1.*',
  88. 'example' => '>2.0-dev,<2.4-dev',
  89. ),
  90. 'suggest' => array(
  91. 'foo/bar' => 'Foo bar is very useful',
  92. ),
  93. 'autoload' => array(
  94. 'psr-0' => array(
  95. 'Foo\\Bar' => 'src/',
  96. '' => 'fallback/libs/',
  97. ),
  98. 'classmap' => array(
  99. 'dir/',
  100. 'dir2/file.php',
  101. ),
  102. 'files' => array(
  103. 'functions.php',
  104. ),
  105. ),
  106. 'include-path' => array(
  107. 'lib/',
  108. ),
  109. 'target-dir' => 'Foo/Bar',
  110. 'minimum-stability' => 'dev',
  111. 'repositories' => array(
  112. array(
  113. 'type' => 'composer',
  114. 'url' => 'https://packagist.org/',
  115. ),
  116. ),
  117. 'config' => array(
  118. 'bin-dir' => 'bin',
  119. 'vendor-dir' => 'vendor',
  120. 'process-timeout' => 10000,
  121. ),
  122. 'archive' => array(
  123. 'exclude' => array('/foo/bar', 'baz', '!/foo/bar/baz'),
  124. ),
  125. 'scripts' => array(
  126. 'post-update-cmd' => 'Foo\\Bar\\Baz::doSomething',
  127. 'post-install-cmd' => array(
  128. 'Foo\\Bar\\Baz::doSomething',
  129. ),
  130. ),
  131. 'extra' => array(
  132. 'random' => array('stuff' => array('deeply' => 'nested')),
  133. 'branch-alias' => array(
  134. 'dev-master' => '2.0-dev',
  135. 'dev-old' => '1.0.x-dev',
  136. '3.x-dev' => '3.1.x-dev',
  137. ),
  138. ),
  139. 'bin' => array(
  140. 'bin/foo',
  141. 'bin/bar',
  142. ),
  143. 'transport-options' => array('ssl' => array('local_cert' => '/opt/certs/test.pem')),
  144. ),
  145. ),
  146. array( // test as array
  147. array(
  148. 'name' => 'foo/bar',
  149. 'license' => array('MIT', 'WTFPL'),
  150. ),
  151. ),
  152. );
  153. }
  154. /**
  155. * @dataProvider errorProvider
  156. */
  157. public function testLoadFailureThrowsException($config, $expectedErrors)
  158. {
  159. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  160. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  161. try {
  162. $loader->load($config);
  163. $this->fail('Expected exception to be thrown');
  164. } catch (InvalidPackageException $e) {
  165. $errors = $e->getErrors();
  166. sort($expectedErrors);
  167. sort($errors);
  168. $this->assertEquals($expectedErrors, $errors);
  169. }
  170. }
  171. /**
  172. * @dataProvider warningProvider
  173. */
  174. public function testLoadWarnings($config, $expectedWarnings)
  175. {
  176. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  177. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  178. $loader->load($config);
  179. $warnings = $loader->getWarnings();
  180. sort($expectedWarnings);
  181. sort($warnings);
  182. $this->assertEquals($expectedWarnings, $warnings);
  183. }
  184. /**
  185. * @dataProvider warningProvider
  186. */
  187. public function testLoadSkipsWarningDataWhenIgnoringErrors($config, $expectedWarnings, $mustCheck = true)
  188. {
  189. if (!$mustCheck) {
  190. $this->assertTrue(true);
  191. return;
  192. }
  193. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  194. $internalLoader
  195. ->expects($this->once())
  196. ->method('load')
  197. ->with(array('name' => 'a/b'));
  198. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  199. $config['name'] = 'a/b';
  200. $loader->load($config);
  201. }
  202. public function errorProvider()
  203. {
  204. return array(
  205. array(
  206. array(
  207. 'name' => 'foo',
  208. ),
  209. array(
  210. 'name : invalid value (foo), must match [A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*',
  211. ),
  212. ),
  213. array(
  214. array(
  215. 'name' => 'foo/bar',
  216. 'homepage' => 43,
  217. ),
  218. array(
  219. 'homepage : should be a string, integer given',
  220. ),
  221. ),
  222. array(
  223. array(
  224. 'name' => 'foo/bar',
  225. 'support' => array(
  226. 'source' => array(),
  227. ),
  228. ),
  229. array(
  230. 'support.source : invalid value, must be a string',
  231. ),
  232. ),
  233. array(
  234. array(
  235. 'name' => 'foo/bar',
  236. 'autoload' => 'strings',
  237. ),
  238. array(
  239. 'autoload : should be an array, string given',
  240. ),
  241. ),
  242. array(
  243. array(
  244. 'name' => 'foo/bar',
  245. 'autoload' => array(
  246. 'psr0' => array(
  247. 'foo' => 'src',
  248. ),
  249. ),
  250. ),
  251. array(
  252. 'autoload : invalid value (psr0), must be one of psr-0, psr-4, classmap, files, exclude-from-classmap',
  253. ),
  254. ),
  255. array(
  256. array(
  257. 'name' => 'foo/bar',
  258. 'transport-options' => 'test',
  259. ),
  260. array(
  261. 'transport-options : should be an array, string given',
  262. ),
  263. ),
  264. );
  265. }
  266. public function warningProvider()
  267. {
  268. return array(
  269. array(
  270. array(
  271. 'name' => 'foo/bar',
  272. 'homepage' => 'foo:bar',
  273. ),
  274. array(
  275. 'homepage : invalid value (foo:bar), must be an http/https URL',
  276. ),
  277. ),
  278. array(
  279. array(
  280. 'name' => 'foo/bar',
  281. 'support' => array(
  282. 'source' => 'foo:bar',
  283. 'forum' => 'foo:bar',
  284. 'issues' => 'foo:bar',
  285. 'wiki' => 'foo:bar',
  286. ),
  287. ),
  288. array(
  289. 'support.source : invalid value (foo:bar), must be an http/https URL',
  290. 'support.forum : invalid value (foo:bar), must be an http/https URL',
  291. 'support.issues : invalid value (foo:bar), must be an http/https URL',
  292. 'support.wiki : invalid value (foo:bar), must be an http/https URL',
  293. ),
  294. ),
  295. array(
  296. array(
  297. 'name' => 'foo/bar',
  298. 'require' => array(
  299. 'foo/baz' => '*',
  300. 'bar/baz' => '>=1.0',
  301. 'bar/foo' => 'dev-master',
  302. 'bar/hacked' => '@stable',
  303. 'bar/woo' => '1.0.0',
  304. ),
  305. ),
  306. array(
  307. 'require.foo/baz : unbound version constraints (*) should be avoided',
  308. 'require.bar/baz : unbound version constraints (>=1.0) should be avoided',
  309. 'require.bar/foo : unbound version constraints (dev-master) should be avoided',
  310. 'require.bar/hacked : unbound version constraints (@stable) should be avoided',
  311. 'require.bar/woo : exact version constraints (1.0.0) should be avoided if the package follows semantic versioning',
  312. ),
  313. false,
  314. ),
  315. array(
  316. array(
  317. 'name' => 'foo/bar',
  318. 'require' => array(
  319. 'bar/unstable' => '0.3.0',
  320. ),
  321. ),
  322. array(
  323. // using an exact version constraint for an unstable version should not trigger a warning
  324. ),
  325. false,
  326. ),
  327. array(
  328. array(
  329. 'name' => 'foo/bar',
  330. 'extra' => array(
  331. 'branch-alias' => array(
  332. '5.x-dev' => '3.1.x-dev',
  333. ),
  334. ),
  335. ),
  336. array(
  337. 'extra.branch-alias.5.x-dev : the target branch (3.1.x-dev) is not a valid numeric alias for this version',
  338. ),
  339. false,
  340. ),
  341. array(
  342. array(
  343. 'name' => 'foo/bar',
  344. 'extra' => array(
  345. 'branch-alias' => array(
  346. '5.x-dev' => '3.1-dev',
  347. ),
  348. ),
  349. ),
  350. array(
  351. 'extra.branch-alias.5.x-dev : the target branch (3.1-dev) is not a valid numeric alias for this version',
  352. ),
  353. false,
  354. ),
  355. );
  356. }
  357. }