ValidatingArrayLoaderTest.php 13 KB

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