|
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'; |
|
|
|
|
|
|
14
|
|
|
public $bars; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
class B |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
public $b = 'baz'; |
|
|
|
|
|
|
20
|
|
|
public $baz = 'b'; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
final class CollectionStrategyTest extends \Codeception\Test\Unit |
|
|
|
|
|
|
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
|
|
|
|
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.