These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\EventListener\Response; |
||
10 | |||
11 | use KleijnWeb\PhpApi\Descriptions\Description\Operation; |
||
12 | use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema; |
||
13 | use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\SchemaValidator; |
||
14 | use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\ValidationResult; |
||
15 | use KleijnWeb\SwaggerBundle\Hydrator\ObjectHydrator; |
||
16 | use KleijnWeb\PhpApi\RoutingBundle\Routing\RequestMeta; |
||
17 | use KleijnWeb\SwaggerBundle\EventListener\Response\ResponseFactory; |
||
18 | use KleijnWeb\SwaggerBundle\Exception\ValidationException; |
||
19 | use PHPUnit\Framework\TestCase; |
||
20 | use Symfony\Component\HttpFoundation\Request; |
||
21 | |||
22 | /** |
||
23 | * @author John Kleijn <[email protected]> |
||
24 | */ |
||
25 | class ResponseFactoryTest extends TestCase |
||
26 | { |
||
27 | /** |
||
28 | * @test |
||
29 | */ |
||
30 | public function willUseFirst2xxStatusCodeFromDocument() |
||
31 | { |
||
32 | $this->assertEquals(201, $this->createResponse([201, 200], [])->getStatusCode()); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @test |
||
37 | */ |
||
38 | public function willUse204ForNullResponsesWhenFoundInDocument() |
||
39 | { |
||
40 | $this->assertEquals(204, $this->createResponse([200, 201, 204])->getStatusCode()); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @test |
||
45 | */ |
||
46 | public function willNotUse204ForNullResponsesWhenNotInDocument() |
||
47 | { |
||
48 | $this->assertNotEquals(204, $this->createResponse([200, 201])->getStatusCode()); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @test |
||
53 | */ |
||
54 | public function canValidateUsingSchemaAndBody() |
||
55 | { |
||
56 | $this->assertNotEquals(204, $this->createResponse([200, 201], (object)[], true)->getStatusCode()); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @test |
||
61 | */ |
||
62 | public function canInvalidateUsingSchemaAndBody() |
||
63 | { |
||
64 | try { |
||
65 | $this->assertNotEquals(204, $this->createResponse([200, 201], (object)[], true, false)->getStatusCode()); |
||
66 | } catch (ValidationException $e) { |
||
67 | $this->assertSame(['foo' => 'invalid'], $e->getValidationErrors()); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param array $statusCodes |
||
73 | * @param mixed $data |
||
74 | * |
||
75 | * @param bool $useValidator |
||
76 | * |
||
77 | * @param bool $stubValid |
||
78 | * |
||
79 | * @return \Symfony\Component\HttpFoundation\Response |
||
80 | * @throws \KleijnWeb\SwaggerBundle\Exception\ValidationException |
||
81 | */ |
||
82 | private function createResponse(array $statusCodes, $data = null, $useValidator = false, $stubValid = true) |
||
83 | { |
||
84 | $operationMock = $this->getMockBuilder(Operation::class)->disableOriginalConstructor()->getMock(); |
||
85 | $operationMock->expects($this->any()) |
||
86 | ->method('getStatusCodes') |
||
87 | ->willReturn($statusCodes); |
||
88 | |||
89 | $metaMock = $this->getMockBuilder(RequestMeta::class)->disableOriginalConstructor()->getMock(); |
||
90 | $metaMock->expects($this->any()) |
||
91 | ->method('getOperation') |
||
92 | ->willReturn($operationMock); |
||
93 | |||
94 | $mockValidator = null; |
||
95 | if ($useValidator) { |
||
96 | $mockValidator = $this->getMockBuilder(SchemaValidator::class)->disableOriginalConstructor()->getMock(); |
||
97 | $mockValidator |
||
98 | ->expects($this->once()) |
||
99 | ->method('validate') |
||
100 | ->with($this->isInstanceOf(Schema::class), $data) |
||
101 | ->willReturn( |
||
102 | $stubValid ? new ValidationResult(true) : new ValidationResult(false, ['foo' => 'invalid']) |
||
103 | ); |
||
104 | } |
||
105 | |||
106 | $hydrator = $this->getMockBuilder(ObjectHydrator::class)->disableOriginalConstructor()->getMock(); |
||
107 | $hydrator |
||
108 | ->expects($this->any()) |
||
109 | ->method('dehydrate') |
||
110 | ->willReturn($data); |
||
111 | |||
112 | $factory = new ResponseFactory($hydrator, $mockValidator); |
||
0 ignored issues
–
show
|
|||
113 | $request = new Request(); |
||
114 | $request->attributes->set(RequestMeta::ATTRIBUTE_URI, 'tests/Functional/PetStore/app/swagger/composite.yml'); |
||
115 | $request->attributes->set(RequestMeta::ATTRIBUTE, $metaMock); |
||
116 | |||
117 | return $factory->createResponse($request, $data); |
||
118 | } |
||
119 | } |
||
120 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: