Completed
Push — master ( 8796f9...232efd )
by
unknown
02:43
created

ResponseModelFactoryTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 160
Duplicated Lines 60 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 96
loc 160
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace tests\MediaMonks\RestApi\Model;
4
5
use MediaMonks\RestApi\Exception\ValidationException;
6
use MediaMonks\RestApi\Model\ResponseModel;
7
use MediaMonks\RestApi\Model\ResponseModelFactory;
8
use MediaMonks\RestApi\Response\OffsetPaginatedResponse;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13
class ResponseModelFactoryTest extends \PHPUnit_Framework_TestCase
14
{
15
    public function testAutoDetectException()
16
    {
17
        $exception         = new \Exception('foo');
18
        $responseContainer = $this->createResponseModel($exception);
19
20
        $this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $responseContainer->getStatusCode());
21
        $this->assertNull($responseContainer->getData());
22
        $this->assertEquals($exception, $responseContainer->getException());
23
        $this->assertNull($responseContainer->getResponse());
24
        $this->assertNull($responseContainer->getPagination());
25
        $this->assertFalse($responseContainer->isEmpty());
26
27
        $responseContainerArray = $responseContainer->toArray();
28
        $this->assertArrayHasKey('error', $responseContainerArray);
29
        $this->assertEquals($responseContainerArray['error']['code'], 'error');
30
        $this->assertEquals($responseContainerArray['error']['message'], 'foo');
31
    }
32
33
    public function testAutoDetectHttpException()
34
    {
35
        $notFoundHttpException = new NotFoundHttpException('foo');
36
        $responseContainer     = $this->createResponseModel($notFoundHttpException);
37
38
        $this->assertEquals(Response::HTTP_NOT_FOUND, $responseContainer->getStatusCode());
39
        $this->assertNull($responseContainer->getData());
40
        $this->assertEquals($notFoundHttpException, $responseContainer->getException());
41
        $this->assertNull($responseContainer->getResponse());
42
        $this->assertNull($responseContainer->getPagination());
43
        $this->assertFalse($responseContainer->isEmpty());
44
45
        $responseContainerArray = $responseContainer->toArray();
46
        $this->assertArrayHasKey('error', $responseContainerArray);
47
        $this->assertEquals($responseContainerArray['error']['code'], 'error.http.not_found');
48
        $this->assertEquals($responseContainerArray['error']['message'], 'foo');
49
    }
50
51
    public function testAutoDetectPaginatedResponse()
52
    {
53
        $paginatedResponse = new OffsetPaginatedResponse('foo', 1, 2, 3);
54
        $responseContainer = $this->createResponseModel($paginatedResponse);
55
56
        $this->assertEquals(Response::HTTP_OK, $responseContainer->getStatusCode());
57
        $this->assertInternalType('string', $responseContainer->getData());
58
        $this->assertNull($responseContainer->getException());
59
        $this->assertNull($responseContainer->getResponse());
60
        $this->assertEquals($paginatedResponse, $responseContainer->getPagination());
61
        $this->assertFalse($responseContainer->isEmpty());
62
63
        $responseContainerArray = $responseContainer->toArray();
64
        $this->assertArrayHasKey('data', $responseContainerArray);
65
        $this->assertArrayHasKey('pagination', $responseContainerArray);
66
    }
67
68
    public function testAutoDetectEmptyResponse()
69
    {
70
        $responseContainer = $this->createResponseModel(null);
71
        $this->assertEquals(Response::HTTP_NO_CONTENT, $responseContainer->getStatusCode());
72
        $this->assertNull($responseContainer->getData());
73
        $this->assertNull($responseContainer->getException());
74
        $this->assertNull($responseContainer->getResponse());
75
        $this->assertNull($responseContainer->getPagination());
76
        $this->assertTrue($responseContainer->isEmpty());
77
    }
78
79
    public function testAutoDetectStringResponse()
