HelperRegistryTest.php 3.7 KB

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