ValidatingArrayLoaderTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. array( // package name with dashes
  171. array(
  172. 'name' => 'foo/bar-baz',
  173. ),
  174. ),
  175. array( // package name with dashes
  176. array(
  177. 'name' => 'foo/bar--baz',
  178. ),
  179. ),
  180. array( // package name with dashes
  181. array(
  182. 'name' => 'foo/b-ar--ba-z',
  183. ),
  184. ),
  185. array( // package name with dashes
  186. array(
  187. 'name' => 'npm-asset/angular--core',
  188. ),
  189. ),
  190. );
  191. }
  192. /**
  193. * @dataProvider errorProvider
  194. */
  195. public function testLoadFailureThrowsException($config, $expectedErrors)
  196. {
  197. $internalLoader = $this->getMockBuilder('Composer\Package\Loader\LoaderInterface')->getMock();
  198. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  199. try {
  200. $loader->load($config);
  201. $this->fail('Expected exception to be thrown');
  202. } catch (InvalidPackageException $e) {
  203. $errors = $e->getErrors();
  204. sort($expectedErrors);
  205. sort($errors);
  206. $this->assertEquals($expectedErrors, $errors);
  207. }
  208. }
  209. /**
  210. * @dataProvider warningProvider
  211. */
  212. public function testLoadWarnings($config, $expectedWarnings)
  213. {
  214. $internalLoader = $this->getMockBuilder('Composer\Package\Loader\LoaderInterface')->getMock();
  215. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  216. $loader->load($config);
  217. $warnings = $loader->getWarnings();
  218. sort($expectedWarnings);
  219. sort($warnings);
  220. $this->assertEquals($expectedWarnings, $warnings);
  221. }
  222. /**
  223. * @dataProvider warningProvider
  224. */
  225. public function testLoadSkipsWarningDataWhenIgnoringErrors($config, $expectedWarnings, $mustCheck = true)
  226. {
  227. if (!$mustCheck) {
  228. $this->assertTrue(true);
  229. return;
  230. }
  231. $internalLoader = $this->getMockBuilder('Composer\Package\Loader\LoaderInterface')->getMock();
  232. $internalLoader
  233. ->expects($this->once())
  234. ->method('load')
  235. ->with(array('name' => 'a/b'));
  236. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  237. $config['name'] = 'a/b';
  238. $loader->load($config);
  239. }
  240. public function errorProvider()
  241. {
  242. $invalidNames = array(
  243. 'foo',
  244. 'foo/-bar-',
  245. 'foo/-bar',
  246. );
  247. $invalidNaming = array();
  248. foreach($invalidNames as $invalidName) {
  249. $invalidNaming[] = array(
  250. array(
  251. 'name' => $invalidName,
  252. ),
  253. array(
  254. "name : invalid value ($invalidName), must match [A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*",
  255. ),
  256. );
  257. }
  258. return array_merge($invalidNaming, array(
  259. array(
  260. array(
  261. 'name' => 'foo/bar',
  262. 'homepage' => 43,
  263. ),
  264. array(
  265. 'homepage : should be a string, integer given',
  266. ),
  267. ),
  268. array(
  269. array(
  270. 'name' => 'foo/bar',
  271. 'support' => array(
  272. 'source' => array(),
  273. ),
  274. ),
  275. array(
  276. 'support.source : invalid value, must be a string',
  277. ),
  278. ),
  279. array(
  280. array(
  281. 'name' => 'foo/bar',
  282. 'autoload' => 'strings',
  283. ),
  284. array(
  285. 'autoload : should be an array, string given',
  286. ),
  287. ),
  288. array(
  289. array(
  290. 'name' => 'foo/bar',
  291. 'autoload' => array(
  292. 'psr0' => array(
  293. 'foo' => 'src',
  294. ),
  295. ),
  296. ),
  297. array(
  298. 'autoload : invalid value (psr0), must be one of psr-0, psr-4, classmap, files, exclude-from-classmap',
  299. ),
  300. ),
  301. array(
  302. array(
  303. 'name' => 'foo/bar',
  304. 'transport-options' => 'test',
  305. ),
  306. array(
  307. 'transport-options : should be an array, string given',
  308. ),
  309. ),
  310. ));
  311. }
  312. public function warningProvider()
  313. {
  314. $invalidNames = array(
  315. 'fo--oo/bar',
  316. 'fo-oo/bar__baz',
  317. 'fo-oo/bar_.baz',
  318. 'foo/bar---baz',
  319. );
  320. $invalidNaming = array();
  321. foreach($invalidNames as $invalidName) {
  322. $invalidNaming[] = array(
  323. array(
  324. 'name' => $invalidName,
  325. ),
  326. array(
  327. "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.",
  328. ),
  329. false,
  330. );
  331. }
  332. return array_merge($invalidNaming, array(
  333. array(
  334. array(
  335. 'name' => 'foo/bar',
  336. 'homepage' => 'foo:bar',
  337. ),
  338. array(
  339. 'homepage : invalid value (foo:bar), must be an http/https URL',
  340. ),
  341. ),
  342. array(
  343. array(
  344. 'name' => 'foo/bar.json',
  345. ),
  346. array(
  347. '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.',
  348. ),
  349. ),
  350. array(
  351. array(
  352. 'name' => 'com1/foo',
  353. ),
  354. array(
  355. '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.',
  356. ),
  357. ),
  358. array(
  359. array(
  360. 'name' => 'Foo/Bar',
  361. ),
  362. array(
  363. '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.',
  364. ),
  365. ),
  366. array(
  367. array(
  368. 'name' => 'foo/bar',
  369. 'support' => array(
  370. 'source' => 'foo:bar',
  371. 'forum' => 'foo:bar',
  372. 'issues' => 'foo:bar',
  373. 'wiki' => 'foo:bar',
  374. 'chat' => 'foo:bar',
  375. ),
  376. ),
  377. array(
  378. 'support.source : invalid value (foo:bar), must be an http/https URL',
  379. 'support.forum : invalid value (foo:bar), must be an http/https URL',
  380. 'support.issues : invalid value (foo:bar), must be an http/https URL',
  381. 'support.wiki : invalid value (foo:bar), must be an http/https URL',
  382. 'support.chat : invalid value (foo:bar), must be an http/https URL',
  383. ),
  384. ),
  385. array(
  386. array(
  387. 'name' => 'foo/bar',
  388. 'require' => array(
  389. 'foo/baz' => '*',
  390. 'bar/baz' => '>=1.0',
  391. 'bar/foo' => 'dev-master',
  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/foo : unbound version constraints (dev-master) should be avoided',
  400. 'require.bar/hacked : unbound version constraints (@stable) should be avoided',
  401. 'require.bar/woo : exact version constraints (1.0.0) should be avoided if the package follows semantic versioning',
  402. ),
  403. false,
  404. ),
  405. array(
  406. array(
  407. 'name' => 'foo/bar',
  408. 'require' => array(
  409. 'Foo/Baz' => '^1.0',
  410. ),
  411. ),
  412. array(
  413. '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.',
  414. ),
  415. false,
  416. ),
  417. array(
  418. array(
  419. 'name' => 'foo/bar',
  420. 'require' => array(
  421. 'bar/unstable' => '0.3.0',
  422. ),
  423. ),
  424. array(
  425. // using an exact version constraint for an unstable version should not trigger a warning
  426. ),
  427. false,
  428. ),
  429. array(
  430. array(
  431. 'name' => 'foo/bar',
  432. 'extra' => array(
  433. 'branch-alias' => array(
  434. '5.x-dev' => '3.1.x-dev',
  435. ),
  436. ),
  437. ),
  438. array(
  439. 'extra.branch-alias.5.x-dev : the target branch (3.1.x-dev) is not a valid numeric alias for this version',
  440. ),
  441. false,
  442. ),
  443. array(
  444. array(
  445. 'name' => 'foo/bar',
  446. 'extra' => array(
  447. 'branch-alias' => array(
  448. '5.x-dev' => '3.1-dev',
  449. ),
  450. ),
  451. ),
  452. array(
  453. 'extra.branch-alias.5.x-dev : the target branch (3.1-dev) is not a valid numeric alias for this version',
  454. ),
  455. false,
  456. ),
  457. ));
  458. }
  459. }