|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\DataObjects\Tests\Casts; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\DataObjects\Casts\Metadata\Metadata; |
|
6
|
|
|
use ByTIC\DataObjects\Tests\AbstractTest; |
|
7
|
|
|
use ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class SerializedCollectionTest |
|
11
|
|
|
* @package ByTIC\DataObjects\Tests\Casts |
|
12
|
|
|
*/ |
|
13
|
|
|
class AsMetadataObjectTest extends AbstractTest |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @dataProvider data_cast_values |
|
17
|
|
|
*/ |
|
18
|
|
|
public function test_cast_values($string, $return) |
|
19
|
|
|
{ |
|
20
|
|
|
$book = new Book(); |
|
21
|
|
|
$book->fill(['metadata' => $string]); |
|
22
|
|
|
|
|
23
|
|
|
/** @var Metadata $propertiesValue */ |
|
24
|
|
|
$propertiesValue = $book->get('metadata'); |
|
25
|
|
|
self::assertInstanceOf(\ByTIC\DataObjects\Casts\Metadata\Metadata::class, $propertiesValue); |
|
26
|
|
|
|
|
27
|
|
|
self::assertSame($return, $propertiesValue->getArrayCopy()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return array[] |
|
32
|
|
|
*/ |
|
33
|
|
|
public function data_cast_values() |
|
34
|
|
|
{ |
|
35
|
|
|
return [ |
|
36
|
|
|
[null, []], |
|
37
|
|
|
['', []], |
|
38
|
|
|
['{}', []], |
|
39
|
|
|
[json_encode(['currency' => 'RON']), ['currency' => 'RON']], |
|
40
|
|
|
[json_encode(['test' => 1]), ['test' => 1]], |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function test_cast_null() |
|
45
|
|
|
{ |
|
46
|
|
|
$book = new Book(); |
|
47
|
|
|
$book->fill( |
|
48
|
|
|
[ |
|
49
|
|
|
'metadata' => null |
|
50
|
|
|
] |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
/** @var Metadata $propertiesValue */ |
|
54
|
|
|
$propertiesValue = $book->get('metadata'); |
|
55
|
|
|
self::assertInstanceOf(Metadata::class, $propertiesValue); |
|
56
|
|
|
self::assertArrayNotHasKey('option1', $propertiesValue, 1); |
|
57
|
|
|
self::assertSame(null, $book->getAttribute('metadata')); |
|
58
|
|
|
|
|
59
|
|
|
$propertiesValue['options3'] = 'value3'; |
|
60
|
|
|
$book->set('metadata', $propertiesValue); |
|
61
|
|
|
|
|
62
|
|
|
self::assertSame( |
|
63
|
|
|
'{"options3":"value3"}', |
|
64
|
|
|
$book->getAttribute('metadata') |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function test_cast_string() |
|
69
|
|
|
{ |
|
70
|
|
|
$book = new Book(); |
|
71
|
|
|
$book->fill( |
|
72
|
|
|
[ |
|
73
|
|
|
'metadata' => '{}' |
|
74
|
|
|
] |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
/** @var Metadata $propertiesValue */ |
|
78
|
|
|
$propertiesValue = $book->get('metadata'); |
|
79
|
|
|
self::assertInstanceOf(Metadata::class, $propertiesValue); |
|
80
|
|
|
self::assertArrayNotHasKey('option1', $propertiesValue, 1); |
|
81
|
|
|
self::assertSame('{}', $book->getAttribute('metadata')); |
|
82
|
|
|
|
|
83
|
|
|
$propertiesValue['options3'] = 'value3'; |
|
84
|
|
|
$book->set('metadata', $propertiesValue); |
|
85
|
|
|
|
|
86
|
|
|
self::assertSame( |
|
87
|
|
|
'{"options3":"value3"}', |
|
88
|
|
|
$book->getAttribute('metadata') |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function test_cast_invalid() |
|
93
|
|
|
{ |
|
94
|
|
|
$book = new Book(); |
|
95
|
|
|
$book->fill( |
|
96
|
|
|
[ |
|
97
|
|
|
'metadata' => '{789}' |
|
98
|
|
|
] |
|
99
|
|
|
); |
|
100
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
101
|
|
|
$book->get('metadata'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function test_cast() |
|
105
|
|
|
{ |
|
106
|
|
|
$properties = ['option1' => 1, 'option2' => '2', 'option3' => ['opt33' => '33']]; |
|
107
|
|
|
$propertiesSerialized = json_encode($properties); |
|
108
|
|
|
$book = new Book(); |
|
109
|
|
|
$book->fill( |
|
110
|
|
|
[ |
|
111
|
|
|
'metadata' => $propertiesSerialized |
|
112
|
|
|
] |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
/** @var Metadata $propertiesValue */ |
|
116
|
|
|
$propertiesValue = $book->get('metadata'); |
|
117
|
|
|
self::assertInstanceOf(Metadata::class, $propertiesValue); |
|
118
|
|
|
self::assertSame($propertiesValue->get('option1'), 1); |
|
119
|
|
|
self::assertSame($propertiesValue->get('option3.opt33'), '33'); |
|
120
|
|
|
self::assertSame($propertiesSerialized, $book->getAttribute('metadata')); |
|
121
|
|
|
|
|
122
|
|
|
$book->metadata->set('option2', '22'); |
|
|
|
|
|
|
123
|
|
|
$book->metadata->set('option3.opt33', '333'); |
|
124
|
|
|
|
|
125
|
|
|
self::assertSame( |
|
126
|
|
|
'{"option1":1,"option2":"22","option3":{"opt33":"333"}}', |
|
127
|
|
|
$book->getAttribute('metadata') |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.