HelperRegistryTest.php 3.2 KB

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