TaskRegistryTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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. * setUp
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $shell = $this->getMock('Cake\Console\Shell', array(), array(), '', false);
  33. $this->Tasks = new TaskRegistry($shell);
  34. }
  35. /**
  36. * tearDown
  37. *
  38. * @return void
  39. */
  40. public function tearDown() {
  41. unset($this->Tasks);
  42. parent::tearDown();
  43. }
  44. /**
  45. * test triggering callbacks on loaded tasks
  46. *
  47. * @return void
  48. */
  49. public function testLoad() {
  50. $result = $this->Tasks->load('Project');
  51. $this->assertInstanceOf('Cake\Console\Command\Task\ProjectTask', $result);
  52. $this->assertInstanceOf('Cake\Console\Command\Task\ProjectTask', $this->Tasks->Project);
  53. $result = $this->Tasks->loaded();
  54. $this->assertEquals(['Project'], $result, 'loaded() results are wrong.');
  55. }
  56. /**
  57. * test missingtask exception
  58. *
  59. * @expectedException \Cake\Console\Error\MissingTaskException
  60. * @return void
  61. */
  62. public function testLoadMissingTask() {
  63. $this->Tasks->load('ThisTaskShouldAlwaysBeMissing');
  64. }
  65. /**
  66. * test loading a plugin helper.
  67. *
  68. * @return void
  69. */
  70. public function testLoadPluginTask() {
  71. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', array(), array(), '', false);
  72. $shell = $this->getMock('Cake\Console\Shell', array(), array(), '', false);
  73. Plugin::load('TestPlugin');
  74. $this->Tasks = new TaskRegistry($shell, $dispatcher);
  75. $result = $this->Tasks->load('TestPlugin.OtherTask');
  76. $this->assertInstanceOf('TestPlugin\Console\Command\Task\OtherTaskTask', $result, 'Task class is wrong.');
  77. $this->assertInstanceOf('TestPlugin\Console\Command\Task\OtherTaskTask', $this->Tasks->OtherTask, 'Class is wrong');
  78. Plugin::unload();
  79. }
  80. /**
  81. * Tests loading as an alias
  82. *
  83. * @return void
  84. */
  85. public function testLoadWithAlias() {
  86. Plugin::load('TestPlugin');
  87. $result = $this->Tasks->load('ProjectAliased', array('className' => 'Project'));
  88. $this->assertInstanceOf('Cake\Console\Command\Task\ProjectTask', $result);
  89. $this->assertInstanceOf('Cake\Console\Command\Task\ProjectTask', $this->Tasks->ProjectAliased);
  90. $result = $this->Tasks->loaded();
  91. $this->assertEquals(array('ProjectAliased'), $result, 'loaded() results are wrong.');
  92. $result = $this->Tasks->load('SomeTask', array('className' => 'TestPlugin.OtherTask'));
  93. $this->assertInstanceOf('TestPlugin\Console\Command\Task\OtherTaskTask', $result);
  94. $this->assertInstanceOf('TestPlugin\Console\Command\Task\OtherTaskTask', $this->Tasks->SomeTask);
  95. $result = $this->Tasks->loaded();
  96. $this->assertEquals(array('ProjectAliased', 'SomeTask'), $result, 'loaded() results are wrong.');
  97. }
  98. }