ValidatingArrayLoaderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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->getMock('Composer\Package\Loader\LoaderInterface');
  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://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 as array
  149. array(
  150. 'name' => 'foo/bar',
  151. 'license' => array('MIT', 'WTFPL'),
  152. ),
  153. ),
  154. );
  155. }
  156. /**
  157. * @dataProvider errorProvider
  158. */
  159. public function testLoadFailureThrowsException($config, $expectedErrors)
  160. {
  161. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  162. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  163. try {
  164. $loader->load($config);
  165. $this->fail('Expected exception to be thrown');
  166. } catch (InvalidPackageException $e) {
  167. $errors = $e->getErrors();
  168. sort($expectedErrors);
  169. sort($errors);
  170. $this->assertEquals($expectedErrors, $errors);
  171. }
  172. }
  173. /**
  174. * @dataProvider warningProvider
  175. */
  176. public function testLoadWarnings($config, $expectedWarnings)
  177. {
  178. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  179. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  180. $loader->load($config);
  181. $warnings = $loader->getWarnings();
  182. sort($expectedWarnings);
  183. sort($warnings);
  184. $this->assertEquals($expectedWarnings, $warnings);
  185. }
  186. /**
  187. * @dataProvider warningProvider
  188. */
  189. public function testLoadSkipsWarningDataWhenIgnoringErrors($config, $expectedWarnings, $mustCheck = true)
  190. {
  191. if (!$mustCheck) {
  192. $this->assertTrue(true);
  193. return;
  194. }
  195. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  196. $internalLoader
  197. ->expects($this->once())
  198. ->method('load')
  199. ->with(array('name' => 'a/b'));
  200. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  201. $config['name'] = 'a/b';
  202. $loader->load($config);
  203. }
  204. public function errorProvider()
  205. {
  206. return array(
  207. array(
  208. array(
  209. 'name' => 'foo',
  210. ),
  211. array(
  212. 'name : invalid value (foo), must match [A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*',
  213. ),
  214. ),
  215. array(
  216. array(
  217. 'name' => 'foo/bar',
  218. 'homepage' => 43,
  219. ),
  220. array(
  221. 'homepage : should be a string, integer given',
  222. ),
  223. ),
  224. array(
  225. array(
  226. 'name' => 'foo/bar',
  227. 'support' => array(
  228. 'source' => array(),
  229. ),
  230. ),
  231. array(
  232. 'support.source : invalid value, must be a string',
  233. ),
  234. ),
  235. array(
  236. array(
  237. 'name' => 'foo/bar',
  238. 'autoload' => 'strings',
  239. ),
  240. array(
  241. 'autoload : should be an array, string given',
  242. ),
  243. ),
  244. array(
  245. array(
  246. 'name' => 'foo/bar',
  247. 'autoload' => array(
  248. 'psr0' => array(
  249. 'foo' => 'src',
  250. ),
  251. ),
  252. ),
  253. array(
  254. 'autoload : invalid value (psr0), must be one of psr-0, psr-4, classmap, files, exclude-from-classmap',
  255. ),
  256. ),
  257. array(
  258. array(
  259. 'name' => 'foo/bar',
  260. 'transport-options' => 'test',
  261. ),
  262. array(
  263. 'transport-options : should be an array, string given',
  264. ),
  265. ),
  266. );
  267. }
  268. public function warningProvider()
  269. {
  270. return array(
  271. array(
  272. array(
  273. 'name' => 'foo/bar',
  274. 'homepage' => 'foo:bar',
  275. ),
  276. array(
  277. 'homepage : invalid value (foo:bar), must be an http/https URL',
  278. ),
  279. ),
  280. array(
  281. array(
  282. 'name' => 'foo/bar',
  283. 'support' => array(
  284. 'source' => 'foo:bar',
  285. 'forum' => 'foo:bar',
  286. 'issues' => 'foo:bar',
  287. 'wiki' => 'foo:bar',
  288. ),
  289. ),
  290. array(
  291. 'support.source : invalid value (foo:bar), must be an http/https URL',
  292. 'support.forum : invalid value (foo:bar), must be an http/https URL',
  293. 'support.issues : invalid value (foo:bar), must be an http/https URL',
  294. 'support.wiki : invalid value (foo:bar), must be an http/https URL',
  295. ),
  296. ),
  297. array(
  298. array(
  299. 'name' => 'foo/bar',
  300. 'require' => array(
  301. 'foo/baz' => '*',
  302. 'bar/baz' => '>=1.0',
  303. 'bar/foo' => 'dev-master',
  304. 'bar/hacked' => '@stable',
  305. 'bar/woo' => '1.0.0',
  306. ),
  307. ),
  308. array(
  309. 'require.foo/baz : unbound version constraints (*) should be avoided',
  310. 'require.bar/baz : unbound version constraints (>=1.0) should be avoided',
  311. 'require.bar/foo : unbound version constraints (dev-master) should be avoided',
  312. 'require.bar/hacked : unbound version constraints (@stable) should be avoided',
  313. 'require.bar/woo : exact version constraints (1.0.0) should be avoided if the package follows semantic versioning',
  314. ),
  315. false,
  316. ),
  317. array(
  318. array(
  319. 'name' => 'foo/bar',
  320. 'require' => array(
  321. 'bar/unstable' => '0.3.0',
  322. ),
  323. ),
  324. array(
  325. // using an exact version constraint for an unstable version should not trigger a warning
  326. ),
  327. false,
  328. ),
  329. array(
  330. array(
  331. 'name' => 'foo/bar',
  332. 'extra' => array(
  333. 'branch-alias' => array(
  334. '5.x-dev' => '3.1.x-dev',
  335. ),
  336. ),
  337. ),
  338. array(
  339. 'extra.branch-alias.5.x-dev : the target branch (3.1.x-dev) is not a valid numeric alias for this version',
  340. ),
  341. false,
  342. ),
  343. array(
  344. array(
  345. 'name' => 'foo/bar',
  346. 'extra' => array(
  347. 'branch-alias' => array(
  348. '5.x-dev' => '3.1-dev',
  349. ),
  350. ),
  351. ),
  352. array(
  353. 'extra.branch-alias.5.x-dev : the target branch (3.1-dev) is not a valid numeric alias for this version',
  354. ),
  355. false,
  356. ),
  357. );
  358. }
  359. }