StringTemplateTest.php 2.2 KB

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