From 9bafac8568427e20e95894e9a86109fdfca47073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Mo=CC=88ller?= Date: Mon, 5 Oct 2020 13:24:20 +0200 Subject: [PATCH] Enhancement: Implement DataProvider\ObjectProvider --- .php_cs | 4 +- CHANGELOG.md | 5 + README.md | 7 + phpstan-baseline.neon | 135 ++++++++++++++++++ psalm-baseline.xml | 18 ++- src/DataProvider/ObjectProvider.php | 31 ++++ test/Unit/DataProvider/ObjectProviderTest.php | 46 ++++++ .../Util/DataProvider/Specification/Equal.php | 34 +++++ 8 files changed, 276 insertions(+), 4 deletions(-) create mode 100644 src/DataProvider/ObjectProvider.php create mode 100644 test/Unit/DataProvider/ObjectProviderTest.php create mode 100644 test/Util/DataProvider/Specification/Equal.php diff --git a/.php_cs b/.php_cs index 6040c79b..9d47e580 100644 --- a/.php_cs +++ b/.php_cs @@ -26,7 +26,9 @@ $license = License\Type\MIT::markdown( $license->save(); -$config = PhpCsFixer\Config\Factory::fromRuleSet(new PhpCsFixer\Config\RuleSet\Php71($license->header())); +$config = PhpCsFixer\Config\Factory::fromRuleSet(new PhpCsFixer\Config\RuleSet\Php71($license->header()), [ + 'strict_comparison' => false, +]); $config->getFinder() ->ignoreDotFiles(false) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fa869f5..18d3c1ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), For a full diff see [`1.2.0...main`][1.2.0...main]. +### Added + +* Added `DataProvider\ObjectProvider` ([#343]), by [@localheinz] +* ## [`1.2.0`][1.2.0] For a full diff see [`1.1.0...1.2.0`][1.1.0...1.2.0]. @@ -132,6 +136,7 @@ For a full diff see [`0.7.0...0.8.0`][0.7.0...0.8.0]. [#334]: https://github.com/ergebnis/test-util/pull/334 [#335]: https://github.com/ergebnis/test-util/pull/335 [#341]: https://github.com/ergebnis/test-util/pull/341 +[#343]: https://github.com/ergebnis/test-util/pull/343 [@ergebnis]: https://github.com/ergebnis [@localheinz]: https://github.com/localheinz diff --git a/README.md b/README.md index 6ca105cb..ae210f3d 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ This package provides the following generic data providers: * [`Ergebnis\Test\Util\DataProvider\FloatProvider`](https://github.com/ergebnis/test-util#dataproviderfloatprovider) * [`Ergebnis\Test\Util\DataProvider\IntProvider`](https://github.com/ergebnis/test-util#dataproviderintprovider) * [`Ergebnis\Test\Util\DataProvider\NullProvider`](https://github.com/ergebnis/test-util#dataprovidernullprovider) +* [`Ergebnis\Test\Util\DataProvider\ObjectProvider`](https://github.com/ergebnis/test-util#dataproviderobjectprovider) * [`Ergebnis\Test\Util\DataProvider\StringProvider`](https://github.com/ergebnis/test-util#dataproviderstringprovider) Since it is possible to use multiple `@dataProvider` annotations for test methods, these generic data providers allow for reuse and composition of data providers: @@ -174,6 +175,12 @@ For examples, see [`Ergebnis\Test\Util\Test\Unit\DataProvider\IntProviderTest`]( For examples, see [`Ergebnis\Test\Util\Test\Unit\DataProvider\NullProviderTest`](test/Unit/DataProvider/NullProviderTest.php). +#### `DataProvider\ObjectProvider` + +* `object()` provides an instance of `stdClass` + +For examples, see [`Ergebnis\Test\Util\Test\Unit\DataProvider\ObjectProviderTest`](test/Unit/DataProvider/ObjectProviderTest.php). + #### `DataProvider\StringProvider` * `arbitrary()` provides arbitrary `string`s diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d200dbf4..3b6b2fbf 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -460,6 +460,121 @@ parameters: count: 1 path: src/DataProvider/NullProvider.php + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:faker\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertClassesAreAbstractOrFinal\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Parameter \\#1 \\$argument of class ReflectionClass constructor expects class\\-string\\\\|T of object, string given\\.$#" + count: 2 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertClassesHaveTests\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Call to an undefined static method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertEmpty\\(\\)\\.$#" + count: 2 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertClassyConstructsSatisfySpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertClassExists\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Call to an undefined static method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertTrue\\(\\)\\.$#" + count: 11 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertClassExtends\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertClassImplementsInterface\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertClassIsAbstract\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertClassIsFinal\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertClassSatisfiesSpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertClassUsesTrait\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Call to an undefined static method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertContains\\(\\)\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertInterfaceExists\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertInterfaceExtends\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertInterfaceSatisfiesSpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertTraitExists\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:assertTraitSatisfiesSpecification\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:provideDataForValues\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:provideDataForValuesWhere\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ObjectProvider\\:\\:provideDataForValuesWhereNot\\(\\) is protected, but since the containing class is final, it can be private\\.$#" + count: 1 + path: src/DataProvider/ObjectProvider.php + - message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\StringProvider\\:\\:faker\\(\\) is protected, but since the containing class is final, it can be private\\.$#" count: 1 @@ -1375,6 +1490,26 @@ parameters: count: 1 path: test/Util/DataProvider/Specification/Closure.php + - + message: "#^Property Ergebnis\\\\Test\\\\Util\\\\Test\\\\Util\\\\DataProvider\\\\Specification\\\\Equal\\:\\:\\$value has no typehint specified\\.$#" + count: 1 + path: test/Util/DataProvider/Specification/Equal.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\Test\\\\Util\\\\DataProvider\\\\Specification\\\\Equal\\:\\:__construct\\(\\) has parameter \\$value with no typehint specified\\.$#" + count: 1 + path: test/Util/DataProvider/Specification/Equal.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\Test\\\\Util\\\\DataProvider\\\\Specification\\\\Equal\\:\\:create\\(\\) has parameter \\$value with no typehint specified\\.$#" + count: 1 + path: test/Util/DataProvider/Specification/Equal.php + + - + message: "#^Method Ergebnis\\\\Test\\\\Util\\\\Test\\\\Util\\\\DataProvider\\\\Specification\\\\Equal\\:\\:isSatisfiedBy\\(\\) has parameter \\$value with no typehint specified\\.$#" + count: 1 + path: test/Util/DataProvider/Specification/Equal.php + - message: "#^Property Ergebnis\\\\Test\\\\Util\\\\Test\\\\Util\\\\DataProvider\\\\Specification\\\\Identical\\:\\:\\$value has no typehint specified\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 7fe75586..ca023e38 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -11,9 +11,6 @@ - - -1 * $faker->randomFloat(null, 1.01) - \Generator<string, array{0: float}> \Generator<string, array{0: float}> @@ -40,6 +37,11 @@ \Generator<string, array{0: null}> + + + \Generator<string, array{0: bool}> + + @@ -144,6 +146,16 @@ $closure($value) + + + $value + $value + $value + + + $value + + $value diff --git a/src/DataProvider/ObjectProvider.php b/src/DataProvider/ObjectProvider.php new file mode 100644 index 00000000..8b335620 --- /dev/null +++ b/src/DataProvider/ObjectProvider.php @@ -0,0 +1,31 @@ + + */ + public static function object(): \Generator + { + yield from self::provideDataForValues([ + 'object' => new \stdClass(), + ]); + } +} diff --git a/test/Unit/DataProvider/ObjectProviderTest.php b/test/Unit/DataProvider/ObjectProviderTest.php new file mode 100644 index 00000000..4a3f91de --- /dev/null +++ b/test/Unit/DataProvider/ObjectProviderTest.php @@ -0,0 +1,46 @@ + Util\DataProvider\Specification\Equal::create(new \stdClass()), + ]; + + $provider = ObjectProvider::object(); + + self::assertProvidesDataSetsForValuesSatisfyingSpecifications($specifications, $provider); + } +} diff --git a/test/Util/DataProvider/Specification/Equal.php b/test/Util/DataProvider/Specification/Equal.php new file mode 100644 index 00000000..a243f7cf --- /dev/null +++ b/test/Util/DataProvider/Specification/Equal.php @@ -0,0 +1,34 @@ +value = $value; + } + + public static function create($value): self + { + return new self($value); + } + + public function isSatisfiedBy($value): bool + { + return $this->value == $value; + } +}