Skip to content

Commit

Permalink
chore: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed May 8, 2023
1 parent d602fa1 commit 23c085e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 35 deletions.
23 changes: 6 additions & 17 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
executionOrder="random"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="false"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
cacheDirectory=".phpunit.cache"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="true"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="./vendor/autoload.php" colors="true" executionOrder="random" beStrictAboutChangesToGlobalState="true" beStrictAboutOutputDuringTests="false" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" cacheDirectory=".phpunit.cache" requireCoverageMetadata="false" beStrictAboutCoverageMetadata="true">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests/unit</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<html outputDirectory="build/logs/coverage/html-coverage"/>
<clover outputFile="build/logs/coverage/clover.xml"/>
Expand All @@ -30,4 +14,9 @@
<logging>
<junit outputFile="build/logs/coverage/junit.xml"/>
</logging>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions tests/unit/InterruptableIterableIteratorAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public function testBreakInterruption()
$i = 0;

while (true) {
yield $i++;
yield sprintf('[%s]', $i) => $i++;
}
};

$subject = new InterruptableIterableIteratorAggregate($naturals());

self::assertSame(
range(0, 10),
array_combine(array_map(static fn (int $a): string => sprintf('[%s]', $a), range(0, 10)), range(0, 10)),
iterator_to_array($this->getSubjectGenerator($subject))
);
}
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/MapIterableAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ final class MapIterableAggregateTest extends TestCase
{
public function testBasic(): void
{
$input = array_combine(range('a', 'c'), range('a', 'c'));

$iterator = (new MapIterableAggregate(
range('a', 'c'),
static fn (string $letter, int $key, iterable $iterable): string => sprintf('%s::%s::%s', $key, $letter, gettype($iterable))
$input,
static fn (string $letter, string $key, iterable $iterable): string => sprintf('%s::%s::%s', $key, $letter, gettype($iterable))
));

$expected = [
'0::a::array',
'1::b::array',
'2::c::array',
'a' => 'a::a::array',
'b' => 'b::b::array',
'c' => 'c::c::array',
];

self::assertSame($expected, iterator_to_array($iterator));
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/MersenneTwisterRNGIteratorAggregatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,42 @@ public function testBasic(): void
self::assertNotSame($expected, iterator_to_array($iterator));
}

public function testSeed(): void
{
$seed = -1;
$innerIterator = (new MersenneTwisterRNGIteratorAggregate())->withSeed($seed);

$iterator = new LimitIterator($innerIterator->getIterator(), 0, 10);

$expected = [
-7422378986580066014,
7607120784305990727,
5331760257347419712,
5165154706003526138,
-8892443392435847392,
8660855838641975931,
1718781808164485227,
-6527986301322975030,
7551637291286289549,
991259994952170583,
];

self::assertSame($expected, iterator_to_array($iterator));

$iterator = new LimitIterator($innerIterator->withSeed(-1)->getIterator(), 0, 10);

self::assertSame($expected, iterator_to_array($iterator));
}

public function testWithers(): void
{
$iterator = (new MersenneTwisterRNGIteratorAggregate());

self::assertNotEquals($iterator, $iterator->withMax(10));
self::assertNotEquals($iterator, $iterator->withMin(0));
self::assertNotEquals($iterator, $iterator->withSeed(123));
}

public function testWithSeed(): void
{
$seed = 123;
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/PausableIteratorAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class PausableIteratorAggregateTest extends TestCase
public function testGetRest(): void
{
$input = static function (): Generator {
yield from range('a', 'e');
yield from array_combine(range('a', 'e'), range('a', 'e'));
};

$iterator = (new PausableIteratorAggregate($input()));
Expand Down Expand Up @@ -54,16 +54,16 @@ public function testGetRest(): void
}

$expected = [
[0, 'a'],
[1, 'b'],
[2, 'c'],
[3, 'd'],
[4, 'e'],
[0, 'a'],
[1, 'b'],
[2, 'c'],
[3, 'd'],
[4, 'e'],
['a', 'a'],
['b', 'b'],
['c', 'c'],
['d', 'd'],
['e', 'e'],
['a', 'a'],
['b', 'b'],
['c', 'c'],
['d', 'd'],
['e', 'e'],
];

self::assertSame($expected, $a);
Expand Down

0 comments on commit 23c085e

Please sign in to comment.