HelperRegistryTest.php 3.2 KB

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