ValidatingArrayLoaderTest.php 12 KB

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