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

Enhancement: Implement DataProvider\ObjectProvider #343

Merged
merged 1 commit into from Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .php_cs
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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].
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
135 changes: 135 additions & 0 deletions phpstan-baseline.neon
Expand Up @@ -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\\>\\|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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions psalm-baseline.xml
Expand Up @@ -11,9 +11,6 @@
</RedundantIdentityWithTrue>
</file>
<file src="src/DataProvider/FloatProvider.php">
<InvalidOperand occurrences="1">
<code>-1 * $faker-&gt;randomFloat(null, 1.01)</code>
</InvalidOperand>
<MoreSpecificReturnType occurrences="7">
<code>\Generator&lt;string, array{0: float}&gt;</code>
<code>\Generator&lt;string, array{0: float}&gt;</code>
Expand All @@ -40,6 +37,11 @@
<code>\Generator&lt;string, array{0: null}&gt;</code>
</MoreSpecificReturnType>
</file>
<file src="src/DataProvider/ObjectProvider.php">
<MoreSpecificReturnType occurrences="1">
<code>\Generator&lt;string, array{0: bool}&gt;</code>
</MoreSpecificReturnType>
</file>
<file src="src/DataProvider/StringProvider.php">
<MixedArgumentTypeCoercion occurrences="2"/>
<MoreSpecificReturnType occurrences="5">
Expand Down Expand Up @@ -144,6 +146,16 @@
<code>$closure($value)</code>
</MixedReturnStatement>
</file>
<file src="test/Util/DataProvider/Specification/Equal.php">
<MissingParamType occurrences="3">
<code>$value</code>
<code>$value</code>
<code>$value</code>
</MissingParamType>
<MissingPropertyType occurrences="1">
<code>$value</code>
</MissingPropertyType>
</file>
<file src="test/Util/DataProvider/Specification/Identical.php">
<MissingParamType occurrences="3">
<code>$value</code>
Expand Down
31 changes: 31 additions & 0 deletions src/DataProvider/ObjectProvider.php
@@ -0,0 +1,31 @@
<?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 ObjectProvider
{
use Util\Helper;

/**
* @return \Generator<string, array{0: bool}>
*/
public static function object(): \Generator
{
yield from self::provideDataForValues([
'object' => new \stdClass(),
]);
}
}
46 changes: 46 additions & 0 deletions test/Unit/DataProvider/ObjectProviderTest.php
@@ -0,0 +1,46 @@
<?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 Ergebnis\Test\Util\DataProvider\ObjectProvider;
use Ergebnis\Test\Util\Test\Util;

/**
* @internal
*
* @covers \Ergebnis\Test\Util\DataProvider\ObjectProvider
*/
final class ObjectProviderTest extends AbstractProviderTestCase
{
/**
* @dataProvider \Ergebnis\Test\Util\DataProvider\ObjectProvider::object()
*
* @param mixed $value
*/
public function testObjectProvidesObject($value): void
{
self::assertIsObject($value);
}

public function testObjectReturnsGeneratorThatProvidesObject(): void
{
$specifications = [
'object' => Util\DataProvider\Specification\Equal::create(new \stdClass()),
];

$provider = ObjectProvider::object();

self::assertProvidesDataSetsForValuesSatisfyingSpecifications($specifications, $provider);
}
}
34 changes: 34 additions & 0 deletions test/Util/DataProvider/Specification/Equal.php
@@ -0,0 +1,34 @@
<?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\Util\DataProvider\Specification;

final class Equal implements Specification
{
private $value;

private function __construct($value)
{
$this->value = $value;
}

public static function create($value): self
{
return new self($value);
}

public function isSatisfiedBy($value): bool
{
return $this->value == $value;
}
}