TwigExtensionTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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()
  15. {
  16. $this->grav = Fixtures::get('grav');
  17. $this->twig_ext = new TwigExtension();
  18. }
  19. public function testInflectorFilter()
  20. {
  21. $this->assertSame('people', $this->twig_ext->inflectorFilter('plural', 'person'));
  22. $this->assertSame('shoe', $this->twig_ext->inflectorFilter('singular', 'shoes'));
  23. $this->assertSame('Welcome Page', $this->twig_ext->inflectorFilter('title', 'welcome page'));
  24. $this->assertSame('SendEmail', $this->twig_ext->inflectorFilter('camel', 'send_email'));
  25. $this->assertSame('camel_cased', $this->twig_ext->inflectorFilter('underscor', 'CamelCased'));
  26. $this->assertSame('something-text', $this->twig_ext->inflectorFilter('hyphen', 'Something Text'));
  27. $this->assertSame('Something text to read', $this->twig_ext->inflectorFilter('human', 'something_text_to_read'));
  28. $this->assertSame(5, $this->twig_ext->inflectorFilter('month', 175));
  29. $this->assertSame('10th', $this->twig_ext->inflectorFilter('ordinal', 10));
  30. }
  31. public function testMd5Filter()
  32. {
  33. $this->assertSame(md5('grav'), $this->twig_ext->md5Filter('grav'));
  34. $this->assertSame(md5('devs@getgrav.org'), $this->twig_ext->md5Filter('devs@getgrav.org'));
  35. }
  36. public function testKsortFilter()
  37. {
  38. $object = array("name"=>"Bob","age"=>8,"colour"=>"red");
  39. $this->assertSame(array("age"=>8,"colour"=>"red","name"=>"Bob"), $this->twig_ext->ksortFilter($object));
  40. }
  41. public function testContainsFilter()
  42. {
  43. $this->assertTrue($this->twig_ext->containsFilter('grav','grav'));
  44. $this->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()
  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. $this->assertSame('No date provided', $this->twig_ext->nicetimeFunc(null));
  56. for ($i=0; $i<count($measures); $i++) {
  57. $time = 'three' . ucfirst($measures[$i]);
  58. $this->assertSame('3 ' . $measures[$i] . ' ago', $this->twig_ext->nicetimeFunc($$time));
  59. }
  60. }
  61. public function testRandomizeFilter()
  62. {
  63. $array = [1,2,3,4,5];
  64. $this->assertContains(2, $this->twig_ext->randomizeFilter($array));
  65. $this->assertSame($array, $this->twig_ext->randomizeFilter($array, 5));
  66. $this->assertSame($array[0], $this->twig_ext->randomizeFilter($array, 1)[0]);
  67. $this->assertSame($array[3], $this->twig_ext->randomizeFilter($array, 4)[3]);
  68. $this->assertSame($array[1], $this->twig_ext->randomizeFilter($array, 4)[1]);
  69. }
  70. public function testModulusFilter()
  71. {
  72. $this->assertSame(3, $this->twig_ext->modulusFilter(3,4));
  73. $this->assertSame(1, $this->twig_ext->modulusFilter(11,2));
  74. $this->assertSame(0, $this->twig_ext->modulusFilter(10,2));
  75. $this->assertSame(2, $this->twig_ext->modulusFilter(10,4));
  76. }
  77. public function testAbsoluteUrlFilter()
  78. {
  79. }
  80. public function testMarkdownFilter()
  81. {
  82. }
  83. public function testStartsWithFilter()
  84. {
  85. }
  86. public function testEndsWithFilter()
  87. {
  88. }
  89. public function testDefinedDefaultFilter()
  90. {
  91. }
  92. public function testRtrimFilter()
  93. {
  94. }
  95. public function testLtrimFilter()
  96. {
  97. }
  98. public function testRepeatFunc()
  99. {
  100. }
  101. public function testRegexReplace()
  102. {
  103. }
  104. public function testUrlFunc()
  105. {
  106. }
  107. public function testEvaluateFunc()
  108. {
  109. }
  110. public function testDump()
  111. {
  112. }
  113. public function testGistFunc()
  114. {
  115. }
  116. public function testRandomStringFunc()
  117. {
  118. }
  119. public function testPadFilter()
  120. {
  121. }
  122. public function testArrayFunc()
  123. {
  124. $this->assertSame('this is my text',
  125. $this->twig_ext->regexReplace('<p>this is my text</p>', '(<\/?p>)', ''));
  126. $this->assertSame('<i>this is my text</i>',
  127. $this->twig_ext->regexReplace('<p>this is my text</p>', ['(<p>)','(<\/p>)'], ['<i>','</i>']));
  128. }
  129. public function testArrayKeyValue()
  130. {
  131. $this->assertSame(['meat' => 'steak'],
  132. $this->twig_ext->arrayKeyValueFunc('meat', 'steak'));
  133. $this->assertSame(['fruit' => 'apple', 'meat' => 'steak'],
  134. $this->twig_ext->arrayKeyValueFunc('meat', 'steak', ['fruit' => 'apple']));
  135. }
  136. public function stringFunc()
  137. {
  138. }
  139. public function testRangeFunc()
  140. {
  141. $hundred = [];
  142. for($i = 0; $i <= 100; $i++) { $hundred[] = $i; }
  143. $this->assertSame([0], $this->twig_ext->rangeFunc(0, 0));
  144. $this->assertSame([0, 1, 2], $this->twig_ext->rangeFunc(0, 2));
  145. $this->assertSame([0, 5, 10, 15], $this->twig_ext->rangeFunc(0, 16, 5));
  146. // default (min 0, max 100, step 1)
  147. $this->assertSame($hundred, $this->twig_ext->rangeFunc());
  148. // 95 items, starting from 5, (min 5, max 100, step 1)
  149. $this->assertSame(array_slice($hundred, 5), $this->twig_ext->rangeFunc(5));
  150. // reversed range
  151. $this->assertSame(array_reverse($hundred), $this->twig_ext->rangeFunc(100, 0));
  152. $this->assertSame([4, 2, 0], $this->twig_ext->rangeFunc(4, 0, 2));
  153. }
  154. }