ValidatingArrayLoaderTest.php 10 KB

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