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
|
|
|
public function test_cast_empty() |
15
|
|
|
{ |
16
|
|
|
$book = new Book(); |
17
|
|
|
$book->fill( |
18
|
|
|
[ |
19
|
|
|
'properties' => '' |
20
|
|
|
] |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
/** @var \ArrayObject $propertiesValue */ |
24
|
|
|
$propertiesValue = $book->get('properties'); |
25
|
|
|
self::assertInstanceOf(\ArrayObject::class, $propertiesValue); |
26
|
|
|
self::assertArrayNotHasKey('option1', $propertiesValue, 1); |
27
|
|
|
self::assertSame('', $book->getAttribute('properties')); |
28
|
|
|
|
29
|
|
|
$propertiesValue['options3'] = 'value3'; |
30
|
|
|
$book->set('properties', $propertiesValue); |
31
|
|
|
|
32
|
|
|
self::assertSame( |
33
|
|
|
'a:1:{s:8:"options3";s:6:"value3";}', |
34
|
|
|
$book->getAttribute('properties') |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
public function test_cast_null() |
38
|
|
|
{ |
39
|
|
|
$book = new Book(); |
40
|
|
|
$book->fill( |
41
|
|
|
[ |
42
|
|
|
'properties' => null |
43
|
|
|
] |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
/** @var \ArrayObject $propertiesValue */ |
47
|
|
|
$propertiesValue = $book->get('properties'); |
48
|
|
|
self::assertInstanceOf(\ArrayObject::class, $propertiesValue); |
49
|
|
|
self::assertArrayNotHasKey('option1', $propertiesValue, 1); |
50
|
|
|
self::assertSame(null, $book->getAttribute('properties')); |
51
|
|
|
|
52
|
|
|
$propertiesValue['options3'] = 'value3'; |
53
|
|
|
$book->set('properties', $propertiesValue); |
54
|
|
|
|
55
|
|
|
self::assertSame( |
56
|
|
|
'a:1:{s:8:"options3";s:6:"value3";}', |
57
|
|
|
$book->getAttribute('properties') |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
public function test_cast_string() |
61
|
|
|
{ |
62
|
|
|
$book = new Book(); |
63
|
|
|
$book->fill( |
64
|
|
|
[ |
65
|
|
|
'properties' => 'N;' |
66
|
|
|
] |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
/** @var \ArrayObject $propertiesValue */ |
70
|
|
|
$propertiesValue = $book->get('properties'); |
71
|
|
|
self::assertInstanceOf(\ArrayObject::class, $propertiesValue); |
72
|
|
|
self::assertArrayNotHasKey('option1', $propertiesValue, 1); |
73
|
|
|
self::assertSame('N;', $book->getAttribute('properties')); |
74
|
|
|
|
75
|
|
|
$propertiesValue['options3'] = 'value3'; |
76
|
|
|
$book->set('properties', $propertiesValue); |
77
|
|
|
|
78
|
|
|
self::assertSame( |
79
|
|
|
'a:1:{s:8:"options3";s:6:"value3";}', |
80
|
|
|
$book->getAttribute('properties') |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function test_cast_invalid() |
85
|
|
|
{ |
86
|
|
|
$book = new Book(); |
87
|
|
|
$book->fill( |
88
|
|
|
[ |
89
|
|
|
'properties' => '{789}' |
90
|
|
|
] |
91
|
|
|
); |
92
|
|
|
$this->expectError(); |
93
|
|
|
$book->get('properties'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function test_cast() |
97
|
|
|
{ |
98
|
|
|
$properties = ['option1' => 1, 'option2' => '2']; |
99
|
|
|
$propertiesSerialized = serialize($properties); |
100
|
|
|
$book = new Book(); |
101
|
|
|
$book->fill( |
102
|
|
|
[ |
103
|
|
|
'properties' => $propertiesSerialized |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
/** @var \ArrayObject $propertiesValue */ |
108
|
|
|
$propertiesValue = $book->get('properties'); |
109
|
|
|
self::assertInstanceOf(\ArrayObject::class, $propertiesValue); |
110
|
|
|
self::assertSame($propertiesValue['option1'], 1); |
111
|
|
|
self::assertSame($propertiesValue['option2'], '2'); |
112
|
|
|
self::assertSame($propertiesSerialized, $book->getAttribute('properties')); |
113
|
|
|
// self::assertSame($propertiesValue->serialize(), $book->getAttribute('properties')); |
114
|
|
|
|
115
|
|
|
$propertiesValue['options3'] = 'value3'; |
116
|
|
|
$book->set('properties', $propertiesValue); |
117
|
|
|
|
118
|
|
|
self::assertSame( |
119
|
|
|
'a:3:{s:7:"option1";i:1;s:7:"option2";s:1:"2";s:8:"options3";s:6:"value3";}', |
120
|
|
|
$book->getAttribute('properties') |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|