TaskRegistryTest.php 3.7 KB

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