Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #328 from ergebnis/feature/string-provider
Browse files Browse the repository at this point in the history
Enhancement: Implement StringProvider
  • Loading branch information
ergebnis-bot authored Oct 3, 2020
2 parents 4fd4964 + b619a23 commit ee46561
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on: # yamllint disable-line rule:truthy
env:
ERGEBNIS_BOT_NAME: "ergebnis-bot"
MIN_COVERED_MSI: 95
MIN_MSI: 90
MIN_MSI: 66
PHP_EXTENSIONS: "mbstring"

jobs:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ For a full diff see [`1.1.0...main`][1.1.0...main].

* Added `DataProvider\BooleanProvider` ([#326]), by [@localheinz]
* Added `DataProvider\NullProvider` ([#327]), by [@localheinz]
* Added `DataProvider\StringProvider` ([#328]), by [@localheinz]

## [`1.1.0`][1.1.0]

Expand Down Expand Up @@ -115,6 +116,7 @@ For a full diff see [`0.7.0...0.8.0`][0.7.0...0.8.0].
[#302]: https://github.com/ergebnis/test-util/pull/302
[#326]: https://github.com/ergebnis/test-util/pull/326
[#327]: https://github.com/ergebnis/test-util/pull/327
[#328]: https://github.com/ergebnis/test-util/pull/328

[@ergebnis]: https://github.com/ergebnis
[@localheinz]: https://github.com/localheinz
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MIN_COVERED_MSI:=95
MIN_MSI:=90
MIN_MSI:=66

.PHONY: it
it: coding-standards static-code-analysis tests ## Runs the coding-standards, static-code-analysis, and tests targets
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ For examples, see [`Ergebnis\Test\Util\Test\Unit\DataProvider\BooleanProviderTes

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

#### `DataProvider\StringProvider`

* `arbitrary()` provides non-empty `string`s without leading and trailing whitespade
* `blank()` provides `string`s consisting of whitespace characters only
* `empty()` provides an empty `string`
* `untrimmed()` provides `string`s with leading and trailing whitespace
*
For examples, see [`Ergebnis\Test\Util\Test\Unit\DataProvider\StringProviderTest`](test/Unit/DataProvider/StringProviderTest.php).

## Changelog

Please have a look at [`CHANGELOG.md`](CHANGELOG.md).
Expand Down
115 changes: 115 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,121 @@ parameters:
count: 1
path: src/DataProvider/NullProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:faker\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertClassesAreAbstractOrFinal\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Parameter \\#1 \\$argument of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|T of object, string given\\.$#"
count: 2
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertClassesHaveTests\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Call to an undefined static method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertEmpty\\(\\)\\.$#"
count: 2
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertClassyConstructsSatisfySpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertClassExists\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Call to an undefined static method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertTrue\\(\\)\\.$#"
count: 11
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertClassExtends\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertClassImplementsInterface\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertClassIsAbstract\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertClassIsFinal\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertClassSatisfiesSpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertClassUsesTrait\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Call to an undefined static method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertContains\\(\\)\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertInterfaceExists\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertInterfaceExtends\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertInterfaceSatisfiesSpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertTraitExists\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:assertTraitSatisfiesSpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:provide\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:provideWhere\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:provideWhereNot\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/StringProvider.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\Test\\\\AutoReview\\\\SrcCodeTest\\:\\:faker\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
Expand Down
9 changes: 9 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
<code>\Generator&lt;string, array{0: null}&gt;</code>
</MoreSpecificReturnType>
</file>
<file src="src/DataProvider/StringProvider.php">
<MixedArgumentTypeCoercion occurrences="2"/>
<MoreSpecificReturnType occurrences="4">
<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>
</MoreSpecificReturnType>
</file>
<file src="src/Helper.php">
<MissingClosureParamType occurrences="2">
<code>$excludeClassyName</code>
Expand Down
134 changes: 134 additions & 0 deletions src/DataProvider/StringProvider.php
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",
];
}
}
67 changes: 67 additions & 0 deletions test/Unit/DataProvider/StringProviderTest.php
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));
}
}

0 comments on commit ee46561

Please sign in to comment.