HelperRegistryTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @since 3.1.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Console;
  15. use Cake\Console\HelperRegistry;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * HelperRegistryTest
  21. */
  22. class HelperRegistryTest extends TestCase
  23. {
  24. /**
  25. * setUp
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. Configure::write('App.namespace', 'TestApp');
  33. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->helpers = new HelperRegistry();
  37. $this->helpers->setIo($io);
  38. }
  39. /**
  40. * tearDown
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  45. {
  46. unset($this->helpers);
  47. parent::tearDown();
  48. }
  49. /**
  50. * test loading helpers.
  51. *
  52. * @return void
  53. */
  54. public function testLoad()
  55. {
  56. $result = $this->helpers->load('Simple');
  57. $this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $result);
  58. $this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $this->helpers->Simple);
  59. $result = $this->helpers->loaded();
  60. $this->assertEquals(['Simple'], $result, 'loaded() results are wrong.');
  61. }
  62. /**
  63. * test triggering callbacks on loaded helpers
  64. *
  65. * @return void
  66. */
  67. public function testLoadWithConfig()
  68. {
  69. $result = $this->helpers->load('Simple', ['key' => 'value']);
  70. $this->assertEquals('value', $result->config('key'));
  71. }
  72. /**
  73. * test missing helper exception
  74. *
  75. * @expectedException \Cake\Console\Exception\MissingHelperException
  76. * @return void
  77. */
  78. public function testLoadMissingHelper()
  79. {
  80. $this->helpers->load('ThisTaskShouldAlwaysBeMissing');
  81. }
  82. /**
  83. * Tests loading as an alias
  84. *
  85. * @return void
  86. */
  87. public function testLoadWithAlias()
  88. {
  89. Plugin::load('TestPlugin');
  90. $result = $this->helpers->load('SimpleAliased', ['className' => 'Simple']);
  91. $this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $result);
  92. $this->assertInstanceOf('TestApp\Shell\Helper\SimpleHelper', $this->helpers->SimpleAliased);
  93. $result = $this->helpers->loaded();
  94. $this->assertEquals(['SimpleAliased'], $result, 'loaded() results are wrong.');
  95. $result = $this->helpers->load('SomeHelper', ['className' => 'TestPlugin.Example']);
  96. $this->assertInstanceOf('TestPlugin\Shell\Helper\ExampleHelper', $result);
  97. $this->assertInstanceOf('TestPlugin\Shell\Helper\ExampleHelper', $this->helpers->SomeHelper);
  98. $result = $this->helpers->loaded();
  99. $this->assertEquals(['SimpleAliased', 'SomeHelper'], $result, 'loaded() results are wrong.');
  100. }
  101. }