TaskRegistryTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Console;
  17. use Cake\Console\TaskRegistry;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * TaskRegistryTest
  21. */
  22. class TaskRegistryTest extends TestCase
  23. {
  24. /**
  25. * @var \Cake\Console\TaskRegistry
  26. */
  27. protected $Tasks;
  28. /**
  29. * setUp
  30. */
  31. public function setUp(): void
  32. {
  33. parent::setUp();
  34. $shell = $this->getMockBuilder('Cake\Console\Shell')
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->Tasks = new TaskRegistry($shell);
  38. }
  39. /**
  40. * tearDown
  41. */
  42. public function tearDown(): void
  43. {
  44. unset($this->Tasks);
  45. parent::tearDown();
  46. }
  47. /**
  48. * test triggering callbacks on loaded tasks
  49. */
  50. public function testLoad(): void
  51. {
  52. $result = $this->Tasks->load('Command');
  53. $this->assertInstanceOf('Cake\Shell\Task\CommandTask', $result);
  54. $this->assertInstanceOf('Cake\Shell\Task\CommandTask', $this->Tasks->Command);
  55. $result = $this->Tasks->loaded();
  56. $this->assertEquals(['Command'], $result, 'loaded() results are wrong.');
  57. }
  58. /**
  59. * test missingtask exception
  60. */
  61. public function testLoadMissingTask(): void
  62. {
  63. $this->expectException(\Cake\Console\Exception\MissingTaskException::class);
  64. $this->Tasks->load('ThisTaskShouldAlwaysBeMissing');
  65. }
  66. /**
  67. * test loading a plugin helper.
  68. */
  69. public function testLoadPluginTask(): void
  70. {
  71. $shell = $this->getMockBuilder('Cake\Console\Shell')
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->loadPlugins(['TestPlugin']);
  75. $this->Tasks = new TaskRegistry($shell);
  76. $result = $this->Tasks->load('TestPlugin.OtherTask');
  77. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $result, 'Task class is wrong.');
  78. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $this->Tasks->OtherTask, 'Class is wrong');
  79. $this->clearPlugins();
  80. }
  81. /**
  82. * Tests loading as an alias
  83. */
  84. public function testLoadWithAlias(): void
  85. {
  86. $this->loadPlugins(['TestPlugin']);
  87. $result = $this->Tasks->load('CommandAliased', ['className' => 'Command']);
  88. $this->assertInstanceOf('Cake\Shell\Task\CommandTask', $result);
  89. $this->assertInstanceOf('Cake\Shell\Task\CommandTask', $this->Tasks->CommandAliased);
  90. $result = $this->Tasks->loaded();
  91. $this->assertEquals(['CommandAliased'], $result, 'loaded() results are wrong.');
  92. $result = $this->Tasks->load('SomeTask', ['className' => 'TestPlugin.OtherTask']);
  93. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $result);
  94. $this->assertInstanceOf('TestPlugin\Shell\Task\OtherTaskTask', $this->Tasks->SomeTask);
  95. $result = $this->Tasks->loaded();
  96. $this->assertEquals(['CommandAliased', 'SomeTask'], $result, 'loaded() results are wrong.');
  97. $this->clearPlugins();
  98. }
  99. }