SuggestedPackagesReporterTest.php 7.3 KB

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