StringTemplateTraitTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Core\InstanceConfigTrait;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\Helper\StringTemplate;
  19. use Cake\View\Helper\StringTemplateTrait;
  20. /**
  21. * TestStringTemplate
  22. *
  23. */
  24. class TestStringTemplate {
  25. use InstanceConfigTrait;
  26. use StringTemplateTrait;
  27. /**
  28. * _defaultConfig
  29. *
  30. * @var array
  31. */
  32. protected $_defaultConfig = [];
  33. }
  34. /**
  35. * StringTemplateTraitTest class
  36. *
  37. */
  38. class StringTemplateTraitTest extends TestCase {
  39. /**
  40. * setUp method
  41. *
  42. * @return void
  43. */
  44. public function setUp() {
  45. parent::setUp();
  46. $this->Template = new TestStringTemplate;
  47. }
  48. /**
  49. * testInitStringTemplates
  50. *
  51. * @return void
  52. */
  53. public function testInitStringTemplates() {
  54. $templates = [
  55. 'text' => '<p>{{text}}</p>',
  56. ];
  57. $this->Template->templates($templates);
  58. $this->assertEquals(
  59. [
  60. 'attribute' => '{{name}}="{{value}}"',
  61. 'compactAttribute' => '{{name}}="{{value}}"',
  62. 'text' => '<p>{{text}}</p>'
  63. ],
  64. $this->Template->templates(),
  65. 'newly added template should be inlcuded in template list'
  66. );
  67. }
  68. /**
  69. * test settings['templates']
  70. *
  71. * @return void
  72. */
  73. public function testInitStringTemplatesArrayForm() {
  74. $this->Template->config(
  75. 'templates.text',
  76. '<p>{{text}}</p>'
  77. );
  78. $this->assertEquals(
  79. [
  80. 'attribute' => '{{name}}="{{value}}"',
  81. 'compactAttribute' => '{{name}}="{{value}}"',
  82. 'text' => '<p>{{text}}</p>'
  83. ],
  84. $this->Template->templates(),
  85. 'Configured templates should be included in template list'
  86. );
  87. }
  88. /**
  89. * testFormatStringTemplate
  90. *
  91. * @return void
  92. */
  93. public function testFormatStringTemplate() {
  94. $templates = [
  95. 'text' => '<p>{{text}}</p>',
  96. ];
  97. $this->Template->templates($templates);
  98. $result = $this->Template->formatTemplate('text', [
  99. 'text' => 'CakePHP'
  100. ]);
  101. $this->assertEquals(
  102. '<p>CakePHP</p>',
  103. $result
  104. );
  105. }
  106. /**
  107. * testGetTemplater
  108. *
  109. * @return void
  110. */
  111. public function testGetTemplater() {
  112. $templates = [
  113. 'text' => '<p>{{text}}</p>',
  114. ];
  115. $this->Template->templates($templates);
  116. $result = $this->Template->templater();
  117. $this->assertInstanceOf('\Cake\View\StringTemplate', $result);
  118. }
  119. }