StringTemplateTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 CakePHP(tm) v3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Core\Plugin;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\StringTemplate;
  19. class StringTemplateTest extends TestCase {
  20. /**
  21. * setUp
  22. *
  23. * @return void
  24. */
  25. public function setUp() {
  26. parent::setUp();
  27. $this->template = new StringTemplate();
  28. }
  29. /**
  30. * test adding templates.
  31. *
  32. * @return void
  33. */
  34. public function testAdd() {
  35. $templates = [
  36. 'link' => '<a href="{{url}}">{{text}}</a>'
  37. ];
  38. $result = $this->template->add($templates);
  39. $this->assertNull($result, 'No return');
  40. $this->assertEquals($templates['link'], $this->template->get('link'));
  41. }
  42. /**
  43. * Test remove.
  44. *
  45. * @return void
  46. */
  47. public function testRemove() {
  48. $templates = [
  49. 'link' => '<a href="{{url}}">{{text}}</a>'
  50. ];
  51. $this->template->add($templates);
  52. $this->assertNull($this->template->remove('link'), 'No return');
  53. $this->assertNull($this->template->get('link'), 'Template should be gone.');
  54. }
  55. /**
  56. * Test formatting strings.
  57. *
  58. * @return void
  59. */
  60. public function testFormat() {
  61. $templates = [
  62. 'link' => '<a href="{{url}}">{{text}}</a>'
  63. ];
  64. $this->template->add($templates);
  65. $result = $this->template->format('not there', []);
  66. $this->assertSame('', $result);
  67. $result = $this->template->format('link', [
  68. 'url' => '/',
  69. 'text' => 'example'
  70. ]);
  71. $this->assertEquals('<a href="/">example</a>', $result);
  72. }
  73. /**
  74. * Test loading templates files in the app.
  75. *
  76. * @return void
  77. */
  78. public function testLoad() {
  79. $this->assertEquals([], $this->template->get());
  80. $this->assertNull($this->template->load('test_templates'));
  81. $this->assertEquals('<a href="{{url}}">{{text}}</a>', $this->template->get('link'));
  82. }
  83. /**
  84. * Test loading templates files from a plugin
  85. *
  86. * @return void
  87. */
  88. public function testLoadPlugin() {
  89. Plugin::load('TestPlugin');
  90. $this->assertNull($this->template->load('TestPlugin.test_templates'));
  91. $this->assertEquals('<em>{{text}}</em>', $this->template->get('italic'));
  92. }
  93. /**
  94. * Test that loading non-existing templates causes errors.
  95. *
  96. * @expectedException Cake\Error\Exception
  97. * @expectedExceptionMessage Could not load configuration file
  98. */
  99. public function testLoadErrorNoFile() {
  100. $this->template->load('no_such_file');
  101. }
  102. }