Skip to content

Refactor: Use pest for tests #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 24, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ on:
- cron: "0 0 1 * *"

jobs:
phpunit:
name: "PHPUnit"
unit-tests:
name: "Unit Tests"
runs-on: "ubuntu-20.04"

strategy:
Expand Down Expand Up @@ -41,20 +41,20 @@ jobs:
with:
dependency-versions: "${{ matrix.dependencies }}"

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

- name: "Upload coverage file"
uses: "actions/upload-artifact@v2"
with:
name: "phpunit-${{ matrix.deps }}-${{ matrix.php-version }}.coverage"
name: "pest-${{ matrix.deps }}-${{ matrix.php-version }}.coverage"
path: "coverage.xml"

upload_coverage:
upload-coverage:
name: "Upload coverage to Codecov"
runs-on: "ubuntu-20.04"
needs:
- "phpunit"
- "unit-tests"

steps:
- name: "Checkout"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ require_once '/path/to/this/library/src/iterable-functions.php';
Unit tests
==========
```
./vendor/bin/phpunit
php vendor/bin/pest
```

[CodeCov Master]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"require-dev": {
"doctrine/coding-standard": "^8.2",
"phpunit/phpunit": "^9",
"pestphp/pest": "^1.0",
"symfony/var-dumper": "^5.2"
},
"config": {
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<exclude name="Generic.Formatting.MultipleStatementAlignment"/>
<exclude name="Squiz.Commenting.FunctionComment.SpacingAfterParamType"/>
<exclude name="SlevomatCodingStandard.Functions.StaticClosure"/>
</rule>

<file>src/</file>
Expand Down
48 changes: 27 additions & 21 deletions tests/IterableFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@

namespace BenTools\IterableFunctions\Tests;

use PHPUnit\Framework\TestCase;
use SplFixedArray;

use function BenTools\IterableFunctions\iterable_filter;
use function BenTools\IterableFunctions\iterable_to_array;
use function it;
use function PHPUnit\Framework\assertEquals;

final class IterableFilterTest extends TestCase
{
public function testArrayFilter(): void
{
$iterable = ['foo', 'bar'];
$filter = static function ($input) {
return $input === 'bar';
};
$this->assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
}

public function testTraversableFilter(): void
{
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
$filter = static function ($input) {
return $input === 'bar';
};
$this->assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
}
}
it('filters an array', function (): void {
$iterable = [false, true];
assertEquals([1 => true], iterable_to_array(iterable_filter($iterable)));
});

it('filters a Travsersable object', function (): void {
$iterable = SplFixedArray::fromArray([false, true]);
assertEquals([1 => true], iterable_to_array(iterable_filter($iterable)));
});

it('filters an array with a callback', function (): void {
$iterable = ['foo', 'bar'];
$filter = static function ($input) {
return $input === 'bar';
};
assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
});

it('filters a Travsersable object with a callback', function (): void {
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
$filter = static function ($input) {
return $input === 'bar';
};
assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
});
28 changes: 12 additions & 16 deletions tests/IterableMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@

namespace BenTools\IterableFunctions\Tests;

use PHPUnit\Framework\TestCase;
use SplFixedArray;

use function BenTools\IterableFunctions\iterable_map;
use function BenTools\IterableFunctions\iterable_to_array;
use function it;
use function PHPUnit\Framework\assertEquals;

final class IterableMapTest extends TestCase
{
public function testArrayMap(): void
{
$iterable = ['foo', 'bar'];
$map = 'strtoupper';
$this->assertEquals(['FOO', 'BAR'], iterable_to_array(iterable_map($iterable, $map)));
}
it('maps an array', function (): void {
$iterable = ['foo', 'bar'];
$map = 'strtoupper';
assertEquals(['FOO', 'BAR'], iterable_to_array(iterable_map($iterable, $map)));
});

public function testTraversableMap(): void
{
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
$map = 'strtoupper';
$this->assertEquals(['FOO', 'BAR'], iterable_to_array(iterable_map($iterable, $map)));
}
}
it('maps a Traversable object', function (): void {
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
$map = 'strtoupper';
assertEquals(['FOO', 'BAR'], iterable_to_array(iterable_map($iterable, $map)));
});
171 changes: 84 additions & 87 deletions tests/IterableObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,100 +4,97 @@

namespace BenTools\IterableFunctions\Tests;

use PHPUnit\Framework\TestCase;
use BenTools\IterableFunctions\IterableObject;
use SplFixedArray;

use function BenTools\IterableFunctions\iterable;
use function it;
use function iterator_to_array;
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertInstanceOf;
use function test;

final class IterableObjectTest extends TestCase
{
/**
* @param array<mixed> $data
* @param array<mixed> $expectedResult
*
* @dataProvider dataProvider
*/
public function testFromArrayToIterator(array $data, ?callable $filter, ?string $map, array $expectedResult): void
{
$iterableObject = iterable($data, $filter, $map);
$this->assertEquals($expectedResult, iterator_to_array($iterableObject));
}
$dataProvider = function () {
$data = ['foo', 'bar'];
$filter = static function ($value) {
return $value === 'bar';
};
$map = 'strtoupper';

/**
* @param array<mixed> $data
* @param array<mixed> $expectedResult
*
* @dataProvider dataProvider
*/
public function testFromArrayToArray(array $data, ?callable $filter, ?string $map, array $expectedResult): void
{
$iterableObject = iterable($data, $filter, $map);
$this->assertEquals($expectedResult, $iterableObject->asArray());
}
yield from [
[
$data,
null,
null,
['foo', 'bar'],
],
[
$data,
$filter,
null,
[1 => 'bar'],
],
[
$data,
null,
$map,
['FOO', 'BAR'],
],
[
$data,
$filter,
$map,
[1 => 'BAR'],
],
];
};

/**
* @param array<mixed> $data
* @param array<mixed> $expectedResult
*
* @dataProvider dataProvider
*/
public function testFromTraversableToIterator(array $data, ?callable $filter, ?string $map, array $expectedResult): void
{
$data = SplFixedArray::fromArray($data);
$iterableObject = iterable($data, $filter, $map);
$this->assertEquals($expectedResult, iterator_to_array($iterableObject));
}
test('input: array | output: traversable', function ($data, $filter, $map, $expectedResult): void {
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, iterator_to_array($iterableObject));
})->with($dataProvider());

/**
* @param array<mixed> $data
* @param array<mixed> $expectedResult
*
* @dataProvider dataProvider
*/
public function testFromTraversableToArray(array $data, ?callable $filter, ?string $map, array $expectedResult): void
{
$data = SplFixedArray::fromArray($data);
$iterableObject = iterable($data, $filter, $map);
$this->assertEquals($expectedResult, $iterableObject->asArray());
}
test('input: array | output: array', function ($data, $filter, $map, $expectedResult): void {
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, $iterableObject->asArray());
})->with($dataProvider());

