TemplateTaskTest.php 4.7 KB

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