ValidatingArrayLoaderTest.php 16 KB

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