SuggestedPackagesReporterTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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\Installer;
  12. use Composer\Installer\SuggestedPackagesReporter;
  13. use Composer\Test\TestCase;
  14. /**
  15. * @coversDefaultClass Composer\Installer\SuggestedPackagesReporter
  16. */
  17. class SuggestedPackagesReporterTest extends TestCase
  18. {
  19. private $io;
  20. private $suggestedPackagesReporter;
  21. protected function setUp()
  22. {
  23. $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  24. $this->suggestedPackagesReporter = new SuggestedPackagesReporter($this->io);
  25. }
  26. /**
  27. * @covers ::__construct
  28. */
  29. public function testConstructor()
  30. {
  31. $this->io->expects($this->once())
  32. ->method('write');
  33. $this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
  34. $this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_LIST);
  35. }
  36. /**
  37. * @covers ::getPackages
  38. */
  39. public function testGetPackagesEmptyByDefault()
  40. {
  41. $this->assertEmpty($this->suggestedPackagesReporter->getPackages());
  42. }
  43. /**
  44. * @covers ::getPackages
  45. * @covers ::addPackage
  46. */
  47. public function testGetPackages()
  48. {
  49. $suggestedPackage = $this->getSuggestedPackageArray();
  50. $this->suggestedPackagesReporter->addPackage(
  51. $suggestedPackage['source'],
  52. $suggestedPackage['target'],
  53. $suggestedPackage['reason']
  54. );
  55. $this->assertSame(
  56. array($suggestedPackage),
  57. $this->suggestedPackagesReporter->getPackages()
  58. );
  59. }
  60. /**
  61. * Test addPackage appends packages.
  62. * Also test targets can be duplicated.
  63. *
  64. * @covers ::addPackage
  65. */
  66. public function testAddPackageAppends()
  67. {
  68. $suggestedPackageA = $this->getSuggestedPackageArray();
  69. $suggestedPackageB = $this->getSuggestedPackageArray();
  70. $suggestedPackageB['source'] = 'different source';
  71. $suggestedPackageB['reason'] = 'different reason';
  72. $this->suggestedPackagesReporter->addPackage(
  73. $suggestedPackageA['source'],
  74. $suggestedPackageA['target'],
  75. $suggestedPackageA['reason']
  76. );
  77. $this->suggestedPackagesReporter->addPackage(
  78. $suggestedPackageB['source'],
  79. $suggestedPackageB['target'],
  80. $suggestedPackageB['reason']
  81. );
  82. $this->assertSame(
  83. array($suggestedPackageA, $suggestedPackageB),
  84. $this->suggestedPackagesReporter->getPackages()
  85. );
  86. }
  87. /**
  88. * @covers ::addSuggestionsFromPackage
  89. */
  90. public function testAddSuggestionsFromPackage()
  91. {
  92. $package = $this->createPackageMock();
  93. $package->expects($this->once())
  94. ->method('getSuggests')
  95. ->will($this->returnValue(array(
  96. 'target-a' => 'reason-a',
  97. 'target-b' => 'reason-b',
  98. )));
  99. $package->expects($this->once())
  100. ->method('getPrettyName')
  101. ->will($this->returnValue('package-pretty-name'));
  102. $this->suggestedPackagesReporter->addSuggestionsFromPackage($package);
  103. $this->assertSame(array(
  104. array(
  105. 'source' => 'package-pretty-name',
  106. 'target' => 'target-a',
  107. 'reason' => 'reason-a',
  108. ),
  109. array(
  110. 'source' => 'package-pretty-name',
  111. 'target' => 'target-b',
  112. 'reason' => 'reason-b',
  113. ),
  114. ), $this->suggestedPackagesReporter->getPackages());
  115. }
  116. /**
  117. * @covers ::output
  118. */
  119. public function testOutput()
  120. {
  121. $this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
  122. $this->io->expects($this->at(0))
  123. ->method('write')
  124. ->with('<comment>a</comment> suggests:');
  125. $this->io->expects($this->at(1))
  126. ->method('write')
  127. ->with(' - <info>b</info>: c');
  128. $this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE);
  129. }
  130. /**
  131. * @covers ::output
  132. */
  133. public function testOutputWithNoSuggestionReason()
  134. {
  135. $this->suggestedPackagesReporter->addPackage('a', 'b', '');
  136. $this->io->expects($this->at(0))
  137. ->method('write')
  138. ->with('<comment>a</comment> suggests:');
  139. $this->io->expects($this->at(1))
  140. ->method('write')
  141. ->with(' - <info>b</info>');
  142. $this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE);
  143. }
  144. /**
  145. * @covers ::output
  146. */
  147. public function testOutputIgnoresFormatting()
  148. {
  149. $this->suggestedPackagesReporter->addPackage('source', 'target1', "\x1b[1;37;42m Like us\r\non Facebook \x1b[0m");
  150. $this->suggestedPackagesReporter->addPackage('source', 'target2', "<bg=green>Like us on Facebook</>");
  151. $this->io->expects($this->at(0))
  152. ->method('write')
  153. ->with('<comment>source</comment> suggests:');
  154. $this->io->expects($this->at(1))
  155. ->method('write')
  156. ->with(' - <info>target1</info>: [1;37;42m Like us on Facebook [0m');
  157. $this->io->expects($this->at(2))
  158. ->method('write')
  159. ->with(' - <info>target2</info>: \\<bg=green>Like us on Facebook\\</>');
  160. $this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE);
  161. }
  162. /**
  163. * @covers ::output
  164. */
  165. public function testOutputMultiplePackages()
  166. {
  167. $this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
  168. $this->suggestedPackagesReporter->addPackage('source package', 'target', 'because reasons');
  169. $this->io->expects($this->at(0))
  170. ->method('write')
  171. ->with('<comment>a</comment> suggests:');
  172. $this->io->expects($this->at(1))
  173. ->method('write')
  174. ->with(' - <info>b</info>: c');
  175. $this->io->expects($this->at(2))
  176. ->method('write')
  177. ->with('');
  178. $this->io->expects($this->at(3))
  179. ->method('write')
  180. ->with('<comment>source package</comment> suggests:');
  181. $this->io->expects($this->at(4))
  182. ->method('write')
  183. ->with(' - <info>target</info>: because reasons');
  184. $this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE);
  185. }
  186. /**
  187. * @covers ::output
  188. */
  189. public function testOutputSkipInstalledPackages()
  190. {
  191. $repository = $this->getMockBuilder('Composer\Repository\InstalledRepository')->disableOriginalConstructor()->getMock();
  192. $package1 = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  193. $package2 = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  194. $package1->expects($this->once())
  195. ->method('getNames')
  196. ->will($this->returnValue(array('x', 'y')));
  197. $package2->expects($this->once())
  198. ->method('getNames')
  199. ->will($this->returnValue(array('b')));
  200. $repository->expects($this->once())
  201. ->method('getPackages')
  202. ->will($this->returnValue(array(
  203. $package1,
  204. $package2,
  205. )));
  206. $this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
  207. $this->suggestedPackagesReporter->addPackage('source package', 'target', 'because reasons');
  208. $this->io->expects($this->at(0))
  209. ->method('write')
  210. ->with('<comment>source package</comment> suggests:');
  211. $this->io->expects($this->at(1))
  212. ->method('write')
  213. ->with(' - <info>target</info>: because reasons');
  214. $this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE, $repository);
  215. }
  216. /**
  217. * @covers ::output
  218. */
  219. public function testOutputNotGettingInstalledPackagesWhenNoSuggestions()
  220. {
  221. $repository = $this->getMockBuilder('Composer\Repository\InstalledRepository')->disableOriginalConstructor()->getMock();
  222. $repository->expects($this->exactly(0))
  223. ->method('getPackages');
  224. $this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE, $repository);
  225. }
  226. private function getSuggestedPackageArray()
  227. {
  228. return array(
  229. 'source' => 'a',
  230. 'target' => 'b',
  231. 'reason' => 'c',
  232. );
  233. }
  234. private function createPackageMock()
  235. {
  236. return $this->getMockBuilder('Composer\Package\Package')
  237. ->setConstructorArgs(array(md5(mt_rand()), '1.0.0.0', '1.0.0'))
  238. ->getMock();
  239. }
  240. }