Skip to content

Commit 50a8827

Browse files
committed
improved tests
PHP 8.5 changed ReflectionNamedType::getName() to resolve `self` and `parent` to actual class names.
1 parent b3765f8 commit 50a8827

31 files changed

Lines changed: 2187 additions & 1446 deletions

.github/workflows/tests.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
coverage: none
2121

2222
- run: composer install --no-progress --prefer-dist
23-
- run: vendor/bin/tester tests -s -C
23+
- run: composer tester
2424
- if: failure()
2525
uses: actions/upload-artifact@v4
2626
with:
@@ -39,12 +39,13 @@ jobs:
3939
coverage: none
4040

4141
- run: composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable
42-
- run: vendor/bin/tester tests -s -C
42+
- run: composer tester
4343

4444

4545
code_coverage:
4646
name: Code Coverage
4747
runs-on: ubuntu-latest
48+
continue-on-error: true
4849
steps:
4950
- uses: actions/checkout@v4
5051
- uses: shivammathur/setup-php@v2
@@ -53,7 +54,7 @@ jobs:
5354
coverage: none
5455

5556
- run: composer install --no-progress --prefer-dist
56-
- run: vendor/bin/tester -p phpdbg tests -s -C --coverage ./coverage.xml --coverage-src ./src
57+
- run: composer tester -- -p phpdbg --coverage ./coverage.xml --coverage-src ./src
5758
- run: wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.4.3/php-coveralls.phar
5859
- env:
5960
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"nette/utils": "^4.0.6"
2020
},
2121
"require-dev": {
22-
"nette/tester": "^2.4",
22+
"nette/tester": "^2.6",
2323
"nikic/php-parser": "^5.0",
2424
"tracy/tracy": "^2.8",
2525
"phpstan/phpstan-nette": "^2.0@stable",

tests/PhpGenerator/ClassManipulator.implement.84.phpt

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,46 @@ abstract class TestAbstract extends ParentAbstract
3636
}
3737

3838

39-
$class = new ClassType('TestClass');
40-
$manipulator = new ClassManipulator($class);
41-
42-
// Test interface implementation
43-
$manipulator->implement(TestInterface::class);
44-
Assert::match(<<<'XX'
45-
class TestClass implements TestInterface
46-
{
47-
public array $interfaceProperty;
39+
test('implement adds interface properties with hooks and methods', function () {
40+
$class = new ClassType('TestClass');
41+
$manipulator = new ClassManipulator($class);
42+
$manipulator->implement(TestInterface::class);
43+
Assert::match(<<<'XX'
44+
class TestClass implements TestInterface
45+
{
46+
public array $interfaceProperty;
4847
4948
50-
function interfaceMethod()
51-
{
49+
function interfaceMethod()
50+
{
51+
}
5252
}
53-
}
5453

55-
XX, (string) $class);
54+
XX, (string) $class);
55+
});
5656

5757

58-
// Test abstract class extension
59-
$class = new ClassType('TestClass');
60-
$manipulator = new ClassManipulator($class);
61-
$manipulator->implement(TestAbstract::class);
62-
Assert::match(<<<'XX'
63-
class TestClass extends TestAbstract
64-
{
65-
public array $abstractProperty;
58+
test('implement extends abstract class and adds abstract properties with hooks', function () {
59+
$class = new ClassType('TestClass');
60+
$manipulator = new ClassManipulator($class);
61+
$manipulator->implement(TestAbstract::class);
62+
Assert::match(<<<'XX'
63+
class TestClass extends TestAbstract
64+
{
65+
public array $abstractProperty;
6666
6767
68-
public function abstractMethod()
69-
{
68+
public function abstractMethod()
69+
{
70+
}
7071
}
71-
}
7272

73-
XX, (string) $class);
73+
XX, (string) $class);
74+
});
7475

7576

76-
// Test exception for regular class
77-
Assert::exception(
78-
fn() => $manipulator->implement(stdClass::class),
79-
InvalidArgumentException::class,
80-
"'stdClass' is not an interface or abstract class."
81-
);
77+
testException('implement throws exception for regular class', function () {
78+
$class = new ClassType('TestClass');
79+
$manipulator = new ClassManipulator($class);
80+
$manipulator->implement(stdClass::class);
81+
}, InvalidArgumentException::class, "'stdClass' is not an interface or abstract class.");

tests/PhpGenerator/ClassManipulator.implement.phpt

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,41 @@ abstract class TestAbstract extends ParentAbstract
3434
}
3535

3636

37-
$class = new ClassType('TestClass');
38-
$manipulator = new ClassManipulator($class);
37+
test('implement adds interface methods', function () {
38+
$class = new ClassType('TestClass');
39+
$manipulator = new ClassManipulator($class);
3940

40-
// Test interface implementation
41-
$manipulator->implement(TestInterface::class);
42-
Assert::match(<<<'XX'
43-
class TestClass implements TestInterface
44-
{
45-
function interfaceMethod()
41+
$manipulator->implement(TestInterface::class);
42+
Assert::match(<<<'XX'
43+
class TestClass implements TestInterface
4644
{
45+
function interfaceMethod()
46+
{
47+
}
4748
}
48-
}
4949

50-
XX, (string) $class);
50+
XX, (string) $class);
51+
});
5152

5253

53-
// Test abstract class extension
54-
$class = new ClassType('TestClass');
55-
$manipulator = new ClassManipulator($class);
56-
$manipulator->implement(TestAbstract::class);
57-
Assert::match(<<<'XX'
58-
class TestClass extends TestAbstract
59-
{
60-
public function abstractMethod()
54+
test('implement extends abstract class and adds abstract methods', function () {
55+
$class = new ClassType('TestClass');
56+
$manipulator = new ClassManipulator($class);
57+
$manipulator->implement(TestAbstract::class);
58+
Assert::match(<<<'XX'
59+
class TestClass extends TestAbstract
6160
{
61+
public function abstractMethod()
62+
{
63+
}
6264
}
63-
}
6465

65-
XX, (string) $class);
66+
XX, (string) $class);
67+
});
6668

6769

68-
// Test exception for regular class
69-
Assert::exception(
70-
fn() => $manipulator->implement(stdClass::class),
71-
InvalidArgumentException::class,
72-
"'stdClass' is not an interface or abstract class.",
73-
);
70+
testException('implement throws exception for regular class', function () {
71+
$class = new ClassType('TestClass');
72+
$manipulator = new ClassManipulator($class);
73+
$manipulator->implement(stdClass::class);
74+
}, InvalidArgumentException::class, "'stdClass' is not an interface or abstract class.");

tests/PhpGenerator/ClassManipulator.inheritMethod.phpt

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,42 @@ class Foo
1515
}
1616

