TemplateTaskTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * TemplateTask file
  4. *
  5. * Test Case for TemplateTask generation shell task
  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://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Test.Case.Console.Command.Task
  17. * @since CakePHP(tm) v 1.3
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('ShellDispatcher', 'Console');
  21. App::uses('ConsoleOutput', 'Console');
  22. App::uses('ConsoleInput', 'Console');
  23. App::uses('Shell', 'Console');
  24. App::uses('TemplateTask', 'Console/Command/Task');
  25. /**
  26. * TemplateTaskTest class
  27. *
  28. * @package Cake.Test.Case.Console.Command.Task
  29. */
  30. class TemplateTaskTest extends CakeTestCase {
  31. /**
  32. * setUp method
  33. *
  34. * @return void
  35. */
  36. public function setUp() {
  37. parent::setUp();
  38. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  39. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  40. $this->Task = $this->getMock('TemplateTask',
  41. array('in', 'err', 'createFile', '_stop', 'clear'),
  42. array($out, $out, $in)
  43. );
  44. }
  45. /**
  46. * tearDown method
  47. *
  48. * @return void
  49. */
  50. public function tearDown() {
  51. parent::tearDown();
  52. unset($this->Task);
  53. }
  54. /**
  55. * test that set sets variables
  56. *
  57. * @return void
  58. */
  59. public function testSet() {
  60. $this->Task->set('one', 'two');
  61. $this->assertTrue(isset($this->Task->templateVars['one']));
  62. $this->assertEquals('two', $this->Task->templateVars['one']);
  63. $this->Task->set(array('one' => 'three', 'four' => 'five'));
  64. $this->assertTrue(isset($this->Task->templateVars['one']));
  65. $this->assertEquals('three', $this->Task->templateVars['one']);
  66. $this->assertTrue(isset($this->Task->templateVars['four']));
  67. $this->assertEquals('five', $this->Task->templateVars['four']);
  68. $this->Task->templateVars = array();
  69. $this->Task->set(array(3 => 'three', 4 => 'four'));
  70. $this->Task->set(array(1 => 'one', 2 => 'two'));
  71. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  72. $this->assertEquals($expected, $this->Task->templateVars);
  73. }
  74. /**
  75. * test finding themes installed in
  76. *
  77. * @return void
  78. */
  79. public function testFindingInstalledThemesForBake() {
  80. $consoleLibs = CAKE . 'Console' . DS;
  81. $this->Task->initialize();
  82. $this->assertEquals($this->Task->templatePaths['default'], $consoleLibs . 'Templates' . DS . 'default' . DS);
  83. }
  84. /**
  85. * test getting the correct theme name. Ensure that with only one theme, or a theme param
  86. * that the user is not bugged. If there are more, find and return the correct theme name
  87. *
  88. * @return void
  89. */
  90. public function testGetThemePath() {
  91. $defaultTheme = CAKE . 'Console' . DS . 'Templates' . DS . 'default' . DS;
  92. $this->Task->templatePaths = array('default' => $defaultTheme);
  93. $this->Task->expects($this->exactly(1))->method('in')->will($this->returnValue('1'));
  94. $result = $this->Task->getThemePath();
  95. $this->assertEquals($defaultTheme, $result);
  96. $this->Task->templatePaths = array('other' => '/some/path', 'default' => $defaultTheme);
  97. $this->Task->params['theme'] = 'other';
  98. $result = $this->Task->getThemePath();
  99. $this->assertEquals('/some/path', $result);
  100. $this->Task->params = array();
  101. $result = $this->Task->getThemePath();
  102. $this->assertEquals('/some/path', $result);
  103. $this->assertEquals('other', $this->Task->params['theme']);
  104. }
  105. /**
  106. * test generate
  107. *
  108. * @return void
  109. */
  110. public function testGenerate() {
  111. App::build(array(
  112. 'Console' => array(
  113. CAKE . 'Test' . DS . 'test_app' . DS . 'Console' . DS
  114. )
  115. ));
  116. $this->Task->initialize();
  117. $this->Task->expects($this->any())->method('in')->will($this->returnValue(1));
  118. $result = $this->Task->generate('classes', 'test_object', array('test' => 'foo'));
  119. $expected = "I got rendered\nfoo";
  120. $this->assertTextEquals($expected, $result);
  121. }
  122. /**
  123. * test generate with a missing template in the chosen theme.
  124. * ensure fallback to default works.
  125. *
  126. * @return void
  127. */
  128. public function testGenerateWithTemplateFallbacks() {
  129. App::build(array(
  130. 'Console' => array(
  131. CAKE . 'Test' . DS . 'test_app' . DS . 'Console' . DS,
  132. CAKE_CORE_INCLUDE_PATH . DS . 'console' . DS
  133. )
  134. ));
  135. $this->Task->initialize();
  136. $this->Task->params['theme'] = 'test';
  137. $this->Task->set(array(
  138. 'model' => 'Article',
  139. 'table' => 'articles',
  140. 'import' => false,
  141. 'records' => false,
  142. 'schema' => ''
  143. ));
  144. $result = $this->Task->generate('classes', 'fixture');
  145. $this->assertRegExp('/ArticleFixture extends CakeTestFixture/', $result);
  146. }
  147. }