TwigExtensionTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. use Codeception\Util\Fixtures;
  3. use Grav\Common\Grav;
  4. use Grav\Common\Twig\TwigExtension;
  5. /**
  6. * Class TwigExtensionTest
  7. */
  8. class TwigExtensionTest extends \Codeception\TestCase\Test
  9. {
  10. /** @var Grav $grav */
  11. protected $grav;
  12. /** @var TwigExtension $twig_ext */
  13. protected $twig_ext;
  14. protected function _before(): void
  15. {
  16. $this->grav = Fixtures::get('grav');
  17. $this->twig_ext = new TwigExtension();
  18. }
  19. public function testInflectorFilter(): void
  20. {
  21. self::assertSame('people', $this->twig_ext->inflectorFilter('plural', 'person'));
  22. self::assertSame('shoe', $this->twig_ext->inflectorFilter('singular', 'shoes'));
  23. self::assertSame('Welcome Page', $this->twig_ext->inflectorFilter('title', 'welcome page'));
  24. self::assertSame('SendEmail', $this->twig_ext->inflectorFilter('camel', 'send_email'));
  25. self::assertSame('camel_cased', $this->twig_ext->inflectorFilter('underscor', 'CamelCased'));
  26. self::assertSame('something-text', $this->twig_ext->inflectorFilter('hyphen', 'Something Text'));
  27. self::assertSame('Something text to read', $this->twig_ext->inflectorFilter('human', 'something_text_to_read'));
  28. self::assertSame(5, $this->twig_ext->inflectorFilter('month', '175'));
  29. self::assertSame('10th', $this->twig_ext->inflectorFilter('ordinal', '10'));
  30. }
  31. public function testMd5Filter(): void
  32. {
  33. self::assertSame(md5('grav'), $this->twig_ext->md5Filter('grav'));
  34. self::assertSame(md5('devs@getgrav.org'), $this->twig_ext->md5Filter('devs@getgrav.org'));
  35. }
  36. public function testKsortFilter(): void
  37. {
  38. $object = array("name"=>"Bob","age"=>8,"colour"=>"red");
  39. self::assertSame(array("age"=>8,"colour"=>"red","name"=>"Bob"), $this->twig_ext->ksortFilter($object));
  40. }
  41. public function testContainsFilter(): void
  42. {
  43. self::assertTrue($this->twig_ext->containsFilter('grav', 'grav'));
  44. self::assertTrue($this->twig_ext->containsFilter('So, I found this new cms, called grav, and it\'s pretty awesome guys', 'grav'));
  45. }
  46. public function testNicetimeFilter(): void
  47. {
  48. $now = time();
  49. $threeMinutes = time() - (60*3);
  50. $threeHours = time() - (60*60*3);
  51. $threeDays = time() - (60*60*24*3);
  52. $threeMonths = time() - (60*60*24*30*3);
  53. $threeYears = time() - (60*60*24*365*3);
  54. $measures = ['minutes','hours','days','months','years'];
  55. self::assertSame('No date provided', $this->twig_ext->nicetimeFunc(null));
  56. for ($i=0; $i<count($measures); $i++) {
  57. $time = 'three' . ucfirst($measures[$i]);
  58. self::assertSame('3 ' . $measures[$i] . ' ago', $this->twig_ext->nicetimeFunc($$time));
  59. }
  60. }
  61. public function testRandomizeFilter(): void
  62. {
  63. $array = [1,2,3,4,5];
  64. self::assertContains(2, $this->twig_ext->randomizeFilter($array));
  65. self::assertSame($array, $this->twig_ext->randomizeFilter($array, 5));
  66. self::assertSame($array[0], $this->twig_ext->randomizeFilter($array, 1)[0]);
  67. self::assertSame($array[3], $this->twig_ext->randomizeFilter($array, 4)[3]);
  68. self::assertSame($array[1], $this->twig_ext->randomizeFilter($array, 4)[1]);
  69. }
  70. public function testModulusFilter(): void
  71. {
  72. self::assertSame(3, $this->twig_ext->modulusFilter(3, 4));
  73. self::assertSame(1, $this->twig_ext->modulusFilter(11, 2));
  74. self::assertSame(0, $this->twig_ext->modulusFilter(10, 2));
  75. self::assertSame(2, $this->twig_ext->modulusFilter(10, 4));
  76. }
  77. public function testAbsoluteUrlFilter(): void
  78. {
  79. }
  80. public function testMarkdownFilter(): void
  81. {
  82. }
  83. public function testStartsWithFilter(): void
  84. {
  85. }
  86. public function testEndsWithFilter(): void
  87. {
  88. }
  89. public function testDefinedDefaultFilter(): void
  90. {
  91. }
  92. public function testRtrimFilter(): void
  93. {
  94. }
  95. public function testLtrimFilter(): void
  96. {
  97. }
  98. public function testRepeatFunc(): void
  99. {
  100. }
  101. public function testRegexReplace(): void
  102. {
  103. }
  104. public function testUrlFunc(): void
  105. {
  106. }
  107. public function testEvaluateFunc(): void
  108. {
  109. }
  110. public function testDump(): void
  111. {
  112. }
  113. public function testGistFunc(): void
  114. {
  115. }
  116. public function testRandomStringFunc(): void
  117. {
  118. }
  119. public function testPadFilter(): void
  120. {
  121. }
  122. public function testArrayFunc(): void
  123. {
  124. self::assertSame(
  125. 'this is my text',
  126. $this->twig_ext->regexReplace('<p>this is my text</p>', '(<\/?p>)', '')
  127. );
  128. self::assertSame(
  129. '<i>this is my text</i>',
  130. $this->twig_ext->regexReplace('<p>this is my text</p>', ['(<p>)','(<\/p>)'], ['<i>','</i>'])
  131. );
  132. }
  133. public function testArrayKeyValue(): void
  134. {
  135. self::assertSame(
  136. ['meat' => 'steak'],
  137. $this->twig_ext->arrayKeyValueFunc('meat', 'steak')
  138. );
  139. self::assertSame(
  140. ['fruit' => 'apple', 'meat' => 'steak'],
  141. $this->twig_ext->arrayKeyValueFunc('meat', 'steak', ['fruit' => 'apple'])
  142. );
  143. }
  144. public function stringFunc(): void
  145. {
  146. }
  147. public function testRangeFunc(): void
  148. {
  149. $hundred = [];
  150. for ($i = 0; $i <= 100; $i++) {
  151. $hundred[] = $i;
  152. }
  153. self::assertSame([0], $this->twig_ext->rangeFunc(0, 0));
  154. self::assertSame([0, 1, 2], $this->twig_ext->rangeFunc(0, 2));
  155. self::assertSame([0, 5, 10, 15], $this->twig_ext->rangeFunc(0, 16, 5));
  156. // default (min 0, max 100, step 1)
  157. self::assertSame($hundred, $this->twig_ext->rangeFunc());
  158. // 95 items, starting from 5, (min 5, max 100, step 1)
  159. self::assertSame(array_slice($hundred, 5), $this->twig_ext->rangeFunc(5));
  160. // reversed range
  161. self::assertSame(array_reverse($hundred), $this->twig_ext->rangeFunc(100, 0));
  162. self::assertSame([4, 2, 0], $this->twig_ext->rangeFunc(4, 0, 2));
  163. }
  164. }