TaskRegistryTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://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. * TaskRegistryTest
  21. */
  22. class TaskRegistryTest extends TestCase
  23. {
  24. /**
  25. * setUp
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $shell = $this->getMockBuilder('Cake\Console\Shell')
  33. ->disableOriginalConstructor()
  34. ->getMock();
  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->getMockBuilder('Cake\Console\ShellDispatcher')
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $shell = $this->getMockBuilder('Cake\Console\Shell')
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. Plugin::load('TestPlugin');
  84. $this->Tasks = new TaskRegistry($shell, $dispatcher);
  85. $result = $this->Tasks->load('TestPlugin.OtherTask');
  86. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $result, 'Task class is wrong.');
  87. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $this->Tasks->OtherTask, 'Class is wrong');
  88. Plugin::unload();
  89. }
  90. /**
  91. * Tests loading as an alias
  92. *
  93. * @return void
  94. */
  95. public function testLoadWithAlias()
  96. {
  97. Plugin::load('TestPlugin');
  98. $result = $this->Tasks->load('CommandAliased', ['className' => 'Command']);
  99. $this->assertInstanceOf('Cake\Shell\Task\CommandTask', $result);
  100. $this->assertInstanceOf('Cake\Shell\Task\CommandTask', $this->Tasks->CommandAliased);
  101. $result = $this->Tasks->loaded();
  102. $this->assertEquals(['CommandAliased'], $result, 'loaded() results are wrong.');
  103. $result = $this->Tasks->load('SomeTask', ['className' => 'TestPlugin.OtherTask']);
  104. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $result);
  105. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $this->Tasks->SomeTask);
  106. $result = $this->Tasks->loaded();
  107. $this->assertEquals(['CommandAliased', 'SomeTask'], $result, 'loaded() results are wrong.');
  108. }
  109. }