1717

18-
// missing parent
19-
$class = new ClassType('Test');
20-
$manipulator = new ClassManipulator($class);
21-
Assert::exception(
22-
fn() => $manipulator->inheritMethod('bar'),
23-
Nette\InvalidStateException::class,
24-
"Class 'Test' has neither setExtends() nor setImplements() set.",
25-
);
26-
27-
$class->setExtends('Unknown1');
28-
$class->addImplement('Unknown2');
29-
Assert::exception(
30-
fn() => $manipulator->inheritMethod('bar'),
31-
Nette\InvalidStateException::class,
32-
"Method 'bar' has not been found in any ancestor: Unknown1, Unknown2",
33-
);
34-
35-
36-
// implement method
37-
$class = new ClassType('Test');
38-
$class->setExtends(Foo::class);
39-
$manipulator = new ClassManipulator($class);
40-
$method = $manipulator->inheritMethod('bar');
41-
Assert::match(<<<'XX'
42-
public function bar(int $a, ...$b): void
43-
{
44-
}
45-
46-
XX, (string) $method);
47-
48-
Assert::same($method, $manipulator->inheritMethod('bar', returnIfExists: true));
49-
Assert::exception(
50-
fn() => $manipulator->inheritMethod('bar', returnIfExists: false),
51-
Nette\InvalidStateException::class,
52-
"Cannot inherit method 'bar', because it already exists.",
53-
);
18+
testException('inheritMethod throws exception when class has no parent', function () {
19+
$class = new ClassType('Test');
20+
$manipulator = new ClassManipulator($class);
21+
$manipulator->inheritMethod('bar');
22+
}, Nette\InvalidStateException::class, "Class 'Test' has neither setExtends() nor setImplements() set.");
23+
24+
25+
testException('inheritMethod throws exception when method not found in ancestors', function () {
26+
$class = new ClassType('Test');
27+
$class->setExtends('Unknown1');
28+
$class->addImplement('Unknown2');
29+
$manipulator = new ClassManipulator($class);
30+
$manipulator->inheritMethod('bar');
31+
}, Nette\InvalidStateException::class, "Method 'bar' has not been found in any ancestor: Unknown1, Unknown2");
32+
33+
34+
test('inheritMethod creates method from parent', function () {
35+
$class = new ClassType('Test');
36+
$class->setExtends(Foo::class);
37+
$manipulator = new ClassManipulator($class);
38+
$method = $manipulator->inheritMethod('bar');
39+
Assert::match(<<<'XX'
40+
public function bar(int $a, ...$b): void
41+
{
42+
}
43+
44+
XX, (string) $method);
45+
46+
Assert::same($method, $manipulator->inheritMethod('bar', returnIfExists: true));
47+
});
48+
49+
50+
testException('inheritMethod throws exception when method already exists', function () {
51+
$class = new ClassType('Test');
52+
$class->setExtends(Foo::class);
53+
$manipulator = new ClassManipulator($class);
54+
$manipulator->inheritMethod('bar');
55+
$manipulator->inheritMethod('bar', returnIfExists: false);
56+
}, Nette\InvalidStateException::class, "Cannot inherit method 'bar', because it already exists.");

