Skip to content

Commit 452a023

Browse files
committed
Add broken unit tests for PHP8.0-PHP8.2 files and SelfValueTransformer
1 parent 6bcf76b commit 452a023

File tree

4 files changed

+386
-24
lines changed

4 files changed

+386
-24
lines changed

Diff for: tests/Go/Instrument/Transformer/SelfValueTransformerTest.php

+42-24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Go\Core\AspectContainer;
1616
use Go\Core\AspectKernel;
17+
use PHPUnit\Framework\Attributes\DataProvider;
1718
use PHPUnit\Framework\MockObject\MockObject;
1819
use PHPUnit\Framework\TestCase;
1920

@@ -43,15 +44,7 @@ public function setUp(): void
4344
*/
4445
protected function getKernelMock(array $options): AspectKernel
4546
{
46-
$mock = $this->getMockForAbstractClass(
47-
AspectKernel::class,
48-
[],
49-
'',
50-
false,
51-
true,
52-
true,
53-
['getOptions', 'getContainer']
54-
);
47+
$mock = $this->createMock(AspectKernel::class);
5548
$mock
5649
->method('getOptions')
5750
->willReturn($options);
@@ -63,23 +56,48 @@ protected function getKernelMock(array $options): AspectKernel
6356
return $mock;
6457
}
6558

