1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\DataObjects\Tests\Casts; |
4
|
|
|
|
5
|
|
|
use ByTIC\DataObjects\Tests\AbstractTest; |
6
|
|
|
use ByTIC\DataObjects\Tests\Fixtures\Models\Books\Book; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SerializedCollectionTest |
10
|
|
|
* @package ByTIC\DataObjects\Tests\Casts |
11
|
|
|
*/ |
12
|
|
|
class AsArrayObjectTest extends AbstractTest |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @dataProvider data_cast_values |
16
|
|
|
*/ |
17
|
|
|
public function test_cast_values($string, $return) |
18
|
|
|
{ |
19
|
|
|
$book = new Book(); |
20
|
|
|
$book->fill(['properties' => $string]); |
21
|
|
|
|
22
|
|
|
/** @var \ArrayObject $propertiesValue */ |
23
|
|
|
$propertiesValue = $book->get('properties'); |
24
|
|
|
self::assertInstanceOf(\ArrayObject::class, $propertiesValue); |
25
|
|
|
|
26
|
|
|
self::assertSame($return, $propertiesValue->getArrayCopy()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function data_cast_values() |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
|
|
[null, []], |
33
|
|
|
['', []], |
34
|
|
|
['N;', []], |
35
|
|
|
['a:0:{}', []], |
36
|
|
|
['b:0;', []], |
37
|
|
|
['a:1:{s:8:"currency";s:3:"RON";}', ['currency' => 'RON']], |
38
|
|
|
[serialize(['test' => 1]), ['test' => 1]], |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function test_cast_null() |
43
|
|
|
{ |
44
|
|
|
$book = new Book(); |
45
|
|
|
$book->fill( |
46
|
|
|
[ |
47
|
|
|
'properties' => null |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
/** @var \ArrayObject $propertiesValue */ |
52
|
|
|
$propertiesValue = $book->get('properties'); |
53
|
|
|
self::assertInstanceOf(\ArrayObject::class, $propertiesValue); |
54
|
|
|
self::assertArrayNotHasKey('option1', $propertiesValue, 1); |
55
|
|
|
self::assertSame(null, $book->getAttribute('properties')); |
56
|
|
|
|
57
|
|
|
$propertiesValue['options3'] = 'value3'; |
58
|
|
|
$book->set('properties', $propertiesValue); |
59
|
|
|
|
60
|
|
|
self::assertSame( |
61
|
|
|
'a:1:{s:8:"options3";s:6:"value3";}', |
62
|
|
|
$book->getAttribute('properties') |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function test_cast_string() |
67
|
|
|
{ |
68
|
|
|
$book = new Book(); |
69
|
|
|
$book->fill( |
70
|
|
|
[ |
71
|
|
|
'properties' => 'N;' |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
/** @var \ArrayObject $propertiesValue */ |
76
|
|
|
$propertiesValue = $book->get('properties'); |
77
|
|
|
self::assertInstanceOf(\ArrayObject::class, $propertiesValue); |
78
|
|
|
self::assertArrayNotHasKey('option1', $propertiesValue, 1); |
79
|
|
|
self::assertSame('N;', $book->getAttribute('properties')); |
80
|
|
|
|
81
|
|
|
$propertiesValue['options3'] = 'value3'; |
82
|
|
|
$book->set('properties', $propertiesValue); |
83
|
|
|
|
84
|
|
|
self::assertSame( |
85
|
|
|
'a:1:{s:8:"options3";s:6:"value3";}', |
86
|
|
|
$book->getAttribute('properties') |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function test_cast_invalid() |
91
|
|
|
{ |
92
|
|
|
$book = new Book(); |
93
|
|
|
$book->fill( |
94
|
|
|
[ |
95
|
|
|
'properties' => '{789}' |
96
|
|
|
] |
97
|
|
|
); |
98
|
|
|
$this->expectError(); |
99
|
|
|
$book->get('properties'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function test_cast() |
103
|
|
|
{ |
104
|
|
|
$properties = ['option1' => 1, 'option2' => '2']; |
105
|
|
|
$propertiesSerialized = serialize($properties); |
106
|
|
|
$book = new Book(); |
107
|
|
|
$book->fill( |
108
|
|
|
[ |
109
|
|
|
'properties' => $propertiesSerialized |
110
|
|
|
] |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
/** @var \ArrayObject $propertiesValue */ |
114
|
|
|
$propertiesValue = $book->get('properties'); |
115
|
|
|
self::assertInstanceOf(\ArrayObject::class, $propertiesValue); |
116
|
|
|
self::assertSame($propertiesValue['option1'], 1); |
117
|
|
|
self::assertSame($propertiesValue['option2'], '2'); |
118
|
|
|
self::assertSame($propertiesSerialized, $book->getAttribute('properties')); |
119
|
|
|
// self::assertSame($propertiesValue->serialize(), $book->getAttribute('properties')); |
120
|
|
|
|
121
|
|
|
$propertiesValue['options3'] = 'value3'; |
122
|
|
|
$book->set('properties', $propertiesValue); |
123
|
|
|
|
124
|
|
|
self::assertSame( |
125
|
|
|
'a:3:{s:7:"option1";i:1;s:7:"option2";s:1:"2";s:8:"options3";s:6:"value3";}', |
126
|
|
|
$book->getAttribute('properties') |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|