1
|
|
|
<?php |
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; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Document\DocumentRepository; |
12
|
|
|
use KleijnWeb\SwaggerBundle\Response\ResponseFactory; |
13
|
|
|
use KleijnWeb\SwaggerBundle\Serializer\ArraySerializer; |
14
|
|
|
use KleijnWeb\SwaggerBundle\Serializer\SerializerAdapter; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author John Kleijn <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ResponseFactoryTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @test |
24
|
|
|
*/ |
25
|
|
|
public function willUseFirst2xxStatusCodeFromDocument() |
26
|
|
|
{ |
27
|
|
|
$this->assertEquals(201, $this->createResponse([], '/pet', 'POST')->getStatusCode()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @test |
32
|
|
|
*/ |
33
|
|
|
public function willUse204ForNullResponsesWhenFoundInDocument() |
34
|
|
|
{ |
35
|
|
|
$this->assertEquals(204, $this->createResponse(null, '/pet/{id}', 'DELETE')->getStatusCode()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @test |
40
|
|
|
*/ |
41
|
|
|
public function willNotUse204ForNullResponsesWhenNotInDocument() |
42
|
|
|
{ |
43
|
|
|
$this->assertNotEquals(204, $this->createResponse(null, '/pet/{id}', 'PUT')->getStatusCode()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param mixed $data |
48
|
|
|
* @param string $path |
49
|
|
|
* @param string $method |
50
|
|
|
* |
51
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
52
|
|
|
*/ |
53
|
|
|
private function createResponse($data, $path, $method) |
54
|
|
|
{ |
55
|
|
|
$serializer = new SerializerAdapter(new ArraySerializer()); |
56
|
|
|
$factory = new ResponseFactory(new DocumentRepository(), $serializer); |
57
|
|
|
$request = new Request(); |
58
|
|
|
$request->server->set('REQUEST_METHOD', $method); |
59
|
|
|
$request->attributes->set('_definition', 'src/Tests/Functional/PetStore/app/swagger/composite.yml'); |
60
|
|
|
$request->attributes->set('_swagger_path', $path); |
61
|
|
|
|
62
|
|
|
return $factory->createResponse($request, $data); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|