ValidatingArrayLoaderTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. 'composer-runtime-api' => '*',
  84. ),
  85. 'require-dev' => array(
  86. 'a/b' => '1.*',
  87. 'b/c' => '*',
  88. 'example' => '>2.0-dev,<2.4-dev',
  89. ),
  90. 'conflict' => array(
  91. 'a/b' => '1.*',
  92. 'b/c' => '>2.7',
  93. 'example' => '>2.0-dev,<2.4-dev',
  94. ),
  95. 'replace' => array(
  96. 'a/b' => '1.*',
  97. 'example' => '>2.0-dev,<2.4-dev',
  98. ),
  99. 'provide' => array(
  100. 'a/b' => '1.*',
  101. 'example' => '>2.0-dev,<2.4-dev',
  102. ),
  103. 'suggest' => array(
  104. 'foo/bar' => 'Foo bar is very useful',
  105. ),
  106. 'autoload' => array(
  107. 'psr-0' => array(
  108. 'Foo\\Bar' => 'src/',
  109. '' => 'fallback/libs/',
  110. ),
  111. 'classmap' => array(
  112. 'dir/',
  113. 'dir2/file.php',
  114. ),
  115. 'files' => array(
  116. 'functions.php',
  117. ),
  118. ),
  119. 'include-path' => array(
  120. 'lib/',
  121. ),
  122. 'target-dir' => 'Foo/Bar',
  123. 'minimum-stability' => 'dev',
  124. 'repositories' => array(
  125. array(
  126. 'type' => 'composer',
  127. 'url' => 'https://repo.packagist.org/',
  128. ),
  129. ),
  130. 'config' => array(
  131. 'bin-dir' => 'bin',
  132. 'vendor-dir' => 'vendor',
  133. 'process-timeout' => 10000,
  134. ),
  135. 'archive' => array(
  136. 'exclude' => array('/foo/bar', 'baz', '!/foo/bar/baz'),
  137. ),
  138. 'scripts' => array(
  139. 'post-update-cmd' => 'Foo\\Bar\\Baz::doSomething',
  140. 'post-install-cmd' => array(
  141. 'Foo\\Bar\\Baz::doSomething',
  142. ),
  143. ),
  144. 'extra' => array(
  145. 'random' => array('stuff' => array('deeply' => 'nested')),
  146. 'branch-alias' => array(
  147. 'dev-master' => '2.0-dev',
  148. 'dev-old' => '1.0.x-dev',
  149. '3.x-dev' => '3.1.x-dev',
  150. ),
  151. ),
  152. 'bin' => array(
  153. 'bin/foo',
  154. 'bin/bar',
  155. ),
  156. 'transport-options' => array('ssl' => array('local_cert' => '/opt/certs/test.pem')),
  157. ),
  158. ),
  159. array( // test licenses as array
  160. array(
  161. 'name' => 'foo/bar',
  162. 'license' => array('MIT', 'WTFPL'),
  163. ),
  164. ),
  165. array( // test bin as string
  166. array(
  167. 'name' => 'foo/bar',
  168. 'bin' => 'bin1',
  169. ),
  170. ),
  171. array( // package name with dashes
  172. array(
  173. 'name' => 'foo/bar-baz',
  174. ),
  175. ),
  176. array( // package name with dashes
  177. array(
  178. 'name' => 'foo/bar--baz',
  179. ),
  180. ),
  181. array( // package name with dashes
  182. array(
  183. 'name' => 'foo/b-ar--ba-z',
  184. ),
  185. ),
  186. array( // package name with dashes
  187. array(
  188. 'name' => 'npm-asset/angular--core',
  189. ),
  190. ),
  191. );
  192. }
  193. /**
  194. * @dataProvider errorProvider
  195. */
  196. public function testLoadFailureThrowsException($config, $expectedErrors)
  197. {
  198. $internalLoader = $this->getMockBuilder('Composer\Package\Loader\LoaderInterface')->getMock();
  199. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  200. try {
  201. $loader->load($config);
  202. $this->fail('Expected exception to be thrown');
  203. } catch (InvalidPackageException $e) {
  204. $errors = $e->getErrors();
  205. sort($expectedErrors);
  206. sort($errors);
  207. $this->assertEquals($expectedErrors, $errors);
  208. }
  209. }
  210. /**
  211. * @dataProvider warningProvider
  212. */
  213. public function testLoadWarnings($config, $expectedWarnings)
  214. {
  215. $internalLoader = $this->getMockBuilder('Composer\Package\Loader\LoaderInterface')->getMock();
  216. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  217. $loader->load($config);
  218. $warnings = $loader->getWarnings();
  219. sort($expectedWarnings);
  220. sort($warnings);
  221. $this->assertEquals($expectedWarnings, $warnings);
  222. }
  223. /**
  224. * @dataProvider warningProvider
  225. */
  226. public function testLoadSkipsWarningDataWhenIgnoringErrors($config, $expectedWarnings, $mustCheck = true)
  227. {
  228. if (!$mustCheck) {
  229. $this->assertTrue(true);
  230. return;
  231. }
  232. $internalLoader = $this->getMockBuilder('Composer\Package\Loader\LoaderInterface')->getMock();
  233. $internalLoader
  234. ->expects($this->once())
  235. ->method('load')
  236. ->with(array('name' => 'a/b'));
  237. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  238. $config['name'] = 'a/b';
  239. $loader->load($config);
  240. }
  241. public function errorProvider()
  242. {
  243. $invalidNames = array(
  244. 'foo',
  245. 'foo/-bar-',
  246. 'foo/-bar',
  247. );
  248. $invalidNaming = array();
  249. foreach($invalidNames as $invalidName) {
  250. $invalidNaming[] = array(
  251. array(
  252. 'name' => $invalidName,
  253. ),
  254. array(
  255. "name : invalid value ($invalidName), must match [A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*",
  256. ),
  257. );
  258. }
  259. return array_merge($invalidNaming, array(
  260. array(
  261. array(
  262. 'name' => 'foo/bar',
  263. 'homepage' => 43,
  264. ),
  265. array(
  266. 'homepage : should be a string, integer given',
  267. ),
  268. ),
  269. array(
  270. array(
  271. 'name' => 'foo/bar',
  272. 'support' => array(
  273. 'source' => array(),
  274. ),
  275. ),
  276. array(
  277. 'support.source : invalid value, must be a string',
  278. ),
  279. ),
  280. array(
  281. array(
  282. 'name' => 'foo/bar',
  283. 'autoload' => 'strings',
  284. ),
  285. array(
  286. 'autoload : should be an array, string given',
  287. ),
  288. ),
  289. array(
  290. array(
  291. 'name' => 'foo/bar',
  292. 'autoload' => array(
  293. 'psr0' => array(
  294. 'foo' => 'src',
  295. ),
  296. ),
  297. ),
  298. array(
  299. 'autoload : invalid value (psr0), must be one of psr-0, psr-4, classmap, files, exclude-from-classmap',
  300. ),
  301. ),
  302. array(
  303. array(
  304. 'name' => 'foo/bar',
  305. 'transport-options' => 'test',
  306. ),
  307. array(
  308. 'transport-options : should be an array, string given',
  309. ),
  310. ),
  311. ));
  312. }
  313. public function warningProvider()
  314. {
  315. $invalidNames = array(
  316. 'fo--oo/bar',
  317. 'fo-oo/bar__baz',
  318. 'fo-oo/bar_.baz',
  319. 'foo/bar---baz',
  320. );
  321. $invalidNaming = array();
  322. foreach($invalidNames as $invalidName) {
  323. $invalidNaming[] = array(
  324. array(
  325. 'name' => $invalidName,
  326. ),
  327. array(
  328. "Deprecation warning: Your package name $invalidName is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match \"^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$\". Make sure you fix this as Composer 2.0 will error.",
  329. ),
  330. false,
  331. );
  332. }
  333. return array_merge($invalidNaming, array(
  334. array(
  335. array(
  336. 'name' => 'foo/bar',
  337. 'homepage' => 'foo:bar',
  338. ),
  339. array(
  340. 'homepage : invalid value (foo:bar), must be an http/https URL',
  341. ),
  342. ),
  343. array(
  344. array(
  345. 'name' => 'foo/bar.json',
  346. ),
  347. array(
  348. '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.',
  349. ),
  350. ),
  351. array(
  352. array(
  353. 'name' => 'com1/foo',
  354. ),
  355. array(
  356. '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.',
  357. ),
  358. ),
  359. array(
  360. array(
  361. 'name' => 'Foo/Bar',
  362. ),
  363. array(
  364. '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.',
  365. ),
  366. ),
  367. array(
  368. array(
  369. 'name' => 'foo/bar',
  370. 'support' => array(
  371. 'source' => 'foo:bar',
  372. 'forum' => 'foo:bar',
  373. 'issues' => 'foo:bar',
  374. 'wiki' => 'foo:bar',
  375. 'chat' => 'foo:bar',
  376. ),
  377. ),
  378. array(
  379. 'support.source : invalid value (foo:bar), must be an http/https URL',
  380. 'support.forum : invalid value (foo:bar), must be an http/https URL',
  381. 'support.issues : invalid value (foo:bar), must be an http/https URL',
  382. 'support.wiki : invalid value (foo:bar), must be an http/https URL',
  383. 'support.chat : invalid value (foo:bar), must be an http/https URL',
  384. ),
  385. ),
  386. array(
  387. array(
  388. 'name' => 'foo/bar',
  389. 'require' => array(
  390. 'foo/baz' => '*',
  391. 'bar/baz' => '>=1.0',
  392. 'bar/hacked' => '@stable',
  393. 'bar/woo' => '1.0.0',
  394. ),
  395. ),
  396. array(
  397. 'require.foo/baz : unbound version constraints (*) should be avoided',
  398. 'require.bar/baz : unbound version constraints (>=1.0) should be avoided',
  399. 'require.bar/hacked : unbound version constraints (@stable) should be avoided',
  400. 'require.bar/woo : exact version constraints (1.0.0) should be avoided if the package follows semantic versioning',
  401. ),
  402. false,
  403. ),
  404. array(
  405. array(
  406. 'name' => 'foo/bar',
  407. 'require' => array(
  408. 'Foo/Baz' => '^1.0',
  409. ),
  410. ),
  411. array(
  412. '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.',
  413. ),
  414. false,
  415. ),
  416. array(
  417. array(
  418. 'name' => 'foo/bar',
  419. 'require' => array(
  420. 'bar/unstable' => '0.3.0',
  421. ),
  422. ),
  423. array(
  424. // using an exact version constraint for an unstable version should not trigger a warning
  425. ),
  426. false,
  427. ),
  428. array(
  429. array(
  430. 'name' => 'foo/bar',
  431. 'extra' => array(
  432. 'branch-alias' => array(
  433. '5.x-dev' => '3.1.x-dev',
  434. ),
  435. ),
  436. ),
  437. array(
  438. 'extra.branch-alias.5.x-dev : the target branch (3.1.x-dev) is not a valid numeric alias for this version',
  439. ),
  440. false,
  441. ),
  442. array(
  443. array(
  444. 'name' => 'foo/bar',
  445. 'extra' => array(
  446. 'branch-alias' => array(
  447. '5.x-dev' => '3.1-dev',
  448. ),
  449. ),
  450. ),
  451. array(
  452. 'extra.branch-alias.5.x-dev : the target branch (3.1-dev) is not a valid numeric alias for this version',
  453. ),
  454. false,
  455. ),
  456. ));
  457. }
  458. }