In this repo i store all my websites, each in a different branch
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

188 wiersze
5.5 KiB

  1. <?php
  2. /**
  3. * Slim Framework (http://slimframework.com)
  4. *
  5. * @link https://github.com/codeguy/Slim
  6. * @copyright Copyright (c) 2011-2015 Josh Lockhart
  7. * @license https://github.com/codeguy/Slim/blob/master/LICENSE (MIT License)
  8. */
  9. namespace Slim\Tests\Views;
  10. use Slim\Views\Twig;
  11. require dirname(__DIR__) . '/vendor/autoload.php';
  12. class TwigTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testFetch()
  15. {
  16. $view = new Twig(dirname(__FILE__) . '/templates');
  17. $output = $view->fetch('example.html', [
  18. 'name' => 'Josh'
  19. ]);
  20. $this->assertEquals("<p>Hi, my name is Josh.</p>\n", $output);
  21. }
  22. public function testFetchFromString()
  23. {
  24. $view = new Twig(dirname(__FILE__) . '/templates');
  25. $output = $view->fetchFromString("<p>Hi, my name is {{ name }}.</p>", [
  26. 'name' => 'Josh'
  27. ]);
  28. $this->assertEquals("<p>Hi, my name is Josh.</p>", $output);
  29. }
  30. public function testFetchBlock()
  31. {
  32. $view = new Twig(dirname(__FILE__) . '/templates');
  33. $outputOne = $view->fetchBlock('block_example.html', 'first', [
  34. 'name' => 'Josh'
  35. ]);
  36. $outputTwo = $view->fetchBlock('block_example.html', 'second', [
  37. 'name' => 'Peter'
  38. ]);
  39. $this->assertEquals("<p>Hi, my name is Josh.</p>", $outputOne);
  40. $this->assertEquals("<p>My name is not Peter.</p>", $outputTwo);
  41. }
  42. public function testSingleNamespaceAndMultipleDirectories()
  43. {
  44. $weekday = (new \DateTimeImmutable('2016-03-08'))->format('l');
  45. $view = new Twig(
  46. [
  47. 'namespace' => [
  48. __DIR__.'/another',
  49. __DIR__.'/templates',
  50. __DIR__.'/multi',
  51. ],
  52. ]
  53. );
  54. $anotherDirectory = $view->fetch('@namespace/example.html', [
  55. 'name' => 'Peter'
  56. ]);
  57. $templatesDirectory = $view->fetch('@namespace/another_example.html', [
  58. 'name' => 'Peter',
  59. 'gender' => 'male',
  60. ]);
  61. $outputMulti = $view->fetch('@namespace/directory/template/example.html', [
  62. 'weekday' => $weekday,
  63. ]);
  64. $this->assertEquals("<p>Hi, my name is Peter.</p>\n", $anotherDirectory);
  65. $this->assertEquals("<p>Hi, my name is Peter and I am male.</p>\n", $templatesDirectory);
  66. $this->assertEquals('Happy Tuesday!', $outputMulti);
  67. }
  68. public function testArrayWithASingleTemplateWithANamespace()
  69. {
  70. $views = new Twig([
  71. 'One' => [
  72. __DIR__.'/templates',
  73. ],
  74. ]);
  75. $output = $views->fetch('@One/example.html', [
  76. 'name' => 'Josh'
  77. ]);
  78. $this->assertEquals("<p>Hi, my name is Josh.</p>\n", $output);
  79. }
  80. public function testASingleTemplateWithANamespace()
  81. {
  82. $views = new Twig([
  83. 'One' => __DIR__.'/templates',
  84. ]);
  85. $output = $views->fetch('@One/example.html', [
  86. 'name' => 'Josh'
  87. ]);
  88. $this->assertEquals("<p>Hi, my name is Josh.</p>\n", $output);
  89. }
  90. public function testMultipleTemplatesWithMultipleNamespace()
  91. {
  92. $weekday = (new \DateTimeImmutable('2016-03-08'))->format('l');
  93. $views = new Twig([
  94. 'One' => __DIR__.'/templates',
  95. 'Two' => __DIR__.'/another',
  96. 'Three' => [
  97. __DIR__.'/multi',
  98. ],
  99. ]);
  100. $outputOne = $views->fetch('@One/example.html', [
  101. 'name' => 'Peter'
  102. ]);
  103. $outputTwo = $views->fetch('@Two/another_example.html', [
  104. 'name' => 'Peter',
  105. 'gender' => 'male'
  106. ]);
  107. $outputThree = $views->fetch('@Three/directory/template/example.html', [
  108. 'weekday' => $weekday,
  109. ]);
  110. $this->assertEquals("<p>Hi, my name is Peter.</p>\n", $outputOne);
  111. $this->assertEquals("<p>Hi, my name is Peter and I am male.</p>\n", $outputTwo);
  112. $this->assertEquals('Happy Tuesday!', $outputThree);
  113. }
  114. public function testMultipleDirectoriesWithoutNamespaces()
  115. {
  116. $weekday = (new \DateTimeImmutable('2016-03-08'))->format('l');
  117. $view = new Twig([__DIR__.'/multi/', __DIR__.'/another/']);
  118. $rootDirectory = $view->fetch('directory/template/example.html', [
  119. 'weekday' => $weekday,
  120. ]);
  121. $multiDirectory = $view->fetch('another_example.html', [
  122. 'name' => 'Peter',
  123. 'gender' => 'male',
  124. ]);
  125. $this->assertEquals('Happy Tuesday!', $rootDirectory);
  126. $this->assertEquals("<p>Hi, my name is Peter and I am male.</p>\n", $multiDirectory);
  127. }
  128. public function testRender()
  129. {
  130. $view = new Twig(dirname(__FILE__) . '/templates');
  131. $mockBody = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $mockResponse = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')
  135. ->disableOriginalConstructor()
  136. ->getMock();
  137. $mockBody->expects($this->once())
  138. ->method('write')
  139. ->with("<p>Hi, my name is Josh.</p>\n")
  140. ->willReturn(28);
  141. $mockResponse->expects($this->once())
  142. ->method('getBody')
  143. ->willReturn($mockBody);
  144. $response = $view->render($mockResponse, 'example.html', [
  145. 'name' => 'Josh'
  146. ]);
  147. $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
  148. }
  149. }