Completed
Push — master ( b00bb5...35568e )
by Ross
02:39
created

canCreateAnErrorArrayFromTheResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
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\Responses;
23
24
use RossMitchell\UpdateCloudFlare\Factories\Responses\ErrorFactory;
25
use RossMitchell\UpdateCloudFlare\Tests\AbstractTestClass;
26
27
/**
28
 * Class ErrorFactoryTest
29
 * @testdox RossMitchell\UpdateCloudFlare\Factories\Responses\ErrorFactory
30
 * @package RossMitchell\UpdateCloudFlare\Tests\Responses
31
 */
32
class ErrorFactoryTest extends AbstractTestClass
33
{
34
    /**
35
     * @Inject
36
     * @var ErrorFactory
37
     */
38
    private $factory;
39
40
    /**
41
     * @test
42
     */
43
    public function canCreateAnErrorArrayFromTheResponse()
44
    {
45
        $code        = 123;
46
        $message     = 'An Error Happened';
47
        $errorString = <<<JSON
48
[
49
    {"code": $code, "message": "$message"}
50
]
51
JSON;
52
        $errorArray  = \json_decode($errorString);
53
54
        $errors = $this->factory->create($errorArray);
55
        $this->assertInternalType('array', $errors);
56
        $this->assertCount(1, $errors);
57
        $error = $errors[0];
58
        $this->assertEquals($code, $error->getCode());
59
        $this->assertEquals($message, $error->getMessage());
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function canHandleMultipleErrors()
66
    {
67
        $firstCode     = 123;
68
        $firstMessage  = 'An Error Happened';
69
        $secondCode    = 345;
70
        $secondMessage = 'A different Error Happened';
71
        $errorString   = <<<JSON
72
[
73
    {"code": $firstCode, "message": "$firstMessage"},
74
    {"code": $secondCode, "message": "$secondMessage"}
75
]
76
JSON;
77
        $errorArray    = \json_decode($errorString);
78
        $errors        = $this->factory->create($errorArray);
79
        $this->assertInternalType('array', $errors);
80
        $this->assertCount(2, $errors);
81
        $error = $errors[1];
82
        $this->assertEquals($secondCode, $error->getCode());
83
        $this->assertEquals($secondMessage, $error->getMessage());
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function canHandleMalformedErrors()
90
    {
91
        $firstMessage  = 'An Error Happened';
92
        $secondCode    = 345;
93
        $secondMessage = 'A different Error Happened';
94
        $errorString   = <<<JSON
95
[
96
    {"message": "$firstMessage"},
97
    {"code": $secondCode, "message": "$secondMessage"}
98
]
99
JSON;
100
        $errorArray    = \json_decode($errorString);
101
        $errors        = $this->factory->create($errorArray);
102
        $this->assertInternalType('array', $errors);
103
        $this->assertCount(1, $errors);
104
        $error = $errors[0];
105
        $this->assertEquals($secondCode, $error->getCode());
106
        $this->assertEquals($secondMessage, $error->getMessage());
107
    }
108
}
109