PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/msadouni/cakephp2x
PHP | 184 lines | 96 code | 17 blank | 71 comment | 2 complexity | d3147727f30590b6f320251c88e9bd09 MD5 | raw file
  1. <?php
  2. /**
  3. * TemplateTask file
  4. *
  5. * Test Case for TemplateTask generation shell task
  6. *
  7. *
  8. * PHP Version 5.x
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  11. * Copyright 2005-2009, 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-2009, 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. }
  87. /**
  88. * test finding themes installed in
  89. *
  90. * @return void
  91. * @access public
  92. */
  93. function testFindingInstalledThemesForBake() {
  94. $consoleLibs = CAKE_CORE_INCLUDE_PATH . DS . CAKE . 'console' . DS;
  95. $this->Task->Dispatch->shellPaths = array($consoleLibs);
  96. $this->Task->initialize();
  97. $this->assertEqual($this->Task->templatePaths, array('default' => $consoleLibs . 'templates' . DS . 'default' . DS));
  98. }
  99. /**
  100. * test getting the correct theme name. Ensure that with only one theme, or a theme param
  101. * that the user is not bugged. If there are more, find and return the correct theme name
  102. *
  103. * @return void
  104. * @access public
  105. */
  106. function testGetThemePath() {
  107. $defaultTheme = CAKE_CORE_INCLUDE_PATH . DS . dirname(CONSOLE_LIBS) . 'templates' . DS . 'default' .DS;
  108. $this->Task->templatePaths = array('default' => $defaultTheme);
  109. $this->Task->expectCallCount('in', 1);
  110. $result = $this->Task->getThemePath();
  111. $this->assertEqual($result, $defaultTheme);
  112. $this->Task->templatePaths = array('default' => $defaultTheme, 'other' => '/some/path');
  113. $this->Task->params['theme'] = 'other';
  114. $result = $this->Task->getThemePath();
  115. $this->assertEqual($result, '/some/path');
  116. $this->Task->params = array();
  117. $this->Task->setReturnValueAt(0, 'in', '1');
  118. $result = $this->Task->getThemePath();
  119. $this->assertEqual($result, $defaultTheme);
  120. $this->assertEqual($this->Dispatcher->params['theme'], 'default');
  121. }
  122. /**
  123. * test generate
  124. *
  125. * @return void
  126. * @access public
  127. */
  128. function testGenerate() {
  129. App::build(array(
  130. 'shells' => array(
  131. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'shells' . DS
  132. )
  133. ));
  134. $this->Task->initialize();
  135. $this->Task->setReturnValue('in', 1);
  136. $result = $this->Task->generate('classes', 'test_object', array('test' => 'foo'));
  137. $expected = "I got rendered\nfoo";
  138. $this->assertEqual($result, $expected);
  139. }
  140. /**
  141. * test generate with a missing template in the chosen theme.
  142. * ensure fallback to default works.
  143. *
  144. * @return void
  145. * @access public
  146. */
  147. function testGenerateWithTemplateFallbacks() {
  148. App::build(array(
  149. 'shells' => array(
  150. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'shells' . DS,
  151. CAKE_CORE_INCLUDE_PATH . DS . 'console' . DS
  152. )
  153. ));
  154. $this->Task->initialize();
  155. $this->Task->params['theme'] = 'test';
  156. $this->Task->set(array(
  157. 'model' => 'Article',
  158. 'table' => 'articles',
  159. 'import' => false,
  160. 'records' => false,
  161. 'schema' => ''
  162. ));
  163. $result = $this->Task->generate('classes', 'fixture');
  164. $this->assertPattern('/ArticleFixture extends CakeTestFixture/', $result);
  165. }
  166. }
  167. ?>