StringTemplateTraitTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\StringTemplate;
  19. use TestApp\View\TestStringTemplate;
  20. /**
  21. * StringTemplateTraitTest class
  22. */
  23. class StringTemplateTraitTest extends TestCase
  24. {
  25. /**
  26. * @var \TestApp\View\TestStringTemplate
  27. */
  28. protected $Template;
  29. /**
  30. * setUp method
  31. */
  32. public function setUp(): void
  33. {
  34. parent::setUp();
  35. $this->Template = new TestStringTemplate();
  36. }
  37. /**
  38. * testInitStringTemplates
  39. */
  40. public function testInitStringTemplates(): void
  41. {
  42. $templates = [
  43. 'text' => '<p>{{text}}</p>',
  44. ];
  45. $this->Template->setTemplates($templates);
  46. $this->assertEquals(
  47. [
  48. 'text' => '<p>{{text}}</p>',
  49. ],
  50. $this->Template->getTemplates(),
  51. 'newly added template should be included in template list'
  52. );
  53. }
  54. /**
  55. * test settings['templates']
  56. */
  57. public function testInitStringTemplatesArrayForm(): void
  58. {
  59. $this->Template->setConfig(
  60. 'templates.text',
  61. '<p>{{text}}</p>'
  62. );
  63. $this->assertEquals(
  64. [
  65. 'text' => '<p>{{text}}</p>',
  66. ],
  67. $this->Template->getTemplates(),
  68. 'Configured templates should be included in template list'
  69. );
  70. }
  71. /**
  72. * testFormatStringTemplate
  73. */
  74. public function testFormatStringTemplate(): void
  75. {
  76. $templates = [
  77. 'text' => '<p>{{text}}</p>',
  78. ];
  79. $this->Template->setTemplates($templates);
  80. $result = $this->Template->formatTemplate('text', [
  81. 'text' => 'CakePHP',
  82. ]);
  83. $this->assertSame(
  84. '<p>CakePHP</p>',
  85. $result
  86. );
  87. }
  88. /**
  89. * testGetTemplater
  90. */
  91. public function testGetTemplater(): void
  92. {
  93. $templates = [
  94. 'text' => '<p>{{text}}</p>',
  95. ];
  96. $this->Template->setTemplates($templates);
  97. $result = $this->Template->templater();
  98. $this->assertInstanceOf(StringTemplate::class, $result);
  99. }
  100. }