Completed
Pull Request — master (#24)
by Quentin
08:30
created

LightsaberCollection   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 1
c 1
b 1
f 0
lcom 0
cbo 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Majora\Framework\Normalizer\Tests;
4
5
use Majora\Framework\Model\CollectionableInterface;
6
use Majora\Framework\Model\EntityCollection;
7
use Majora\Framework\Normalizer\MajoraNormalizer;
8
use Majora\Framework\Normalizer\Model\NormalizableTrait;
9
10
/**
11
 * Unit test class for MajoraNormalizer.
12
 */
13
class MajoraNormalizerTest extends \PHPUnit_Framework_TestCase
14
{
15
    public function collectionNormalizationCasesProvider()
16
    {
17
        return array(
18
            'scalar_scope' => array('id', array(42, 66)),
19
            'plain_scope' => array('default', array(
20
                array('id' => 42, 'color' => 'purple', 'bladesNumber' => 1),
21
                array('id' => 66, 'color' => 'red', 'bladesNumber' => 3),
22
            )),
23
            'embeded_scope' => array('full', array(
24
                array('id' => 42, 'color' => 'purple', 'bladesNumber' => 1, 'owner' => 'Mace Windu'),
25
                array('id' => 66, 'color' => 'red', 'bladesNumber' => 3, 'owner' => 'Kylo Ren'),
26
            )),
27
        );
28
    }
29
30
    /**
31
     * Tests EntityCollection normalization.
32
     *
33
     * @dataProvider collectionNormalizationCasesProvider
34
     */
35
    public function testCollectionNormalization($scope, $expectedNormalization)
36
    {
37
        $lightsaber1 = new Lightsaber(42, 1);
38
        $lightsaber1->setColor('purple');
39
        $lightsaber1->owner = 'Mace Windu';
40
41
        $lightsaber2 = new Lightsaber(66, 3);
42
        $lightsaber2->setColor('red');
43
        $lightsaber2->owner = 'Kylo Ren';
44
45
        $this->assertEquals(
46
            $expectedNormalization,
47
            MajoraNormalizer::createNormalizer()->normalize(
48
                new LightsaberCollection(array($lightsaber1, $lightsaber2)),
49
                $scope
50
            )
51
        );
52
    }
53
54
    /**
55
     * Tests denormalize() method through constructor.
56
     */
57
    public function testDenormalizeConstruct()
58
    {
59
        $expectedLightsaber = new Lightsaber(42, 1);
60
        $expectedLightsaber->setColor('purple');
61
        $expectedLightsaber->owner = 'Mace Windu';
62
63
        $lightsaber = MajoraNormalizer::createNormalizer()->denormalize(
64
            array('id' => 42, 'color' => 'purple', 'blades_number' => 1, 'owner' => 'Mace Windu'),
65
            Lightsaber::class
66
        );
67
68
        $this->assertEquals($expectedLightsaber, $lightsaber);
69
    }
70
71
    /**
72
     * Tests denormalize() method through constructor with plain object.
73
     */
74
    public function testDenormalizeConstructPlainObject()
75
    {
76
        $expectedDate = new \DateTime('2016-03-01');
77
78
        $date = MajoraNormalizer::createNormalizer()->denormalize(
79
            '2016-03-01',
80
            \DateTime::class
81
        );
82
83
        $this->assertEquals($expectedDate, $date);
84
    }
85
86
    public function denormalizePerformanceProvider()
87
    {
88
        $normalizer = MajoraNormalizer::createNormalizer();
89
        $start = microtime(true);
90
        $normalizer->denormalize(
91
            array('id' => 42, 'color' => 'purple', 'blades_number' => 1, 'owner' => 'Mace Windu'),
92
            Lightsaber::class
93
        );
94
        $end = microtime(true);
95
        $diff = ($end - $start);
96
97
        $nTime = 20000;
98
99
        return array(
100
            array(($diff * $nTime), $nTime),
101
        );
102
    }
103
104
    /**
105
     * @dataProvider denormalizePerformanceProvider
106
     * @coversNothing
107
     */
108
    public function testDenormalizePerformance($maxExecutionTime, $nTime)
109
    {
110
        $normalizer = MajoraNormalizer::createNormalizer();
111
112
        $start = microtime(true);
113
114
        for ($i = 0; $i < $nTime; ++$i) {
115
            $normalizer->denormalize(
116
                array('id' => 42, 'color' => 'purple', 'blades_number' => 1, 'owner' => 'Mace Windu'),
117
                Lightsaber::class
118
            );
119
        }
120
121
        $end = microtime(true);
122
        $diff = ($end - $start);
123
124
        $this->assertLessThan($maxExecutionTime, $diff);
125
    }
126
}
127
128
class Lightsaber implements CollectionableInterface
129
{
130
    use NormalizableTrait;
131
132
    protected $id;
133
    protected $bladesNumber;
134
    protected $color;
135
    public $owner;
136
137
    public static function getScopes()
138
    {
139
        return array(
140
            'id' => 'id',
141
            'default' => array('id', 'color', 'bladesNumber'),
142
            'full' => array('@default', 'owner'),
143
        );
144
    }
145
146
    /**
147
     * Construct.
148
     */
149
    public function __construct($id, $bladesNumber = null, $nothing = 'wrong')
150
    {
151
        $this->id = $id;
152
        $this->bladesNumber = $bladesNumber;
153
    }
154
155
    /**
156
     * Returns object id.
157
     *
158
     * @return mixed
159
     */
160
    public function getId()
161
    {
162
        return $this->id;
163
    }
164
165
    public function setColor($color)
166
    {
167
        $this->color = $color;
168
    }
169
}
170
171
class LightsaberCollection extends EntityCollection
172
{
173
    public function getEntityClass()
174
    {
175
        return Lightsaber::class;
176
    }
177
}
178