1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche/Metadata component. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Metadata\Tests\Units; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Metadata\MethodMetadata; |
15
|
|
|
use Cubiche\Core\Metadata\Tests\Fixtures\User; |
16
|
|
|
use Cubiche\Core\Metadata\Tests\Fixtures\UserId; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* MethodMetadataTests class. |
20
|
|
|
* |
21
|
|
|
* Generated by TestGenerator on 2017-05-16 at 13:17:21. |
22
|
|
|
*/ |
23
|
|
|
class MethodMetadataTests extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
protected function createMethodMetadata($className, $methodName) |
29
|
|
|
{ |
30
|
|
|
return new MethodMetadata($className, $methodName); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Test ClassName method. |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function testClassName() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$this |
39
|
|
|
->given($className = User::class) |
40
|
|
|
->and($methodName = 'username') |
41
|
|
|
->when($methodMetadata = $this->createMethodMetadata($className, $methodName)) |
42
|
|
|
->then() |
43
|
|
|
->string($methodMetadata->className()) |
44
|
|
|
->isEqualTo($className) |
45
|
|
|
; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Test MethodName method. |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function testMethodName() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this |
54
|
|
|
->given($className = User::class) |
55
|
|
|
->and($methodName = 'username') |
56
|
|
|
->when($methodMetadata = $this->createMethodMetadata($className, $methodName)) |
57
|
|
|
->then() |
58
|
|
|
->string($methodMetadata->methodName()) |
59
|
|
|
->isEqualTo($methodName) |
60
|
|
|
; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Test metadata methods. |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function testMetadata() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$this |
69
|
|
|
->given($propertyMetadata = $this->createMethodMetadata(User::class, 'id')) |
70
|
|
|
->when($propertyMetadata->addMetadata('async', true)) |
71
|
|
|
->and($propertyMetadata->addMetadata('methodName', 'getId')) |
72
|
|
|
->then() |
73
|
|
|
->array($propertyMetadata->metadata()) |
74
|
|
|
->hasKey('async') |
75
|
|
|
->hasKey('methodName') |
76
|
|
|
->boolean($propertyMetadata->getMetadata('async')) |
77
|
|
|
->isTrue() |
78
|
|
|
->string($propertyMetadata->getMetadata('methodName')) |
79
|
|
|
->isEqualTo('getId') |
80
|
|
|
; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Test Reflection method. |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function testReflection() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$this |
89
|
|
|
->given($className = User::class) |
90
|
|
|
->and($methodName = 'username') |
91
|
|
|
->when($methodMetadata = $this->createMethodMetadata($className, $methodName)) |
92
|
|
|
->then() |
93
|
|
|
->object($methodMetadata->reflection()) |
94
|
|
|
->isInstanceOf(\ReflectionMethod::class) |
95
|
|
|
->string($methodMetadata->reflection()->class) |
96
|
|
|
->isEqualTo($className) |
97
|
|
|
->string($methodMetadata->reflection()->name) |
98
|
|
|
->isEqualTo($methodName) |
99
|
|
|
; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Test invoke method. |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function testInvoke() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$this |
108
|
|
|
->given($user = new User(UserId::next(), 'Ivan', 'ivannis', 36, '[email protected]')) |
109
|
|
|
->when($methodMetadata = $this->createMethodMetadata(User::class, 'username')) |
110
|
|
|
->then() |
111
|
|
|
->string($methodMetadata->invoke($user)->toNative()) |
112
|
|
|
->isEqualTo('ivannis') |
113
|
|
|
; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Test Serialize/Unserialize method. |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function testSerialize() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$this |
122
|
|
|
->given($methodMetadata = $this->createMethodMetadata(User::class, 'username')) |
123
|
|
|
->and($methodMetadata1 = $this->createMethodMetadata(User::class, 'email')) |
124
|
|
|
->when($methodMetadata->unserialize($methodMetadata1->serialize())) |
125
|
|
|
->then() |
126
|
|
|
->string($methodMetadata->className()) |
127
|
|
|
->isEqualTo(User::class) |
128
|
|
|
->string($methodMetadata->methodName()) |
129
|
|
|
->isEqualTo('email') |
130
|
|
|
; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.