HelperRegistryTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\ConsoleIo;
  17. use Cake\Console\Exception\MissingHelperException;
  18. use Cake\Console\HelperRegistry;
  19. use Cake\Console\TestSuite\StubConsoleInput;
  20. use Cake\Console\TestSuite\StubConsoleOutput;
  21. use Cake\TestSuite\TestCase;
  22. use TestApp\Command\Helper\CommandHelper;
  23. use TestApp\Command\Helper\SimpleHelper;
  24. use TestPlugin\Command\Helper\ExampleHelper;
  25. /**
  26. * HelperRegistryTest
  27. */
  28. class HelperRegistryTest extends TestCase
  29. {
  30. /**
  31. * @var \Cake\Console\HelperRegistry
  32. */
  33. protected HelperRegistry $helpers;
  34. /**
  35. * setUp
  36. */
  37. public function setUp(): void
  38. {
  39. parent::setUp();
  40. static::setAppNamespace();
  41. $io = new ConsoleIo(
  42. new StubConsoleOutput(),
  43. new StubConsoleOutput(),
  44. new StubConsoleInput([]),
  45. );
  46. $this->helpers = new HelperRegistry();
  47. $this->helpers->setIo($io);
  48. }
  49. /**
  50. * tearDown
  51. */
  52. public function tearDown(): void
  53. {
  54. unset($this->helpers);
  55. parent::tearDown();
  56. }
  57. /**
  58. * test loading helpers.
  59. */
  60. public function testLoad(): void
  61. {
  62. $result = $this->helpers->load('Simple');
  63. $this->assertInstanceOf(SimpleHelper::class, $result);
  64. $this->assertInstanceOf(SimpleHelper::class, $this->helpers->Simple);
  65. $result = $this->helpers->loaded();
  66. $this->assertEquals(['Simple'], $result, 'loaded() results are wrong.');
  67. }
  68. /**
  69. * test loading helpers.
  70. */
  71. public function testLoadCommandNamespace(): void
  72. {
  73. $result = $this->helpers->load('Command');
  74. $this->assertInstanceOf(CommandHelper::class, $result);
  75. $this->assertInstanceOf(CommandHelper::class, $this->helpers->Command);
  76. $result = $this->helpers->loaded();
  77. $this->assertEquals(['Command'], $result, 'loaded() results are wrong.');
  78. }
  79. /**
  80. * test triggering callbacks on loaded helpers
  81. */
  82. public function testLoadWithConfig(): void
  83. {
  84. $result = $this->helpers->load('Simple', ['key' => 'value']);
  85. $this->assertSame('value', $result->getConfig('key'));
  86. }
  87. /**
  88. * test missing helper exception
  89. */
  90. public function testLoadMissingHelper(): void
  91. {
  92. $this->expectException(MissingHelperException::class);
  93. $this->helpers->load('ThisTaskShouldAlwaysBeMissing');
  94. }
  95. /**
  96. * Tests loading as an alias
  97. */
  98. public function testLoadWithAlias(): void
  99. {
  100. $this->loadPlugins(['TestPlugin']);
  101. $result = $this->helpers->load('SimpleAliased', ['className' => 'Simple']);
  102. $this->assertInstanceOf(SimpleHelper::class, $result);
  103. $this->assertInstanceOf(SimpleHelper::class, $this->helpers->SimpleAliased);
  104. $result = $this->helpers->loaded();
  105. $this->assertEquals(['SimpleAliased'], $result, 'loaded() results are wrong.');
  106. $result = $this->helpers->load('SomeHelper', ['className' => 'TestPlugin.Example']);
  107. $this->assertInstanceOf(ExampleHelper::class, $result);
  108. $this->assertInstanceOf(ExampleHelper::class, $this->helpers->SomeHelper);
  109. $result = $this->helpers->loaded();
  110. $this->assertEquals(['SimpleAliased', 'SomeHelper'], $result, 'loaded() results are wrong.');
  111. $this->clearPlugins();
  112. }
  113. }