TaskRegistryTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console;
  16. use Cake\Console\TaskRegistry;
  17. use Cake\Core\App;
  18. use Cake\Core\Plugin;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Class TaskRegistryTest
  22. *
  23. */
  24. class TaskRegistryTest extends TestCase
  25. {
  26. /**
  27. * setUp
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. $shell = $this->getMock('Cake\Console\Shell', [], [], '', false);
  35. $this->Tasks = new TaskRegistry($shell);
  36. }
  37. /**
  38. * tearDown
  39. *
  40. * @return void
  41. */
  42. public function tearDown()
  43. {
  44. unset($this->Tasks);
  45. parent::tearDown();
  46. }
  47. /**
  48. * test triggering callbacks on loaded tasks
  49. *
  50. * @return void
  51. */
  52. public function testLoad()
  53. {
  54. $result = $this->Tasks->load('Command');
  55. $this->assertInstanceOf('Cake\Shell\Task\CommandTask', $result);
  56. $this->assertInstanceOf('Cake\Shell\Task\CommandTask', $this->Tasks->Command);
  57. $result = $this->Tasks->loaded();
  58. $this->assertEquals(['Command'], $result, 'loaded() results are wrong.');
  59. }
  60. /**
  61. * test missingtask exception
  62. *
  63. * @expectedException \Cake\Console\Exception\MissingTaskException
  64. * @return void
  65. */
  66. public function testLoadMissingTask()
  67. {
  68. $this->Tasks->load('ThisTaskShouldAlwaysBeMissing');
  69. }
  70. /**
  71. * test loading a plugin helper.
  72. *
  73. * @return void
  74. */
  75. public function testLoadPluginTask()
  76. {
  77. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', [], [], '', false);
  78. $shell = $this->getMock('Cake\Console\Shell', [], [], '', false);
  79. Plugin::load('TestPlugin');
  80. $this->Tasks = new TaskRegistry($shell, $dispatcher);
  81. $result = $this->Tasks->load('TestPlugin.OtherTask');
  82. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $result, 'Task class is wrong.');
  83. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $this->Tasks->OtherTask, 'Class is wrong');
  84. Plugin::unload();
  85. }
  86. /**
  87. * Tests loading as an alias
  88. *
  89. * @return void
  90. */
  91. public function testLoadWithAlias()
  92. {
  93. Plugin::load('TestPlugin');
  94. $result = $this->Tasks->load('CommandAliased', ['className' => 'Command']);
  95. $this->assertInstanceOf('Cake\Shell\Task\CommandTask', $result);
  96. $this->assertInstanceOf('Cake\Shell\Task\CommandTask', $this->Tasks->CommandAliased);
  97. $result = $this->Tasks->loaded();
  98. $this->assertEquals(['CommandAliased'], $result, 'loaded() results are wrong.');
  99. $result = $this->Tasks->load('SomeTask', ['className' => 'TestPlugin.OtherTask']);
  100. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $result);
  101. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $this->Tasks->SomeTask);
  102. $result = $this->Tasks->loaded();
  103. $this->assertEquals(['CommandAliased', 'SomeTask'], $result, 'loaded() results are wrong.');
  104. }
  105. }