TraitUseAdaptationTest.php
3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
class TraitUseAdaptationTest extends \PHPUnit\Framework\TestCase
{
protected function createTraitUseAdaptationBuilder($trait, $method) {
return new TraitUseAdaptation($trait, $method);
}
public function testAsMake() {
$builder = $this->createTraitUseAdaptationBuilder(null, 'foo');
$this->assertEquals(
new Stmt\TraitUseAdaptation\Alias(null, 'foo', null, 'bar'),
(clone $builder)->as('bar')->getNode()
);
$this->assertEquals(
new Stmt\TraitUseAdaptation\Alias(null, 'foo', Class_::MODIFIER_PUBLIC, null),
(clone $builder)->makePublic()->getNode()
);
$this->assertEquals(
new Stmt\TraitUseAdaptation\Alias(null, 'foo', Class_::MODIFIER_PROTECTED, null),
(clone $builder)->makeProtected()->getNode()
);
$this->assertEquals(
new Stmt\TraitUseAdaptation\Alias(null, 'foo', Class_::MODIFIER_PRIVATE, null),
(clone $builder)->makePrivate()->getNode()
);
}
public function testInsteadof() {
$node = $this->createTraitUseAdaptationBuilder('SomeTrait', 'foo')
->insteadof('AnotherTrait')
->getNode()
;
$this->assertEquals(
new Stmt\TraitUseAdaptation\Precedence(
new Name('SomeTrait'),
'foo',
[new Name('AnotherTrait')]
),
$node
);
}
public function testAsOnNotAlias() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot set alias for not alias adaptation buider');
$this->createTraitUseAdaptationBuilder('Test', 'foo')
->insteadof('AnotherTrait')
->as('bar')
;
}
public function testInsteadofOnNotPrecedence() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot add overwritten traits for not precedence adaptation buider');
$this->createTraitUseAdaptationBuilder('Test', 'foo')
->as('bar')
->insteadof('AnotherTrait')
;
}
public function testInsteadofWithoutTrait() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Precedence adaptation must have trait');
$this->createTraitUseAdaptationBuilder(null, 'foo')
->insteadof('AnotherTrait')
;
}
public function testMakeOnNotAlias() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot set access modifier for not alias adaptation buider');
$this->createTraitUseAdaptationBuilder('Test', 'foo')
->insteadof('AnotherTrait')
->makePublic()
;
}
public function testMultipleMake() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Multiple access type modifiers are not allowed');
$this->createTraitUseAdaptationBuilder(null, 'foo')
->makePrivate()
->makePublic()
;
}
public function testUndefinedType() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Type of adaptation is not defined');
$this->createTraitUseAdaptationBuilder(null, 'foo')
->getNode()
;
}
}