Skip to content

Commit

Permalink
Prevent DNF type tests from being loaded on PHP 8.2
Browse files Browse the repository at this point in the history
dktapps committed Mar 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent a5da330 commit cb9f88f
Showing 11 changed files with 59 additions and 30 deletions.
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd" colors="true" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="CallbackValidator Test Suite">
<directory>./tests</directory>
<directory>./tests/Base</directory>
<directory phpVersion="8.2.0" phpVersionOperator=">=">./tests/Php82</directory>
</testsuite>
</testsuites>
<source>
14 changes: 3 additions & 11 deletions tests/CallbackTypeTest.php → tests/Base/CallbackTypeTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace DaveRandom\CallbackValidator\Test;
namespace DaveRandom\CallbackValidator\Test\Base;

use DaveRandom\CallbackValidator\CallbackType;
use DaveRandom\CallbackValidator\Test\Fixtures\Interface1;
use DaveRandom\CallbackValidator\Test\Fixtures\Interface2;
use DaveRandom\CallbackValidator\Test\Base\Fixtures\Interface1;
use DaveRandom\CallbackValidator\Test\Base\Fixtures\Interface2;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

@@ -26,10 +26,6 @@ public static function returnCovarianceProvider() : \Generator{
yield [function() : Interface1{}, function() : Interface1&Interface2{}, true, "covariant intersection type"];
yield [function() : Interface1&Interface2{}, function() : Interface1{}, false, "given type not covariant with required intersection"];

//DNF types - PHP 8.2+ only
yield [function() : (Interface1&Interface2)|string{}, function() : Interface1{}, false, "given type not covariant with any part of required union"];
yield [function() : (Interface1&Interface2)|string{}, function() : Interface1&Interface2{}, true, "given type covariant with at least 1 part of required union"];

yield [function() : mixed{}, function() : int{}, true, "int is covariant with mixed"];
yield [function() : mixed{}, function() : int|string{}, true, "int|string is covariant with mixed"];
yield [function() : mixed{}, function() : Interface1&Interface2{}, true, "intersection is covariant with mixed"];
@@ -55,9 +51,6 @@ public static function paramContravarianceProvider() : \Generator{
yield [function(Interface1&Interface2 $a) : void{}, function(Interface1&Interface2 $a) : void{}, true, "same type"];
yield [function(Interface1 $a) : void{}, function(Interface1&Interface2 $a) : void{}, false, "intersection given is not contravariant with required"];

yield [function((Interface1&Interface2)|string $a) : void{}, function(Interface1&Interface2 $a) : void{}, false, "given type must accept string"];
yield [function(Interface1&Interface2 $a) : void{}, function((Interface1&Interface2)|string $a) : void{}, true, "given type accepts string, which is not required by the signature"];

yield [function(int $a) : void{}, function(float $a) : void{}, true, "float is contravariant with int"];

yield [function(int $a = 0) : void{}, function(int $a) : void{}, false, "required parameter cannot satisfy optional"];
@@ -77,7 +70,6 @@ public static function paramContravarianceProvider() : \Generator{
yield [function(int $a) : void{}, function(mixed $a) : void{}, true, "mixed is contravariant with int"];
yield [function(int|string $a) : void{}, function(mixed $a) : void{}, true, "mixed is contravariant with int|string"];
yield [function(Interface1&Interface2 $a) : void{}, function(mixed $a) : void{}, true, "mixed is contravariant with intersection"];
yield [function((Interface1&Interface2)|string $a) : void{}, function(mixed $a) : void{}, true, "mixed is contravariant with DNF type"];

yield [function(mixed $a) : void{}, function($a) : void{}, true, "unspecified parameter type is equivalent to mixed"];

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace DaveRandom\CallbackValidator\Test\Fixtures;
namespace DaveRandom\CallbackValidator\Test\Base\Fixtures;

class ClassImplementingInvoke
{
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php declare(strict_types = 1);

namespace DaveRandom\CallbackValidator\Test\Fixtures;
namespace DaveRandom\CallbackValidator\Test\Base\Fixtures;

abstract class ClassImplementingIteratorAggregate implements \IteratorAggregate {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php declare(strict_types = 1);

namespace DaveRandom\CallbackValidator\Test\Fixtures;
namespace DaveRandom\CallbackValidator\Test\Base\Fixtures;

class ClassImplementingNothing {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace DaveRandom\CallbackValidator\Test\Fixtures;
namespace DaveRandom\CallbackValidator\Test\Base\Fixtures;

class ClassImplementingToString
{
7 changes: 7 additions & 0 deletions tests/Base/Fixtures/Interface1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace DaveRandom\CallbackValidator\Test\Base\Fixtures;

interface Interface1{

}
7 changes: 7 additions & 0 deletions tests/Base/Fixtures/Interface2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace DaveRandom\CallbackValidator\Test\Base\Fixtures;

interface Interface2{

}
7 changes: 0 additions & 7 deletions tests/Fixtures/Interface1.php

This file was deleted.

7 changes: 0 additions & 7 deletions tests/Fixtures/Interface2.php

This file was deleted.

36 changes: 36 additions & 0 deletions tests/Php82/CallbackTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace DaveRandom\CallbackValidator\Test\Php82;

use DaveRandom\CallbackValidator\CallbackType;
use DaveRandom\CallbackValidator\Test\Base\Fixtures\Interface1;
use DaveRandom\CallbackValidator\Test\Base\Fixtures\Interface2;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class CallbackTypeTest extends TestCase{

public static function returnCovarianceProvider() : \Generator{
//DNF types - PHP 8.2+ only
yield [function() : (Interface1&Interface2)|string{}, function() : Interface1{}, false, "given type not covariant with any part of required union"];
yield [function() : (Interface1&Interface2)|string{}, function() : Interface1&Interface2{}, true, "given type covariant with at least 1 part of required union"];
}

public static function paramContravarianceProvider() : \Generator{
//DNF types - PHP 8.2+ only
yield [function((Interface1&Interface2)|string $a) : void{}, function(Interface1&Interface2 $a) : void{}, false, "given type must accept string"];
yield [function(Interface1&Interface2 $a) : void{}, function((Interface1&Interface2)|string $a) : void{}, true, "given type accepts string, which is not required by the signature"];

yield [function((Interface1&Interface2)|string $a) : void{}, function(mixed $a) : void{}, true, "mixed is contravariant with DNF type"];
}

#[DataProvider('returnCovarianceProvider')]
#[DataProvider('paramContravarianceProvider')]
public function testCompatibility(\Closure $required, \Closure $given, bool $matches, string $reason) : void{
$required = CallbackType::createFromCallable($required);

$serializedRequire = (string) $required;
$serializedGiven = (string) CallbackType::createFromCallable($given);
self::assertSame($required->isSatisfiedBy($given), $matches, $reason . " ($serializedRequire, $serializedGiven)");
}
}

0 comments on commit cb9f88f

Please sign in to comment.