FormattingTest.php 9.2 KB

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