TaskCollectionTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * TaskCollectionTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.Console
  15. * @since CakePHP(tm) v 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('TaskCollection', 'Console');
  19. App::uses('Shell', 'Console');
  20. /**
  21. * Extended Task
  22. */
  23. class DbConfigAliasedTask extends Shell {
  24. }
  25. /**
  26. * Class TaskCollectionTest
  27. *
  28. * @package Cake.Test.Case.Console
  29. */
  30. class TaskCollectionTest extends CakeTestCase {
  31. /**
  32. * setUp
  33. *
  34. * @return void
  35. */
  36. public function setUp() {
  37. parent::setUp();
  38. $shell = $this->getMock('Shell', array(), array(), '', false);
  39. $dispatcher = $this->getMock('ShellDispatcher', array(), array(), '', false);
  40. $this->Tasks = new TaskCollection($shell, $dispatcher);
  41. }
  42. /**
  43. * tearDown
  44. *
  45. * @return void
  46. */
  47. public function tearDown() {
  48. unset($this->Tasks);
  49. parent::tearDown();
  50. }
  51. /**
  52. * test triggering callbacks on loaded tasks
  53. *
  54. * @return void
  55. */
  56. public function testLoad() {
  57. $result = $this->Tasks->load('DbConfig');
  58. $this->assertInstanceOf('DbConfigTask', $result);
  59. $this->assertInstanceOf('DbConfigTask', $this->Tasks->DbConfig);
  60. $result = $this->Tasks->loaded();
  61. $this->assertEquals(array('DbConfig'), $result, 'loaded() results are wrong.');
  62. }
  63. /**
  64. * test load and enable = false
  65. *
  66. * @return void
  67. */
  68. public function testLoadWithEnableFalse() {
  69. $result = $this->Tasks->load('DbConfig', array('enabled' => false));
  70. $this->assertInstanceOf('DbConfigTask', $result);
  71. $this->assertInstanceOf('DbConfigTask', $this->Tasks->DbConfig);
  72. $this->assertFalse($this->Tasks->enabled('DbConfig'), 'DbConfigTask should be disabled');
  73. }
  74. /**
  75. * test missingtask exception
  76. *
  77. * @expectedException MissingTaskException
  78. * @return void
  79. */
  80. public function testLoadMissingTask() {
  81. $this->Tasks->load('ThisTaskShouldAlwaysBeMissing');
  82. }
  83. /**
  84. * test loading a plugin helper.
  85. *
  86. * @return void
  87. */
  88. public function testLoadPluginTask() {
  89. $dispatcher = $this->getMock('ShellDispatcher', array(), array(), '', false);
  90. $shell = $this->getMock('Shell', array(), array(), '', false);
  91. App::build(array(
  92. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  93. ));
  94. CakePlugin::load('TestPlugin');
  95. $this->Tasks = new TaskCollection($shell, $dispatcher);
  96. $result = $this->Tasks->load('TestPlugin.OtherTask');
  97. $this->assertInstanceOf('OtherTaskTask', $result, 'Task class is wrong.');
  98. $this->assertInstanceOf('OtherTaskTask', $this->Tasks->OtherTask, 'Class is wrong');
  99. CakePlugin::unload();
  100. }
  101. /**
  102. * test unload()
  103. *
  104. * @return void
  105. */
  106. public function testUnload() {
  107. $this->Tasks->load('Extract');
  108. $this->Tasks->load('DbConfig');
  109. $result = $this->Tasks->loaded();
  110. $this->assertEquals(array('Extract', 'DbConfig'), $result, 'loaded tasks is wrong');
  111. $this->Tasks->unload('DbConfig');
  112. $this->assertFalse(isset($this->Tasks->DbConfig));
  113. $this->assertTrue(isset($this->Tasks->Extract));
  114. $result = $this->Tasks->loaded();
  115. $this->assertEquals(array('Extract'), $result, 'loaded tasks is wrong');
  116. }
  117. /**
  118. * Tests loading as an alias
  119. *
  120. * @return void
  121. */
  122. public function testLoadWithAlias() {
  123. $result = $this->Tasks->load('DbConfig', array('className' => 'DbConfigAliased'));
  124. $this->assertInstanceOf('DbConfigAliasedTask', $result);
  125. $this->assertInstanceOf('DbConfigAliasedTask', $this->Tasks->DbConfig);
  126. $result = $this->Tasks->loaded();
  127. $this->assertEquals(array('DbConfig'), $result, 'loaded() results are wrong.');
  128. $result = $this->Tasks->load('SomeTask', array('className' => 'TestPlugin.OtherTask'));
  129. $this->assertInstanceOf('OtherTaskTask', $result);
  130. $this->assertInstanceOf('OtherTaskTask', $this->Tasks->SomeTask);
  131. $result = $this->Tasks->loaded();
  132. $this->assertEquals(array('DbConfig', 'SomeTask'), $result, 'loaded() results are wrong.');
  133. }
  134. }