diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bb3b0bb..2c6111ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,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] +* Added `DataProvider\IntProvider` ([#335]), by [@localheinz] * ### Changed @@ -122,6 +123,7 @@ For a full diff see [`0.7.0...0.8.0`][0.7.0...0.8.0]. [#327]: https://github.com/ergebnis/test-util/pull/327 [#328]: https://github.com/ergebnis/test-util/pull/328 [#334]: https://github.com/ergebnis/test-util/pull/334 +[#335]: https://github.com/ergebnis/test-util/pull/335 [@ergebnis]: https://github.com/ergebnis [@localheinz]: https://github.com/localheinz diff --git a/README.md b/README.md index 1f38c332..daef7534 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,18 @@ final class ExampleTest extends Framework\TestCase For examples, see [`Ergebnis\Test\Util\Test\Unit\DataProvider\BoolProviderTest`](test/Unit/DataProvider/BoolProviderTest.php). +#### `DataProvider\IntProvider` + +* `arbitrary()` provides arbitrary `int`s +* `greaterThanOne()` provides `int`s greater than `1` +* `greaterThanZero()` provides `int`s greater than `0` +* `lessThanOne()` provides `int`s less than `1` +* `lessThanZero()` provides `int`s less than `0` +* `one()` provides `1` +* `zero()` provides `0` + +For examples, see [`Ergebnis\Test\Util\Test\Unit\DataProvider\IntProviderTest`](test/Unit/DataProvider/IntProviderTest.php). + #### `DataProvider\NullProvider` * `null()` provides `null` diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 6cf03715..aaf6a2ba 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -115,6 +115,121 @@ parameters: count: 1 path: src/DataProvider/BoolProvider.php + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:faker\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertClassesAreAbstractOrFinal\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Parameter \\#1 \\$argument of class ReflectionClass constructor expects class\\-string\\\\|T of object, string given\\.$#" + count: 2 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertClassesHaveTests\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Call to an undefined static method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertEmpty\\(\\)\\.$#" + count: 2 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertClassyConstructsSatisfySpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertClassExists\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Call to an undefined static method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertTrue\\(\\)\\.$#" + count: 11 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertClassExtends\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertClassImplementsInterface\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertClassIsAbstract\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertClassIsFinal\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertClassSatisfiesSpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertClassUsesTrait\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Call to an undefined static method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertContains\\(\\)\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertInterfaceExists\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertInterfaceExtends\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertInterfaceSatisfiesSpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertTraitExists\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:assertTraitSatisfiesSpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:provideDataForValues\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:provideDataForValuesWhere\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\IntProvider\\:\\:provideDataForValuesWhereNot\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/IntProvider.php + - message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\NullProvider\\:\\:faker\\(\\) is protected, but since the containing class is final, it can be private\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index eec48d3e..086e4f96 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -10,6 +10,17 @@ true === $value + + + \Generator<string, array{0: int}> + \Generator<string, array{0: int}> + \Generator<string, array{0: int}> + \Generator<string, array{0: int}> + \Generator<string, array{0: int}> + \Generator<string, array{0: int}> + \Generator<string, array{0: int}> + + \Generator<string, array{0: null}> @@ -43,6 +54,11 @@ class-string + + + $value + + \Generator<array<null|array|bool|float|int|resource|\stdClass>> diff --git a/src/DataProvider/IntProvider.php b/src/DataProvider/IntProvider.php new file mode 100644 index 00000000..051c8481 --- /dev/null +++ b/src/DataProvider/IntProvider.php @@ -0,0 +1,105 @@ + + */ + public static function arbitrary(): \Generator + { + yield from self::provideDataForValues(self::values()); + } + + /** + * @return \Generator + */ + public static function lessThanZero(): \Generator + { + yield from self::provideDataForValuesWhere(self::values(), static function (int $value): bool { + return 0 > $value; + }); + } + + /** + * @return \Generator + */ + public static function zero(): \Generator + { + yield from self::provideDataForValuesWhere(self::values(), static function (int $value): bool { + return 0 === $value; + }); + } + + /** + * @return \Generator + */ + public static function greaterThanZero(): \Generator + { + yield from self::provideDataForValuesWhere(self::values(), static function (int $value): bool { + return 0 < $value; + }); + } + + /** + * @return \Generator + */ + public static function lessThanOne(): \Generator + { + yield from self::provideDataForValuesWhere(self::values(), static function (int $value): bool { + return 1 > $value; + }); + } + + /** + * @return \Generator + */ + public static function one(): \Generator + { + yield from self::provideDataForValuesWhere(self::values(), static function (int $value): bool { + return 1 === $value; + }); + } + + /** + * @return \Generator + */ + public static function greaterThanOne(): \Generator + { + yield from self::provideDataForValuesWhere(self::values(), static function (int $value): bool { + return 1 < $value; + }); + } + + /** + * @return array + */ + private static function values(): array + { + $faker = self::faker(); + + return [ + 'int-less-than-minus-one' => -1 * $faker->numberBetween(1), + 'int-minus-one' => -1, + 'int-zero' => 0, + 'int-plus-one' => 1, + 'int-greater-than-plus-one' => $faker->numberBetween(1), + ]; + } +} diff --git a/test/Unit/DataProvider/IntProviderTest.php b/test/Unit/DataProvider/IntProviderTest.php new file mode 100644 index 00000000..e9a677a9 --- /dev/null +++ b/test/Unit/DataProvider/IntProviderTest.php @@ -0,0 +1,140 @@ + $value; + }; + + $provider = IntProvider::lessThanZero(); + + self::assertProvidesDataForValuesWhere($test, $provider); + } + + /** + * @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::zero() + * + * @param int $value + */ + public function testZeroProvidesZero(int $value): void + { + self::assertSame(0, $value); + } + + public function testZeroReturnsGeneratorThatProvidesZero(): void + { + $values = [ + 'int-zero' => 0, + ]; + + $provider = IntProvider::zero(); + + self::assertProvidesDataForValues($values, $provider); + } + + /** + * @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::greaterThanZero() + * + * @param int $value + */ + public function testGreaterThanZeroProvidesIntGreaterThanZero(int $value): void + { + self::assertGreaterThan(0, $value); + } + + public function testGreaterThanZeroReturnsGeneratorThatProvidesIntGreaterThanZero(): void + { + $test = static function (int $value): bool { + return 0 < $value; + }; + + $provider = IntProvider::greaterThanZero(); + + self::assertProvidesDataForValuesWhere($test, $provider); + } + + public function testOneReturnsGeneratorThatProvidesOne(): void + { + $values = [ + 'int-plus-one' => 1, + ]; + + $provider = IntProvider::one(); + + self::assertProvidesDataForValues($values, $provider); + } + + /** + * @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::greaterThanOne() + * + * @param int $value + */ + public function testGreaterThanOneProvidesIntGreaterThanOne(int $value): void + { + self::assertGreaterThan(1, $value); + } + + public function testGreaterThanOneReturnsGeneratorThatProvidesIntGreaterThanOne(): void + { + $test = static function (int $value): bool { + return 1 < $value; + }; + + $provider = IntProvider::greaterThanOne(); + + self::assertProvidesDataForValuesWhere($test, $provider); + } +}