TemplateHydratorTest::testHydrate()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 2
eloc 16
nc 2
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2014 Roave, LLC.
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 *   * Redistributions of source code must retain the above copyright
11
 *     notice, this list of conditions and the following disclaimer.
12
 *
13
 *   * Redistributions in binary form must reproduce the above copyright
14
 *     notice, this list of conditions and the following disclaimer in
15
 *     the documentation and/or other materials provided with the
16
 *     distribution.
17
 *
18
 *   * Neither the names of the copyright holders nor the names of the
19
 *     contributors may be used to endorse or promote products derived
20
 *     from this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
 * POSSIBILITY OF SUCH DAMAGE.
34
 *
35
 * @author    Antoine Hedgecock
36
 *
37
 * @copyright 2014 Roave, LLC
38
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
39
 */
40
41
namespace EmailTemplatesTest\Hydrator;
42
43
use DateTime;
44
use PHPUnit_Framework_TestCase;
45
use Roave\EmailTemplates\Entity\TemplateEntity;
46
use Roave\EmailTemplates\Hydrator\Exception\InvalidArgumentException;
47
use Roave\EmailTemplates\Hydrator\TemplateHydrator;
48
use stdClass;
49
50
/**
51
 * Class TemplateHydratorTest
52
 *
53
 * @coversDefaultClass \Roave\EmailTemplates\Hydrator\TemplateHydrator
54
 * @covers ::<!public>
55
 *
56
 * @group unit
57
 */
58
class TemplateHydratorTest extends PHPUnit_Framework_TestCase
59
{
60
    /**
61
     * @var TemplateHydrator
62
     */
63
    protected $hydrator;
64
65
    protected function setUp()
66
    {
67
        $this->hydrator = new TemplateHydrator();
68
    }
69
70
    /**
71
     * @covers ::hydrate
72
     */
73
    public function testHydrateThrowsInvalidArgumentException()
74
    {
75
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
76
            InvalidArgumentException::class,
77
            sprintf('Object must be an instance of %s', TemplateEntity::class)
78
        );
79
80
        $this->hydrator->hydrate([], new stdClass());
81
    }
82
83
    /**
84
     * @covers ::extract
85
     */
86
    public function testExtractThrowsInvalidArgumentException()
87
    {
88
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
89
            InvalidArgumentException::class,
90
            sprintf('Object must be an instance of %s', TemplateEntity::class)
91
        );
92
93
        $this->hydrator->extract(new stdClass());
94
    }
95
96
    /**
97
     * Helper for setting properties for a template
98
     *
99
     * @return TemplateEntity
100
     */
101
    private function getTemplate()
102
    {
103
        $date = new DateTime('2011-01-01');
104
105
        $template = new TemplateEntity();
106
        $template->setId(1);
107
        $template->setUuid('123');
108
        $template->setLocale('uk-US');
109
        $template->setSubject('42');
110
        $template->setTextBody('Body made of text');
111
        $template->setHtmlBody('Body made of html');
112
        $template->setDescription('This describes everything');
113
        $template->setParameters(['foo' => 'bar']);
114
        $template->setUpdateParameters('true');
115
116
        $template->setCreatedAt($date);
117
        $template->setUpdatedAt($date);
118
        $template->setparametersUpdatedAt($date);
119
120
        return $template;
121
    }
122
123
    /**
124
     * Helper for testing hydration and extraction
125
     *
126
     * @return array
127
     */
128
    private function getExtractedData()
129
    {
130
        $dateTime = new DateTime('2011-01-01');
131
132
        return [
133
            'id'               => 1,
134
            'uuid'             => '123',
135
            'locale'           => 'uk-US',
136
            'subject'          => '42',
137
            'textBody'         => 'Body made of text',
138
            'htmlBody'         => 'Body made of html',
139
            'description'      => 'This describes everything',
140
            'parameters'       => ['foo' => 'bar'],
141
            'updateParameters' => true,
142
143
            'createdAt'           => $dateTime,
144
            'updatedAt'           => $dateTime,
145
            'parametersUpdatedAt' => $dateTime,
146
        ];
147
    }
148
149
    /**
150
     * @covers ::extract
151
     */
152
    public function testExtract()
153
    {
154
        $result = $this->hydrator->extract($this->getTemplate());
155
156
        // Assert that the extracted data matches the response.
157
        $this->assertEquals($this->getExtractedData(), $result);
158
    }
159
160
    /**
161
     * @covers ::hydrate
162
     */
163
    public function testHydrate()
164
    {
165
        $data = $this->getExtractedData();
166
167
        $template = $this->createMock(TemplateEntity::class);
168
169
        $expectedProperties = [
170
            'setSubject'          => 'subject',
171
            'setTextBody'         => 'textBody',
172
            'setHtmlBody'         => 'htmlBody',
173
            'setDescription'      => 'description',
174
            'setParameters'       => 'parameters',
175
            'setUpdateParameters' => 'updateParameters',
176
        ];
177
178
        foreach ($expectedProperties as $method => $property) {
179
180
            $template
181
                ->expects($this->once())
182
                ->method($method)
183
                ->with($data[$property]);
184
        }
185
186
        $this->hydrator->hydrate($data, $template);
187
    }
188
}
189