TaskCollectionTest.php 4.2 KB

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