HelperSetTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\HelperSet;
  14. class HelperSetTest extends TestCase
  15. {
  16. public function testConstructor()
  17. {
  18. $mock_helper = $this->getGenericMockHelper('fake_helper');
  19. $helperset = new HelperSet(['fake_helper_alias' => $mock_helper]);
  20. $this->assertEquals($mock_helper, $helperset->get('fake_helper_alias'), '__construct sets given helper to helpers');
  21. $this->assertTrue($helperset->has('fake_helper_alias'), '__construct sets helper alias for given helper');
  22. }
  23. public function testSet()
  24. {
  25. $helperset = new HelperSet();
  26. $helperset->set($this->getGenericMockHelper('fake_helper', $helperset));
  27. $this->assertTrue($helperset->has('fake_helper'), '->set() adds helper to helpers');
  28. $helperset = new HelperSet();
  29. $helperset->set($this->getGenericMockHelper('fake_helper_01', $helperset));
  30. $helperset->set($this->getGenericMockHelper('fake_helper_02', $helperset));
  31. $this->assertTrue($helperset->has('fake_helper_01'), '->set() will set multiple helpers on consecutive calls');
  32. $this->assertTrue($helperset->has('fake_helper_02'), '->set() will set multiple helpers on consecutive calls');
  33. $helperset = new HelperSet();
  34. $helperset->set($this->getGenericMockHelper('fake_helper', $helperset), 'fake_helper_alias');
  35. $this->assertTrue($helperset->has('fake_helper'), '->set() adds helper alias when set');
  36. $this->assertTrue($helperset->has('fake_helper_alias'), '->set() adds helper alias when set');
  37. }
  38. public function testHas()
  39. {
  40. $helperset = new HelperSet(['fake_helper_alias' => $this->getGenericMockHelper('fake_helper')]);
  41. $this->assertTrue($helperset->has('fake_helper'), '->has() finds set helper');
  42. $this->assertTrue($helperset->has('fake_helper_alias'), '->has() finds set helper by alias');
  43. }
  44. public function testGet()
  45. {
  46. $helper_01 = $this->getGenericMockHelper('fake_helper_01');
  47. $helper_02 = $this->getGenericMockHelper('fake_helper_02');
  48. $helperset = new HelperSet(['fake_helper_01_alias' => $helper_01, 'fake_helper_02_alias' => $helper_02]);
  49. $this->assertEquals($helper_01, $helperset->get('fake_helper_01'), '->get() returns correct helper by name');
  50. $this->assertEquals($helper_01, $helperset->get('fake_helper_01_alias'), '->get() returns correct helper by alias');
  51. $this->assertEquals($helper_02, $helperset->get('fake_helper_02'), '->get() returns correct helper by name');
  52. $this->assertEquals($helper_02, $helperset->get('fake_helper_02_alias'), '->get() returns correct helper by alias');
  53. $helperset = new HelperSet();
  54. try {
  55. $helperset->get('foo');
  56. $this->fail('->get() throws InvalidArgumentException when helper not found');
  57. } catch (\Exception $e) {
  58. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws InvalidArgumentException when helper not found');
  59. $this->assertInstanceOf('Symfony\Component\Console\Exception\ExceptionInterface', $e, '->get() throws domain specific exception when helper not found');
  60. $this->assertContains('The helper "foo" is not defined.', $e->getMessage(), '->get() throws InvalidArgumentException when helper not found');
  61. }
  62. }
  63. public function testSetCommand()
  64. {
  65. $cmd_01 = new Command('foo');
  66. $cmd_02 = new Command('bar');
  67. $helperset = new HelperSet();
  68. $helperset->setCommand($cmd_01);
  69. $this->assertEquals($cmd_01, $helperset->getCommand(), '->setCommand() stores given command');
  70. $helperset = new HelperSet();
  71. $helperset->setCommand($cmd_01);
  72. $helperset->setCommand($cmd_02);
  73. $this->assertEquals($cmd_02, $helperset->getCommand(), '->setCommand() overwrites stored command with consecutive calls');
  74. }
  75. public function testGetCommand()
  76. {
  77. $cmd = new Command('foo');
  78. $helperset = new HelperSet();
  79. $helperset->setCommand($cmd);
  80. $this->assertEquals($cmd, $helperset->getCommand(), '->getCommand() retrieves stored command');
  81. }
  82. public function testIteration()
  83. {
  84. $helperset = new HelperSet();
  85. $helperset->set($this->getGenericMockHelper('fake_helper_01', $helperset));
  86. $helperset->set($this->getGenericMockHelper('fake_helper_02', $helperset));
  87. $helpers = ['fake_helper_01', 'fake_helper_02'];
  88. $i = 0;
  89. foreach ($helperset as $helper) {
  90. $this->assertEquals($helpers[$i++], $helper->getName());
  91. }
  92. }
  93. private function getGenericMockHelper($name, HelperSet $helperset = null)
  94. {
  95. $mock_helper = $this->getMockBuilder('\Symfony\Component\Console\Helper\HelperInterface')->getMock();
  96. $mock_helper->expects($this->any())
  97. ->method('getName')
  98. ->willReturn($name);
  99. if ($helperset) {
  100. $mock_helper->expects($this->any())
  101. ->method('setHelperSet')
  102. ->with($this->equalTo($helperset));
  103. }
  104. return $mock_helper;
  105. }
  106. }