canCreateAnErrorArrayFromTheResponse()   A
last analyzed

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
declare(strict_types = 1);
3
/**
4
 *
5
 * Copyright (C) 2018  Ross Mitchell
6
 *
7
 * This file is part of RossMitchell/UpdateCloudFlare.
8
 *
9
 * RossMitchell/UpdateCloudFlare is free software: you can redistribute
10
 * it and/or modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
namespace RossMitchell\UpdateCloudFlare\Tests\Responses;
24
25
use RossMitchell\UpdateCloudFlare\Factories\Responses\ErrorFactory;
26
use RossMitchell\UpdateCloudFlare\Tests\AbstractTestClass;
27
28
/**
29
 * Class ErrorFactoryTest
30
 * @testdox RossMitchell\UpdateCloudFlare\Factories\Responses\ErrorFactory
31
 * @package RossMitchell\UpdateCloudFlare\Tests\Responses
32
 */
33
class ErrorFactoryTest extends AbstractTestClass
34
{
35
    /**
36
     * @Inject
37
     * @var ErrorFactory
38
     */
39
    private $factory;
40
41
    /**
42
     * @test
43
     */
44
    public function canCreateAnErrorArrayFromTheResponse(): void
45
    {
46
        $code        = 123;
47
        $message     = 'An Error Happened';
48
        $errorString = <<<JSON
49
[
50
    {"code": $code, "message": "$message"}
51
]
52
JSON;
53
        $errorArray  = \json_decode($errorString);
54
55
        $errors = $this->factory->create($errorArray);
56
        $this->assertInternalType('array', $errors);
57
        $this->assertCount(1, $errors);
58
        $error = $errors[0];
59
        $this->assertEquals($code, $error->getCode());
60
        $this->assertEquals($message, $error->getMessage());
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function canHandleMultipleErrors(): void
67
    {
68
        $firstCode     = 123;
69
        $firstMessage  = 'An Error Happened';
70
        $secondCode    = 345;
71
        $secondMessage = 'A different Error Happened';
72
        $errorString   = <<<JSON
73
[
74
    {"code": $firstCode, "message": "$firstMessage"},
75
    {"code": $secondCode, "message": "$secondMessage"}
76
]
77
JSON;
78
        $errorArray    = \json_decode($errorString);
79
        $errors        = $this->factory->create($errorArray);
80
        $this->assertInternalType('array', $errors);
81
        $this->assertCount(2, $errors);
82
        $error = $errors[1];
83
        $this->assertEquals($secondCode, $error->getCode());
84
        $this->assertEquals($secondMessage, $error->getMessage());
85
    }
86
87
    /**
88
     * @test
89
     */
90
    public function canHandleMalformedErrors(): void
91
    {
92
        $firstMessage  = 'An Error Happened';
93
        $secondCode    = 345;
94
        $secondMessage = 'A different Error Happened';
95
        $errorString   = <<<JSON
96
[
97
    {"message": "$firstMessage"},
98
    {"code": $secondCode, "message": "$secondMessage"}
99
]
100
JSON;
101
        $errorArray    = \json_decode($errorString);
102
        $errors        = $this->factory->create($errorArray);
103
        $this->assertInternalType('array', $errors);
104
        $this->assertCount(1, $errors);
105
        $error = $errors[0];
106
        $this->assertEquals($secondCode, $error->getCode());
107
        $this->assertEquals($secondMessage, $error->getMessage());
108
    }
109
}
110