ValidatingArrayLoaderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. ),
  138. ),
  139. 'bin' => array(
  140. 'bin/foo',
  141. 'bin/bar',
  142. ),
  143. 'transport-options' => array('ssl' => array('local_cert' => '/opt/certs/test.pem'))
  144. ),
  145. ),
  146. array( // test as array
  147. array(
  148. 'name' => 'foo/bar',
  149. 'license' => array('MIT', 'WTFPL'),
  150. ),
  151. ),
  152. );
  153. }
  154. /**
  155. * @dataProvider errorProvider
  156. */
  157. public function testLoadFailureThrowsException($config, $expectedErrors)
  158. {
  159. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  160. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  161. try {
  162. $loader->load($config);
  163. $this->fail('Expected exception to be thrown');
  164. } catch (InvalidPackageException $e) {
  165. $errors = $e->getErrors();
  166. sort($expectedErrors);
  167. sort($errors);
  168. $this->assertEquals($expectedErrors, $errors);
  169. }
  170. }
  171. /**
  172. * @dataProvider warningProvider
  173. */
  174. public function testLoadWarnings($config, $expectedWarnings)
  175. {
  176. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  177. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  178. $loader->load($config);
  179. $warnings = $loader->getWarnings();
  180. sort($expectedWarnings);
  181. sort($warnings);
  182. $this->assertEquals($expectedWarnings, $warnings);
  183. }
  184. /**
  185. * @dataProvider warningProvider
  186. */
  187. public function testLoadSkipsWarningDataWhenIgnoringErrors($config, $expectedWarnings, $mustCheck = true)
  188. {
  189. if (!$mustCheck) {
  190. $this->assertTrue(true);
  191. return;
  192. }
  193. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  194. $internalLoader
  195. ->expects($this->once())
  196. ->method('load')
  197. ->with(array('name' => 'a/b'));
  198. $loader = new ValidatingArrayLoader($internalLoader, true, null, ValidatingArrayLoader::CHECK_ALL);
  199. $config['name'] = 'a/b';
  200. $loader->load($config);
  201. }
  202. public function errorProvider()
  203. {
  204. return array(
  205. array(
  206. array(
  207. 'name' => 'foo',
  208. ),
  209. array(
  210. 'name : invalid value (foo), must match [A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*'
  211. )
  212. ),
  213. array(
  214. array(
  215. 'name' => 'foo/bar',
  216. 'homepage' => 43,
  217. ),
  218. array(
  219. 'homepage : should be a string, integer given',
  220. )
  221. ),
  222. array(
  223. array(
  224. 'name' => 'foo/bar',
  225. 'support' => array(
  226. 'source' => array(),
  227. ),
  228. ),
  229. array(
  230. 'support.source : invalid value, must be a string',
  231. )
  232. ),
  233. array(
  234. array(
  235. 'name' => 'foo/bar',
  236. 'autoload' => 'strings',
  237. ),
  238. array(
  239. 'autoload : should be an array, string given'
  240. )
  241. ),
  242. array(
  243. array(
  244. 'name' => 'foo/bar',
  245. 'autoload' => array(
  246. 'psr0' => array(
  247. 'foo' => 'src',
  248. ),
  249. ),
  250. ),
  251. array(
  252. 'autoload : invalid value (psr0), must be one of psr-0, psr-4, classmap, files'
  253. )
  254. ),
  255. array(
  256. array(
  257. 'name' => 'foo/bar',
  258. 'transport-options' => 'test',
  259. ),
  260. array(
  261. 'transport-options : should be an array, string given'
  262. )
  263. ),
  264. );
  265. }
  266. public function warningProvider()
  267. {
  268. return array(
  269. array(
  270. array(
  271. 'name' => 'foo/bar',
  272. 'homepage' => 'foo:bar',
  273. ),
  274. array(
  275. 'homepage : invalid value (foo:bar), must be an http/https URL'
  276. )
  277. ),
  278. array(
  279. array(
  280. 'name' => 'foo/bar',
  281. 'support' => array(
  282. 'source' => 'foo:bar',
  283. 'forum' => 'foo:bar',
  284. 'issues' => 'foo:bar',
  285. 'wiki' => 'foo:bar',
  286. ),
  287. ),
  288. array(
  289. 'support.source : invalid value (foo:bar), must be an http/https URL',
  290. 'support.forum : invalid value (foo:bar), must be an http/https URL',
  291. 'support.issues : invalid value (foo:bar), must be an http/https URL',
  292. 'support.wiki : invalid value (foo:bar), must be an http/https URL',
  293. )
  294. ),
  295. array(
  296. array(
  297. 'name' => 'foo/bar',
  298. 'require' => array(
  299. 'foo/baz' => '*',
  300. 'bar/baz' => '>=1.0',
  301. 'bar/foo' => 'dev-master',
  302. 'bar/hacked' => '@stable',
  303. ),
  304. ),
  305. array(
  306. 'require.foo/baz : unbound version constraints (*) should be avoided',
  307. 'require.bar/baz : unbound version constraints (>=1.0) should be avoided',
  308. 'require.bar/foo : unbound version constraints (dev-master) should be avoided',
  309. 'require.bar/hacked : unbound version constraints (@stable) should be avoided',
  310. ),
  311. false
  312. ),
  313. );
  314. }
  315. }