HelperSetTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Helper;
  11. use Symfony\Component\Console\Helper\HelperSet;
  12. use Symfony\Component\Console\Command\Command;
  13. class HelperSetTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @covers \Symfony\Component\Console\Helper\HelperSet::__construct
  17. */
  18. public function testConstructor()
  19. {
  20. $mock_helper = $this->getGenericMockHelper('fake_helper');
  21. $helperset = new HelperSet(array('fake_helper_alias' => $mock_helper));
  22. $this->assertEquals($mock_helper, $helperset->get('fake_helper_alias'), '__construct sets given helper to helpers');
  23. $this->assertTrue($helperset->has('fake_helper_alias'), '__construct sets helper alias for given helper');
  24. }
  25. /**
  26. * @covers \Symfony\Component\Console\Helper\HelperSet::set
  27. */
  28. public function testSet()
  29. {
  30. $helperset = new HelperSet();
  31. $helperset->set($this->getGenericMockHelper('fake_helper', $helperset));
  32. $this->assertTrue($helperset->has('fake_helper'), '->set() adds helper to helpers');
  33. $helperset = new HelperSet();
  34. $helperset->set($this->getGenericMockHelper('fake_helper_01', $helperset));
  35. $helperset->set($this->getGenericMockHelper('fake_helper_02', $helperset));
  36. $this->assertTrue($helperset->has('fake_helper_01'), '->set() will set multiple helpers on consecutive calls');
  37. $this->assertTrue($helperset->has('fake_helper_02'), '->set() will set multiple helpers on consecutive calls');
  38. $helperset = new HelperSet();
  39. $helperset->set($this->getGenericMockHelper('fake_helper', $helperset), 'fake_helper_alias');
  40. $this->assertTrue($helperset->has('fake_helper'), '->set() adds helper alias when set');
  41. $this->assertTrue($helperset->has('fake_helper_alias'), '->set() adds helper alias when set');
  42. }
  43. /**
  44. * @covers \Symfony\Component\Console\Helper\HelperSet::has
  45. */
  46. public function testHas()
  47. {
  48. $helperset = new HelperSet(array('fake_helper_alias' => $this->getGenericMockHelper('fake_helper')));
  49. $this->assertTrue($helperset->has('fake_helper'), '->has() finds set helper');
  50. $this->assertTrue($helperset->has('fake_helper_alias'), '->has() finds set helper by alias');
  51. }
  52. /**
  53. * @covers \Symfony\Component\Console\Helper\HelperSet::get
  54. */
  55. public function testGet()
  56. {
  57. $helper_01 = $this->getGenericMockHelper('fake_helper_01');
  58. $helper_02 = $this->getGenericMockHelper('fake_helper_02');
  59. $helperset = new HelperSet(array('fake_helper_01_alias' => $helper_01, 'fake_helper_02_alias' => $helper_02));
  60. $this->assertEquals($helper_01, $helperset->get('fake_helper_01'), '->get() returns correct helper by name');
  61. $this->assertEquals($helper_01, $helperset->get('fake_helper_01_alias'), '->get() returns correct helper by alias');
  62. $this->assertEquals($helper_02, $helperset->get('fake_helper_02'), '->get() returns correct helper by name');
  63. $this->assertEquals($helper_02, $helperset->get('fake_helper_02_alias'), '->get() returns correct helper by alias');
  64. $helperset = new HelperSet();
  65. try {
  66. $helperset->get('foo');
  67. $this->fail('->get() throws \InvalidArgumentException when helper not found');
  68. } catch (\Exception $e) {
  69. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws \InvalidArgumentException when helper not found');
  70. $this->assertContains('The helper "foo" is not defined.', $e->getMessage(), '->get() throws \InvalidArgumentException when helper not found');
  71. }
  72. }
  73. /**
  74. * @covers \Symfony\Component\Console\Helper\HelperSet::setCommand
  75. */
  76. public function testSetCommand()
  77. {
  78. $cmd_01 = new Command('foo');
  79. $cmd_02 = new Command('bar');
  80. $helperset = new HelperSet();
  81. $helperset->setCommand($cmd_01);
  82. $this->assertEquals($cmd_01, $helperset->getCommand(), '->setCommand() stores given command');
  83. $helperset = new HelperSet();
  84. $helperset->setCommand($cmd_01);
  85. $helperset->setCommand($cmd_02);
  86. $this->assertEquals($cmd_02, $helperset->getCommand(), '->setCommand() overwrites stored command with consecutive calls');
  87. }
  88. /**
  89. * @covers \Symfony\Component\Console\Helper\HelperSet::getCommand
  90. */
  91. public function testGetCommand()
  92. {
  93. $cmd = new Command('foo');
  94. $helperset = new HelperSet();
  95. $helperset->setCommand($cmd);
  96. $this->assertEquals($cmd, $helperset->getCommand(), '->getCommand() retrieves stored command');
  97. }
  98. /**
  99. * @covers \Symfony\Component\Console\Helper\HelperSet::getIterator
  100. */
  101. public function testIteration()
  102. {
  103. $helperset = new HelperSet();
  104. $helperset->set($this->getGenericMockHelper('fake_helper_01', $helperset));
  105. $helperset->set($this->getGenericMockHelper('fake_helper_02', $helperset));
  106. $helpers = array('fake_helper_01', 'fake_helper_02');
  107. $i = 0;
  108. foreach ($helperset as $helper) {
  109. $this->assertEquals($helpers[$i++], $helper->getName());
  110. }
  111. }
  112. /**
  113. * Create a generic mock for the helper interface. Optionally check for a call to setHelperSet with a specific
  114. * helperset instance.
  115. *
  116. * @param string $name
  117. * @param HelperSet $helperset allows a mock to verify a particular helperset set is being added to the Helper
  118. */
  119. private function getGenericMockHelper($name, HelperSet $helperset = null)
  120. {
  121. $mock_helper = $this->getMock('\Symfony\Component\Console\Helper\HelperInterface');
  122. $mock_helper->expects($this->any())
  123. ->method('getName')
  124. ->will($this->returnValue($name));
  125. if ($helperset) {
  126. $mock_helper->expects($this->any())
  127. ->method('setHelperSet')
  128. ->with($this->equalTo($helperset));
  129. }
  130. return $mock_helper;
  131. }
  132. }