Skip to content

Commit 90ec44b

Browse files
committed
Add DataProvider.entries()
1 parent 3b607cb commit 90ec44b

File tree

6 files changed

+121
-4
lines changed

6 files changed

+121
-4
lines changed

.github/assets/example/entries.png

44.3 KB
Loading

ReadMe.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ with `zip()`, `join()`, `cross()`, `pairs()`, `slice()`, `map()` and more.
3434
* [`DataProvider::dictionary()`](#dataproviderdictionary)
3535
* [`DataProvider.map()`](#dataprovidermap)
3636
* [`DataProvider.slice()`](#dataproviderslice)
37+
* [`DataProvider.entries()`](#dataproviderentries)
3738
3. [Documentation](#documentation)
3839
* [Functionalities](#functionalities)
3940
* [Features](#features)
@@ -251,7 +252,7 @@ Provide multiple arguments for each a test. `DataProvider::tuples()` [names](#na
251252
* @test
252253
* @dataProvider colors
253254
*/
254-
public function test(string color, string $thing): void {
255+
public function test(string $color, string $thing): void {
255256
// your test here
256257
}
257258

@@ -276,7 +277,7 @@ provided array key.
276277
* @test
277278
* @dataProvider colors
278279
*/
279-
public function test(string color): void {
280+
public function test(string $color): void {
280281
// your test here
281282
}
282283

@@ -352,7 +353,7 @@ Remove leading or trailing rows from `DataProvider`.
352353
* @test
353354
* @dataProvider limitedColors
354355
*/
355-
public function test(string color, string $thing): void {
356+
public function test(string $color, string $thing): void {
356357
// your test here
357358
}
358359

@@ -369,6 +370,31 @@ public function colors(): DataProvider {
369370
}
370371
```
371372

373+
### `DataProvider.entries()`
374+
375+
Provide two arguments for each a test, from key-value pairs.
376+
`DataProvider::entries()` [names](#names) each row based on the key-value pair.
377+
378+
```php
379+
/**
380+
* @test
381+
* @dataProvider colors
382+
*/
383+
public function test(string $color, string $thing): void {
384+
// your test here
385+
}
386+
387+
public function colors(): DataProvider {
388+
return DataProvider::entries(
389+
'blue' => 'sky',
390+
'yellow' => 'sun',
391+
'red' => 'apple',
392+
);
393+
}
394+
```
395+
396+
![entries.png](.github/assets/example/entries.png)
397+
372398
# Documentation
373399

374400
### Functionalities
@@ -408,7 +434,7 @@ public function colors(): DataProvider {
408434
* @test
409435
* @dataProvider colors
410436
*/
411-
public function test(string color, string $thing): void {
437+
public function test(string $color, string $thing): void {
412438
// your test here
413439
}
414440

src/DataProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use TRegx\PhpUnit\DataProviders\Internal\Provider\DictionaryProvider;
77
use TRegx\PhpUnit\DataProviders\Internal\Provider\DistinctPairsProvider;
88
use TRegx\PhpUnit\DataProviders\Internal\Provider\DropProvider;
9+
use TRegx\PhpUnit\DataProviders\Internal\Provider\EntriesProvider;
910
use TRegx\PhpUnit\DataProviders\Internal\Provider\JoinProvider;
1011
use TRegx\PhpUnit\DataProviders\Internal\Provider\ListProvider;
1112
use TRegx\PhpUnit\DataProviders\Internal\Provider\MapProvider;
@@ -37,6 +38,11 @@ public static function dictionary(array $dictionary): DataProvider
3738
return new DictionaryProvider($dictionary);
3839
}
3940

41+
public static function entries(array $dictionary): DataProvider
42+
{
43+
return new EntriesProvider($dictionary);
44+
}
45+
4046
public static function join(iterable $dataProvider, iterable ...$dataProviders): DataProvider
4147
{
4248
return new JoinProvider(\array_merge([$dataProvider], $dataProviders));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace TRegx\PhpUnit\DataProviders\Internal\Provider;
3+
4+
use TRegx\PhpUnit\DataProviders\DataProvider;
5+
use TRegx\PhpUnit\DataProviders\Internal\Frame\DataRow;
6+
7+
class EntriesProvider extends DataProvider
8+
{
9+
/** @var mixed[] */
10+
private $dictionary;
11+
12+
public function __construct(array $dictionary)
13+
{
14+
$this->dictionary = $dictionary;
15+
}
16+
17+
protected function dataset(): array
18+
{
19+
$dataset = [];
20+
foreach ($this->dictionary as $key => $value) {
21+
$dataset[] = new DataRow([$key, $value], [true, true], [$key, $value]);
22+
}
23+
return $dataset;
24+
}
25+
}

test/Unit/entry/EntriesTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace Test\Unit\entry;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use Test\Fixture\Assertion\AssertsIteration;
6+
use TRegx\PhpUnit\DataProviders\DataProvider;
7+
8+
class EntriesTest extends TestCase
9+
{
10+
use AssertsIteration;
11+
12+
/**
13+
* @test
14+
*/
15+
public function shouldIterate()
16+
{
17+
$dictionary = DataProvider::entries([
18+
'Eddard' => 'Stark',
19+
'Robert' => 'Baratheon',
20+
'Balon' => 'Greyjoy',
21+
'Rhaegar' => 'Targaryen',
22+
]);
23+
$this->assertIteratesValues($dictionary, [
24+
['Eddard', 'Stark'],
25+
['Robert', 'Baratheon'],
26+
['Balon', 'Greyjoy'],
27+
['Rhaegar', 'Targaryen'],
28+
]);
29+
}
30+
}

test/Unit/names/EntriesTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace Test\Unit\names;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use Test\Fixture\Assertion\AssertsIteration;
6+
use TRegx\PhpUnit\DataProviders\DataProvider;
7+
8+
class EntriesTest extends TestCase
9+
{
10+
use AssertsIteration;
11+
12+
/**
13+
* @test
14+
*/
15+
public function shouldName()
16+
{
17+
$dictionary = DataProvider::entries([
18+
'Eddard' => 'Stark',
19+
'Robert' => 'Baratheon',
20+
'Balon' => 'Greyjoy',
21+
'Rhaegar' => 'Targaryen',
22+
]);
23+
$this->assertIteratesNames($dictionary, [
24+
"'Eddard', 'Stark'",
25+
"'Robert', 'Baratheon'",
26+
"'Balon', 'Greyjoy'",
27+
"'Rhaegar', 'Targaryen'",
28+
]);
29+
}
30+
}

0 commit comments

Comments
 (0)