SuggestedPackagesReporterTest.php 7.7 KB

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