/cake/tests/cases/console/libs/tasks/template.test.php

https://github.com/tsep/tsep1 · PHP · 189 lines · 100 code · 18 blank · 71 comment · 2 complexity · b3683236d517a258c15cefce1bf4a91f MD5 · raw file

  1. <?php
  2. /**
  3. * TemplateTask file
  4. *
  5. * Test Case for TemplateTask generation shell task
  6. *
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  11. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP(tm) Project
  18. * @package cake
  19. * @subpackage cake.tests.cases.console.libs.tasks
  20. * @since CakePHP(tm) v 1.3
  21. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  22. */
  23. App::import('Shell', 'Shell', false);
  24. if (!defined('DISABLE_AUTO_DISPATCH')) {
  25. define('DISABLE_AUTO_DISPATCH', true);
  26. }
  27. if (!class_exists('ShellDispatcher')) {
  28. ob_start();
  29. $argv = false;
  30. require CAKE . 'console' . DS . 'cake.php';
  31. ob_end_clean();
  32. }
  33. require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php';
  34. Mock::generatePartial(
  35. 'ShellDispatcher', 'TestTemplateTaskMockShellDispatcher',
  36. array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
  37. );
  38. Mock::generatePartial(
  39. 'TemplateTask', 'MockTemplateTask',
  40. array('in', 'out', 'err', 'createFile', '_stop')
  41. );
  42. /**
  43. * TemplateTaskTest class
  44. *
  45. * @package cake
  46. * @subpackage cake.tests.cases.console.libs.tasks
  47. */
  48. class TemplateTaskTest extends CakeTestCase {
  49. /**
  50. * startTest method
  51. *
  52. * @return void
  53. * @access public
  54. */
  55. function startTest() {
  56. $this->Dispatcher =& new TestTemplateTaskMockShellDispatcher();
  57. $this->Task =& new MockTemplateTask($this->Dispatcher);
  58. $this->Task->Dispatch =& $this->Dispatcher;
  59. $this->Task->Dispatch->shellPaths = App::path('shells');
  60. }
  61. /**
  62. * endTest method
  63. *
  64. * @return void
  65. * @access public
  66. */
  67. function endTest() {
  68. unset($this->Task, $this->Dispatcher);
  69. ClassRegistry::flush();
  70. }
  71. /**
  72. * test that set sets variables
  73. *
  74. * @return void
  75. * @access public
  76. */
  77. function testSet() {
  78. $this->Task->set('one', 'two');
  79. $this->assertTrue(isset($this->Task->templateVars['one']));
  80. $this->assertEqual($this->Task->templateVars['one'], 'two');
  81. $this->Task->set(array('one' => 'three', 'four' => 'five'));
  82. $this->assertTrue(isset($this->Task->templateVars['one']));
  83. $this->assertEqual($this->Task->templateVars['one'], 'three');
  84. $this->assertTrue(isset($this->Task->templateVars['four']));
  85. $this->assertEqual($this->Task->templateVars['four'], 'five');
  86. $this->Task->templateVars = array();
  87. $this->Task->set(array(3 => 'three', 4 => 'four'));
  88. $this->Task->set(array(1 => 'one', 2 => 'two'));
  89. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  90. $this->assertEqual($this->Task->templateVars, $expected);
  91. }
  92. /**
  93. * test finding themes installed in
  94. *
  95. * @return void
  96. * @access public
  97. */
  98. function testFindingInstalledThemesForBake() {
  99. $consoleLibs = CAKE_CORE_INCLUDE_PATH . DS . CAKE . 'console' . DS;
  100. $this->Task->Dispatch->shellPaths = array($consoleLibs);
  101. $this->Task->initialize();
  102. $this->assertEqual($this->Task->templatePaths, array('default' => $consoleLibs . 'templates' . DS . 'default' . DS));
  103. }
  104. /**
  105. * test getting the correct theme name. Ensure that with only one theme, or a theme param
  106. * that the user is not bugged. If there are more, find and return the correct theme name
  107. *
  108. * @return void
  109. * @access public
  110. */
  111. function testGetThemePath() {
  112. $defaultTheme = CAKE_CORE_INCLUDE_PATH . DS . dirname(CONSOLE_LIBS) . 'templates' . DS . 'default' .DS;
  113. $this->Task->templatePaths = array('default' => $defaultTheme);
  114. $this->Task->expectCallCount('in', 1);
  115. $result = $this->Task->getThemePath();
  116. $this->assertEqual($result, $defaultTheme);
  117. $this->Task->templatePaths = array('default' => $defaultTheme, 'other' => '/some/path');
  118. $this->Task->params['theme'] = 'other';
  119. $result = $this->Task->getThemePath();
  120. $this->assertEqual($result, '/some/path');
  121. $this->Task->params = array();
  122. $this->Task->setReturnValueAt(0, 'in', '1');
  123. $result = $this->Task->getThemePath();
  124. $this->assertEqual($result, $defaultTheme);
  125. $this->assertEqual($this->Dispatcher->params['theme'], 'default');
  126. }
  127. /**
  128. * test generate
  129. *
  130. * @return void
  131. * @access public
  132. */
  133. function testGenerate() {
  134. App::build(array(
  135. 'shells' => array(
  136. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'shells' . DS
  137. )
  138. ));
  139. $this->Task->initialize();
  140. $this->Task->setReturnValue('in', 1);
  141. $result = $this->Task->generate('classes', 'test_object', array('test' => 'foo'));
  142. $expected = "I got rendered\nfoo";
  143. $this->assertEqual($result, $expected);
  144. }
  145. /**
  146. * test generate with a missing template in the chosen theme.
  147. * ensure fallback to default works.
  148. *
  149. * @return void
  150. * @access public
  151. */
  152. function testGenerateWithTemplateFallbacks() {
  153. App::build(array(
  154. 'shells' => array(
  155. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'shells' . DS,
  156. CAKE_CORE_INCLUDE_PATH . DS . 'console' . DS
  157. )
  158. ));
  159. $this->Task->initialize();
  160. $this->Task->params['theme'] = 'test';
  161. $this->Task->set(array(
  162. 'model' => 'Article',
  163. 'table' => 'articles',
  164. 'import' => false,
  165. 'records' => false,
  166. 'schema' => ''
  167. ));
  168. $result = $this->Task->generate('classes', 'fixture');
  169. $this->assertPattern('/ArticleFixture extends CakeTestFixture/', $result);
  170. }
  171. }