80
    {
81
        $stringData        = 'foo';
82
        $responseContainer = $this->createResponseModel($stringData);
83
84
        $this->assertEquals(Response::HTTP_OK, $responseContainer->getStatusCode());
85
        $this->assertInternalType('string', $responseContainer->getData());
86
        $this->assertNull($responseContainer->getException());
87
        $this->assertNull($responseContainer->getResponse());
88
        $this->assertNull($responseContainer->getPagination());
89
        $this->assertFalse($responseContainer->isEmpty());
90
    }
91
92
    public function testAutoDetectArrayResponse()
93
    {
94
        $arrayData         = ['foo', 'bar'];
95
        $responseContainer = $this->createResponseModel($arrayData);
96
97
        $this->assertEquals(Response::HTTP_OK, $responseContainer->getStatusCode());
98
        $this->assertInternalType('array', $responseContainer->getData());
99
        $this->assertNull($responseContainer->getException());
100
        $this->assertNull($responseContainer->getResponse());
101
        $this->assertNull($responseContainer->getPagination());
102
        $this->assertFalse($responseContainer->isEmpty());
103
    }
104
105
    public function testAutoDetectRedirectResponse()
106
    {
107
        $uri               = 'http://www.mediamonks.com';
108
        $redirect          = new RedirectResponse($uri, Response::HTTP_MOVED_PERMANENTLY);
109
        $responseContainer = $this->createResponseModel($redirect);
110
111
        $this->assertEquals(Response::HTTP_MOVED_PERMANENTLY, $responseContainer->getStatusCode());
112
        $this->assertNull($responseContainer->getException());
113
        $this->assertEquals($redirect, $responseContainer->getResponse());
114
        $this->assertNull($responseContainer->getPagination());
115
        $this->assertFalse($responseContainer->isEmpty());
116
117
        $data = $responseContainer->toArray();
118
119
        $this->assertEquals($uri, $data['location']);
120
    }
121
122
    public function testAutoDetectSymfonyResponse()
123
    {
124
        $data              = 'foo';
125
        $response          = new Response($data);
126
        $responseContainer = $this->createResponseModel($response);
127
128
        $this->assertEquals(Response::HTTP_OK, $responseContainer->getStatusCode());
129
        $this->assertEquals($data, $responseContainer->getData());
130
        $this->assertNull($responseContainer->getException());
131
        $this->assertEquals($response, $responseContainer->getResponse());
132
        $this->assertNull($responseContainer->getPagination());
133
        $this->assertFalse($responseContainer->isEmpty());
134
    }
135
136
    public function testAutoDetectMediaMonksResponse()
137
    {
138
        $data              = ['foo'];
139
        $response          = new \MediaMonks\RestApi\Response\Response($data);
140
        $responseContainer = $this->createResponseModel($response);
141
142
        $this->assertEquals(Response::HTTP_OK, $responseContainer->getStatusCode());
143
        $this->assertEquals($data, $responseContainer->getData());
144
        $this->assertNull($responseContainer->getException());
145
        $this->assertEquals($response, $responseContainer->getResponse());
146
        $this->assertNull($responseContainer->getPagination());
147
        $this->assertFalse($responseContainer->isEmpty());
148
    }
149
150
    public function testAutoDetectValidationExceptionResponse()
151
    {
152
        $exception         = new ValidationException([]);
153
        $responseContainer = $this->createResponseModel($exception);
154
155
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $responseContainer->getStatusCode());
156
        $this->assertNull($responseContainer->getData());
157
        $this->assertEquals($exception, $responseContainer->getException());
158
        $this->assertNull($responseContainer->getResponse());
159
        $this->assertNull($responseContainer->getPagination());
160
        $this->assertFalse($responseContainer->isEmpty());
161
    }
162
163
    /**
164
     * @param $content
165
     * @return ResponseModel
166
     */
167
    protected function createResponseModel($content)
168
    {
169
        $responseModelFactory = new ResponseModelFactory(new ResponseModel());
170
        return $responseModelFactory->createFromContent($content);
171
    }
172
}
173