TaskRegistryTest.php 3.5 KB

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