Completed
Push — master ( 0456c5...25ee32 )
by Oleg
04:45
created

CollectionStrategyTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 113
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testExtract() 0 24 1
A testHydrate() 0 24 1
A testExtractWrongType() 0 10 1
A testExtractWrongChildrenType() 0 7 1
A testHydrateWrongType() 0 9 1
A testDuringConstruct() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace DataFlow\Tests\Unit\Doctrine\Hydrator\Strategy;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
use SlayerBirden\DataFlowServer\Doctrine\Hydrator\Strategy\CollectionStrategy;
8
use Zend\Hydrator\HydratorInterface;
9
use Zend\Hydrator\ObjectProperty;
10
11
class A
12
{
13
    public $a = 'bar';
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
14
    public $bars;
15
}
16
17
class B
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
18
{
19
    public $b = 'baz';
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
20
    public $baz = 'b';
21
}
22
23
final class CollectionStrategyTest extends \Codeception\Test\Unit
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
24
{
25
    /**
26
     * @var HydratorInterface
27
     */
28
    private $hydrator;
29
30
    protected function setUp()
31
    {
32
        $this->hydrator = new ObjectProperty();
33
        $this->hydrator->addStrategy('bars', new CollectionStrategy(new ObjectProperty(), B::class));
34
    }
35
36
    public function testExtract(): void
37
    {
38
        $b2 = new B();
39
        $b2->baz = 'c';
40
        $b2->b = 'c';
41
        $a = new A();
42
        $a->bars = new ArrayCollection([new B(), $b2]);
43
44
        $expected = [
45
            'a' => 'bar',
46
            'bars' => [
47
                [
48
                    'baz' => 'b',
49
                    'b' => 'baz',
50
                ],
51
                [
52
                    'baz' => 'c',
53
                    'b' => 'c',
54
                ],
55
            ],
56
        ];
57
58
        $this->assertEquals($expected, $this->hydrator->extract($a));
59
    }
60
61
    public function testHydrate(): void
62
    {
63
        $b1 = new B();
64
        $b2 = new B();
65
        $b2->baz = 'c';
66
        $a = new A();
67
        $a->bars = new ArrayCollection([$b1, $b2]);
68
69
        $data = [
70
            'a' => 'bar',
71
            'bars' => [
72
                [
73
                    'baz' => 'b',
74
                    'b' => 'baz',
75
                ],
76
                [
77
                    'baz' => 'c',
78
                    'b' => 'baz',
79
                ],
80
            ],
81
        ];
82
83
        $this->assertEquals($a, $this->hydrator->hydrate($data, new A()));
84
    }
85
86
    /**
87
     * @expectedException \Zend\Hydrator\Exception\InvalidArgumentException
88
     * @expectedExceptionMessage Value needs to be a Doctrine Collection, got array instead
89
     */
90
    public function testExtractWrongType(): void
91
    {
92
        $b2 = new B();
93
        $b2->baz = 'c';
94
        $b2->b = 'c';
95
        $a = new A();
96
        $a->bars = [new B(), $b2];
97
98
        $this->hydrator->extract($a);
99
    }
100
101
    /**
102
     * @expectedException \Zend\Hydrator\Exception\InvalidArgumentException
103
     * @expectedExceptionMessage Value needs to be an instance of
104
     */
105
    public function testExtractWrongChildrenType(): void
106
    {
107
        $a = new A();
108
        $a->bars = new ArrayCollection([new B(), new \stdClass()]);
109
110
        $this->hydrator->extract($a);
111
    }
112
113
    /**
114
     * @expectedException \Zend\Hydrator\Exception\InvalidArgumentException
115
     * @expectedExceptionMessage Value needs to be an Iterable, got stdClass instead.
116
     */
117
    public function testHydrateWrongType(): void
118
    {
119
        $data = [
120
            'a' => 'bar',
121
            'bars' => new \stdClass(),
122
        ];
123
124
        $this->hydrator->hydrate($data, new A());
125
    }
126
127
    /**
128
     * @expectedException \Zend\Hydrator\Exception\InvalidArgumentException
129
     * @expectedExceptionMessage Object class name does not exist: "baz"
130
     */
131
    public function testDuringConstruct(): void
132
    {
133
        new CollectionStrategy(new ObjectProperty(), 'baz');
134
    }
135
}
136