Skip to content

Commit 46e91fe

Browse files
committed
improved tests
1 parent 1be42b2 commit 46e91fe

24 files changed

+1733
-1440
lines changed

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
@@ -36,40 +36,41 @@ abstract class TestAbstract extends ParentAbstract
3636
}
3737

3838

39-
$class = new ClassType('TestClass');
40-
$manipulator = new ClassManipulator($class);
39+
test('implement adds interface methods', function () {
40+
$class = new ClassType('TestClass');
41+
$manipulator = new ClassManipulator($class);
4142

42-
// Test interface implementation
43-
$manipulator->implement(TestInterface::class);
44-
Assert::match(<<<'XX'
45-
class TestClass implements TestInterface
46-
{
47-
function interfaceMethod()
43+
$manipulator->implement(TestInterface::class);
44+
Assert::match(<<<'XX'
45+
class TestClass implements TestInterface
4846
{
47+
function interfaceMethod()
48+
{
49+
}
4950
}
50-
}
5151

52-
XX, (string) $class);
52+
XX, (string) $class);
53+
});
5354

5455

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

67-
XX, (string) $class);
68+
XX, (string) $class);
69+
});
6870

6971

70-
// Test exception for regular class
71-
Assert::exception(
72-
fn() => $manipulator->implement(stdClass::class),
73-
InvalidArgumentException::class,
74-
"'stdClass' is not an interface or abstract class.",
75-
);
72+
testException('implement throws exception for regular class', function () {
73+
$class = new ClassType('TestClass');
74+
$manipulator = new ClassManipulator($class);
75+
$manipulator->implement(stdClass::class);
76+
}, 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
@@ -17,39 +17,42 @@ class Foo
1717
}
1818

1919

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

tests/PhpGenerator/ClassType.addMember.phpt

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,32 @@ use Tester\Assert;
88
require __DIR__ . '/../bootstrap.php';
99

1010

11-
$class = (new ClassType('Example'))
12-
->addMember($method = new Nette\PhpGenerator\Method('getHandle'))
13-
->addMember($property = new Nette\PhpGenerator\Property('handle'))
14-
->addMember($const = new Nette\PhpGenerator\Constant('ROLE'))
15-
->addMember($trait = new Nette\PhpGenerator\TraitUse('Foo\Bar'));
16-
17-
Assert::same(['getHandle' => $method], $class->getMethods());
18-
Assert::same(['handle' => $property], $class->getProperties());
19-
Assert::same(['ROLE' => $const], $class->getConstants());
20-
Assert::same(['Foo\Bar' => $trait], $class->getTraits());
21-
Assert::same('', $method->getBody());
22-
23-
24-
// duplicity
25-
$class = new ClassType('Example');
26-
$class->addMember(new Nette\PhpGenerator\Method('foo'));
27-
Assert::exception(
28-
fn() => $class->addMember(new Nette\PhpGenerator\Method('FOO')),
29-
Nette\InvalidStateException::class,
30-
"Cannot add member 'FOO', because it already exists.",
31-
);
32-
33-
$class->addMember($new = new Nette\PhpGenerator\Method('FOO'), overwrite: true);
34-
Assert::same($new, $class->getMethod('FOO'));
11+
test('addMember adds different types of members', function () {
12+
$class = (new ClassType('Example'))
13+
->addMember($method = new Nette\PhpGenerator\Method('getHandle'))
14+
->addMember($property = new Nette\PhpGenerator\Property('handle'))
15+
->addMember($const = new Nette\PhpGenerator\Constant('ROLE'))
16+
->addMember($trait = new Nette\PhpGenerator\TraitUse('Foo\Bar'));
17+
18+
Assert::same(['getHandle' => $method], $class->getMethods());
19+
Assert::same(['handle' => $property], $class->getProperties());
20+
Assert::same(['ROLE' => $const], $class->getConstants());
21+
Assert::same(['Foo\Bar' => $trait], $class->getTraits());
22+
Assert::same('', $method->getBody());
23+
});
24+
25+
26+
testException('addMember throws exception on duplicate member', function () {
27+
$class = new ClassType('Example');
28+
$class->addMember(new Nette\PhpGenerator\Method('foo'));
29+
$class->addMember(new Nette\PhpGenerator\Method('FOO'));
30+
}, Nette\InvalidStateException::class, "Cannot add member 'FOO', because it already exists.");
31+
32+
33+
test('addMember with overwrite replaces existing member', function () {
34+
$class = new ClassType('Example');
35+
$class->addMember(new Nette\PhpGenerator\Method('foo'));
36+
$class->addMember($new = new Nette\PhpGenerator\Method('FOO'), overwrite: true);
37+
38+
Assert::same($new, $class->getMethod('FOO'));
39+
});

0 commit comments

Comments
 (0)