TemplateTaskTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console\Command\Task;
  16. use Cake\Console\Command\Task\TemplateTask;
  17. use Cake\Core\App;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * TemplateTaskTest class
  21. */
  22. class TemplateTaskTest extends TestCase {
  23. /**
  24. * setUp method
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  31. $this->Task = $this->getMock('Cake\Console\Command\Task\TemplateTask',
  32. array('in', 'err', 'createFile', '_stop', 'clear'),
  33. array($io)
  34. );
  35. }
  36. /**
  37. * tearDown method
  38. *
  39. * @return void
  40. */
  41. public function tearDown() {
  42. parent::tearDown();
  43. unset($this->Task);
  44. }
  45. /**
  46. * test finding themes installed in
  47. *
  48. * @return void
  49. */
  50. public function testFindingInstalledThemesForBake() {
  51. $consoleLibs = CAKE . 'Console' . DS;
  52. $this->Task->initialize();
  53. $this->assertPathEquals($this->Task->templatePaths['default'], $consoleLibs . 'Templates/default/');
  54. }
  55. /**
  56. * test using an invalid theme name.
  57. *
  58. * @expectedException \RuntimeException
  59. * @expectedExceptionMessage Unable to locate "nope" bake theme
  60. * @return void
  61. */
  62. public function testGetThemePathInvalid() {
  63. $defaultTheme = CAKE . 'Console/Templates/default/';
  64. $this->Task->templatePaths = ['default' => $defaultTheme];
  65. $this->Task->params['theme'] = 'nope';
  66. $this->Task->getThemePath();
  67. }
  68. /**
  69. * test getting the correct theme name. Ensure that with only one theme, or a theme param
  70. * that the user is not bugged. If there are more, find and return the correct theme name
  71. *
  72. * @return void
  73. */
  74. public function testGetThemePath() {
  75. $defaultTheme = CAKE . 'Console/Templates/default/';
  76. $this->Task->templatePaths = ['default' => $defaultTheme];
  77. $result = $this->Task->getThemePath();
  78. $this->assertEquals($defaultTheme, $result);
  79. $this->Task->templatePaths = ['other' => '/some/path', 'default' => $defaultTheme];
  80. $this->Task->params['theme'] = 'other';
  81. $result = $this->Task->getThemePath();
  82. $this->assertEquals('/some/path', $result);
  83. $this->Task->params = array();
  84. $result = $this->Task->getThemePath();
  85. $this->assertEquals($defaultTheme, $result);
  86. $this->assertEquals('default', $this->Task->params['theme']);
  87. }
  88. /**
  89. * test generate
  90. *
  91. * @return void
  92. */
  93. public function testGenerate() {
  94. $this->Task->initialize();
  95. $this->Task->expects($this->any())->method('in')->will($this->returnValue(1));
  96. $result = $this->Task->generate('classes', 'test_object', array('test' => 'foo'));
  97. $expected = "I got rendered\nfoo";
  98. $this->assertTextEquals($expected, $result);
  99. }
  100. /**
  101. * test generate with a missing template in the chosen theme.
  102. * ensure fallback to default works.
  103. *
  104. * @return void
  105. */
  106. public function testGenerateWithTemplateFallbacks() {
  107. $this->Task->initialize();
  108. $this->Task->params['theme'] = 'test';
  109. $this->Task->set(array(
  110. 'name' => 'Article',
  111. 'model' => 'Article',
  112. 'table' => 'articles',
  113. 'import' => false,
  114. 'records' => false,
  115. 'schema' => '',
  116. 'namespace' => ''
  117. ));
  118. $result = $this->Task->generate('classes', 'fixture');
  119. $this->assertRegExp('/ArticleFixture extends .*TestFixture/', $result);
  120. }
  121. }