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