ArrayDumperTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 PHPUnit\Framework\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 testMetapackageShouldNotHaveSourceEntries()
  88. {
  89. $this
  90. ->packageExpects('getPrettyName', 'foo')
  91. ->packageExpects('getPrettyVersion', '1.0')
  92. ->packageExpects('getVersion', '1.0.0.0')
  93. ->packageExpects('getType', 'metapackage')
  94. ->packageExpects('getSourceType', 'composer')
  95. ->packageExpects('getSourceUrl', 'https://packagist.org')
  96. ->packageExpects('getSourceReference', 'packagist')
  97. ;
  98. $config = $this->dumper->dump($this->package);
  99. $this->assertEquals(
  100. array(
  101. 'name' => 'foo',
  102. 'version' => '1.0',
  103. 'version_normalized' => '1.0.0.0',
  104. 'type' => 'metapackage',
  105. ),
  106. $config
  107. );
  108. }
  109. public function testMetapackageShouldNotHaveDistEntries()
  110. {
  111. $this
  112. ->packageExpects('getPrettyName', 'foo')
  113. ->packageExpects('getPrettyVersion', '1.0')
  114. ->packageExpects('getVersion', '1.0.0.0')
  115. ->packageExpects('getType', 'metapackage')
  116. ->packageExpects('getDistType', 'composer')
  117. ->packageExpects('getDistUrl', 'https://packagist.org')
  118. ->packageExpects('getDistReference', 'packagist')
  119. ->packageExpects('getDistSha1Checksum', 'packagist')
  120. ;
  121. $config = $this->dumper->dump($this->package);
  122. $this->assertEquals(
  123. array(
  124. 'name' => 'foo',
  125. 'version' => '1.0',
  126. 'version_normalized' => '1.0.0.0',
  127. 'type' => 'metapackage',
  128. ),
  129. $config
  130. );
  131. }
  132. public function getKeys()
  133. {
  134. return array(
  135. array(
  136. 'type',
  137. 'library',
  138. ),
  139. array(
  140. 'time',
  141. $datetime = new \DateTime('2012-02-01'),
  142. 'ReleaseDate',
  143. $datetime->format(DATE_RFC3339),
  144. ),
  145. array(
  146. 'authors',
  147. array('Nils Adermann <naderman@naderman.de>', 'Jordi Boggiano <j.boggiano@seld.be>'),
  148. ),
  149. array(
  150. 'homepage',
  151. 'https://getcomposer.org',
  152. ),
  153. array(
  154. 'description',
  155. 'Dependency Manager',
  156. ),
  157. array(
  158. 'keywords',
  159. array('package', 'dependency', 'autoload'),
  160. null,
  161. array('autoload', 'dependency', 'package'),
  162. ),
  163. array(
  164. 'bin',
  165. array('bin/composer'),
  166. 'binaries',
  167. ),
  168. array(
  169. 'license',
  170. array('MIT'),
  171. ),
  172. array(
  173. 'autoload',
  174. array('psr-0' => array('Composer' => 'src/')),
  175. ),
  176. array(
  177. 'repositories',
  178. array('packagist' => false),
  179. ),
  180. array(
  181. 'scripts',
  182. array('post-update-cmd' => 'MyVendor\\MyClass::postUpdate'),
  183. ),
  184. array(
  185. 'extra',
  186. array('class' => 'MyVendor\\Installer'),
  187. ),
  188. array(
  189. 'archive',
  190. array('/foo/bar', 'baz', '!/foo/bar/baz'),
  191. 'archiveExcludes',
  192. array(
  193. 'exclude' => array('/foo/bar', 'baz', '!/foo/bar/baz'),
  194. ),
  195. ),
  196. array(
  197. 'require',
  198. array(new Link('foo', 'foo/bar', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0')),
  199. 'requires',
  200. array('foo/bar' => '1.0.0'),
  201. ),
  202. array(
  203. 'require-dev',
  204. array(new Link('foo', 'foo/bar', new Constraint('=', '1.0.0.0'), 'requires (for development)', '1.0.0')),
  205. 'devRequires',
  206. array('foo/bar' => '1.0.0'),
  207. ),
  208. array(
  209. 'suggest',
  210. array('foo/bar' => 'very useful package'),
  211. 'suggests',
  212. ),
  213. array(
  214. 'support',
  215. array('foo' => 'bar'),
  216. ),
  217. array(
  218. 'require',
  219. 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')),
  220. 'requires',
  221. array('bar/baz' => '1.0.0', 'foo/bar' => '1.0.0'),
  222. ),
  223. array(
  224. 'require-dev',
  225. 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')),
  226. 'devRequires',
  227. array('bar/baz' => '1.0.0', 'foo/bar' => '1.0.0'),
  228. ),
  229. array(
  230. 'suggest',
  231. array('foo/bar' => 'very useful package', 'bar/baz' => 'another useful package'),
  232. 'suggests',
  233. array('bar/baz' => 'another useful package', 'foo/bar' => 'very useful package'),
  234. ),
  235. array(
  236. 'provide',
  237. 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')),
  238. 'provides',
  239. array('bar/baz' => '1.0.0', 'foo/bar' => '1.0.0'),
  240. ),
  241. array(
  242. 'replace',
  243. 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')),
  244. 'replaces',
  245. array('bar/baz' => '1.0.0', 'foo/bar' => '1.0.0'),
  246. ),
  247. array(
  248. 'conflict',
  249. 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')),
  250. 'conflicts',
  251. array('bar/baz' => '1.0.0', 'foo/bar' => '1.0.0'),
  252. ),
  253. array(
  254. 'transport-options',
  255. array('ssl' => array('local_cert' => '/opt/certs/test.pem')),
  256. 'transportOptions',
  257. ),
  258. );
  259. }
  260. private function packageExpects($method, $value)
  261. {
  262. $this->package
  263. ->expects($this->any())
  264. ->method($method)
  265. ->will($this->returnValue($value));
  266. return $this;
  267. }
  268. }