1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Exsio\PhpObjectDecorator\Tests; |
4
|
|
|
|
5
|
|
|
use Exsio\PhpObjectDecorator\PhpObjectDecorator; |
6
|
|
|
use Exsio\PhpObjectDecorator\PhpObjectDecoratorBehavior; |
7
|
|
|
use Exsio\PhpObjectDecorator\PhpObjectDecoratorException; |
8
|
|
|
use Exsio\PhpObjectDecorator\PhpObjectDecoratorMethodOverride; |
9
|
|
|
use Exsio\PhpObjectDecorator\Tests\Fixtures\Behaviors\InvalidBehaviorDuplicatedMethodInterface; |
10
|
|
|
use Exsio\PhpObjectDecorator\Tests\Fixtures\Behaviors\InvalidBehaviorDuplicatedMethodTrait; |
11
|
|
|
use Exsio\PhpObjectDecorator\Tests\Fixtures\Behaviors\InvalidBehaviorIncompatibleMethodInterface; |
12
|
|
|
use Exsio\PhpObjectDecorator\Tests\Fixtures\Behaviors\InvalidBehaviorIncompatibleMethodTrait; |
13
|
|
|
use Exsio\PhpObjectDecorator\Tests\Fixtures\Behaviors\InvalidBehaviorMissingMethodInterface; |
14
|
|
|
use Exsio\PhpObjectDecorator\Tests\Fixtures\Behaviors\InvalidBehaviorMissingMethodTrait; |
15
|
|
|
use Exsio\PhpObjectDecorator\Tests\Fixtures\ChildObjectToDecorate; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
class PhpObjectDecorationValidationTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
public function testShouldDetectInvalidBehavior_MissingMethods() |
22
|
|
|
{ |
23
|
|
|
//expect: |
24
|
|
|
$this->expectException(PhpObjectDecoratorException::class); |
25
|
|
|
|
26
|
|
|
//when: |
27
|
|
|
$className = "TestClass_" . uniqid(); |
28
|
|
|
PhpObjectDecorator::decorate(new ChildObjectToDecorate()) |
29
|
|
|
->withClassName($className) |
30
|
|
|
->withBehavior(new PhpObjectDecoratorBehavior(InvalidBehaviorMissingMethodInterface::class, InvalidBehaviorMissingMethodTrait::class)) |
31
|
|
|
->get(); |
32
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testShouldDetectInvalidBehavior_InvalidBehaviorArguments_TraitInsteadOfInterface() |
36
|
|
|
{ |
37
|
|
|
//expect: |
38
|
|
|
$this->expectException(PhpObjectDecoratorException::class); |
39
|
|
|
|
40
|
|
|
//when: |
41
|
|
|
$className = "TestClass_" . uniqid(); |
42
|
|
|
PhpObjectDecorator::decorate(new ChildObjectToDecorate()) |
43
|
|
|
->withClassName($className) |
44
|
|
|
->withBehavior(new PhpObjectDecoratorBehavior(InvalidBehaviorIncompatibleMethodTrait::class, InvalidBehaviorMissingMethodTrait::class)) |
45
|
|
|
->get(); |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testShouldDetectInvalidBehavior_InvalidBehaviorArguments_InterfaceInsteadOfTrait() |
50
|
|
|
{ |
51
|
|
|
//expect: |
52
|
|
|
$this->expectException(PhpObjectDecoratorException::class); |
53
|
|
|
|
54
|
|
|
//when: |
55
|
|
|
$className = "TestClass_" . uniqid(); |
56
|
|
|
PhpObjectDecorator::decorate(new ChildObjectToDecorate()) |
57
|
|
|
->withClassName($className) |
58
|
|
|
->withBehavior(new PhpObjectDecoratorBehavior(InvalidBehaviorMissingMethodInterface::class, InvalidBehaviorMissingMethodInterface::class)) |
59
|
|
|
->get(); |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testShouldDetectInvalidBehavior_IncompatibleMethods() |
64
|
|
|
{ |
65
|
|
|
//expect: |
66
|
|
|
$this->expectException(PhpObjectDecoratorException::class); |
67
|
|
|
|
68
|
|
|
//when: |
69
|
|
|
$className = "TestClass_" . uniqid(); |
70
|
|
|
PhpObjectDecorator::decorate(new ChildObjectToDecorate()) |
71
|
|
|
->withClassName($className) |
72
|
|
|
->withBehavior(new PhpObjectDecoratorBehavior(InvalidBehaviorIncompatibleMethodInterface::class, InvalidBehaviorIncompatibleMethodTrait::class)) |
73
|
|
|
->get(); |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testShouldDetectInvalidBehavior_DuplicatedMethods() |
78
|
|
|
{ |
79
|
|
|
//expect: |
80
|
|
|
$this->expectException(PhpObjectDecoratorException::class); |
81
|
|
|
|
82
|
|
|
//when: |
83
|
|
|
$className = "TestClass_" . uniqid(); |
84
|
|
|
PhpObjectDecorator::decorate(new ChildObjectToDecorate()) |
85
|
|
|
->withClassName($className) |
86
|
|
|
->withBehavior(new PhpObjectDecoratorBehavior(InvalidBehaviorDuplicatedMethodInterface::class, InvalidBehaviorDuplicatedMethodTrait::class)) |
87
|
|
|
->get(); |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testShouldDetectInvalidMethodOverride() |
92
|
|
|
{ |
93
|
|
|
//expect: |
94
|
|
|
$this->expectException(PhpObjectDecoratorException::class); |
95
|
|
|
|
96
|
|
|
//when: |
97
|
|
|
$className = "TestClass_" . uniqid(); |
98
|
|
|
PhpObjectDecorator::decorate(new ChildObjectToDecorate()) |
99
|
|
|
->withClassName($className) |
100
|
|
|
->withMethodOverride(new PhpObjectDecoratorMethodOverride("callInParenta", |
101
|
|
|
" |
102
|
|
|
return 'OVERRIDDEN METHOD ' . %CALL_PARENT%; |
103
|
|
|
")) |
104
|
|
|
->get(); |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
} |