Completed
Pull Request — master (#80)
by John
03:04
created

willSetResponseWithVndErrorHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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\VndError\VndValidationErrorFactory;
15
use KleijnWeb\SwaggerBundle\Response\ErrorResponseFactory\VndErrorResponseFactory;
16
use Ramsey\VndError\VndError;
17
use Symfony\Component\Config\Definition\Exception\Exception;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
21
/**
22
 * @author John Kleijn <[email protected]>
23
 */
24
class VndErrorResponseFactoryTest extends \PHPUnit_Framework_TestCase
25
{
26
    const LOGREF = '123456789';
27
28
    /**
29
     * @var \PHPUnit_Framework_MockObject_MockObject
30
     */
31
    private $errorFactoryMock;
32
33
    /**
34
     * @var VndErrorResponseFactory
35
     */
36
    private $factory;
37
38
    /**
39
     * @var LogRefBuilder
40
     */
41
    private $logRefBuilder;
42
43
    protected function setUp()
44
    {
45
        /** @var VndValidationErrorFactory $errorFactory */
46
        $this->errorFactoryMock = $errorFactory = $this
47
            ->getMockBuilder(VndValidationErrorFactory::class)
48
            ->disableOriginalConstructor()
49
            ->getMock();
50
51
        $this->factory = new VndErrorResponseFactory($errorFactory);
52
53
        $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...
54
        $mockObject->expects($this->any())->method('create')->willReturn(self::LOGREF);
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function willSetResponseWithVndErrorHeader()
61
    {
62
        $response = $this->factory->create(new HttpError(new Request(), new Exception(), $this->logRefBuilder));
63
64
        $this->assertContains('application/vnd.error', $response->headers->get('Content-Type'));
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function willSetResponseWithValidJsonContent()
71
    {
72
        $response = $this->factory->create(new HttpError(new Request(), new Exception(), $this->logRefBuilder));
73
74
        $this->assertNotNull(json_decode($response->getContent()));
75
    }
76
77
    /**
78
     * @test
79
     */
80
    public function willSetResponseWithSimpleMessage()
81
    {
82
        foreach ([400 => 'Bad Request', 500 => 'Internal Server Error'] as $code => $message) {
83
            $response = $this->factory->create(
84
                new HttpError(new Request(), new Exception('Ai caramba!', $code), $this->logRefBuilder)
85
            );
86
            $this->assertNotNull($body = json_decode($response->getContent()));
87
            $this->assertEquals($message, $body->message);
88
        }
89
    }
90
91
    /**
92
     * @test
93
     */
94
    public function willSetResponseWithLogRef()
95
    {
96
        foreach ([400, 500] as $code) {
97
            $response = $this->factory->create(
98
                new HttpError(new Request(), new Exception('Ai caramba!', $code), $this->logRefBuilder)
99
            );
100
            $this->assertSame(self::LOGREF, json_decode($response->getContent())->logref);
101
        }
102
    }
103
104
    /**
105
     * @test
106
     */
107
    public function willReturn404ResponsesForNotFoundHttpException()
108
    {
109
        $response = $this->factory->create(
110
            new HttpError(new Request(), new NotFoundHttpException(), $this->logRefBuilder)
111
        );
112
        $this->assertSame(404, $response->getStatusCode());
113
    }
114
115
    /**
116
     * @test
117
     */
118
    public function willCreateValidationErrorResponse()
119
    {
120
        $exception = new InvalidParametersException('Oh noes', []);
121
        $request   = new Request();
122
123
        $this->errorFactoryMock
124
            ->expects($this->once())
125
            ->method('create')
126
            ->with($request, $exception, self::LOGREF)
127
            ->willReturn(new VndError('Try again'));
128
129
        $response = $this->factory->create(
130
            new HttpError($request, $exception, $this->logRefBuilder)
131
        );
132
        $this->assertSame(400, $response->getStatusCode());
133
    }
134
}
135