ArrayDumperTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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\Dumper;
  12. use Composer\Package\Dumper\ArrayDumper;
  13. use Composer\Package\Link;
  14. use Composer\Semver\Constraint\Constraint;
  15. use Composer\Test\TestCase;
  16. class ArrayDumperTest extends TestCase
  17. {
  18. /**
  19. * @var ArrayDumper
  20. */
  21. private $dumper;
  22. /**
  23. * @var \Composer\Package\CompletePackageInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $package;
  26. public function setUp()
  27. {
  28. $this->dumper = new ArrayDumper();
  29. $this->package = $this->getMockBuilder('Composer\Package\CompletePackageInterface')->getMock();
  30. $this->packageExpects('getTransportOptions', array());
  31. }
  32. public function testRequiredInformation()
  33. {
  34. $this
  35. ->packageExpects('getPrettyName', 'foo')
  36. ->packageExpects('getPrettyVersion', '1.0')
  37. ->packageExpects('getVersion', '1.0.0.0')
  38. ;
  39. $config = $this->dumper->dump($this->package);
  40. $this->assertEquals(
  41. array(
  42. 'name' => 'foo',
  43. 'version' => '1.0',
  44. 'version_normalized' => '1.0.0.0',
  45. ),
  46. $config
  47. );
  48. }
  49. public function testRootPackage()
  50. {
  51. $this->package = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock();
  52. $this
  53. ->packageExpects('getMinimumStability', 'dev')
  54. ->packageExpects('getTransportOptions', array())
  55. ;
  56. $config = $this->dumper->dump($this->package);
  57. $this->assertSame('dev', $config['minimum-stability']);
  58. }
  59. public function testDumpAbandoned()
  60. {
  61. $this->packageExpects('isAbandoned', true);
  62. $this->packageExpects('getReplacementPackage', true);
  63. $config = $this->dumper->dump($this->package);
  64. $this->assertTrue($config['abandoned']);
  65. }
  66. public function testDumpAbandonedReplacement()
  67. {
  68. $this->packageExpects('isAbandoned', true);
  69. $this->packageExpects('getReplacementPackage', 'foo/bar');
  70. $config = $this->dumper->dump($this->package);
  71. $this->assertSame('foo/bar', $config['abandoned']);
  72. }
  73. /**
  74. * @dataProvider getKeys
  75. */
  76. public function testKeys($key, $value, $method = null, $expectedValue = null)
  77. {
  78. $this->package = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock();
  79. $this->packageExpects('get'.ucfirst($method ?: $key), $value);
  80. $this->packageExpects('isAbandoned', $value);
  81. if ($method !== 'transportOptions') {
  82. $this->packageExpects('getTransportOptions', array());
  83. }
  84. $config = $this->dumper->dump($this->package);
  85. $this->assertSame($expectedValue ?: $value, $config[$key]);
  86. }
  87. public function getKeys()
  88. {
  89. return array(
  90. array(
  91. 'type',
  92. 'library',
  93. ),
  94. array(
  95. 'time',
  96. $datetime = new \DateTime('2012-02-01'),
  97. 'ReleaseDate',
  98. $datetime->format(DATE_RFC3339),
  99. ),
  100. array(
  101. 'authors',
  102. array('Nils Adermann <naderman@naderman.de>', 'Jordi Boggiano <j.boggiano@seld.be>'),
  103. ),
  104. array(
  105. 'homepage',
  106. 'https://getcomposer.org',
  107. ),
  108. array(
  109. 'description',
  110. 'Dependency Manager',
  111. ),
  112. array(
  113. 'keywords',
  114. array('package', 'dependency', 'autoload'),
  115. null,
  116. array('autoload', 'dependency', 'package'),
  117. ),
  118. array(
  119. 'bin',
  120. array('bin/composer'),
  121. 'binaries',
  122. ),
  123. array(
  124. 'license',
  125. array('MIT'),
  126. ),
  127. array(
  128. 'autoload',
  129. array('psr-0' => array('Composer' => 'src/')),
  130. ),
  131. array(
  132. 'repositories',
  133. array('packagist' => false),
  134. ),
  135. array(
  136. 'scripts',
  137. array('post-update-cmd' => 'MyVendor\\MyClass::postUpdate'),
  138. ),
  139. array(
  140. 'extra',
  141. array('class' => 'MyVendor\\Installer'),
  142. ),
  143. array(
  144. 'archive',
  145. array('/foo/bar', 'baz', '!/foo/bar/baz'),
  146. 'archiveExcludes',
  147. array(
  148. 'exclude' => array('/foo/bar', 'baz', '!/foo/bar/baz'),
  149. ),
  150. ),
  151. array(
  152. 'require',
  153. array(new Link('foo', 'foo/bar', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0')),
  154. 'requires',
  155. array('foo/bar' => '1.0.0'),
  156. ),
  157. array(
  158. 'require-dev',
  159. array(new Link('foo', 'foo/bar', new Constraint('=', '1.0.0.0'), 'requires (for development)', '1.0.0')),
  160. 'devRequires',
  161. array('foo/bar' => '1.0.0'),
  162. ),
  163. array(
  164. 'suggest',
  165. array('foo/bar' => 'very useful package'),
  166. 'suggests',
  167. ),
  168. array(
  169. 'support',
  170. array('foo' => 'bar'),
  171. ),
  172. array(
  173. 'funding',
  174. array('type' => 'foo', 'url' => 'https://example.com'),
  175. ),
  176. array(
  177. 'require',
  178. array(new Link('foo', 'foo/bar', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0'), new Link('bar', 'bar/baz', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0')),
  179. 'requires',
  180. array('bar/baz' => '1.0.0', 'foo/bar' => '1.0.0'),
  181. ),
  182. array(
  183. 'require-dev',
  184. array(new Link('foo', 'foo/bar', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0'), new Link('bar', 'bar/baz', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0')),
  185. 'devRequires',
  186. array('bar/baz' => '1.0.0', 'foo/bar' => '1.0.0'),
  187. ),
  188. array(
  189. 'suggest',
  190. array('foo/bar' => 'very useful package', 'bar/baz' => 'another useful package'),
  191. 'suggests',
  192. array('bar/baz' => 'another useful package', 'foo/bar' => 'very useful package'),
  193. ),
  194. array(
  195. 'provide',
  196. array(new Link('foo', 'foo/bar', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0'), new Link('bar', 'bar/baz', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0')),
  197. 'provides',
  198. array('bar/baz' => '1.0.0', 'foo/bar' => '1.0.0'),
  199. ),
  200. array(
  201. 'replace',
  202. array(new Link('foo', 'foo/bar', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0'), new Link('bar', 'bar/baz', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0')),
  203. 'replaces',
  204. array('bar/baz' => '1.0.0', 'foo/bar' => '1.0.0'),
  205. ),
  206. array(
  207. 'conflict',
  208. array(new Link('foo', 'foo/bar', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0'), new Link('bar', 'bar/baz', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0')),
  209. 'conflicts',
  210. array('bar/baz' => '1.0.0', 'foo/bar' => '1.0.0'),
  211. ),
  212. array(
  213. 'transport-options',
  214. array('ssl' => array('local_cert' => '/opt/certs/test.pem')),
  215. 'transportOptions',
  216. ),
  217. );
  218. }
  219. private function packageExpects($method, $value)
  220. {
  221. $this->package
  222. ->expects($this->any())
  223. ->method($method)
  224. ->will($this->returnValue($value));
  225. return $this;
  226. }
  227. }