This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #328 from ergebnis/feature/string-provider
Enhancement: Implement StringProvider
- Loading branch information
Showing
8 changed files
with
338 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2017-2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/test-util | ||
*/ | ||
|
||
namespace Ergebnis\Test\Util\DataProvider; | ||
|
||
use Ergebnis\Test\Util; | ||
|
||
final class StringProvider | ||
{ | ||
use Util\Helper; | ||
|
||
/** | ||
* @return \Generator<string, array{0: string}> | ||
*/ | ||
public static function arbitrary(): \Generator | ||
{ | ||
yield from self::provideWhereNot(self::values(), static function (string $value): bool { | ||
return '' === \trim($value); | ||
}); | ||
} | ||
|
||
/** | ||
* @return \Generator<string, array{0: string}> | ||
*/ | ||
public static function blank(): \Generator | ||
{ | ||
yield from self::provideWhere(self::values(), static function (string $value): bool { | ||
return '' === \trim($value) | ||
&& '' !== $value; | ||
}); | ||
} | ||
|
||
/** | ||
* @return \Generator<string, array{0: string}> | ||
*/ | ||
public static function empty(): \Generator | ||
{ | ||
yield from self::provideWhere(self::values(), static function (string $value): bool { | ||
return '' === $value; | ||
}); | ||
} | ||
|
||
/** | ||
* @return \Generator<string, array{0: string}> | ||
*/ | ||
public static function untrimmed(): \Generator | ||
{ | ||
yield from self::provideWhere(self::values(), static function (string $value): bool { | ||
return \trim($value) !== $value | ||
&& '' !== \trim($value); | ||
}); | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
private static function values(): array | ||
{ | ||
$faker = self::faker(); | ||
|
||
$arbitraryValues = [ | ||
'string-arbitrary-sentence' => $faker->sentence, | ||
'string-arbitrary-word' => $faker->word, | ||
]; | ||
|
||
$whitespaceCharacters = self::whitespaceCharacters(); | ||
|
||
/** @var array<string, string> $blankValues */ | ||
$blankValues = \array_combine( | ||
\array_map(static function (string $key): string { | ||
return \sprintf( | ||
'string-blank-%s', | ||
$key | ||
); | ||
}, \array_keys($whitespaceCharacters)), | ||
$whitespaceCharacters | ||
); | ||
|
||
$emptyValues = [ | ||
'string-empty' => '', | ||
]; | ||
|
||
/** @var array<string, string> $untrimmedValues */ | ||
$untrimmedValues = \array_combine( | ||
\array_map(static function (string $key): string { | ||
return \sprintf( | ||
'string-untrimmed-%s', | ||
$key | ||
); | ||
}, \array_keys($whitespaceCharacters)), | ||
\array_map(static function (string $whitespaceCharacter) use ($faker): string { | ||
return \sprintf( | ||
'%s%s%s', | ||
\str_repeat( | ||
$whitespaceCharacter, | ||
$faker->numberBetween(1, 5) | ||
), | ||
$faker->word, | ||
\str_repeat( | ||
$whitespaceCharacter, | ||
$faker->numberBetween(1, 5) | ||
) | ||
); | ||
}, $whitespaceCharacters) | ||
); | ||
|
||
return \array_merge( | ||
$arbitraryValues, | ||
$blankValues, | ||
$emptyValues, | ||
$untrimmedValues | ||
); | ||
} | ||
|
||
private static function whitespaceCharacters(): array | ||
{ | ||
return [ | ||
'carriage-return' => "\r", | ||
'line-feed' => "\n", | ||
'space' => ' ', | ||
'tab' => "\t", | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2017-2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/test-util | ||
*/ | ||
|
||
namespace Ergebnis\Test\Util\Test\Unit\DataProvider; | ||
|
||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\Test\Util\DataProvider\StringProvider | ||
*/ | ||
final class StringProviderTest extends Framework\TestCase | ||
{ | ||
/** | ||
* @dataProvider \Ergebnis\Test\Util\DataProvider\StringProvider::arbitrary() | ||
* | ||
* @param string $value | ||
*/ | ||
public function testArbitraryProvidesString(string $value): void | ||
{ | ||
self::assertNotSame('', \trim($value)); | ||
} | ||
|
||
/** | ||
* @dataProvider \Ergebnis\Test\Util\DataProvider\StringProvider::blank() | ||
* | ||
* @param string $value | ||
*/ | ||
public function testBlankProvidesBlankString(string $value): void | ||
{ | ||
self::assertSame('', \trim($value)); | ||
self::assertNotSame('', $value); | ||
} | ||
|
||
/** | ||
* @dataProvider \Ergebnis\Test\Util\DataProvider\StringProvider::empty() | ||
* | ||
* @param string $value | ||
*/ | ||
public function testEmptyProvidesEmptyString(string $value): void | ||
{ | ||
self::assertSame('', $value); | ||
} | ||
|
||
/** | ||
* @dataProvider \Ergebnis\Test\Util\DataProvider\StringProvider::untrimmed() | ||
* | ||
* @param string $value | ||
*/ | ||
public function testUntrimmedProvidesUntrimmedString(string $value): void | ||
{ | ||
self::assertNotSame(\trim($value), $value); | ||
self::assertNotSame('', $value); | ||
self::assertNotSame('', \trim($value)); | ||
} | ||
} |