TemplateTaskTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * @since CakePHP(tm) v 1.3
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. namespace Cake\Test\TestCase\Console\Command\Task;
  20. use Cake\Console\Command\Task\TemplateTask;
  21. use Cake\Core\App;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * TemplateTaskTest class
  25. *
  26. */
  27. class TemplateTaskTest extends TestCase {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. $out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  36. $in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  37. $this->Task = $this->getMock('Cake\Console\Command\Task\TemplateTask',
  38. array('in', 'err', 'createFile', '_stop', 'clear'),
  39. array($out, $out, $in)
  40. );
  41. }
  42. /**
  43. * tearDown method
  44. *
  45. * @return void
  46. */
  47. public function tearDown() {
  48. parent::tearDown();
  49. unset($this->Task);
  50. }
  51. /**
  52. * test finding themes installed in
  53. *
  54. * @return void
  55. */
  56. public function testFindingInstalledThemesForBake() {
  57. $consoleLibs = CAKE . 'Console/';
  58. $this->Task->initialize();
  59. $this->assertEquals($this->Task->templatePaths['default'], $consoleLibs . 'Templates/default/');
  60. }
  61. /**
  62. * test getting the correct theme name. Ensure that with only one theme, or a theme param
  63. * that the user is not bugged. If there are more, find and return the correct theme name
  64. *
  65. * @return void
  66. */
  67. public function testGetThemePath() {
  68. $defaultTheme = CAKE . 'Console/Templates/default/';
  69. $this->Task->templatePaths = array('default' => $defaultTheme);
  70. $this->Task->expects($this->exactly(1))->method('in')->will($this->returnValue('1'));
  71. $result = $this->Task->getThemePath();
  72. $this->assertEquals($defaultTheme, $result);
  73. $this->Task->templatePaths = array('other' => '/some/path', 'default' => $defaultTheme);
  74. $this->Task->params['theme'] = 'other';
  75. $result = $this->Task->getThemePath();
  76. $this->assertEquals('/some/path', $result);
  77. $this->Task->params = array();
  78. $result = $this->Task->getThemePath();
  79. $this->assertEquals('/some/path', $result);
  80. $this->assertEquals('other', $this->Task->params['theme']);
  81. }
  82. /**
  83. * test generate
  84. *
  85. * @return void
  86. */
  87. public function testGenerate() {
  88. $this->Task->initialize();
  89. $this->Task->expects($this->any())->method('in')->will($this->returnValue(1));
  90. $result = $this->Task->generate('classes', 'test_object', array('test' => 'foo'));
  91. $expected = "I got rendered\nfoo";
  92. $this->assertTextEquals($expected, $result);
  93. }
  94. /**
  95. * test generate with a missing template in the chosen theme.
  96. * ensure fallback to default works.
  97. *
  98. * @return void
  99. */
  100. public function testGenerateWithTemplateFallbacks() {
  101. $this->Task->initialize();
  102. $this->Task->params['theme'] = 'test';
  103. $this->Task->set(array(
  104. 'model' => 'Article',
  105. 'table' => 'articles',
  106. 'import' => false,
  107. 'records' => false,
  108. 'schema' => '',
  109. 'namespace' => ''
  110. ));
  111. $result = $this->Task->generate('classes', 'fixture');
  112. $this->assertRegExp('/ArticleFixture extends .*TestFixture/', $result);
  113. }
  114. }