1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @copyright (c) 2020 Mendel <[email protected]> |
4
|
|
|
* @license see license.txt |
5
|
|
|
*/ |
6
|
|
|
namespace drycart\data\tests; |
7
|
|
|
use drycart\data\MetaDataHelper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author mendel |
11
|
|
|
*/ |
12
|
|
|
class MetadataTest extends \PHPUnit\Framework\TestCase |
13
|
|
|
{ |
14
|
|
|
public function testGetSet() |
15
|
|
|
{ |
16
|
|
|
// Yes, it just for 100% coverage, but thanks to it I find some small bug :) |
17
|
|
|
$helper = new MetaDataHelper(); |
18
|
|
|
$config = [ |
19
|
|
|
'classMeta' => [ |
20
|
|
|
"Description of DummyModel", |
21
|
|
|
'', |
22
|
|
|
"@author mendel" |
23
|
|
|
], |
24
|
|
|
'classRules' => [ |
25
|
|
|
'@author' => [['max']] |
26
|
|
|
] |
27
|
|
|
]; |
28
|
|
|
$helper->setCache([dummy\DummyModel::class=>$config]); |
29
|
|
|
$rules = $helper->classRules(dummy\DummyModel::class); |
30
|
|
|
|
31
|
|
|
$this->assertEquals('max',$rules['@author'][0][0]); |
32
|
|
|
$this->assertEquals([dummy\DummyModel::class=>$config],$helper->getCache()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testClassRules() |
36
|
|
|
{ |
37
|
|
|
$helper = new MetaDataHelper(); |
38
|
|
|
$rules = $helper->classRules(dummy\DummyModel::class); |
39
|
|
|
|
40
|
|
|
$this->assertIsArray($rules); |
41
|
|
|
$this->assertArrayHasKey('@author', $rules); |
42
|
|
|
$this->assertEquals('mendel',$rules['@author'][0][0]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function testDirectFields() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$helper = new MetaDataHelper(); |
48
|
|
|
$fields = $helper->fieldsRules(dummy\DummyModel::class); |
49
|
|
|
|
50
|
|
|
$this->assertIsArray($fields); |
51
|
|
|
$this->assertCount(2, $fields); |
52
|
|
|
$this->assertArrayHasKey('name', $fields); |
53
|
|
|
$this->assertArrayHasKey('age', $fields); |
54
|
|
|
|
55
|
|
|
$this->assertIsArray($fields['name']); |
56
|
|
|
$this->assertCount(1, $fields['name']); |
57
|
|
|
$this->assertArrayHasKey('@var', $fields['name']); |
58
|
|
|
$this->assertEquals([['string','name']],$fields['name']['@var']); |
59
|
|
|
|
60
|
|
|
$this->assertIsArray($fields['age']); |
61
|
|
|
$this->assertCount(1, $fields['age']); |
62
|
|
|
$this->assertArrayHasKey('@var', $fields['age']); |
63
|
|
|
$this->assertEquals([['int','age']],$fields['age']['@var']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testDirectMethods() |
67
|
|
|
{ |
68
|
|
|
$helper = new MetaDataHelper(); |
69
|
|
|
$methods = $helper->methodsRules(dummy\DummyModel::class); |
70
|
|
|
|
71
|
|
|
$this->assertIsArray($methods); |
72
|
|
|
$this->assertCount(2, $methods); |
73
|
|
|
$this->assertArrayHasKey('getSomeString', $methods); |
74
|
|
|
$this->assertArrayHasKey('dehydrate', $methods); |
75
|
|
|
|
76
|
|
|
$this->assertIsArray($methods['getSomeString']); |
77
|
|
|
$this->assertCount(0, $methods['getSomeString']); |
78
|
|
|
|
79
|
|
|
$this->assertIsArray($methods['dehydrate']); |
80
|
|
|
$this->assertCount(3, $methods['dehydrate']); |
81
|
|
|
$this->assertArrayHasKey('@return', $methods['dehydrate']); |
82
|
|
|
$this->assertEquals([['array']],$methods['dehydrate']['@return']); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function testExtendedFields() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$helper = new MetaDataHelper(); |
88
|
|
|
$fields = $helper->fieldsRules(dummy\DummyExtendedModel::class); |
89
|
|
|
|
90
|
|
|
$this->assertIsArray($fields); |
91
|
|
|
$this->assertCount(2, $fields); |
92
|
|
|
$this->assertArrayHasKey('name', $fields); |
93
|
|
|
$this->assertArrayHasKey('age', $fields); |
94
|
|
|
|
95
|
|
|
$this->assertIsArray($fields['name']); |
96
|
|
|
$this->assertCount(1, $fields['name']); |
97
|
|
|
$this->assertArrayHasKey('@var', $fields['name']); |
98
|
|
|
$this->assertEquals([['string','name']],$fields['name']['@var']); |
99
|
|
|
|
100
|
|
|
$this->assertIsArray($fields['age']); |
101
|
|
|
$this->assertCount(1, $fields['age']); |
102
|
|
|
$this->assertArrayHasKey('@var', $fields['age']); |
103
|
|
|
$this->assertEquals([['int','age']],$fields['age']['@var']); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.