SuggestedPackagesReporterTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /**
  14. * @coversDefaultClass Composer\Installer\SuggestedPackagesReporter
  15. */
  16. class SuggestedPackagesReporterTest extends \PHPUnit_Framework_TestCase
  17. {
  18. private $io;
  19. private $suggestedPackagesReporter;
  20. protected function setUp()
  21. {
  22. $this->io = $this->getMock('Composer\IO\IOInterface');
  23. $this->suggestedPackagesReporter = new SuggestedPackagesReporter($this->io);
  24. }
  25. /**
  26. * @covers ::__construct
  27. */
  28. public function testContrsuctor()
  29. {
  30. $this->io->expects($this->once())
  31. ->method('writeError');
  32. $suggestedPackagesReporter = new SuggestedPackagesReporter($this->io);
  33. $suggestedPackagesReporter->addPackage('a', 'b', 'c');
  34. $suggestedPackagesReporter->output();
  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->once())
  123. ->method('writeError')
  124. ->with('a suggests installing b (c)');
  125. $this->suggestedPackagesReporter->output();
  126. }
  127. /**
  128. * @covers ::output
  129. */
  130. public function testOutputIgnoresFormatting()
  131. {
  132. $this->suggestedPackagesReporter->addPackage('source', 'target1', "\x1b[1;37;42m Like us\r\non Facebook \x1b[0m");
  133. $this->suggestedPackagesReporter->addPackage('source', 'target2', "<bg=green>Like us on Facebook</>");
  134. $this->io->expects($this->at(0))
  135. ->method('writeError')
  136. ->with("source suggests installing target1 ([1;37;42m Like us on Facebook [0m)");
  137. $this->io->expects($this->at(1))
  138. ->method('writeError')
  139. ->with('source suggests installing target2 (\\<bg=green>Like us on Facebook\\</>)');
  140. $this->suggestedPackagesReporter->output();
  141. }
  142. /**
  143. * @covers ::output
  144. */
  145. public function testOutputMultiplePackages()
  146. {
  147. $this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
  148. $this->suggestedPackagesReporter->addPackage('source package', 'target', 'because reasons');
  149. $this->io->expects($this->at(0))
  150. ->method('writeError')
  151. ->with('a suggests installing b (c)');
  152. $this->io->expects($this->at(1))
  153. ->method('writeError')
  154. ->with('source package suggests installing target (because reasons)');
  155. $this->suggestedPackagesReporter->output();
  156. }
  157. /**
  158. * @covers ::output
  159. */
  160. public function testOutputSkipInstalledPackages()
  161. {
  162. $repository = $this->getMock('Composer\Repository\RepositoryInterface');
  163. $package1 = $this->getMock('Composer\Package\PackageInterface');
  164. $package2 = $this->getMock('Composer\Package\PackageInterface');
  165. $package1->expects($this->once())
  166. ->method('getNames')
  167. ->will($this->returnValue(array('x', 'y')));
  168. $package2->expects($this->once())
  169. ->method('getNames')
  170. ->will($this->returnValue(array('b')));
  171. $repository->expects($this->once())
  172. ->method('getPackages')
  173. ->will($this->returnValue(array(
  174. $package1,
  175. $package2,
  176. )));
  177. $this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
  178. $this->suggestedPackagesReporter->addPackage('source package', 'target', 'because reasons');
  179. $this->io->expects($this->once())
  180. ->method('writeError')
  181. ->with('source package suggests installing target (because reasons)');
  182. $this->suggestedPackagesReporter->output($repository);
  183. }
  184. /**
  185. * @covers ::output
  186. */
  187. public function testOutputNotGettingInstalledPackagesWhenNoSuggestions()
  188. {
  189. $repository = $this->getMock('Composer\Repository\RepositoryInterface');
  190. $repository->expects($this->exactly(0))
  191. ->method('getPackages');
  192. $this->suggestedPackagesReporter->output($repository);
  193. }
  194. private function getSuggestedPackageArray()
  195. {
  196. return array(
  197. 'source' => 'a',
  198. 'target' => 'b',
  199. 'reason' => 'c',
  200. );
  201. }
  202. private function createPackageMock()
  203. {
  204. return $this->getMockBuilder('Composer\Package\Package')
  205. ->setConstructorArgs(array(md5(mt_rand()), '1.0.0.0', '1.0.0'))
  206. ->getMock();
  207. }
  208. }