SuggestedPackagesReporterTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 PHPUnit\Framework\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->getMock('Composer\IO\IOInterface');
  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 testOutputIgnoresFormatting()
  132. {
  133. $this->suggestedPackagesReporter->addPackage('source', 'target1', "\x1b[1;37;42m Like us\r\non Facebook \x1b[0m");
  134. $this->suggestedPackagesReporter->addPackage('source', 'target2', "<bg=green>Like us on Facebook</>");
  135. $this->io->expects($this->at(0))
  136. ->method('writeError')
  137. ->with("source suggests installing target1 ([1;37;42m Like us on Facebook [0m)");
  138. $this->io->expects($this->at(1))
  139. ->method('writeError')
  140. ->with('source suggests installing target2 (\\<bg=green>Like us on Facebook\\</>)');
  141. $this->suggestedPackagesReporter->output();
  142. }
  143. /**
  144. * @covers ::output
  145. */
  146. public function testOutputMultiplePackages()
  147. {
  148. $this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
  149. $this->suggestedPackagesReporter->addPackage('source package', 'target', 'because reasons');
  150. $this->io->expects($this->at(0))
  151. ->method('writeError')
  152. ->with('a suggests installing b (c)');
  153. $this->io->expects($this->at(1))
  154. ->method('writeError')
  155. ->with('source package suggests installing target (because reasons)');
  156. $this->suggestedPackagesReporter->output();
  157. }
  158. /**
  159. * @covers ::output
  160. */
  161. public function testOutputSkipInstalledPackages()
  162. {
  163. $repository = $this->getMock('Composer\Repository\RepositoryInterface');
  164. $package1 = $this->getMock('Composer\Package\PackageInterface');
  165. $package2 = $this->getMock('Composer\Package\PackageInterface');
  166. $package1->expects($this->once())
  167. ->method('getNames')
  168. ->will($this->returnValue(array('x', 'y')));
  169. $package2->expects($this->once())
  170. ->method('getNames')
  171. ->will($this->returnValue(array('b')));
  172. $repository->expects($this->once())
  173. ->method('getPackages')
  174. ->will($this->returnValue(array(
  175. $package1,
  176. $package2,
  177. )));
  178. $this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
  179. $this->suggestedPackagesReporter->addPackage('source package', 'target', 'because reasons');
  180. $this->io->expects($this->once())
  181. ->method('writeError')
  182. ->with('source package suggests installing target (because reasons)');
  183. $this->suggestedPackagesReporter->output($repository);
  184. }
  185. /**
  186. * @covers ::output
  187. */
  188. public function testOutputNotGettingInstalledPackagesWhenNoSuggestions()
  189. {
  190. $repository = $this->getMock('Composer\Repository\RepositoryInterface');
  191. $repository->expects($this->exactly(0))
  192. ->method('getPackages');
  193. $this->suggestedPackagesReporter->output($repository);
  194. }
  195. private function getSuggestedPackageArray()
  196. {
  197. return array(
  198. 'source' => 'a',
  199. 'target' => 'b',
  200. 'reason' => 'c',
  201. );
  202. }
  203. private function createPackageMock()
  204. {
  205. return $this->getMockBuilder('Composer\Package\Package')
  206. ->setConstructorArgs(array(md5(mt_rand()), '1.0.0.0', '1.0.0'))
  207. ->getMock();
  208. }
  209. }