HelperRegistryTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @since 3.1.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console;
  16. use Cake\Console\Exception\MissingHelperException;
  17. use Cake\Console\HelperRegistry;
  18. use Cake\TestSuite\TestCase;
  19. use TestApp\Command\Helper\CommandHelper;
  20. use TestApp\Shell\Helper\SimpleHelper;
  21. use TestPlugin\Shell\Helper\ExampleHelper;
  22. /**
  23. * HelperRegistryTest
  24. */
  25. class HelperRegistryTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\Console\HelperRegistry
  29. */
  30. protected $helpers;
  31. /**
  32. * setUp
  33. */
  34. public function setUp(): void
  35. {
  36. parent::setUp();
  37. static::setAppNamespace();
  38. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->helpers = new HelperRegistry();
  42. $this->helpers->setIo($io);
  43. }
  44. /**
  45. * tearDown
  46. */
  47. public function tearDown(): void
  48. {
  49. unset($this->helpers);
  50. parent::tearDown();
  51. }
  52. /**
  53. * test loading helpers.
  54. */
  55. public function testLoad(): void
  56. {
  57. $result = $this->helpers->load('Simple');
  58. $this->assertInstanceOf(SimpleHelper::class, $result);
  59. $this->assertInstanceOf(SimpleHelper::class, $this->helpers->Simple);
  60. $result = $this->helpers->loaded();
  61. $this->assertEquals(['Simple'], $result, 'loaded() results are wrong.');
  62. }
  63. /**
  64. * test loading helpers.
  65. */
  66. public function testLoadCommandNamespace(): void
  67. {
  68. $result = $this->helpers->load('Command');
  69. $this->assertInstanceOf(CommandHelper::class, $result);
  70. $this->assertInstanceOf(CommandHelper::class, $this->helpers->Command);
  71. $result = $this->helpers->loaded();
  72. $this->assertEquals(['Command'], $result, 'loaded() results are wrong.');
  73. }
  74. /**
  75. * test triggering callbacks on loaded helpers
  76. */
  77. public function testLoadWithConfig(): void
  78. {
  79. $result = $this->helpers->load('Simple', ['key' => 'value']);
  80. $this->assertSame('value', $result->getConfig('key'));
  81. }
  82. /**
  83. * test missing helper exception
  84. */
  85. public function testLoadMissingHelper(): void
  86. {
  87. $this->expectException(MissingHelperException::class);
  88. $this->helpers->load('ThisTaskShouldAlwaysBeMissing');
  89. }
  90. /**
  91. * Tests loading as an alias
  92. */
  93. public function testLoadWithAlias(): void
  94. {
  95. $this->loadPlugins(['TestPlugin']);
  96. $result = $this->helpers->load('SimpleAliased', ['className' => 'Simple']);
  97. $this->assertInstanceOf(SimpleHelper::class, $result);
  98. $this->assertInstanceOf(SimpleHelper::class, $this->helpers->SimpleAliased);
  99. $result = $this->helpers->loaded();
  100. $this->assertEquals(['SimpleAliased'], $result, 'loaded() results are wrong.');
  101. $result = $this->helpers->load('SomeHelper', ['className' => 'TestPlugin.Example']);
  102. $this->assertInstanceOf(ExampleHelper::class, $result);
  103. $this->assertInstanceOf(ExampleHelper::class, $this->helpers->SomeHelper);
  104. $result = $this->helpers->loaded();
  105. $this->assertEquals(['SimpleAliased', 'SomeHelper'], $result, 'loaded() results are wrong.');
  106. $this->clearPlugins();
  107. }
  108. }