Completed
Push — master ( 133b57...4c004c )
by Kamil
20:32
created

CountryApiTest::testCreateCountryResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\CoreBundle\Tests\Controller;
13
14
use Lakion\ApiTestCase\JsonApiTestCase;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @author Jeroen Thora <[email protected]>
19
 */
20
class CountryApiTest extends JsonApiTestCase
21
{
22
    /**
23
     * @var array
24
     */
25
    private static $authorizedHeaderWithContentType = [
26
        'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
27
        'CONTENT_TYPE' => 'application/json',
28
    ];
29
30
    /**
31
     * @var array
32
     */
33
    private static $authorizedHeaderWithAccept = [
34
        'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
35
        'ACCEPT' => 'application/json',
36
    ];
37
38
    public function testCreateCountryAccessDeniedResponse()
39
    {
40
        $this->client->request('POST', '/api/countries/');
41
42
        $response = $this->client->getResponse();
43
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
44
    }
45
46
    public function testCreateCountryValidationFailResponse()
47
    {
48
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
49
50
        $this->client->request('POST', '/api/countries/', [], [], static::$authorizedHeaderWithContentType);
0 ignored issues
show
Bug introduced by
Since $authorizedHeaderWithContentType is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $authorizedHeaderWithContentType to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
51
52
        $response = $this->client->getResponse();
53
        $this->assertResponse($response, 'country/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
54
    }
55
56
    public function testCreateCountryResponse()
57
    {
58
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
59
60
        $data =
61
            <<<EOT
62
        {
63
            "code": "BE"
64
        }
65
EOT;
66
67
        $this->client->request('POST', '/api/countries/', [], [], static::$authorizedHeaderWithContentType, $data);
0 ignored issues
show
Bug introduced by
Since $authorizedHeaderWithContentType is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $authorizedHeaderWithContentType to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
68
69
        $response = $this->client->getResponse();
70
        $this->assertResponse($response, 'country/create_response', Response::HTTP_CREATED);
71
    }
72
73
    public function testGetSCountryWhichDoesNotExistResponse()
74
    {
75
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
76
77
        $this->client->request('GET', '/api/countries/-1', [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Bug introduced by
Since $authorizedHeaderWithAccept is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $authorizedHeaderWithAccept to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
78
79
        $response = $this->client->getResponse();
80
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
81
    }
82
83
    public function testGetCountriesListResponse()
84
    {
85
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
86
        $this->loadFixturesFromFile('resources/countries.yml');
87
88
        $this->client->request('GET', '/api/countries/', [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Bug introduced by
Since $authorizedHeaderWithAccept is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $authorizedHeaderWithAccept to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
89
90
        $response = $this->client->getResponse();
91
        $this->assertResponse($response, 'country/index_response', Response::HTTP_OK);
92
    }
93
94
    public function testGetCountryResponse()
95
    {
96
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
97
        $countries = $this->loadFixturesFromFile('resources/countries.yml');
98
99
        $this->client->request('GET', '/api/countries/'.$countries['country_NL']->getId(), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Bug introduced by
Since $authorizedHeaderWithAccept is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $authorizedHeaderWithAccept to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
100
101
        $response = $this->client->getResponse();
102
        $this->assertResponse($response, 'country/show_response', Response::HTTP_OK);
103
    }
104
105
    public function testGetCountryAccessDeniedResponse()
106
    {
107
        $this->loadFixturesFromFile('resources/countries.yml');
108
        $this->client->request('GET', '/api/countries/1');
109
110
        $response = $this->client->getResponse();
111
        $this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
112
    }
113
114
115
    public function testDeleteCountryWhichDoesNotExistResponse()
116
    {
117
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
118
119
        $this->client->request('DELETE', '/api/countries/-1', [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Bug introduced by
Since $authorizedHeaderWithAccept is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $authorizedHeaderWithAccept to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
120
121
        $response = $this->client->getResponse();
122
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
123
    }
124
125
    public function testDeleteCountryResponse()
126
    {
127
        $this->loadFixturesFromFile('authentication/api_administrator.yml');
128
        $countries = $this->loadFixturesFromFile('resources/countries.yml');
129
130
        $this->client->request('DELETE', '/api/countries/' . $countries['country_NL']->getId(), [], [], static::$authorizedHeaderWithContentType, []);
0 ignored issues
show
Bug introduced by
Since $authorizedHeaderWithContentType is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $authorizedHeaderWithContentType to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
131
132
        $response = $this->client->getResponse();
133
        $this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
134
135
        $this->client->request('GET', '/api/countries/' . $countries['country_NL']->getId(), [], [], static::$authorizedHeaderWithAccept);
0 ignored issues
show
Bug introduced by
Since $authorizedHeaderWithAccept is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $authorizedHeaderWithAccept to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
136
137
        $response = $this->client->getResponse();
138
        $this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
139
    }
140
}
141