Completed
Pull Request — master (#52)
by John
06:05
created

parameterValidationErrorWillContainDefaultMessageAndLogref()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
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\Functional;
10
11
use KleijnWeb\SwaggerBundle\Response\VndValidationErrorFactory;
12
use KleijnWeb\SwaggerBundle\Test\ApiResponseErrorException;
13
use KleijnWeb\SwaggerBundle\Test\ApiTestCase;
14
use Nocarrier\Hal;
15
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
16
17
/**
18
 * @author John Kleijn <[email protected]>
19
 */
20
class VndParameterValidationErrorTest extends WebTestCase
21
{
22
    use ApiTestCase;
23
24
    /**
25
     * Use config_basic.yml
26
     *
27
     * @var bool
28
     */
29
    protected $env = 'basic';
30
31
    public static function setUpBeforeClass()
32
    {
33
        static::initSchemaManager(__DIR__ . '/PetStore/app/swagger/petstore.yml');
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function parameterValidationErrorWillContainDefaultMessageAndLogref()
40
    {
41
        try {
42
            $this->get('/v2/pet/findByStatus', ['status' => 'bogus']);
43
        } catch (ApiResponseErrorException $e) {
44
            $data = Hal::fromJson($e->getJson(), 10)->getData();
45
            $this->assertSame(VndValidationErrorFactory::DEFAULT_MESSAGE, $data['message']);
46
            $this->assertRegExp('/[0-9a-z]+/', $data['logref']);
47
48
            return;
49
        }
50
        $this->fail("Expected exception");
51
    }
52
53
    /**
54
     * @test
55
     */
56 View Code Duplication
    public function parameterValidationErrorWillContainSpecificationPointer()
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...
57
    {
58
        try {
59
            $this->get('/v2/pet/findByStatus', ['status' => 'bogus']);
60
        } catch (ApiResponseErrorException $e) {
61
            $error = Hal::fromJson($e->getJson(), 10);
62
            $resource = $error->getFirstResource('errors');
63
            $specLink = 'http://petstore.swagger.io/swagger/petstore.yml#/paths/~1pet~1findByStatus/get/parameters/0';
64
            $this->assertSame($specLink, $resource->getUri());
65
66
            return;
67
        }
68
        $this->fail("Expected exception");
69
    }
70
71
    /**
72
     * @test
73
     */
74 View Code Duplication
    public function parameterValidationErrorWillContainSchemaPointer()
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...
75
    {
76
        try {
77
            $this->get('/v2/pet/findByStatus', ['status' => 'bogus']);
78
        } catch (ApiResponseErrorException $e) {
79
            $error = Hal::fromJson($e->getJson(), 10);
80
            $resource = $error->getFirstResource('errors');
81
            $data = $resource->getData();
82
            $this->assertSame('/paths/~1pet~1findByStatus/get/x-request-schema/properties/status', $data['path']);
83
84
            return;
85
        }
86
        $this->fail("Expected exception");
87
    }
88
89
    /**
90
     * @test
91
     */
92
    public function parameterValidationErrorCanContainMultipleErrors()
93
    {
94
        try {
95
            $this->get('/v2/user/login');
96
        } catch (ApiResponseErrorException $e) {
97
            $error = Hal::fromJson($e->getJson(), 10);
98
            $resources = $error->getResources();
99
            $this->assertArrayHasKey('errors', $resources);
100
            /**
101
             * @var int $i
102
             * @var Hal $resource
103
             */
104
            foreach ($resources['errors'] as $i => $resource) {
105
                $data = $resource->getData();
106
                if ($i == 0) {
107
                    $this->assertSame('/paths/~1user~1login/get/x-request-schema/properties/username', $data['path']);
108
                    $uri = 'http://petstore.swagger.io/swagger/petstore.yml#/paths/~1user~1login/get/parameters/0';
109
                    $this->assertSame($uri, $resource->getUri());
110
                    continue;
111
                }
112
                $this->assertSame('/paths/~1user~1login/get/x-request-schema/properties/password', $data['path']);
113
                $uri = 'http://petstore.swagger.io/swagger/petstore.yml#/paths/~1user~1login/get/parameters/1';
114
                $this->assertSame($uri, $resource->getUri());
115
            }
116
117
            return;
118
        }
119
        $this->fail("Expected exception");
120
121
    }
122
}
123