Completed
Pull Request — master (#24)
by Quentin
03: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
87
class Lightsaber implements CollectionableInterface
88
{
89
    use NormalizableTrait;
90
91
    protected $id;
92
    protected $bladesNumber;
93
    protected $color;
94
    public $owner;
95
96
    public static function getScopes()
97
    {
98
        return array(
99
            'id' => 'id',
100
            'default' => array('id', 'color', 'bladesNumber'),
101
            'full' => array('@default', 'owner'),
102
        );
103
    }
104
105
    /**
106
     * Construct.
107
     */
108
    public function __construct($id, $bladesNumber = null, $nothing = 'wrong')
109
    {
110
        $this->id = $id;
111
        $this->bladesNumber = $bladesNumber;
112
    }
113
114
    /**
115
     * Returns object id.
116
     *
117
     * @return mixed
118
     */
119
    public function getId()
120
    {
121
        return $this->id;
122
    }
123
124
    public function setColor($color)
125
    {
126
        $this->color = $color;
127
    }
128
}
129
130
class LightsaberCollection extends EntityCollection
131
{
132
    public function getEntityClass()
133
    {
134
        return Lightsaber::class;
135
    }
136
}
137