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 #344 from ergebnis/feature/resource-provider
Browse files Browse the repository at this point in the history
Enhancement: Implement DataProvider\ResourceProvider
  • Loading branch information
ergebnis-bot committed Oct 5, 2020
2 parents 2c263dc + 9dccc61 commit 6dd7ff9
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 4 deletions.
16 changes: 12 additions & 4 deletions CHANGELOG.md
Expand Up @@ -6,12 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

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

## [`1.3.0`][1.3.0]

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

### Added

* Added `DataProvider\ObjectProvider` ([#343]), by [@localheinz]
*
* Added `DataProvider\ResourceProvider` ([#344]), 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 All @@ -23,7 +28,7 @@ For a full diff see [`1.1.0...1.2.0`][1.1.0...1.2.0].
* Added `DataProvider\StringProvider` ([#328]), by [@localheinz]
* Added `DataProvider\IntProvider` ([#335]), by [@localheinz]
* Added `DataProvider\FloatProvider` ([#341]), by [@localheinz]
*

### Changed

* Renamed `DataProvider\BooleanProvider` to `DataProvider\BoolProvider` ([#334]), by [@localheinz]
Expand Down Expand Up @@ -112,6 +117,7 @@ For a full diff see [`0.7.0...0.8.0`][0.7.0...0.8.0].
[1.0.1]: https://github.com/ergebnis/test-util/releases/tag/1.0.1
[1.1.0]: https://github.com/ergebnis/test-util/releases/tag/1.1.0
[1.2.0]: https://github.com/ergebnis/test-util/releases/tag/1.2.0
[1.3.0]: https://github.com/ergebnis/test-util/releases/tag/1.3.0

[0.7.0...0.8.0]: https://github.com/ergebnis/test-util/compare/0.7.0...0.8.0
[0.8.0...0.9.0]: https://github.com/ergebnis/test-util/compare/0.8.0...0.9.0
Expand All @@ -120,7 +126,8 @@ For a full diff see [`0.7.0...0.8.0`][0.7.0...0.8.0].
[1.0.0...1.0.1]: https://github.com/ergebnis/test-util/compare/1.0.0...1.0.1
[1.0.1...1.1.0]: https://github.com/ergebnis/test-util/compare/1.0.1...1.1.0
[1.1.0...1.2.0]: https://github.com/ergebnis/test-util/compare/1.1.0...1.2.0
[1.2.0...main]: https://github.com/ergebnis/test-util/compare/1.2.0...main
[1.2.0...1.3.0]: https://github.com/ergebnis/test-util/compare/1.2.0...1.3.0
[1.3.0...main]: https://github.com/ergebnis/test-util/compare/1.3.0...main

[#118]: https://github.com/ergebnis/test-util/pull/118
[#119]: https://github.com/ergebnis/test-util/pull/119
Expand All @@ -137,6 +144,7 @@ For a full diff see [`0.7.0...0.8.0`][0.7.0...0.8.0].
[#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
[#344]: https://github.com/ergebnis/test-util/pull/344

[@ergebnis]: https://github.com/ergebnis
[@localheinz]: https://github.com/localheinz
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -106,6 +106,7 @@ This package provides the following generic data providers:
* [`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\ResourceProvider`](https://github.com/ergebnis/test-util#dataproviderresourceprovider)
* [`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 @@ -181,6 +182,12 @@ For examples, see [`Ergebnis\Test\Util\Test\Unit\DataProvider\NullProviderTest`]

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

#### `DataProvider\ResourceProvider`

* `resource()` provides a `resource`

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

#### `DataProvider\StringProvider`

* `arbitrary()` provides arbitrary `string`s
Expand Down
115 changes: 115 additions & 0 deletions phpstan-baseline.neon
Expand Up @@ -575,6 +575,121 @@ parameters:
count: 1
path: src/DataProvider/ObjectProvider.php

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\DataProvider\\\\ResourceProvider\\:\\:provideDataForValuesWhereNot\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
path: src/DataProvider/ResourceProvider.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
10 changes: 10 additions & 0 deletions psalm-baseline.xml
Expand Up @@ -42,6 +42,11 @@
<code>\Generator&lt;string, array{0: bool}&gt;</code>
</MoreSpecificReturnType>
</file>
<file src="src/DataProvider/ResourceProvider.php">
<MoreSpecificReturnType occurrences="1">
<code>\Generator&lt;string, array{0: resource}&gt;</code>
</MoreSpecificReturnType>
</file>
<file src="src/DataProvider/StringProvider.php">
<MixedArgumentTypeCoercion occurrences="2"/>
<MoreSpecificReturnType occurrences="5">
Expand Down Expand Up @@ -82,6 +87,11 @@
<code>Util\Test\Util\DataProvider\Specification\Specification</code>
</UnnecessaryVarAnnotation>
</file>
<file src="test/Unit/DataProvider/ResourceProviderTest.php">
<MissingClosureParamType occurrences="1">
<code>$value</code>
</MissingClosureParamType>
</file>
<file src="test/Unit/Exception/InvalidExcludeClassNameTest.php">
<MixedInferredReturnType occurrences="1">
<code>\Generator&lt;array&lt;null|array|bool|float|int|resource|\stdClass&gt;&gt;</code>
Expand Down
31 changes: 31 additions & 0 deletions src/DataProvider/ResourceProvider.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 ResourceProvider
{
use Util\Helper;

/**
* @return \Generator<string, array{0: resource}>
*/
public static function resource(): \Generator
{
yield from self::provideDataForValues([
'resource' => \fopen(__FILE__, 'rb'),
]);
}
}
48 changes: 48 additions & 0 deletions test/Unit/DataProvider/ResourceProviderTest.php
@@ -0,0 +1,48 @@
<?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\ResourceProvider;
use Ergebnis\Test\Util\Test\Util;

/**
* @internal
*
* @covers \Ergebnis\Test\Util\DataProvider\ResourceProvider
*/
final class ResourceProviderTest extends AbstractProviderTestCase
{
/**
* @dataProvider \Ergebnis\Test\Util\DataProvider\ResourceProvider::resource()
*
* @param mixed $value
*/
public function testResourceProvidesResource($value): void
{
self::assertIsResource($value);
}

public function testResourceReturnsGeneratorThatProvidesResource(): void
{
$specifications = [
'resource' => Util\DataProvider\Specification\Closure::create(static function ($value): bool {
return \is_resource($value);
}),
];

$provider = ResourceProvider::resource();

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

0 comments on commit 6dd7ff9

Please sign in to comment.