ArrayDumperTest.php 7.9 KB

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