66-
public function testTransformerReplacesAllSelfPlaces(): void
67-
{
68-
$testFile = fopen(__DIR__ . '/_files/file-with-self.php', 'rb');
69-
$content = stream_get_contents($testFile);
70-
$metadata = new StreamMetaData($testFile, $content);
71-
$this->transformer->transform($metadata);
72-
$expected = file_get_contents(__DIR__ . '/_files/file-with-self-transformed.php');
73-
$this->assertSame($expected, (string) $metadata->source);
59+
#[DataProvider("filesDataProvider")]
60+
public function testTransformerProcessFiles(
61+
string $sourceFileWithContent,
62+
string $fileWithExpectedContent,
63+
): void {
64+
try {
65+
$sourceFile = fopen($sourceFileWithContent, 'rb');
66+
$sourceContent = stream_get_contents($sourceFile);
67+
$sourceMetadata = new StreamMetaData($sourceFile, $sourceContent);
68+
$this->transformer->transform($sourceMetadata);
69+
70+
$expected = file_get_contents($fileWithExpectedContent);
71+
$this->assertSame($expected, $sourceMetadata->source);
72+
73+
} finally {
74+
if (isset($sourceFile) && is_resource($sourceFile)) {
75+
fclose($sourceFile);
76+
}
77+
}
7478
}
7579

76-
public function testTransformerReplacesAllSelfPlacesWithoutNamespace(): void
80+
public static function filesDataProvider(): \Generator
7781
{
78-
$testFile = fopen(__DIR__ . '/_files/file-with-self-no-namespace.php', 'rb');
79-
$content = stream_get_contents($testFile);
80-
$metadata = new StreamMetaData($testFile, $content);
81-
$this->transformer->transform($metadata);
82-
$expected = file_get_contents(__DIR__ . '/_files/file-with-self-no-namespace-transformed.php');
83-
$this->assertSame($expected, (string) $metadata->source);
82+
yield 'file-with-self.php' => [
83+
__DIR__ . '/_files/file-with-self.php',
84+
__DIR__ . '/_files/file-with-self-transformed.php'
85+
];
86+
yield 'file-with-self-no-namespace.php' => [
87+
__DIR__ . '/_files/file-with-self-no-namespace.php',
88+
__DIR__ . '/_files/file-with-self-no-namespace-transformed.php'
89+
];
90+
yield 'php80-file.php' => [
91+
__DIR__ . '/_files/php80-file.php',
92+
__DIR__ . '/_files/php80-file-transformed.php'
93+
];
94+
yield 'php81-file.php' => [
95+
__DIR__ . '/_files/php81-file.php',
96+
__DIR__ . '/_files/php81-file-transformed.php'
97+
];
98+
yield 'php82-file.php' => [
99+
__DIR__ . '/_files/php82-file.php',
100+
__DIR__ . '/_files/php82-file-transformed.php'
101+
];
84102
}
85103
}
+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* Parser Reflection API
4+
*
5+
* @copyright Copyright 2016, Lisachenko Alexander <[email protected]>
6+
*
7+
* This source file is subject to the license that is bundled
8+
* with this source code in the file LICENSE.
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace Go\ParserReflection\Stub;
13+
14+
use Attribute;
15+
use Go\ParserReflection\{ReflectionMethod, ReflectionProperty as P};
16+
17+
class ClassWithPhp80Features
18+
{
19+
public function acceptsStringArrayDefaultToNull(array|string $iterable = null) : array {}
20+
}
21+
22+
/**
23+
* @see https://php.watch/versions/8.0/named-parameters
24+
*/
25+
class ClassWithPHP80NamedCall
26+
{
27+
public static function foo(string $key1 = '', string $key2 = ''): string
28+
{
29+
return $key1 . ':' . $key2;
30+
}
31+
32+
public static function namedCall(): array
33+
{
34+
return [
35+
'key1' => self::foo(key1: 'bar'),
36+
'key2' => self::foo(key2: 'baz'),
37+
'keys' => self::foo(key1: 'A', key2: 'B'),
38+
'reverseKeys' => self::foo(key2: 'A', key1: 'B'),
39+
'unpack' => self::foo(...['key1' => 'C', 'key2' => 'D']),
40+
];
41+
}
42+
}
43+
44+
/**
45+
* @see https://php.watch/versions/8.0/attributes
46+
*/
47+
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)]
48+
readonly class ClassPHP80Attribute
49+
{
50+
private string $value;
51+
52+
public function __construct(string $value)
53+
{
54+
$this->value = $value;
55+
}
56+
57+
public function getValue(): string
58+
{
59+
return $this->value;
60+
}
61+
}
62+
63+
/**
64+
* @see https://php.watch/versions/8.0/attributes
65+
*/
66+
#[ClassPHP80Attribute('class')]
67+
class ClassPHP80WithAttribute
68+
{
69+
#[ClassPHP80Attribute('first')]
70+
#[ClassPHP80Attribute('second')]
71+
public const PUBLIC_CONST = 1;
72+
73+
#[ClassPHP80Attribute('property')]
74+
private string $privateProperty = 'foo';
75+
76+
#[ClassPHP80Attribute('method')]
77+
public function bar(#[ClassPHP80Attribute('parameter')] $parameter)
78+
{}
79+
}
80+
81+
/**
82+
* @see https://php.watch/versions/8.0/constructor-property-promotion
83+
*/
84+
class ClassPHP80WithPropertyPromotion
85+
{
86+
public function __construct(
87+
private string $privateStringValue,
88+
private $privateNonTypedValue,
89+
protected int $protectedIntValue = 42,
90+
public array $publicArrayValue = [M_PI, M_E],
91+
) {}
92+
}
93+
94+
/**
95+
* @see https://php.watch/versions/8.0/union-types
96+
*/
97+
class ClassWithPHP80UnionTypes
98+
{
99+
public string|int|float|bool $scalarValue;
100+
101+
public array|object|null $complexValueOrNull = null;
102+
103+
/**
104+
* Special case, internally iterable should be replaced with Traversable|array
105+
*/
106+
public iterable|object $iterableOrObject;
107+
108+
public static function returnsUnionType(): object|array|null {}
109+
110+
public static function acceptsUnionType(\stdClass|\Traversable|array $iterable): void {}
111+
}
112+
113+
/**
114+
* @see https://php.watch/versions/8.0/mixed-type
115+
*/
116+
class ClassWithPHP80MixedType
117+
{
118+
public mixed $someMixedPublicProperty;
119+
120+
public static function returnsMixed(): mixed {}
121+
122+
public static function acceptsMixed(mixed $value): void {}
123+
}
124+
125+
/**
126+
* @see https://php.watch/versions/8.0/static-return-type
127+
*/
128+
class ClassWithPHP80StaticReturnType
129+
{
130+
public static function create(): static {}
131+
}
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Parser Reflection API
4+
*
5+
* @copyright Copyright 2024, Lisachenko Alexander <[email protected]>
6+
*
7+
* This source file is subject to the license that is bundled
8+
* with this source code in the file LICENSE.
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace Go\ParserReflection\Stub;
13+
14+
/**
15+
* @see https://php.watch/versions/8.1/readonly
16+
*/
17+
class ClassWithPhp81ReadOnlyProperties
18+
{
19+
public readonly int $publicReadonlyInt;
20+
21+
protected readonly array $protectedReadonlyArray;
22+
23+
private readonly object $privateReadonlyObject;
24+
}
25+
26+
/**
27+
* @see https://php.watch/versions/8.1/enums
28+
*/
29+
enum SimplePhp81EnumWithSuit {
30+
case Clubs;
31+
case Diamonds;
32+
case Hearts;
33+
case Spades;
34+
}
35+
36+
/**
37+
* @see https://php.watch/versions/8.1/enums#enums-backed
38+
*/
39+
enum BackedPhp81EnumHTTPMethods: string
40+
{
41+
case GET = 'get';
42+
case POST = 'post';
43+
}
44+
45+
/**
46+
* @see https://php.watch/versions/8.1/enums#enum-methods
47+
*/
48+
enum BackedPhp81EnumHTTPStatusWithMethod: int
49+
{
50+
case OK = 200;
51+
case ACCESS_DENIED = 403;
52+
case NOT_FOUND = 404;
53+
54+
public function label(): string {
55+
return static::getLabel($this);
56+
}
57+
58+
public static function getLabel(self $value): string {
59+
return match ($value) {
60+
self::OK => 'OK',
61+
self::ACCESS_DENIED => 'Access Denied',
62+
self::NOT_FOUND => 'Page Not Found',
63+
};
64+
}
65+
}
66+
67+
/**
68+
* @see https://php.watch/versions/8.1/intersection-types
69+
*/
70+
class ClassWithPhp81IntersectionType implements \Countable
71+
{
72+
private \Iterator&\Countable $countableIterator;
73+
74+
public function __construct(\Iterator&\Countable $countableIterator)
75+
{
76+
$this->countableIterator = $countableIterator;
77+
}
78+
79+
public function count(): int
80+
{
81+
return count($this->countableIterator);
82+
}
83+
}
84+
85+
/**
86+
* @see https://php.watch/versions/8.1/intersection-types
87+
*/
88+
function functionWithPhp81IntersectionType(\Iterator&\Countable $value): \Iterator&\Countable {
89+
foreach($value as $val) {}
90+
count($value);
91+
92+
return $value;
93+
}
94+
95+
/**
96+
* @see https://php.watch/versions/8.1/never-return-type
97+
*/
98+
class ClassWithPhp81NeverReturnType
99+
{
100+
public static function doThis(): never
101+
{
102+
throw new \RuntimeException('Not implemented');
103+
}
104+
}
105+
106+
/**
107+
* @see https://php.watch/versions/8.1/never-return-type
108+
*/
109+
function functionWithPhp81NeverReturnType(): never
110+
{
111+
throw new \RuntimeException('Not implemented');
112+
}
113+
114+
/**
115+
* @see https://php.watch/versions/8.1/final-class-const
116+
*/
117+
class ClassWithPhp81FinalClassConst {
118+
final public const TEST = '1';
119+
}

0 commit comments

Comments
 (0)