Completed
Push — master ( 2e5350...f0c5b4 )
by Tobias
10:07
created

SerializerTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 6
c 3
b 1
f 1
lcom 1
cbo 5
dl 0
loc 62
rs 10
1
<?php
2
3
namespace Happyr\SerializerBundle\Tests\Functional;
4
5
use Happyr\SerializerBundle\PropertyManager\ReflectionPropertyAccess;
6
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
7
use Symfony\Component\Serializer\SerializerInterface;
8
9
class SerializerTestCase extends WebTestCase
10
{
11
    /**
12
     * @var SerializerInterface
13
     */
14
    private $serializer;
15
16
    public function setUp()
17
    {
18
        static::bootKernel();
19
    }
20
21
    /**
22
     * @return SerializerInterface
23
     */
24
    protected function getSerializer()
25
    {
26
        if ($this->serializer) {
27
            return $this->serializer;
28
        }
29
30
        $container = static::$kernel->getContainer();
31
32
        return $this->serializer = $container->get('serializer');
33
    }
34
35
    /**
36
     * @param $obj
37
     *
38
     * @return array assoc
39
     */
40
    protected function serialize($obj, array $context = array())
41
    {
42
        $json = $this->getSerializer()->serialize($obj, 'json', $context);
43
44
        return json_decode($json, true);
45
    }
46
47
    /**
48
     * @param array  $data
49
     * @param string $type
50
     * @param array  $context
51
     *
52
     * @return object
53
     */
54
    protected function deserialize(array $data, $type, array $context = array())
55
    {
56
        return $this->getSerializer()->deserialize(json_encode($data), $type, 'json', $context);
57
    }
58
59
    /**
60
     * Assert $obj->name is $value.
61
     *
62
     * @param object $obj
63
     * @param string $name
64
     * @param mixed  $value
65
     */
66
    protected function assertPropertyValue($obj, $name, $value)
67
    {
68
        $this->assertEquals($value, ReflectionPropertyAccess::get($obj, $name));
69
    }
70
}
71