SerializerAdapterTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 76
rs 10
c 1
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpAdapter() 0 10 1
A testThrowsNotDeserializable() 0 11 1
A setUp() 0 3 1
A testCanNormalize() 0 3 1
A provideNormalizationCases() 0 9 1
A provideDeserializationCases() 0 7 1
A testCanDeserialize() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gorynych\Tests\Adapter;
6
7
use Gorynych\Adapter\SerializerAdapter;
8
use Gorynych\Exception\NotDeserializableException;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
13
use Symfony\Component\Serializer\SerializerInterface;
14
15
class SerializerAdapterTest extends TestCase
16
{
17
    /** @var SerializerInterface|MockObject  */
18
    private $serializerMock;
19
20
    public function setUp(): void
21
    {
22
        $this->serializerMock = $this->createMock(SerializerInterface::class);
23
    }
24
25
    public function testThrowsNotDeserializable(): void
26
    {
27
        $this->expectException(NotDeserializableException::class);
28
29
        $this->serializerMock
30
            ->expects($this->once())
31
            ->method('deserialize')
32
            ->with('foo', self::class, 'json')
33
            ->willThrowException(new NotEncodableValueException());
34
35
        $this->setUpAdapter()->deserialize('foo', self::class, 'json');
36
    }
37
38
    /**
39
     * @dataProvider provideNormalizationCases
40
     * @param mixed $data
41
     */
42
    public function testCanNormalize($data, bool $expectedResult): void
43
    {
44
        $this->assertSame($expectedResult, $this->setUpAdapter()->canNormalize($data));
45
    }
46
47
    /**
48
     * @return \Generator<array>
49
     */
50
    public function provideNormalizationCases(): \Generator
51
    {
52
        yield 'object' => [new \stdClass(), true];
53
54
        yield 'array of objects' => [[new \stdClass()], true];
55
56
        yield 'scalar' => ['foo', false];
57
58
        yield 'empty array' => [[], false];
59
    }
60
61
    /**
62
     * @dataProvider provideDeserializationCases
63
     */
64
    public function testCanDeserialize(string $type, bool $expectedResult): void
65
    {
66
        $this->assertSame($expectedResult, $this->setUpAdapter()->canDeserialize($type));
67
    }
68
69
    /**
70
     * @return \Generator<array>
71
     */
72
    public function provideDeserializationCases(): \Generator
73
    {
74
        yield 'existing class' => [self::class, true];
75
76
        yield 'non existent class' => ['Test', false];
77
78
        yield 'request' => [Request::class, false];
79
    }
80
81
    private function setUpAdapter(): SerializerAdapter
82
    {
83
        $reflection = new \ReflectionClass(SerializerAdapter::class);
84
        $serializerAdapter = $reflection->newInstanceWithoutConstructor();
85
86
        $reflectionProperty = $reflection->getProperty('serializer');
87
        $reflectionProperty->setAccessible(true);
88
        $reflectionProperty->setValue($serializerAdapter, $this->serializerMock);
89
90
        return $serializerAdapter;
91
    }
92
}
93