ValidatingArrayLoaderTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. class ValidatingArrayLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @dataProvider successProvider
  18. */
  19. public function testLoadSuccess($config)
  20. {
  21. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  22. $internalLoader
  23. ->expects($this->once())
  24. ->method('load')
  25. ->with($config);
  26. $loader = new ValidatingArrayLoader($internalLoader, false);
  27. $loader->load($config);
  28. }
  29. public function successProvider()
  30. {
  31. return array(
  32. array( // minimal
  33. array(
  34. 'name' => 'foo/bar',
  35. ),
  36. ),
  37. array( // complete
  38. array(
  39. 'name' => 'foo/bar',
  40. 'description' => 'Foo bar',
  41. 'version' => '1.0.0',
  42. 'type' => 'library',
  43. 'keywords' => array('a', 'b'),
  44. 'homepage' => 'https://foo.com',
  45. 'time' => '2010-10-10T10:10:10+00:00',
  46. 'license' => 'MIT',
  47. 'authors' => array(
  48. array(
  49. 'name' => 'Alice',
  50. 'email' => 'alice@example.org',
  51. 'role' => 'Lead',
  52. 'homepage' => 'http://example.org',
  53. ),
  54. array(
  55. 'name' => 'Bob',
  56. 'homepage' => 'http://example.com',
  57. ),
  58. ),
  59. 'support' => array(
  60. 'email' => 'mail@example.org',
  61. 'issues' => 'http://example.org/',
  62. 'forum' => 'http://example.org/',
  63. 'wiki' => 'http://example.org/',
  64. 'source' => 'http://example.org/',
  65. 'irc' => 'irc://example.org/example',
  66. ),
  67. 'require' => array(
  68. 'a/b' => '1.*',
  69. 'example' => '>2.0-dev,<2.4-dev',
  70. ),
  71. 'require-dev' => array(
  72. 'a/b' => '1.*',
  73. 'example' => '>2.0-dev,<2.4-dev',
  74. ),
  75. 'conflict' => array(
  76. 'a/b' => '1.*',
  77. 'example' => '>2.0-dev,<2.4-dev',
  78. ),
  79. 'replace' => array(
  80. 'a/b' => '1.*',
  81. 'example' => '>2.0-dev,<2.4-dev',
  82. ),
  83. 'provide' => array(
  84. 'a/b' => '1.*',
  85. 'example' => '>2.0-dev,<2.4-dev',
  86. ),
  87. 'suggest' => array(
  88. 'foo/bar' => 'Foo bar is very useful',
  89. ),
  90. 'autoload' => array(
  91. 'psr-0' => array(
  92. 'Foo\\Bar' => 'src/',
  93. '' => 'fallback/libs/',
  94. ),
  95. 'classmap' => array(
  96. 'dir/',
  97. 'dir2/file.php',
  98. ),
  99. 'files' => array(
  100. 'functions.php',
  101. ),
  102. ),
  103. 'include-path' => array(
  104. 'lib/',
  105. ),
  106. 'target-dir' => 'Foo/Bar',
  107. 'minimum-stability' => 'dev',
  108. 'repositories' => array(
  109. array(
  110. 'type' => 'composer',
  111. 'url' => 'http://packagist.org/',
  112. )
  113. ),
  114. 'config' => array(
  115. 'bin-dir' => 'bin',
  116. 'vendor-dir' => 'vendor',
  117. 'process-timeout' => 10000,
  118. ),
  119. 'scripts' => array(
  120. 'post-update-cmd' => 'Foo\\Bar\\Baz::doSomething',
  121. 'post-install-cmd' => array(
  122. 'Foo\\Bar\\Baz::doSomething',
  123. ),
  124. ),
  125. 'extra' => array(
  126. 'random' => array('stuff' => array('deeply' => 'nested')),
  127. ),
  128. 'bin' => array(
  129. 'bin/foo',
  130. 'bin/bar',
  131. ),
  132. ),
  133. ),
  134. array( // test as array
  135. array(
  136. 'name' => 'foo/bar',
  137. 'license' => array('MIT', 'WTFPL'),
  138. ),
  139. ),
  140. );
  141. }
  142. /**
  143. * @dataProvider failureProvider
  144. */
  145. public function testLoadFailureThrowsException($config, $expectedErrors)
  146. {
  147. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  148. $loader = new ValidatingArrayLoader($internalLoader, false);
  149. try {
  150. $loader->load($config);
  151. $this->fail('Expected exception to be thrown');
  152. } catch (\Exception $e) {
  153. $errors = explode("\n", $e->getMessage());
  154. sort($expectedErrors);
  155. sort($errors);
  156. $this->assertEquals($expectedErrors, $errors);
  157. }
  158. }
  159. /**
  160. * @dataProvider failureProvider
  161. */
  162. public function testLoadSkipsInvalidDataWhenIgnoringErrors($config)
  163. {
  164. $internalLoader = $this->getMock('Composer\Package\Loader\LoaderInterface');
  165. $internalLoader
  166. ->expects($this->once())
  167. ->method('load')
  168. ->with(array('name' => 'a/b'));
  169. $loader = new ValidatingArrayLoader($internalLoader, true);
  170. $config['name'] = 'a/b';
  171. $loader->load($config);
  172. }
  173. public function failureProvider()
  174. {
  175. return array(
  176. array(
  177. array(
  178. 'name' => 'foo',
  179. ),
  180. array(
  181. 'name : invalid value, must match [A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*'
  182. )
  183. ),
  184. array(
  185. array(
  186. 'name' => 'foo/bar',
  187. 'homepage' => 'foo:bar',
  188. ),
  189. array(
  190. 'homepage : invalid value, must be a valid http/https URL'
  191. )
  192. ),
  193. array(
  194. array(
  195. 'name' => 'foo/bar',
  196. 'support' => array(
  197. 'source' => 'foo:bar',
  198. 'forum' => 'foo:bar',
  199. 'issues' => 'foo:bar',
  200. 'wiki' => 'foo:bar',
  201. ),
  202. ),
  203. array(
  204. 'support.source : invalid value, must be a valid http/https URL',
  205. 'support.forum : invalid value, must be a valid http/https URL',
  206. 'support.issues : invalid value, must be a valid http/https URL',
  207. 'support.wiki : invalid value, must be a valid http/https URL',
  208. )
  209. ),
  210. );
  211. }
  212. }