Skip to content

Commit 1846858

Browse files
author
Jelle van Oosterbosch
committed
Added unit test for the IterableNormalizer class
1 parent 31f1837 commit 1846858

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["bar",1,[["2020-01-01T00:00:00+00:00","2020-01-01T00:00:00+00:00"]]]
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* This file is part of the GDPR bundle.
4+
*
5+
* @category Bundle
6+
* @package Gdpr
7+
* @author SuperBrave <info@superbrave.nl>
8+
* @copyright 2018 SuperBrave <info@superbrave.nl>
9+
* @license https://github.com/superbrave/gdpr-bundle/blob/master/LICENSE MIT
10+
* @link https://www.superbrave.nl/
11+
*/
12+
13+
namespace Superbrave\GdprBundle\Tests\Serializer\Normalizer;
14+
15+
use Doctrine\Common\Collections\ArrayCollection;
16+
use Superbrave\GdprBundle\Serializer\Normalizer\IterableNormalizer;
17+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
18+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
19+
use Symfony\Component\Serializer\Serializer;
20+
21+
/**
22+
* AnnotationNormalizerTest.
23+
*
24+
* @author Jelle van Oosterbosch <jvo@superbrave.nl>
25+
*/
26+
class IterableNormalizerTest extends \PHPUnit_Framework_TestCase
27+
{
28+
/**
29+
* @var IterableNormalizer
30+
*/
31+
private $normalizer;
32+
33+
/**
34+
* @var Serializer
35+
*/
36+
private $serializer;
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
public function setUp()
42+
{
43+
$this->normalizer = new IterableNormalizer();
44+
$this->serializer = new Serializer([
45+
new DateTimeNormalizer(),
46+
$this->normalizer,
47+
], [
48+
new JsonEncoder()
49+
]);
50+
51+
$this->normalizer->setNormalizer($this->serializer);
52+
}
53+
54+
/**
55+
* Tests if @see IterableNormalizer::supportsNormalization() returns false
56+
* when the data is not iterable.
57+
*
58+
* @return void
59+
*/
60+
public function testSupportsNormalizationReturnsFalseWhenDataIsNotIterable()
61+
{
62+
$this->assertFalse($this->normalizer->supportsNormalization('no object'));
63+
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
64+
}
65+
66+
/**
67+
* Tests if @see IterableNormalizer::supportsNormalization() returns true
68+
* when the data is iterable.
69+
*
70+
* @return void
71+
*/
72+
public function testSupportsNormalizationReturnsTrueWhenDataIsIterable()
73+
{
74+
$this->assertTrue($this->normalizer->supportsNormalization(array()));
75+
$this->assertTrue($this->normalizer->supportsNormalization(new ArrayCollection()));
76+
}
77+
78+
/**
79+
* Tests if @see IterableNormalizer::$normalizer returns the expected array of a iterable object.
80+
*
81+
* @return void
82+
*
83+
* @throws \Exception
84+
*/
85+
public function testNormalize()
86+
{
87+
$collection = new ArrayCollection();
88+
$collection->add(new \DateTime('2020/01/01'));
89+
$collection->add(new \DateTime('2020/01/01'));
90+
91+
$this->assertEquals(
92+
[
93+
'2020-01-01T00:00:00+00:00',
94+
'2020-01-01T00:00:00+00:00',
95+
],
96+
$this->normalizer->normalize($collection)
97+
);
98+
}
99+
100+
/**
101+
* Test is @see IterableNormalizer::$normalizer returns the expected json normalized data
102+
* for serialization through the Serializer.
103+
*
104+
* @throws \Exception
105+
*/
106+
public function testNormalizeThroughJsonSerializer()
107+
{
108+
$data = [
109+
'foo' => 'bar',
110+
'baz' => 1,
111+
'qux' => [
112+
new ArrayCollection([
113+
new \DateTime('2020/01/01'),
114+
new \DateTime('2020/01/01'),
115+
])
116+
]
117+
];
118+
119+
$this->assertStringEqualsFile(
120+
__DIR__ . '/../../Resources/json/iterable_normalize_result.json',
121+
$this->serializer->serialize($data, 'json')
122+
);
123+
}
124+
}

0 commit comments

Comments
 (0)