tests/PhpGenerator/ClassManipulator.inheritProperty.phpt

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,42 @@ class Foo
1313
}
1414

1515

16-
// missing parent
17-
$class = new ClassType('Test');
18-
$manipulator = new ClassManipulator($class);
19-
Assert::exception(
20-
fn() => $manipulator->inheritProperty('bar'),
21-
Nette\InvalidStateException::class,
22-
"Class 'Test' has neither setExtends() nor setImplements() set.",
23-
);
24-
25-
$class->setExtends('Unknown');
26-
Assert::exception(
27-
fn() => $manipulator->inheritProperty('bar'),
28-
Nette\InvalidStateException::class,
29-
"Property 'bar' has not been found in any ancestor: Unknown",
30-
);
31-
32-
33-
// implement property
34-
$class = new ClassType('Test');
35-
$class->setExtends(Foo::class);
36-
$manipulator = new ClassManipulator($class);
37-
$prop = $manipulator->inheritProperty('bar');
38-
Assert::match(<<<'XX'
39-
class Test extends Foo
40-
{
41-
public array $bar = [123];
42-
}
43-
44-
XX, (string) $class);
45-
46-
Assert::same($prop, $manipulator->inheritProperty('bar', returnIfExists: true));
47-
Assert::exception(
48-
fn() => $manipulator->inheritProperty('bar', returnIfExists: false),
49-
Nette\InvalidStateException::class,
50-
"Cannot inherit property 'bar', because it already exists.",
51-
);
16+
testException('inheritProperty throws exception when class has no parent', function () {
17+
$class = new ClassType('Test');
18+
$manipulator = new ClassManipulator($class);
19+
$manipulator->inheritProperty('bar');
20+
}, Nette\InvalidStateException::class, "Class 'Test' has neither setExtends() nor setImplements() set.");
21+
22+
23+
testException('inheritProperty throws exception when property not found in ancestors', function () {
24+
$class = new ClassType('Test');
25+
$class->setExtends('Unknown');
26+
$manipulator = new ClassManipulator($class);
27+
$manipulator->inheritProperty('bar');
28+
}, Nette\InvalidStateException::class, "Property 'bar' has not been found in any ancestor: Unknown");
29+
30+
31+
test('inheritProperty creates property from parent', function () {
32+
$class = new ClassType('Test');
33+
$class->setExtends(Foo::class);
34+
$manipulator = new ClassManipulator($class);
35+
$prop = $manipulator->inheritProperty('bar');
36+
Assert::match(<<<'XX'
37+
class Test extends Foo
38+
{
39+
public array $bar = [123];
40+
}
41+
42+
XX, (string) $class);
43+
44+
Assert::same($prop, $manipulator->inheritProperty('bar', returnIfExists: true));
45+
});
46+
47+
48+
testException('inheritProperty throws exception when property already exists', function () {
49+
$class = new ClassType('Test');
50+
$class->setExtends(Foo::class);
51+
$manipulator = new ClassManipulator($class);
52+
$manipulator->inheritProperty('bar');
53+
$manipulator->inheritProperty('bar', returnIfExists: false);
54+
}, Nette\InvalidStateException::class, "Cannot inherit property 'bar', because it already exists.");

0 commit comments

Comments
 (0)