Skip to content

Commit

Permalink
Enhancement: Implement StringProvider::uuid()
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Feb 10, 2022
1 parent e1cff20 commit ae08391
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Expand Up @@ -27,6 +27,7 @@
$license->save();

$config = PhpCsFixer\Config\Factory::fromRuleSet(new PhpCsFixer\Config\RuleSet\Php74($license->header()), [
'mb_str_functions' => false,
'strict_comparison' => false,
]);

Expand Down
15 changes: 13 additions & 2 deletions CHANGELOG.md
Expand Up @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

For a full diff see [`1.1.0...main`][1.1.0...main].
For a full diff see [`1.2.0...main`][1.2.0...main].

## [`1.2.0`][1.2.0]

For a full diff see [`1.1.0...1.2.0`][1.1.0...1.2.0].

### Added

- Added `StringProvider::uuid()` ([#40]), by [@localheinz]

## [`1.1.0`][1.1.0]

Expand All @@ -27,12 +35,15 @@ For a full diff see [`a5f2657...1.0.0`][a5f2657...1.0.0].

[1.0.0]: https://github.com/ergebnis/data-provider/releases/tag/1.0.0
[1.1.0]: https://github.com/ergebnis/data-provider/releases/tag/1.1.0
[1.2.0]: https://github.com/ergebnis/data-provider/releases/tag/1.2.0

[a5f2657...1.0.0]: https://github.com/ergebnis/data-provider/compare/a5f2657...1.0.0
[1.0.0...1.1.0]: https://github.com/ergebnis/data-provider/compare/1.0.0...1.1.0
[1.1.0...main]: https://github.com/ergebnis/data-provider/compare/1.1.0...main
[1.1.0...1.2.0]: https://github.com/ergebnis/data-provider/compare/1.1.0...1.2.0
[1.2.0...main]: https://github.com/ergebnis/data-provider/compare/1.2.0...main

[#1]: https://github.com/ergebnis/data-provider/pull/1
[#21]: https://github.com/ergebnis/data-provider/pull/21
[#40]: https://github.com/ergebnis/data-provider/pull/40

[@localheinz]: https://github.com/localheinz
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -115,9 +115,11 @@ For examples, see [`Ergebnis\DataProvider\Test\Unit\ResourceProviderTest`](test/
* `empty()` provides an empty `string`
* `trimmed()` provides non-empty, non-blank `strings` without leading and trailing whitespace
* `untrimmed()` provides non-empty, non-blank `string`s with additional leading and trailing whitespace
* `uuid()` provides lower- and upper-case UUID `string`s
* `withWhitespace()` provides non-empty, non-blank, trimmed `string`s containing whitespace

For examples, see [`Ergebnis\DataProvider\Test\Unit\StringProviderTest`](test/Unit/StringProviderTest.php).

## Changelog

Please have a look at [`CHANGELOG.md`](CHANGELOG.md).
Expand Down
5 changes: 3 additions & 2 deletions psalm-baseline.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.18.1@dda05fa913f4dc6eb3386f2f7ce5a45d37a71bcb">
<files psalm-version="4.20.0@f82a70e7edfc6cf2705e9374c8a0b6a974a779ed">
<file src="src/AbstractProvider.php">
<MixedAssignment occurrences="1">
<code>$value</code>
Expand Down Expand Up @@ -53,7 +53,8 @@
</MoreSpecificReturnType>
</file>
<file src="src/StringProvider.php">
<MoreSpecificReturnType occurrences="6">
<MoreSpecificReturnType occurrences="7">
<code>\Generator&lt;string, array{0: string}&gt;</code>
<code>\Generator&lt;string, array{0: string}&gt;</code>
<code>\Generator&lt;string, array{0: string}&gt;</code>
<code>\Generator&lt;string, array{0: string}&gt;</code>
Expand Down
16 changes: 16 additions & 0 deletions src/StringProvider.php
Expand Up @@ -66,6 +66,16 @@ public static function untrimmed(): \Generator
});
}

/**
* @return \Generator<string, array{0: string}>
*/
public static function uuid(): \Generator
{
yield from self::provideDataForValuesWhere(self::values(), static function (string $value): bool {
return 1 === \preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i', $value);
});
}

/**
* @return \Generator<string, array{0: string}>
*/
Expand Down Expand Up @@ -106,6 +116,11 @@ private static function values(): array
'string-empty' => '',
];

$uuidValues = [
'string-uuid-case-lower' => \strtolower($faker->uuid()),
'string-uuid-case-upper' => \strtoupper($faker->uuid()),
];

/** @var array<string, string> $untrimmedValues */
$untrimmedValues = \array_combine(
\array_map(static function (string $key): string {
Expand Down Expand Up @@ -154,6 +169,7 @@ private static function values(): array
$blankValues,
$emptyValues,
$untrimmedValues,
$uuidValues,
$withWhitespaceValues,
);
}
Expand Down
16 changes: 16 additions & 0 deletions test/Unit/StringProviderTest.php
Expand Up @@ -52,6 +52,8 @@ public function testArbitraryReturnsGeneratorThatProvidesArbitraryStrings(): voi
'string-untrimmed-line-feed' => Test\Util\Specification\Pattern::create('/^\n{1,5}\w+\n{1,5}$/'),
'string-untrimmed-space' => Test\Util\Specification\Pattern::create('/^\s{1,5}\w+\s{1,5}$/'),
'string-untrimmed-tab' => Test\Util\Specification\Pattern::create('/^\t{1,5}\w+\t{1,5}$/'),
'string-uuid-case-lower' => Test\Util\Specification\Pattern::create('/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/'),
'string-uuid-case-upper' => Test\Util\Specification\Pattern::create('/^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$/'),
'string-with-whitespace-carriage-return' => Test\Util\Specification\Pattern::create('/^\w+(\r+\w+){1,4}$/'),
'string-with-whitespace-line-feed' => Test\Util\Specification\Pattern::create('/^\w+(\n+\w+){1,4}$/'),
'string-with-whitespace-space' => Test\Util\Specification\Pattern::create('/^\w+(\s+\w+){1,4}$/'),
Expand Down Expand Up @@ -134,6 +136,8 @@ public function testTrimmedReturnsGeneratorThatProvidesStringsThatAreTrimmed():
'string-arbitrary-word' => Test\Util\Specification\Closure::create(static function (string $value): bool {
return '' !== $value && '' !== \trim($value);
}),
'string-uuid-case-lower' => Test\Util\Specification\Pattern::create('/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/'),
'string-uuid-case-upper' => Test\Util\Specification\Pattern::create('/^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$/'),
'string-with-whitespace-carriage-return' => Test\Util\Specification\Pattern::create('/^\w+(\r+\w+){1,4}$/'),
'string-with-whitespace-line-feed' => Test\Util\Specification\Pattern::create('/^\w+(\n+\w+){1,4}$/'),
'string-with-whitespace-space' => Test\Util\Specification\Pattern::create('/^\w+(\s+\w+){1,4}$/'),
Expand All @@ -159,6 +163,18 @@ public function testUntrimmedReturnsGeneratorThatProvidesUntrimmedStrings(): voi
self::assertProvidesDataSetsForValuesSatisfyingSpecifications($specifications, $provider);
}

public function testUuidReturnsGeneratorThatProvidesUuidStrings(): void
{
$specifications = [
'string-uuid-case-lower' => Test\Util\Specification\Pattern::create('/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/'),
'string-uuid-case-upper' => Test\Util\Specification\Pattern::create('/^[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}$/'),
];

$provider = StringProvider::uuid();

self::assertProvidesDataSetsForValuesSatisfyingSpecifications($specifications, $provider);
}

public function testWithWhitespaceReturnsGeneratorThatProvidesTrimmedStringsWithWhitespace(): void
{
$specifications = [
Expand Down

0 comments on commit ae08391

Please sign in to comment.