Skip to content

Commit 1162238

Browse files
authored
Refactor: Use pest for tests (#14)
1 parent 607cf06 commit 1162238

File tree

10 files changed

+187
-208
lines changed

10 files changed

+187
-208
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ on:
77
- cron: "0 0 1 * *"
88

99
jobs:
10-
phpunit:
11-
name: "PHPUnit"
10+
unit-tests:
11+
name: "Unit Tests"
1212
runs-on: "ubuntu-20.04"
1313

1414
strategy:
@@ -41,20 +41,20 @@ jobs:
4141
with:
4242
dependency-versions: "${{ matrix.dependencies }}"
4343

44-
- name: "Run PHPUnit"
45-
run: "vendor/bin/phpunit --coverage-clover=coverage.xml"
44+
- name: "Run unit tests"
45+
run: "vendor/bin/pest --coverage-clover=coverage.xml"
4646

4747
- name: "Upload coverage file"
4848
uses: "actions/upload-artifact@v2"
4949
with:
50-
name: "phpunit-${{ matrix.deps }}-${{ matrix.php-version }}.coverage"
50+
name: "pest-${{ matrix.deps }}-${{ matrix.php-version }}.coverage"
5151
path: "coverage.xml"
5252

53-
upload_coverage:
53+
upload-coverage:
5454
name: "Upload coverage to Codecov"
5555
runs-on: "ubuntu-20.04"
5656
needs:
57-
- "phpunit"
57+
- "unit-tests"
5858

5959
steps:
6060
- name: "Checkout"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Unit tests
174174
==========
175175

176176
```
177-
./vendor/bin/phpunit
177+
php vendor/bin/pest
178178
```
179179

180180
[CodeCov Master]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"require-dev": {
2424
"doctrine/coding-standard": "^8.2",
25-
"phpunit/phpunit": "^9",
25+
"pestphp/pest": "^1.0",
2626
"symfony/var-dumper": "^5.2"
2727
},
2828
"config": {

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
<exclude name="Generic.Formatting.MultipleStatementAlignment"/>
1919
<exclude name="Squiz.Commenting.FunctionComment.SpacingAfterParamType"/>
20+
<exclude name="SlevomatCodingStandard.Functions.StaticClosure"/>
2021
</rule>
2122

2223
<file>src/</file>

tests/IterableFilterTest.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,35 @@
44

55
namespace BenTools\IterableFunctions\Tests;
66

7-
use PHPUnit\Framework\TestCase;
87
use SplFixedArray;
98

109
use function BenTools\IterableFunctions\iterable_filter;
1110
use function BenTools\IterableFunctions\iterable_to_array;
11+
use function it;
12+
use function PHPUnit\Framework\assertEquals;
1213

13-
final class IterableFilterTest extends TestCase
14-
{
15-
public function testArrayFilter(): void
16-
{
17-
$iterable = ['foo', 'bar'];
18-
$filter = static function ($input) {
19-
return $input === 'bar';
20-
};
21-
$this->assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
22-
}
23-
24-
public function testTraversableFilter(): void
25-
{
26-
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
27-
$filter = static function ($input) {
28-
return $input === 'bar';
29-
};
30-
$this->assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
31-
}
32-
}
14+
it('filters an array', function (): void {
15+
$iterable = [false, true];
16+
assertEquals([1 => true], iterable_to_array(iterable_filter($iterable)));
17+
});
18+
19+
it('filters a Travsersable object', function (): void {
20+
$iterable = SplFixedArray::fromArray([false, true]);
21+
assertEquals([1 => true], iterable_to_array(iterable_filter($iterable)));
22+
});
23+
24+
it('filters an array with a callback', function (): void {
25+
$iterable = ['foo', 'bar'];
26+
$filter = static function ($input) {
27+
return $input === 'bar';
28+
};
29+
assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
30+
});
31+
32+
it('filters a Travsersable object with a callback', function (): void {
33+
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
34+
$filter = static function ($input) {
35+
return $input === 'bar';
36+
};
37+
assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
38+
});

tests/IterableMapTest.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,21 @@
44

55
namespace BenTools\IterableFunctions\Tests;
66

7-
use PHPUnit\Framework\TestCase;
87
use SplFixedArray;
98

109
use function BenTools\IterableFunctions\iterable_map;
1110
use function BenTools\IterableFunctions\iterable_to_array;
11+
use function it;
12+
use function PHPUnit\Framework\assertEquals;
1213

13-
final class IterableMapTest extends TestCase
14-
{
15-
public function testArrayMap(): void
16-
{
17-
$iterable = ['foo', 'bar'];
18-
$map = 'strtoupper';
19-
$this->assertEquals(['FOO', 'BAR'], iterable_to_array(iterable_map($iterable, $map)));
20-
}
14+
it('maps an array', function (): void {
15+
$iterable = ['foo', 'bar'];
16+
$map = 'strtoupper';
17+
assertEquals(['FOO', 'BAR'], iterable_to_array(iterable_map($iterable, $map)));
18+
});
2119

22-
public function testTraversableMap(): void
23-
{
24-
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
25-
$map = 'strtoupper';
26-
$this->assertEquals(['FOO', 'BAR'], iterable_to_array(iterable_map($iterable, $map)));
27-
}
28-
}
20+
it('maps a Traversable object', function (): void {
21+
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
22+
$map = 'strtoupper';
23+
assertEquals(['FOO', 'BAR'], iterable_to_array(iterable_map($iterable, $map)));
24+
});

tests/IterableObjectTest.php

Lines changed: 84 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,100 +4,97 @@
44

55
namespace BenTools\IterableFunctions\Tests;
66

7-
use PHPUnit\Framework\TestCase;
7+
use BenTools\IterableFunctions\IterableObject;
88
use SplFixedArray;
99

1010
use function BenTools\IterableFunctions\iterable;
11+
use function it;
1112
use function iterator_to_array;
13+
use function PHPUnit\Framework\assertEquals;
14+
use function PHPUnit\Framework\assertInstanceOf;
15+
use function test;
1216

13-
final class IterableObjectTest extends TestCase
14-
{
15-
/**
16-
* @param array<mixed> $data
17-
* @param array<mixed> $expectedResult
18-
*
19-
* @dataProvider dataProvider
20-
*/
21-
public function testFromArrayToIterator(array $data, ?callable $filter, ?string $map, array $expectedResult): void
22-
{
23-
$iterableObject = iterable($data, $filter, $map);
24-
$this->assertEquals($expectedResult, iterator_to_array($iterableObject));
25-
}
17+
$dataProvider = function () {
18+
$data = ['foo', 'bar'];
19+
$filter = static function ($value) {
20+
return $value === 'bar';
21+
};
22+
$map = 'strtoupper';
2623

27-
/**
28-
* @param array<mixed> $data
29-
* @param array<mixed> $expectedResult
30-
*
31-
* @dataProvider dataProvider
32-
*/
33-
public function testFromArrayToArray(array $data, ?callable $filter, ?string $map, array $expectedResult): void
34-
{
35-
$iterableObject = iterable($data, $filter, $map);
36-
$this->assertEquals($expectedResult, $iterableObject->asArray());
37-
}
24+
yield from [
25+
[
26+
$data,
27+
null,
28+
null,
29+
['foo', 'bar'],
30+
],
31+
[
32+
$data,
33+
$filter,
34+
null,
35+
[1 => 'bar'],
36+
],
37+
[
38+
$data,
39+
null,
40+
$map,
41+
['FOO', 'BAR'],
42+
],
43+
[
44+
$data,
45+
$filter,
46+
$map,
47+
[1 => 'BAR'],
48+
],
49+
];
50+
};
3851

39-
/**
40-
* @param array<mixed> $data
41-
* @param array<mixed> $expectedResult
42-
*
43-
* @dataProvider dataProvider
44-
*/
45-
public function testFromTraversableToIterator(array $data, ?callable $filter, ?string $map, array $expectedResult): void
46-
{
47-
$data = SplFixedArray::fromArray($data);
48-
$iterableObject = iterable($data, $filter, $map);
49-
$this->assertEquals($expectedResult, iterator_to_array($iterableObject));
50-
}
52+
test('input: array | output: traversable', function ($data, $filter, $map, $expectedResult): void {
53+
$iterableObject = iterable($data, $filter, $map);
54+
assertEquals($expectedResult, iterator_to_array($iterableObject));
55+
})->with($dataProvider());
5156

52-
/**
53-
* @param array<mixed> $data
54-
* @param array<mixed> $expectedResult
55-
*
56-
* @dataProvider dataProvider
57-
*/
58-
public function testFromTraversableToArray(array $data, ?callable $filter, ?string $map, array $expectedResult): void
59-
{
60-
$data = SplFixedArray::fromArray($data);
61-
$iterableObject = iterable($data, $filter, $map);
62-
$this->assertEquals($expectedResult, $iterableObject->asArray());
63-
}
57+
test('input: array | output: array', function ($data, $filter, $map, $expectedResult): void {
58+
$iterableObject = iterable($data, $filter, $map);
59+
assertEquals($expectedResult, $iterableObject->asArray());
60+
})->with($dataProvider());
6461

65-
/**
66-
* @return list<array{array<mixed>, callable|null, string|null, array<mixed>}>>
67-
*/
68-
public function dataProvider(): array
69-
{
70-
$data = ['foo', 'bar'];
71-
$filter = static function ($value) {
72-
return $value === 'bar';
73-
};
74-
$map = 'strtoupper';
62+
test('input: traversable | output: traversable', function ($data, $filter, $map, $expectedResult): void {
63+
$data = SplFixedArray::fromArray($data);
64+
$iterableObject = iterable($data, $filter, $map);
65+
assertEquals($expectedResult, iterator_to_array($iterableObject));
66+
})->with($dataProvider());
7567

76-
return [
77-
[
78-
$data,
79-
null,
80-
null,
81-
['foo', 'bar'],
82-
],
83-
[
84-
$data,
85-
$filter,
86-
null,
87-
[1 => 'bar'],
88-
],
89-
[
90-
$data,
91-
null,
92-
$map,
93-
['FOO', 'BAR'],
94-
],
95-
[
96-
$data,
97-
$filter,
98-
$map,
99-
[1 => 'BAR'],
100-
],
101-
];
102-
}
103-
}
68+
test('input: traversable | output: array', function ($data, $filter, $map, $expectedResult): void {
69+
$data = SplFixedArray::fromArray($data);
70+
$iterableObject = iterable($data, $filter, $map);
71+
assertEquals($expectedResult, $iterableObject->asArray());
72+
})->with($dataProvider());
73+
74+
it('filters the subject', function (): void {
75+
$filter = static function ($value) {
76+
return $value === 'bar';
77+
};
78+
$iterableObject = iterable(['foo', 'bar'])->filter($filter);
79+
assertEquals([1 => 'bar'], iterator_to_array($iterableObject));
80+
});
81+
82+
it('maps the subject', function (): void {
83+
$map = 'strtoupper';
84+
$iterableObject = iterable(['foo', 'bar'])->map($map);
85+
assertInstanceOf(IterableObject::class, $iterableObject);
86+
assertEquals(['FOO', 'BAR'], iterator_to_array($iterableObject));
87+
});
88+
89+
it('combines filter and map', function (): void {
90+
$filter = static function ($value) {
91+
return $value === 'bar';
92+
};
93+
$map = 'strtoupper';
94+
$iterableObject = iterable(['foo', 'bar'])->map($map)->filter($filter);
95+
assertInstanceOf(IterableObject::class, $iterableObject);
96+
assertEquals([1 => 'BAR'], iterator_to_array($iterableObject));
97+
$iterableObject = iterable(['foo', 'bar'])->filter($filter)->map($map);
98+
assertInstanceOf(IterableObject::class, $iterableObject);
99+
assertEquals([1 => 'BAR'], iterator_to_array($iterableObject));
100+
});

tests/IterableReduceTest.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,24 @@
44

55
namespace BenTools\IterableFunctions\Tests;
66

7-
use PHPUnit\Framework\TestCase;
87
use SplFixedArray;
98

109
use function BenTools\IterableFunctions\iterable_reduce;
10+
use function it;
11+
use function PHPUnit\Framework\assertSame;
1112

12-
final class IterableReduceTest extends TestCase
13-
{
14-
public function testArrayReduce(): void
15-
{
16-
$iterable = [1, 2];
17-
$reduce = static function ($carry, $item) {
18-
return $carry + $item;
19-
};
20-
self::assertSame(3, iterable_reduce($iterable, $reduce, 0));
21-
}
13+
it('reduces an array', function (): void {
14+
$iterable = [1, 2];
15+
$reduce = static function ($carry, $item) {
16+
return $carry + $item;
17+
};
18+
assertSame(3, iterable_reduce($iterable, $reduce, 0));
19+
});
2220

23-
public function testTraversableReduce(): void
24-
{
25-
$iterable = SplFixedArray::fromArray([1, 2]);
26-
$reduce = static function ($carry, $item) {
27-
return $carry + $item;
28-
};
29-
self::assertSame(3, iterable_reduce($iterable, $reduce, 0));
30-
}
31-
}
21+
it('reduces an traversable', function (): void {
22+
$iterable = SplFixedArray::fromArray([1, 2]);
23+
$reduce = static function ($carry, $item) {
24+
return $carry + $item;
25+
};
26+
assertSame(3, iterable_reduce($iterable, $reduce, 0));
27+
});

0 commit comments

Comments
 (0)