This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
AggregateHydratorFunctionalTest.php
177 lines (146 loc) · 5.64 KB
/
AggregateHydratorFunctionalTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* @see https://github.com/zendframework/zend-hydrator for the canonical source repository
* @copyright Copyright (c) 2010-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-hydrator/blob/master/LICENSE.md New BSD License
*/
declare(strict_types=1);
namespace ZendTest\Hydrator\Aggregate;
use ArrayObject;
use PHPUnit\Framework\TestCase;
use stdClass;
use Zend\Hydrator\Aggregate\AggregateHydrator;
use Zend\Hydrator\Aggregate\ExtractEvent;
use Zend\Hydrator\Aggregate\HydrateEvent;
use Zend\Hydrator\ArraySerializable;
use Zend\Hydrator\ClassMethods;
use Zend\Hydrator\HydratorInterface;
use ZendTest\Hydrator\TestAsset\AggregateObject;
/**
* Integration tests {@see AggregateHydrator}
*/
class AggregateHydratorFunctionalTest extends TestCase
{
/**
* @var AggregateHydrator
*/
protected $hydrator;
/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->hydrator = new AggregateHydrator();
}
/**
* Verifies that no interaction happens when the aggregate hydrator is empty
*/
public function testEmptyAggregate()
{
$object = new ArrayObject(['zaphod' => 'beeblebrox']);
$this->assertSame([], $this->hydrator->extract($object));
$this->assertSame($object, $this->hydrator->hydrate(['arthur' => 'dent'], $object));
$this->assertSame(['zaphod' => 'beeblebrox'], $object->getArrayCopy());
}
/**
* @dataProvider getHydratorSet
*
* Verifies that using a single hydrator will have the aggregate hydrator behave like that single hydrator
*/
public function testSingleHydratorExtraction(HydratorInterface $comparisonHydrator, $object)
{
$blueprint = clone $object;
$this->hydrator->add($comparisonHydrator);
$this->assertSame($comparisonHydrator->extract($blueprint), $this->hydrator->extract($object));
}
/**
* @dataProvider getHydratorSet
*
* Verifies that using a single hydrator will have the aggregate hydrator behave like that single hydrator
*/
public function testSingleHydratorHydration(HydratorInterface $comparisonHydrator, $object, $data)
{
$blueprint = clone $object;
$this->hydrator->add($comparisonHydrator);
$hydratedBlueprint = $comparisonHydrator->hydrate($data, $blueprint);
$hydrated = $this->hydrator->hydrate($data, $object);
$this->assertEquals($hydratedBlueprint, $hydrated);
if ($hydratedBlueprint === $blueprint) {
$this->assertSame($hydrated, $object);
}
}
/**
* Verifies that multiple hydrators in an aggregate merge the extracted data
*/
public function testExtractWithMultipleHydrators()
{
$this->hydrator->add(new ClassMethods());
$this->hydrator->add(new ArraySerializable());
$object = new AggregateObject();
$extracted = $this->hydrator->extract($object);
$this->assertArrayHasKey('maintainer', $extracted);
$this->assertArrayHasKey('president', $extracted);
$this->assertSame('Marvin', $extracted['maintainer']);
$this->assertSame('Zaphod', $extracted['president']);
}
/**
* Verifies that multiple hydrators in an aggregate merge the extracted data
*/
public function testHydrateWithMultipleHydrators()
{
$this->hydrator->add(new ClassMethods());
$this->hydrator->add(new ArraySerializable());
$object = new AggregateObject();
$this->assertSame(
$object,
$this->hydrator->hydrate(['maintainer' => 'Trillian', 'president' => '???'], $object)
);
$this->assertArrayHasKey('maintainer', $object->arrayData);
$this->assertArrayHasKey('president', $object->arrayData);
$this->assertSame('Trillian', $object->arrayData['maintainer']);
$this->assertSame('???', $object->arrayData['president']);
$this->assertSame('Trillian', $object->maintainer);
}
/**
* Verifies that stopping propagation within a listener in the hydrator allows modifying how the
* hydrator behaves
*/
public function testStoppedPropagationInExtraction()
{
$object = new ArrayObject(['president' => 'Zaphod']);
$callback = function (ExtractEvent $event) {
$event->setExtractedData(['Ravenous Bugblatter Beast of Traal']);
$event->stopPropagation();
};
$this->hydrator->add(new ArraySerializable());
$this->hydrator->getEventManager()->attach(ExtractEvent::EVENT_EXTRACT, $callback, 1000);
$this->assertSame(['Ravenous Bugblatter Beast of Traal'], $this->hydrator->extract($object));
}
/**
* Verifies that stopping propagation within a listener in the hydrator allows modifying how the
* hydrator behaves
*/
public function testStoppedPropagationInHydration()
{
$object = new ArrayObject();
$swappedObject = new stdClass();
$callback = function (HydrateEvent $event) use ($swappedObject) {
$event->setHydratedObject($swappedObject);
$event->stopPropagation();
};
$this->hydrator->add(new ArraySerializable());
$this->hydrator->getEventManager()->attach(HydrateEvent::EVENT_HYDRATE, $callback, 1000);
$this->assertSame($swappedObject, $this->hydrator->hydrate(['president' => 'Zaphod'], $object));
}
/**
* Data provider method
*
* @return array
*/
public function getHydratorSet()
{
return [
[new ArraySerializable(), new ArrayObject(['zaphod' => 'beeblebrox']), ['arthur' => 'dent']],
];
}
}