ValidatingArrayLoaderTest.php 15 KB

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