ApiResponseDataFactoryTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 63
c 2
b 0
f 0
dl 0
loc 102
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testSuccessResponse() 0 12 1
A testResponse() 0 12 1
A testUnableToUseNotArrayDataInGenericResponse() 0 16 1
A testGenericSuccessResponse() 0 19 1
A testErrorResponse() 0 12 1
A testGenericResponse() 0 19 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Tests\Unit\Http;
6
7
use App\Http\ApiResponseDataFactory;
8
use HttpSoft\Message\ResponseFactory;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Http\Message\StreamFactoryInterface;
11
use RuntimeException;
12
use Yiisoft\DataResponse\DataResponse;
13
14
final class ApiResponseDataFactoryTest extends TestCase
15
{
16
    public function testResponse(): void
17
    {
18
        $factory = new ApiResponseDataFactory();
19
20
        $response = $factory->createResponse();
21
22
        $this->assertEquals([
23
            'status' => '',
24
            'error_message' => '',
25
            'error_code' => null,
26
            'data' => null,
27
        ], $response->toArray());
28
    }
29
30
    public function testSuccessResponse(): void
31
    {
32
        $factory = new ApiResponseDataFactory();
33
34
        $response = $factory->createSuccessResponse();
35
36
        $this->assertEquals([
37
            'status' => 'success',
38
            'error_message' => '',
39
            'error_code' => null,
40
            'data' => null,
41
        ], $response->toArray());
42
    }
43
44
    public function testErrorResponse(): void
45
    {
46
        $factory = new ApiResponseDataFactory();
47
48
        $response = $factory->createErrorResponse();
49
50
        $this->assertEquals([
51
            'status' => 'failed',
52
            'error_message' => '',
53
            'error_code' => null,
54
            'data' => null,
55
        ], $response->toArray());
56
    }
57
58
    public function testGenericResponse(): void
59
    {
60
        $factory = new ApiResponseDataFactory();
61
62
        $response = new DataResponse(
63
            'error message',
64
            555,
65
            'Testing phase',
66
            new ResponseFactory(),
67
            $this->createStub(StreamFactoryInterface::class),
68
        );
69
        $response = $factory->createFromResponse($response);
70
71
        $this->assertEquals([
72
            'status' => 'failed',
73
            'error_message' => 'error message',
74
            'error_code' => 555,
75
            'data' => null,
76
        ], $response->toArray());
77
    }
78
79
    public function testGenericSuccessResponse(): void
80
    {
81
        $factory = new ApiResponseDataFactory();
82
83
        $response = new DataResponse(
84
            ['message' => 'success message'],
85
            200,
86
            'Testing phase',
87
            new ResponseFactory(),
88
            $this->createStub(StreamFactoryInterface::class),
89
        );
90
        $response = $factory->createFromResponse($response);
91
92
        $this->assertEquals([
93
            'status' => 'success',
94
            'error_message' => '',
95
            'error_code' => null,
96
            'data' => ['message' => 'success message'],
97
        ], $response->toArray());
98
    }
99
100
    public function testUnableToUseNotArrayDataInGenericResponse(): void
101
    {
102
        $factory = new ApiResponseDataFactory();
103
104
        $response = new DataResponse(
105
            'success message',
106
            200,
107
            'Testing phase',
108
            new ResponseFactory(),
109
            $this->createStub(StreamFactoryInterface::class),
110
        );
111
112
        $this->expectException(RuntimeException::class);
113
        $this->expectExceptionMessage('The response data must be either null or an array');
114
115
        $factory->createFromResponse($response);
116
    }
117
}
118