Skip to content

Commit

Permalink
Modernize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed Mar 6, 2021
1 parent 6be8dd8 commit afd1f68
Show file tree
Hide file tree
Showing 96 changed files with 1,196 additions and 1,156 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -17,7 +17,7 @@
}
],
"require": {
"php": "~7|~8"
"php": "^7.1|~8"
},
"require-dev": {
"squizlabs/php_codesniffer": "~3.0",
Expand Down
59 changes: 24 additions & 35 deletions phpunit.xml.dist
@@ -1,3 +1,4 @@
<?xml version="1.0"?>
<!--
* Copyright (C) 2011-2017 by Lars Strojny <lstrojny@php.net>
*
Expand All @@ -19,39 +20,27 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
-->
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
convertWarningsToExceptions="true"
convertNoticesToExceptions="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
colors="true">
<filter>
<whitelist>
<directory suffix=".php">src/Functional</directory>
</whitelist>
</filter>

<testsuites>
<testsuite name="unit-tests">
<directory>tests/Functional/</directory>
</testsuite>
</testsuites>

<logging>
<log type="coverage-html" target="build/coverage"
lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="junit" target="build/logs/junit.xml"/>
</logging>

<php>
<ini name="error_reporting" value="-1"/>
<ini name="date.timezone" value="UTC"/>
<ini name="display_errors" value="on"/>
</php>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" convertWarningsToExceptions="true" convertNoticesToExceptions="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" verbose="true" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">

This comment has been minimized.

Copy link
@phanan

phanan Mar 6, 2021

Collaborator

Most of these attributes can be removed, as their values are the same as the default.

<coverage>
<include>
<directory suffix=".php">src/Functional</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage" lowUpperBound="35" highLowerBound="70"/>
</report>
</coverage>
<testsuites>
<testsuite name="unit-tests">
<directory>tests/Functional/</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/logs/junit.xml"/>
</logging>
<php>
<ini name="error_reporting" value="-1"/>
<ini name="date.timezone" value="UTC"/>
<ini name="display_errors" value="on"/>
</php>
</phpunit>
4 changes: 2 additions & 2 deletions tests/Functional/AbstractPartialTestCase.php
Expand Up @@ -14,9 +14,9 @@

abstract class AbstractPartialTestCase extends AbstractTestCase
{
protected function ratio()
protected function ratio(): callable
{
return function ($initial, ...$args) {
return static function ($initial, ...$args) {
return ratio($args, $initial);
};
}
Expand Down
34 changes: 6 additions & 28 deletions tests/Functional/AbstractTestCase.php
Expand Up @@ -14,12 +14,9 @@
use Functional\Exceptions\InvalidArgumentException;
use PHPUnit\Framework\Error\Deprecated;
use PHPUnit\Framework\TestCase;
use Functional as F;
use Traversable;
use TypeError;

use function method_exists;

class AbstractTestCase extends TestCase
{
/** @var array */
Expand All @@ -34,13 +31,13 @@ class AbstractTestCase extends TestCase
/** @var Traversable */
protected $hashIterator;

protected function expectArgumentError($message)
protected function expectArgumentError(string $message): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($message);
}

protected function expectCallableArgumentError($fn, $position, $actualType = 'string')
protected function expectCallableArgumentError(string $fn, int $position, string $actualType = 'string'): void
{
$this->expectException(TypeError::class);

Expand All @@ -50,26 +47,26 @@ protected function expectCallableArgumentError($fn, $position, $actualType = 'st
$this->expectExceptionMessageMatches(
\sprintf(
'/^%s\(\): Argument \#%d( \(\$callback\))? must be of type \??callable, %s given.*/',
\preg_quote($fn),
\preg_quote($fn, '/'),
$position,
$actualType
)
);
}
}

public function exception()
public function exception(): void
{
if (\func_num_args() < 3) {
throw new DomainException('Callback exception');
}

$args = \func_get_args();
$this->assertGreaterThanOrEqual(3, \count($args));
self::assertGreaterThanOrEqual(3, \count($args));
throw new DomainException(\sprintf('Callback exception: %s', $args[1]));
}

protected function sequenceToArray(Traversable $sequence, $limit)
protected function sequenceToArray(Traversable $sequence, $limit): array
{
$values = [];
$sequence->rewind();
Expand All @@ -81,25 +78,6 @@ protected function sequenceToArray(Traversable $sequence, $limit)
return $values;
}

private function getFunctionName()
{
$testName = \get_class($this);
$namespaceSeperatorPosition = \strrpos($testName, '\\') + 1;
$testName = \substr($testName, $namespaceSeperatorPosition);
$function = \strtolower(
\implode(
'_',
\array_slice(
\preg_split('/([A-Z][a-z]+)/', $testName, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY),
0,
-1
)
)
);

return 'Functional\\' . $function;
}

public function expectDeprecation(): void
{
if (\method_exists(parent::class, __FUNCTION__)) {
Expand Down
6 changes: 1 addition & 5 deletions tests/Functional/AnnotationsTest.php
Expand Up @@ -17,10 +17,6 @@

class AnnotationsTest extends AbstractTestCase
{
public function setUp(): void
{
}

public static function getFunctions(): array
{
return group(
Expand All @@ -37,7 +33,7 @@ static function (string $function): bool {
}

/** @dataProvider getFunctions */
public function testNamedArgumentsNotSupportedInFunctions(string $function)
public function testNamedArgumentsNotSupportedInFunctions(string $function): void
{
$refl = new ReflectionFunction($function);
self::assertStringContainsString(
Expand Down
17 changes: 8 additions & 9 deletions tests/Functional/AryTest.php
Expand Up @@ -16,21 +16,20 @@

class AryTest extends AbstractTestCase
{

public function test()
public function test(): void
{
$f = function ($a = 0, $b = 0, $c = 0) {
$f = static function ($a = 0, $b = 0, $c = 0) {
return $a + $b + $c;
};

$this->assertSame(5, $f(5));
$this->assertSame(5, ary($f, 1)(5));
$this->assertSame(5, ary($f, 1)(5));
$this->assertSame(6, ary($f, -1)(6));
$this->assertSame(7, ary($f, 2)(5, 2));
self::assertSame(5, $f(5));
self::assertSame(5, ary($f, 1)(5));
self::assertSame(5, ary($f, 1)(5));
self::assertSame(6, ary($f, -1)(6));
self::assertSame(7, ary($f, 2)(5, 2));
}

public function testException()
public function testException(): void
{
$this->expectException(InvalidArgumentException::class);
$f = function ($a = 0, $b = 0, $c = 0) {
Expand Down
34 changes: 17 additions & 17 deletions tests/Functional/AverageTest.php
Expand Up @@ -43,7 +43,7 @@ class AverageTest extends AbstractTestCase
private $hashIterator3;

/** @before */
public function createTestData()
public function createTestData(): void
{
$this->hash = ['f0' => 12, 'f1' => 2, 'f3' => true, 'f4' => false, 'f5' => 'str', 'f6' => [], 'f7' => new stdClass(), 'f8' => 1];
$this->hashIterator = new ArrayIterator($this->hash);
Expand All @@ -61,25 +61,25 @@ public function createTestData()
$this->listIterator3 = new ArrayIterator($this->list3);
}

public function test()
public function test(): void
{
$this->assertSame(5, average($this->hash));
$this->assertSame(5, average($this->hashIterator));
$this->assertSame(5, average($this->list));
$this->assertSame(5, average($this->listIterator));

$this->assertEqualsWithDelta(0.833333333, average($this->hash2), 0.001);
$this->assertEqualsWithDelta(0.833333333, average($this->hashIterator2), 0.001);
$this->assertEqualsWithDelta(0.833333333, average($this->list2), 0.001);
$this->assertEqualsWithDelta(0.833333333, average($this->listIterator2), 0.001);

$this->assertNull(average($this->hash3));
$this->assertNull(average($this->hashIterator3));
$this->assertNull(average($this->list3));
$this->assertNull(average($this->listIterator3));
self::assertSame(5, average($this->hash));
self::assertSame(5, average($this->hashIterator));
self::assertSame(5, average($this->list));
self::assertSame(5, average($this->listIterator));

self::assertEqualsWithDelta(0.833333333, average($this->hash2), 0.001);
self::assertEqualsWithDelta(0.833333333, average($this->hashIterator2), 0.001);
self::assertEqualsWithDelta(0.833333333, average($this->list2), 0.001);
self::assertEqualsWithDelta(0.833333333, average($this->listIterator2), 0.001);

self::assertNull(average($this->hash3));
self::assertNull(average($this->hashIterator3));
self::assertNull(average($this->list3));
self::assertNull(average($this->listIterator3));
}

public function testPassNoCollection()
public function testPassNoCollection(): void
{
$this->expectArgumentError('Functional\average() expects parameter 1 to be array or instance of Traversable');
average('invalidCollection');
Expand Down
10 changes: 5 additions & 5 deletions tests/Functional/ButLastTest.php
Expand Up @@ -24,14 +24,14 @@ public function setUp(): void
$this->listIterator = new ArrayIterator($this->list);
}

public function test()
public function test(): void
{
$this->assertSame([0 => 1, 1 => 2, 2 => 3], but_last($this->list));
$this->assertSame([0 => 1, 1 => 2, 2 => 3], but_last($this->listIterator));
$this->assertSame([], but_last([]));
self::assertSame([0 => 1, 1 => 2, 2 => 3], but_last($this->list));
self::assertSame([0 => 1, 1 => 2, 2 => 3], but_last($this->listIterator));
self::assertSame([], but_last([]));
}

public function testPassNoCollection()
public function testPassNoCollection(): void
{
$this->expectArgumentError('Functional\but_last() expects parameter 1 to be array or instance of Traversable');
but_last('invalidCollection');
Expand Down
6 changes: 3 additions & 3 deletions tests/Functional/CaptureTest.php
Expand Up @@ -14,7 +14,7 @@

class CaptureTest extends AbstractTestCase
{
public function testCaptureReturnValue()
public function testCaptureReturnValue(): void
{
$fn = capture(
function (...$args) {
Expand All @@ -23,7 +23,7 @@ function (...$args) {
$result
);

$this->assertSame([1, 2], $fn(1, 2));
$this->assertSame([1, 2], $result);
self::assertSame([1, 2], $fn(1, 2));
self::assertSame([1, 2], $result);
}
}
10 changes: 5 additions & 5 deletions tests/Functional/CompareObjectHashOnTest.php
Expand Up @@ -17,18 +17,18 @@

class CompareObjectHashOnTest extends AbstractTestCase
{
public function testCompareValues()
public function testCompareValues(): void
{
$compare = compare_object_hash_on('strcmp');

$this->assertSame(0, $compare($this, $this));
$this->assertNotSame(0, $compare($this, new stdClass()));
self::assertSame(0, $compare($this, $this));
self::assertNotSame(0, $compare($this, new stdClass()));
}

public function testCompareWithReducer()
public function testCompareWithReducer(): void
{
$compare = compare_object_hash_on('strcmp', const_function(new stdClass()));

$this->assertSame(0, $compare($this, new stdClass()));
self::assertSame(0, $compare($this, new stdClass()));
}
}
12 changes: 6 additions & 6 deletions tests/Functional/CompareOnTest.php
Expand Up @@ -15,19 +15,19 @@

class CompareOnTest extends AbstractTestCase
{
public function testCompareValues()
public function testCompareValues(): void
{
$comparator = compare_on('strcmp');

$this->assertSame(-1, $comparator('a', 'b'));
$this->assertSame(0, $comparator('a', 'a'));
$this->assertSame(1, $comparator('b', 'a'));
self::assertSame(-1, $comparator('a', 'b'));
self::assertSame(0, $comparator('a', 'a'));
self::assertSame(1, $comparator('b', 'a'));
}

public function testCompareWithReducer()
public function testCompareWithReducer(): void
{
$comparator = compare_on('strcmp', const_function(1));

$this->assertSame(0, $comparator(0, 1));
self::assertSame(0, $comparator(0, 1));
}
}

0 comments on commit afd1f68

Please sign in to comment.