/tests/Jyxo/StringUtilTest.php
PHP | 264 lines | 153 code | 34 blank | 77 comment | 5 complexity | 7b8089818b71523dff18a937a155bb12 MD5 | raw file
- <?php declare(strict_types = 1);
- /**
- * Jyxo PHP Library
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file license.txt.
- * It is also available through the world-wide-web at this URL:
- * https://github.com/jyxo/php/blob/master/license.txt
- */
- namespace Jyxo;
- use PHPUnit\Framework\TestCase;
- use function count;
- use function html_entity_decode;
- use function mb_strlen;
- use function preg_quote;
- use function preg_split;
- use function sprintf;
- use function strlen;
- use function strtr;
- use const ENT_COMPAT;
- use const ENT_NOQUOTES;
- use const ENT_QUOTES;
- /**
- * String processing test.
- *
- * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
- * @license https://github.com/jyxo/php/blob/master/license.txt
- * @author Jakub Tománek
- * @author Jaroslav Hanslík
- * @author Ondřej Nešpor
- */
- class StringUtilTest extends TestCase
- {
- /**
- * Tests string trimming.
- */
- public function testCut(): void
- {
- // Trim on space
- $this->assertEquals('žluťoučký kůň...', $this->checkStringCut('žluťoučký kůň příšerně úpěl ďábelské ódy'));
- // Trim on period
- $this->assertEquals('žluťoučký kůň...', $this->checkStringCut('žluťoučký kůň.Příšerně úpěl ďábelské ódy'));
- // Trim on period and space
- $this->assertEquals('žluťoučký kůň...', $this->checkStringCut('žluťoučký kůň. Příšerně úpěl ďábelské ódy'));
- // Trim on comma
- $this->assertEquals('žluťoučký kůň...', $this->checkStringCut('žluťoučký kůň,příšerně úpěl ďábelské ódy'));
- // Trim on semicolon
- $this->assertEquals('žluťoučký kůň...', $this->checkStringCut('žluťoučký kůň;příšerně úpěl ďábelské ódy'));
- // Word boundary just at the end
- $this->assertEquals('abcdefghijklm...', $this->checkStringCut('abcdefghijklmno pqrst'));
- $this->assertEquals('abcdefghijklm...', $this->checkStringCut('abcdefghijklmn opqrst'));
- $this->assertEquals('abcdefghijklm...', $this->checkStringCut('abcdefghijklm nopqrst'));
- // No word boundaries
- $this->assertEquals('abcdefghijklm...', $this->checkStringCut('abcdefghijklmnopqrstuvwxyz'));
- // Etc as HTML entity
- $this->assertEquals(
- 'žluťoučký kůň…',
- $this->checkStringCut('žluťoučký kůň příšerně úpěl ďábelské ódy', 14, '…')
- );
- // Short
- $shorty = '1234567890';
- $this->assertEquals($shorty, $this->checkStringCut($shorty));
- $this->assertEquals('12...', $this->checkStringCut($shorty, 5));
- }
- /**
- * Tests word trimming.
- */
- public function testCutWords(): void
- {
- $this->assertEquals(
- 'žluťoučký kůň příšerně úpěl ďábelské ódy',
- $this->checkStringWordCut('žluťoučký kůň příšerně úpěl ďábelské ódy', 10)
- );
- $this->assertEquals(
- 'žluťo... kůň příšerně úpěl ďábelské ódy',
- $this->checkStringWordCut('žluťoučký kůň příšerně úpěl ďábelské ódy', 8)
- );
- $this->assertEquals(
- 'žl... kůň př... úpěl ďá... ódy',
- $this->checkStringWordCut('žluťoučký kůň příšerně úpěl ďábelské ódy', 5)
- );
- // Word boundary just at the end
- $this->assertEquals('abcdefghijk... pqrst', $this->checkStringWordCut('abcdefghijklmno pqrst', 14));
- $this->assertEquals('abcdefghijklmn opqrst', $this->checkStringWordCut('abcdefghijklmn opqrst', 14));
- $this->assertEquals('abcdefghijklm nopqrst', $this->checkStringWordCut('abcdefghijklm nopqrst', 14));
- // Short
- $shorty = '12345678';
- $this->assertEquals($shorty, $this->checkStringWordCut($shorty));
- $this->assertEquals('12...', $this->checkStringWordCut($shorty, 5));
- }
- /**
- * Test the crc generator.
- */
- public function testCrc(): void
- {
- $this->assertSame(-662733300, StringUtil::crc('test'));
- $this->assertSame(-33591962, StringUtil::crc('žluťoučký kůň příšerně úpěl ďábelské ódy'));
- }
- /**
- * Tests the random string generator.
- */
- public function testRandom(): void
- {
- for ($i = 1; $i <= 32; $i++) {
- $random = StringUtil::random($i);
- $this->assertEquals($i, strlen($random));
- $this->assertMatchesRegularExpression('~^[a-z0-9]+$~i', $random);
- }
- }
- /**
- * Tests line ending conversions.
- */
- public function testFixLineEnding(): void
- {
- $tests = [
- "test\r\nžlutý\r\n",
- "test\ržlutý\r",
- "test\r\nžlutý\r",
- "test\nžlutý\r",
- "test\nžlutý\r\n",
- ];
- // No line ending given
- foreach ($tests as $test) {
- $this->assertEquals("test\nžlutý\n", StringUtil::fixLineEnding($test));
- $this->assertNotEquals($test, StringUtil::fixLineEnding($test));
- }
- $this->assertEquals("test\nžlutý\n", StringUtil::fixLineEnding("test\nžlutý\n"));
- // Line ending given
- foreach ($tests as $test) {
- foreach (["\n", "\r", "\r\n"] as $ending) {
- $this->assertEquals(sprintf('test%1$sžlutý%1$s', $ending), StringUtil::fixLineEnding($test, $ending));
- $this->assertNotEquals("test\nžlutý\r\n", StringUtil::fixLineEnding($test, $ending));
- }
- }
- }
- /**
- * Tests email address obfuscation.
- */
- public function testObfuscateEmail(): void
- {
- $email = 'example@example.com';
- $this->assertEquals('example@example.com', StringUtil::obfuscateEmail($email));
- $this->assertEquals('example@<!---->example.com', StringUtil::obfuscateEmail($email, true));
- }
- /**
- * Tests first letter lowercase.
- */
- public function testLcfirst(): void
- {
- $this->assertEquals('žlutý kůň', StringUtil::lcfirst('Žlutý kůň'));
- $this->assertEquals('žlutý kůň', StringUtil::lcfirst('žlutý kůň'));
- }
- /**
- * Tests special HTML characters escaping.
- */
- public function testEscape(): void
- {
- $this->assertEquals('test & test', StringUtil::escape('test & test'));
- $this->assertEquals('"test" & 'test'', StringUtil::escape('"test" & \'test\''));
- $this->assertEquals('"test" & \'test\'', StringUtil::escape('"test" & \'test\'', ENT_COMPAT));
- $this->assertEquals('"test" & \'test\'', StringUtil::escape('"test" & \'test\'', ENT_NOQUOTES));
- $this->assertEquals('test & test', StringUtil::escape('test & test'));
- $this->assertEquals('test &amp; test', StringUtil::escape('test & test', ENT_QUOTES, true));
- }
- /**
- * Tests byte size conversion.
- */
- public function testFormatBytes(): void
- {
- $this->assertEquals('11 B', StringUtil::formatBytes(10.5));
- $this->assertEquals('11 B', StringUtil::formatBytes(10.5, '.'));
- $this->assertEquals('11 B', StringUtil::formatBytes(10.5, '.', ','));
- $this->assertEquals('1,0 GB', StringUtil::formatBytes(1073741824));
- $this->assertEquals('1.0 GB', StringUtil::formatBytes(1073741824, '.'));
- $this->assertEquals('10,240.0 PB', StringUtil::formatBytes(11805916207174113034240, '.', ','));
- }
- /**
- * Checks one string.
- *
- * @param string $string Input string
- * @param int $max Max length
- * @param string $etc "Etc" definition
- * @return string
- */
- private function checkStringWordCut(string $string, int $max = 8, string $etc = '...'): string
- {
- $cut = StringUtil::cutWords($string, $max, $etc);
- // … has length of 1
- $cut2 = strtr(html_entity_decode($cut, ENT_COMPAT, 'utf-8'), ['…' => '.']);
- $words = preg_split('~\\s+~', $string);
- $trimmedWords = preg_split('~\\s+~', $cut2);
- $this->assertEquals(count($trimmedWords), count($words));
- foreach ($words as $i => $word) {
- if (mb_strlen($word, 'utf-8') <= $max) {
- $this->assertEquals($word, $trimmedWords[$i], 'Word trimmed even though it was short enough');
- } else {
- $this->assertLessThanOrEqual($max, mb_strlen($trimmedWords[$i], 'utf-8'));
- $this->assertMatchesRegularExpression(
- '~' . preg_quote($etc === '…' ? '.' : $etc) . '$~',
- $trimmedWords[$i],
- 'String does not end with ' . $etc
- );
- }
- }
- return $cut;
- }
- /**
- * Checks one string.
- *
- * @param string $string Input string
- * @param int $max Max length
- * @param string $etc "Etc" definition
- * @return string
- */
- private function checkStringCut(string $string, int $max = 16, string $etc = '...'): string
- {
- $cut = StringUtil::cut($string, $max, $etc);
- // … has length of 1
- $cutLength = mb_strlen(strtr(html_entity_decode($cut, ENT_COMPAT, 'utf-8'), ['…' => '.']));
- $this->assertLessThanOrEqual($max, $cutLength, 'String is longer');
- if (mb_strlen($string, 'utf-8') <= $max) {
- $this->assertEquals($string, $cut, 'String trimmed even though it was short enough');
- } else {
- $this->assertMatchesRegularExpression('~' . preg_quote($etc) . '$~', $cut, 'String does not end with ' . $etc);
- }
- return $cut;
- }
- }