[Miscellaneous] [Regex] Implement data providers in tests

This commit is contained in:
Meritoo
2019-12-18 20:32:21 +01:00
parent ddb3f0a544
commit 5cd58aec25
3 changed files with 227 additions and 48 deletions

View File

@@ -889,8 +889,12 @@ class Miscellaneous
* @param string $separator The separator which divides elements of string
* @return array
*/
public static function getStringElements($string, $separator)
public static function getStringElements(string $string, string $separator): array
{
if (empty($string) || empty($separator)) {
return [];
}
$matches = [];
$pattern = sprintf('|[^\%s]+|', $separator);
$matchCount = preg_match_all($pattern, $string, $matches);
@@ -909,21 +913,15 @@ class Miscellaneous
* @param string $separator The separator which divides elements of string
* @return null|string
*/
public static function getLastElementOfString($string, $separator)
public static function getLastElementOfString($string, $separator): ?string
{
$elements = self::getStringElements($string, $separator);
/*
* No elements?
* Nothing to do
*/
if (empty($elements)) {
return null;
}
$element = Arrays::getLastElement($elements);
return trim($element);
return Arrays::getLastElement($elements);
}
/**

View File

@@ -94,22 +94,16 @@ class MiscellaneousTest extends BaseTestCase
self::assertEquals($withoutExtension, Miscellaneous::getFileNameWithoutExtension($fileName));
}
public function testGetFileNameFromPath(): void
/**
* @param string $description Description of test
* @param string $path A path that contains file name
* @param string $expected Expected file name
*
* @dataProvider provideFilePath
*/
public function testGetFileNameFromPath(string $description, string $path, string $expected): void
{
// Path with file
self::assertEquals('sit.amet.JPG', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/sit.amet.JPG'));
// Path with complicated name of file
self::assertEquals('this-1_2 3 & my! 4+file.jpg', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/this-1_2 3 & my! 4+file.jpg'));
// Path without file
self::assertEquals('', Miscellaneous::getFileNameFromPath('lorem/ipsum-dolor/sit-amet'));
// Path with a dot "." in name of directory
self::assertEquals('sit.amet.JPG', Miscellaneous::getFileNameFromPath('lorem/ipsum.dolor/sit.amet.JPG'));
// Relative path
self::assertEquals('sit.amet.JPG', Miscellaneous::getFileNameFromPath('lorem/ipsum/../dolor/sit.amet.JPG'));
static::assertEquals($expected, Miscellaneous::getFileNameFromPath($path), $description);
}
public function testGetUniqueFileName()
@@ -480,12 +474,21 @@ class MiscellaneousTest extends BaseTestCase
self::assertEquals('1.75 MB', Miscellaneous::getHumanReadableSize(1024 * 1024 * 1.75));
}
public function testGetLastElementOfString()
{
self::assertEquals('elit', Miscellaneous::getLastElementOfString($this->stringCommaSeparated, ' '));
self::assertEquals('consectetur adipiscing elit', Miscellaneous::getLastElementOfString($this->stringCommaSeparated, ','));
self::assertEquals(null, Miscellaneous::getLastElementOfString($this->stringCommaSeparated, ';'));
self::assertEquals(null, Miscellaneous::getLastElementOfString($this->stringCommaSeparated, '.'));
/**
* @param string $description
* @param string $string
* @param string $separator
* @param string|null $expected
*
* @dataProvider provideLastElementOfString
*/
public function testGetLastElementOfString(
string $description,
string $string,
string $separator,
?string $expected
): void {
self::assertEquals($expected, Miscellaneous::getLastElementOfString($string, $separator), $description);
}
public function testTrimSmart()
@@ -554,15 +557,21 @@ class MiscellaneousTest extends BaseTestCase
self::assertEquals(sprintf('%s.%s', $fileName, 'txt'), Miscellaneous::includeFileExtension($fileName, 'txt'));
}
public function testGetStringElements()
{
$elements = [
'Lorem ipsum dolor sit amet',
' consectetur adipiscing elit',
];
self::assertEquals($elements, Miscellaneous::getStringElements($this->stringCommaSeparated, ','));
self::assertEquals([], Miscellaneous::getStringElements($this->stringCommaSeparated, ';'));
/**
* @param string $description
* @param string $string
* @param string $separator
* @param array $expected
*
* @dataProvider provideStringElements
*/
public function testGetStringElements(
string $description,
string $string,
string $separator,
array $expected
): void {
self::assertEquals($expected, Miscellaneous::getStringElements($string, $separator), $description);
}
public function testGetStringWithoutLastElement()
@@ -1566,6 +1575,145 @@ class MiscellaneousTest extends BaseTestCase
];
}
public function provideFilePath(): ?Generator
{
yield[
'Path with file',
'lorem/ipsum-dolor/sit.amet.JPG',
'sit.amet.JPG',
];
yield[
'Path with complicated name of file',
'lorem/ipsum-dolor/this-1_2 3 & my! 4+file.jpg',
'this-1_2 3 & my! 4+file.jpg',
];
yield[
'Path without file',
'lorem/ipsum-dolor/sit-amet',
'',
];
yield[
'Path with a dot "." in name of directory',
'lorem/ipsum.dolor/sit.amet.JPG',
'sit.amet.JPG',
];
yield[
'Relative path',
'lorem/ipsum/../dolor/sit.amet.JPG',
'sit.amet.JPG',
];
}
public function provideStringElements(): ?Generator
{
yield[
'An empty string',
'',
'',
[],
];
yield[
'One-character string',
'a',
',',
[],
];
yield[
'String without given separator',
'abc',
',',
[],
];
yield[
'Simple, short string',
'a, b, c',
',',
[
'a',
' b',
' c',
],
];
yield[
'A sentence',
'Lorem ipsum - dolor sit - amet, consectetur adipiscing - elit.',
'-',
[
'Lorem ipsum ',
' dolor sit ',
' amet, consectetur adipiscing ',
' elit.',
],
];
yield[
'A class namespace',
'This\\Is\\My\\Class\\For\\Testing',
'\\',
[
'This',
'Is',
'My',
'Class',
'For',
'Testing',
],
];
}
public function provideLastElementOfString(): ?Generator
{
yield[
'An empty string',
'',
'',
null,
];
yield[
'One-character string',
'a',
',',
null,
];
yield[
'String without given separator',
'abc',
',',
null,
];
yield[
'Simple, short string',
'a, b, c',
',',
' c',
];
yield[
'A sentence',
'Lorem ipsum - dolor sit - amet, consectetur adipiscing - elit.',
'-',
' elit.',
];
yield[
'A class namespace',
'This\\Is\\My\\Class\\For\\Testing',
'\\',
'Testing',
];
}
/**
* {@inheritdoc}
*/

View File

@@ -244,16 +244,16 @@ class RegexTest extends BaseTestCase
self::assertTrue(Regex::contains($this->simpleText, 'l'));
}
public function testIsFileName(): void
/**
* @param string $description Description of test
* @param string $fileName
* @param bool $expected Expected result
*
* @dataProvider provideFileName
*/
public function testIsFileName(string $description, string $fileName, bool $expected): void
{
$filePath = __FILE__;
$directoryPath = dirname($filePath);
self::assertTrue(Regex::isFileName($filePath));
self::assertTrue(Regex::isFileName('this-1_2 3 & my! 4+file.jpg'));
self::assertFalse(Regex::isFileName($directoryPath));
self::assertTrue(Regex::isFileName('directory1/directory2/this-1_2 3 & my! 4+file.jpg'));
static::assertSame($expected, Regex::isFileName($fileName), $description);
}
public function testIsQuoted()
@@ -2214,6 +2214,39 @@ class RegexTest extends BaseTestCase
];
}
public function provideFileName(): ?Generator
{
yield[
'An empty string',
'',
false,
];
yield[
'Path of this file, of file with test case',
__DIR__,
false,
];
yield[
'Name of this file, of file with test case',
__FILE__,
true,
];
yield[
'Complicated name of file',
'this-1_2 3 & my! 4+file.jpg',
true,
];
yield[
'Complicated name of file',
'directory1/directory2/this-1_2 3 & my! 4+file.jpg',
true,
];
}
/**
* {@inheritdoc}
*/