Completed
Push — master ( b45a14...a539e6 )
by Ross
02:34
created

itWillThrowAnExceptionIfThereIsNoSetter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * Copyright (C) 2018  Ross Mitchell
5
 *
6
 * This file is part of RossMitchell/UpdateCloudFlare.
7
 *
8
 * RossMitchell/UpdateCloudFlare is free software: you can redistribute
9
 * it and/or modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace RossMitchell\UpdateCloudFlare\Tests\Helpers;
23
24
use RossMitchell\UpdateCloudFlare\Helpers\Hydrator;
25
use RossMitchell\UpdateCloudFlare\Tests\AbstractTestClass;
26
use RossMitchell\UpdateCloudFlare\Tests\Fakes\SetterExample;
27
use Symfony\Component\Console\Exception\LogicException;
28
29
class HydratorTest extends AbstractTestClass
30
{
31
    /**
32
     * @Inject
33
     * @var Hydrator
34
     */
35
    private $hydrator;
36
    /**
37
     * @Inject
38
     * @var SetterExample
39
     */
40
    private $testClass;
41
42
    /**
43
     * @test
44
     */
45
    public function itCanSetAPropertyThatExistsInTheRawData()
46
    {
47
        $url       = 'http://www.example.com';
48
        $node      = 'url';
49
        $rawData   = "{ \"${node}\": \"${url}\"}";
50
        $testClass = $this->testClass;
51
        $hydrator  = $this->hydrator;
52
        $hydrator->setProperty($testClass, \json_decode($rawData), $node);
53
        $this->assertEquals($url, $testClass->getUrl());
54
    }
55
56
    /**
57
     * @test
58
     */
59
    public function itWontThrowAnExceptionIfThePropertyIsOptional()
60
    {
61
        $testClass = $this->testClass;
62
        $oldUrl    = 'http://www.example.com';
63
        $testClass->setUrl($oldUrl);
64
        $rawData = '{}';
65
        $this->hydrator->setProperty($testClass, \json_decode($rawData), 'url', false);
66
        $this->assertNotEquals($oldUrl, $testClass->getUrl());
67
        $this->assertNull($testClass->getUrl());
68
    }
69
70
    /**
71
     * @test
72
     */
73
    public function itWillThrowAnExceptionIfThePropertyIsRequired()
74
    {
75
        $testClass = $this->testClass;
76
        $rawData = '{}';
77
        $this->expectException(LogicException::class);
78
        $this->hydrator->setProperty($testClass, \json_decode($rawData), 'url');
79
    }
80
81
    public function itWillThrowAnExceptionIfThereIsNoSetter()
82
    {
83
        $testClass = $this->testClass;
84
        $rawData = '{"test":"test"}';
85
        $this->expectException(LogicException::class);
86
        $this->hydrator->setProperty($testClass, \json_decode($rawData), 'test');
87
    }
88
}
89