/**
* @return list<array{array<mixed>, callable|null, string|null, array<mixed>}>>
*/
public function dataProvider(): array
{
$data = ['foo', 'bar'];
$filter = static function ($value) {
return $value === 'bar';
};
$map = 'strtoupper';
test('input: traversable | output: traversable', function ($data, $filter, $map, $expectedResult): void {
$data = SplFixedArray::fromArray($data);
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, iterator_to_array($iterableObject));
})->with($dataProvider());

return [
[
$data,
null,
null,
['foo', 'bar'],
],
[
$data,
$filter,
null,
[1 => 'bar'],
],
[
$data,
null,
$map,
['FOO', 'BAR'],
],
[
$data,
$filter,
$map,
[1 => 'BAR'],
],
];
}
}
test('input: traversable | output: array', function ($data, $filter, $map, $expectedResult): void {
$data = SplFixedArray::fromArray($data);
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, $iterableObject->asArray());
})->with($dataProvider());

it('filters the subject', function (): void {
$filter = static function ($value) {
return $value === 'bar';
};
$iterableObject = iterable(['foo', 'bar'])->filter($filter);
assertEquals([1 => 'bar'], iterator_to_array($iterableObject));
});

it('maps the subject', function (): void {
$map = 'strtoupper';
$iterableObject = iterable(['foo', 'bar'])->map($map);
assertInstanceOf(IterableObject::class, $iterableObject);
assertEquals(['FOO', 'BAR'], iterator_to_array($iterableObject));
});

it('combines filter and map', function (): void {
$filter = static function ($value) {
return $value === 'bar';
};
$map = 'strtoupper';
$iterableObject = iterable(['foo', 'bar'])->map($map)->filter($filter);
assertInstanceOf(IterableObject::class, $iterableObject);
assertEquals([1 => 'BAR'], iterator_to_array($iterableObject));
$iterableObject = iterable(['foo', 'bar'])->filter($filter)->map($map);
assertInstanceOf(IterableObject::class, $iterableObject);
assertEquals([1 => 'BAR'], iterator_to_array($iterableObject));
});
36 changes: 16 additions & 20 deletions tests/IterableReduceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@

namespace BenTools\IterableFunctions\Tests;

use PHPUnit\Framework\TestCase;
use SplFixedArray;

use function BenTools\IterableFunctions\iterable_reduce;
use function it;
use function PHPUnit\Framework\assertSame;

final class IterableReduceTest extends TestCase
{
public function testArrayReduce(): void
{
$iterable = [1, 2];
$reduce = static function ($carry, $item) {
return $carry + $item;
};
self::assertSame(3, iterable_reduce($iterable, $reduce, 0));
}
it('reduces an array', function (): void {
$iterable = [1, 2];
$reduce = static function ($carry, $item) {
return $carry + $item;
};
assertSame(3, iterable_reduce($iterable, $reduce, 0));
});

public function testTraversableReduce(): void
{
$iterable = SplFixedArray::fromArray([1, 2]);
$reduce = static function ($carry, $item) {
return $carry + $item;
};
self::assertSame(3, iterable_reduce($iterable, $reduce, 0));
}
}
it('reduces an traversable', function (): void {
$iterable = SplFixedArray::fromArray([1, 2]);
$reduce = static function ($carry, $item) {
return $carry + $item;
};
assertSame(3, iterable_reduce($iterable, $reduce, 0));
});
Loading