Completed
Push — master ( 27a5d2...ad3b69 )
by John
20s
created

SimpleErrorResponseFactoryTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 27.66 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 11
dl 26
loc 94
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A willSetResponseWithApplicationJsonHeader() 0 6 1
A willSetResponseWithValidJsonContent() 0 6 1
A willSetResponseWithSimpleMessage() 10 10 2
A willSetResponseWithLogRef() 9 9 2
A willReturn404ResponsesForNotFoundHttpException() 7 7 1
A willCreateValidationErrorResponse() 0 11 1

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 declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Tests\Response\ErrorResponseFactory;
10
11
use KleijnWeb\SwaggerBundle\Exception\InvalidParametersException;
12
use KleijnWeb\SwaggerBundle\Response\Error\HttpError;
13
use KleijnWeb\SwaggerBundle\Response\Error\LogRefBuilder;
14
use KleijnWeb\SwaggerBundle\Response\ErrorResponseFactory\SimpleErrorResponseFactory;
15
use Symfony\Component\Config\Definition\Exception\Exception;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
19
/**
20
 * @author John Kleijn <[email protected]>
21
 */
22
class SimpleErrorResponseFactoryTest extends \PHPUnit_Framework_TestCase
23
{
24
    const LOGREF = '123456789';
25
26
    /**
27
     * @var SimpleErrorResponseFactory
28
     */
29
    private $factory;
30
31
    /**
32
     * @var LogRefBuilder
33
     */
34
    private $logRefBuilder;
35
36
    protected function setUp()
37
    {
38
        $this->factory       = new SimpleErrorResponseFactory();
39
        $this->logRefBuilder = $mockObject = $this->getMockForAbstractClass(LogRefBuilder::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $mockObject = $this->get...r\LogRefBuilder::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<KleijnWeb\Swagger...se\Error\LogRefBuilder> of property $logRefBuilder.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
        $mockObject->expects($this->any())->method('create')->willReturn(self::LOGREF);
41
    }
42
43
    /**
44
     * @test
45
     */
46
    public function willSetResponseWithApplicationJsonHeader()
47
    {
48
        $response = $this->factory->create(new HttpError(new Request(), new Exception(), $this->logRefBuilder));
49
50
        $this->assertContains('application/json', $response->headers->get('Content-Type'));
51
    }
52
53
    /**
54
     * @test
55
     */
56
    public function willSetResponseWithValidJsonContent()
57
    {
58
        $response = $this->factory->create(new HttpError(new Request(), new Exception(), $this->logRefBuilder));
59
60
        $this->assertNotNull(json_decode($response->getContent()));
61
    }
62
63
    /**
64
     * @test
65
     */
66 View Code Duplication
    public function willSetResponseWithSimpleMessage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        foreach ([400 => 'Bad Request', 500 => 'Internal Server Error'] as $code => $message) {
69
            $response = $this->factory->create(
70
                new HttpError(new Request(), new Exception('Ai caramba!', $code), $this->logRefBuilder)
71
            );
72
            $this->assertNotNull($body = json_decode($response->getContent()));
73
            $this->assertEquals($message, $body->message);
74
        }
75
    }
76
77
    /**
78
     * @test
79
     */
80 View Code Duplication
    public function willSetResponseWithLogRef()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        foreach ([400, 500] as $code) {
83
            $response = $this->factory->create(
84
                new HttpError(new Request(), new Exception('Ai caramba!', $code), $this->logRefBuilder)
85
            );
86
            $this->assertSame(self::LOGREF, json_decode($response->getContent())->logref);
87
        }
88
    }
89
90
    /**
91
     * @test
92
     */
93 View Code Duplication
    public function willReturn404ResponsesForNotFoundHttpException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $response = $this->factory->create(
96
            new HttpError(new Request(), new NotFoundHttpException(), $this->logRefBuilder)
97
        );
98
        $this->assertSame(404, $response->getStatusCode());
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function willCreateValidationErrorResponse()
105
    {
106
        $validationErrors = ['Wrong.', 'Wrong.', 'Wrong!'];
107
108
        $exception = new InvalidParametersException('Oh noes', $validationErrors);
109
        $request   = new Request();
110
111
        $response = $this->factory->create(new HttpError($request, $exception, $this->logRefBuilder));
112
113
        $this->assertSame($validationErrors, json_decode($response->getContent())->errors);
114
    }
115
}
116