Passed
Push — main ( c2020d...c060b3 )
by Gabriel
13:36
created

AsArrayObjectTest::test_cast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
dl 0
loc 25
rs 9.7666
c 1
b 0
f 1
eloc 15
nc 1
nop 0
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
38
    public function test_cast_invalid()
39
    {
40
        $book = new Book();
41
        $book->fill(
42
            [
43
                'properties' => '{789}'
44
            ]
45
        );
46
        $this->expectError();
47
        $book->get('properties');
48
    }
49
50
    public function test_cast()
51
    {
52
        $properties = ['option1' => 1, 'option2' => '2'];
53
        $propertiesSerialized = serialize($properties);
54
        $book = new Book();
55
        $book->fill(
56
            [
57
                'properties' => $propertiesSerialized
58
            ]
59
        );
60
61
        /** @var \ArrayObject $propertiesValue */
62
        $propertiesValue = $book->get('properties');
63
        self::assertInstanceOf(\ArrayObject::class, $propertiesValue);
64
        self::assertSame($propertiesValue['option1'], 1);
65
        self::assertSame($propertiesValue['option2'], '2');
66
        self::assertSame($propertiesSerialized, $book->getAttribute('properties'));
67
//        self::assertSame($propertiesValue->serialize(), $book->getAttribute('properties'));
68
69
        $propertiesValue['options3'] = 'value3';
70
        $book->set('properties', $propertiesValue);
71
72
        self::assertSame(
73
            'a:3:{s:7:"option1";i:1;s:7:"option2";s:1:"2";s:8:"options3";s:6:"value3";}',
74
            $book->getAttribute('properties')
75
        );
76
    }
77
}
78