FormattingTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace App\Tests\Util;
  19. use App\Util\Exception\ServerException;
  20. use App\Util\Formatting;
  21. use App\Util\TemporaryFile;
  22. use InvalidArgumentException;
  23. use Jchook\AssertThrows\AssertThrows;
  24. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  25. class FormattingTest extends WebTestCase
  26. {
  27. use AssertThrows;
  28. public function testTwigRenderString()
  29. {
  30. static::bootKernel();
  31. // test container allows us to get the private twig service
  32. $container = self::$kernel->getContainer()->get('test.service_container');
  33. $twig = $container->get('twig');
  34. Formatting::setTwig($twig);
  35. static::assertSame('<a href="/test"></a>', Formatting::twigRenderString('<a href="{{ref}}"></a>', ['ref' => '/test']));
  36. }
  37. public function testTwigRenderFile()
  38. {
  39. try {
  40. static::bootKernel();
  41. // test container allows us to get the private twig service
  42. $container = self::$kernel->getContainer()->get('test.service_container');
  43. $twig = $container->get('twig');
  44. Formatting::setTwig($twig);
  45. $dir = INSTALLDIR . '/templates/';
  46. $temp = new TemporaryFile(['directory' => $dir, 'prefix' => '', 'suffix' => '.html.twig', 'permission' => 0777]);
  47. $temp->write('<a href="{{ref}}"></a>');
  48. static::assertSame('<a href="/test"></a>', Formatting::twigRenderFile(Formatting::removePrefix($temp->getRealPath(), $dir), ['ref' => '/test']));
  49. } finally {
  50. unset($temp);
  51. }
  52. }
  53. public function testNormalizePath()
  54. {
  55. static::assertSame('', Formatting::normalizePath(''));
  56. static::assertSame('foo', Formatting::normalizePath('foo'));
  57. static::assertSame('foo/', Formatting::normalizePath('foo//'));
  58. static::assertSame('/foo/bar', Formatting::normalizePath('/foo/bar'));
  59. static::assertSame('/foo/bar', Formatting::normalizePath('\\foo\\bar'));
  60. static::assertSame('/foo/bar', Formatting::normalizePath('\\foo/bar'));
  61. static::assertSame('/foo/bar/', Formatting::normalizePath('/foo\\bar\\'));
  62. }
  63. public function testModuleFromPath()
  64. {
  65. static::assertNull(Formatting::moduleFromPath(''));
  66. static::assertNull(Formatting::moduleFromPath('/'));
  67. static::assertNull(Formatting::moduleFromPath('/var/www/social/src/Kernel.php'));
  68. static::assertSame('foo', Formatting::moduleFromPath('/var/www/social/plugins/foo/Foo.php'));
  69. static::assertSame('foo', Formatting::moduleFromPath('/var/www/social/components/foo/Foo.php'));
  70. static::assertThrows(ServerException::class, fn () => Formatting::moduleFromPath('/components/'));
  71. }
  72. public function testStartsWithString()
  73. {
  74. static::assertTrue(Formatting::startsWith('foobar', 'foo'));
  75. static::assertTrue(Formatting::startsWith('foo', 'foo'));
  76. static::assertFalse(Formatting::startsWith('bar', 'foo'));
  77. static::assertFalse(Formatting::startsWith('', 'foo'));
  78. static::assertFalse(Formatting::startsWith('fo', 'foo'));
  79. static::assertFalse(Formatting::startsWith('oo', 'foo'));
  80. }
  81. public function testStartsWithArray()
  82. {
  83. static::assertTrue(Formatting::startsWith(['foobar', 'fooquux'], 'foo'));
  84. static::assertTrue(Formatting::startsWith(['foo', 'foo'], 'foo'));
  85. static::assertTrue(Formatting::startsWith(['foo1', 'foo2', 'foo3'], 'foo'));
  86. static::assertFalse(Formatting::startsWith(['foobar', 'barquux'], 'foo'));
  87. static::assertFalse(Formatting::startsWith(['', '', ''], 'foo'));
  88. static::assertFalse(Formatting::startsWith(['fo', 'fo'], 'foo'));
  89. static::assertFalse(Formatting::startsWith(['oo', 'oo'], 'foo'));
  90. }
  91. public function testEndsWithString()
  92. {
  93. static::assertTrue(Formatting::endsWith('foobar', 'bar'));
  94. static::assertTrue(Formatting::endsWith('foo', 'foo'));
  95. static::assertFalse(Formatting::endsWith('bar', 'foo'));
  96. static::assertFalse(Formatting::endsWith('', 'foo'));
  97. static::assertFalse(Formatting::endsWith('fo', 'foo'));
  98. static::assertFalse(Formatting::endsWith('oo', 'foo'));
  99. }
  100. public function testEndsWithArray()
  101. {
  102. static::assertTrue(Formatting::endsWith(['foobar', 'quuxbar'], 'bar'));
  103. static::assertTrue(Formatting::endsWith(['foo', 'foo'], 'foo'));
  104. static::assertTrue(Formatting::endsWith(['qwefoo', 'zxcfoo', 'asdfoo'], 'foo'));
  105. static::assertFalse(Formatting::endsWith(['barfoo', 'quuxbar'], 'foo'));
  106. static::assertFalse(Formatting::endsWith(['', '', ''], 'foo'));
  107. static::assertFalse(Formatting::endsWith(['fo', 'fo'], 'foo'));
  108. static::assertFalse(Formatting::endsWith(['oo', 'oo'], 'foo'));
  109. }
  110. public function testRemovePrefix()
  111. {
  112. static::assertSame('', Formatting::removePrefix('', ''));
  113. static::assertSame('', Formatting::removePrefix('', 'foo'));
  114. static::assertSame('foo', Formatting::removePrefix('foo', ''));
  115. static::assertSame('', Formatting::removePrefix('foo', 'foo'));
  116. static::assertSame('foo', Formatting::removePrefix('foo', 'bar'));
  117. static::assertSame('foo', Formatting::removePrefix('barfoo', 'bar'));
  118. static::assertSame('foobar', Formatting::removePrefix('foobar', 'bar'));
  119. static::assertSame('foobar', Formatting::removePrefix('barfoobar', 'bar'));
  120. }
  121. public function testRemoveSuffix()
  122. {
  123. static::assertSame('', Formatting::removeSuffix('', ''));
  124. static::assertSame('', Formatting::removeSuffix('', 'foo'));
  125. static::assertSame('foo', Formatting::removeSuffix('foo', ''));
  126. static::assertSame('', Formatting::removeSuffix('foo', 'foo'));
  127. static::assertSame('foo', Formatting::removeSuffix('foo', 'bar'));
  128. static::assertSame('barfoo', Formatting::removeSuffix('barfoo', 'bar'));
  129. static::assertSame('foo', Formatting::removeSuffix('foobar', 'bar'));
  130. static::assertSame('barfoo', Formatting::removeSuffix('barfoobar', 'bar'));
  131. }
  132. public function testCamelCaseToSnakeCase()
  133. {
  134. static::assertSame('foo_bar', Formatting::camelCaseToSnakeCase('FooBar'));
  135. static::assertSame('foo_bar_quux', Formatting::camelCaseToSnakeCase('FooBarQuux'));
  136. static::assertSame('foo_bar', Formatting::camelCaseToSnakeCase('foo_bar'));
  137. static::assertSame('', Formatting::camelCaseToSnakeCase(''));
  138. }
  139. public function testSnakeCaseToCamelCase()
  140. {
  141. static::assertSame('FooBar', Formatting::snakeCaseToCamelCase('foo_bar'));
  142. static::assertSame('FooBarQuux', Formatting::snakeCaseToCamelCase('foo_bar_quux'));
  143. static::assertSame('FooBar', Formatting::snakeCaseToCamelCase('FooBar'));
  144. static::assertSame('', Formatting::snakeCaseToCamelCase(''));
  145. }
  146. public function testIndent()
  147. {
  148. static::assertSame(' foo', Formatting::indent('foo'));
  149. static::assertSame(' foo', Formatting::indent('foo', level: 1, count: 2));
  150. static::assertSame(" foo\n bar", Formatting::indent("foo\nbar"));
  151. static::assertSame(" foo\n bar", Formatting::indent(['foo', 'bar']));
  152. static::assertThrows(InvalidArgumentException::class, fn () => Formatting::indent(1));
  153. }
  154. public function testToString()
  155. {
  156. static::assertThrows(\Exception::class, function () { return Formatting::toString('foo', ''); });
  157. static::assertSame('', Formatting::toString(''));
  158. static::assertSame('foo', Formatting::toString('foo'));
  159. static::assertSame('42', Formatting::toString(42));
  160. static::assertSame('42, 1', Formatting::toString([42.0, 1]));
  161. static::assertSame('42 1', Formatting::toString([42.0, 1], Formatting::JOIN_BY_SPACE));
  162. }
  163. public function testToArray()
  164. {
  165. static::assertThrows(\Exception::class, function () { return Formatting::toArray('foo', $a, ''); });
  166. static::assertTrue(Formatting::toArray('', $a));
  167. static::assertSame([], $a);
  168. static::assertTrue(Formatting::toArray('foo', $a));
  169. static::assertSame(['foo'], $a);
  170. static::assertTrue(Formatting::toArray('foo, bar', $a));
  171. static::assertSame(['foo', 'bar'], $a);
  172. static::assertTrue(Formatting::toArray('foo bar', $a, Formatting::SPLIT_BY_SPACE));
  173. static::assertSame(['foo', 'bar'], $a);
  174. static::assertFalse(Formatting::toArray('foo,', $a));
  175. static::assertTrue(Formatting::toArray('foo, ', $a));
  176. static::assertSame(['foo', ''], $a);
  177. }
  178. }