Sylius/tests/Controller/LocaleApiTest.php
2016-04-18 14:02:57 +02:00

143 lines
5 KiB
PHP

<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\CoreBundle\Tests\Controller;
use Lakion\ApiTestCase\JsonApiTestCase;
use Symfony\Component\HttpFoundation\Response;
/**
* @author Joeri Timmermans <joeri.timmermans@intracto.com>
* @group joeri
*/
class LocaleApiTest extends JsonApiTestCase
{
/**
* @var array
*/
private static $authorizedHeaderWithContentType = [
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
'CONTENT_TYPE' => 'application/json',
];
/**
* @var array
*/
private static $authorizedHeaderWithAccept = [
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
'ACCEPT' => 'application/json',
];
public function testLocaleAccessDeniedResponse()
{
$this->client->request('POST', '/api/locales/');
$response = $this->client->getResponse();
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
}
public function testGetLocalesListResponse()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$this->loadFixturesFromFile('resources/locales.yml');
$this->client->request('GET', '/api/locales/', [], [], [
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
]);
$response = $this->client->getResponse();
$this->assertResponse($response, 'locale/index_response', Response::HTTP_OK);
}
public function testGetLocaleResponse()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$locales = $this->loadFixturesFromFile('resources/locales.yml');
$this->client->request('GET', '/api/locales/'.$locales['locale_en']->getId(), [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();
$this->assertResponse($response, 'locale/show_response', Response::HTTP_OK);
}
public function testCreateLocaleValidationFailResponse()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$this->client->request('POST', '/api/locales/', [], [], static::$authorizedHeaderWithContentType);
$response = $this->client->getResponse();
$this->assertResponse($response, 'locale/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
}
public function testCreateLocaleResponse()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$data =
<<<EOT
{
"code": "es",
"enabled": true
}
EOT;
$this->client->request('POST', '/api/locales/', [], [], static::$authorizedHeaderWithContentType, $data);
$response = $this->client->getResponse();
$this->assertResponse($response, 'locale/create_response', Response::HTTP_CREATED);
}
public function testGetLocaleAccessDeniedResponse()
{
$this->client->request('GET', '/api/locales/1');
$response = $this->client->getResponse();
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
}
public function testGetLocaleWhichDoesNotExistResponse()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$this->client->request('GET', '/api/locales/-1', [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
}
public function testDeleteLocaleWhichDoesNotExistResponse()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$this->client->request('DELETE', '/api/locales/-1', [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
}
public function testDeleteLocaleResponse()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$locales = $this->loadFixturesFromFile('resources/locales.yml');
$this->client->request('DELETE', '/api/locales/'.$locales['locale_en']->getId(), [], [], static::$authorizedHeaderWithContentType, []);
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
$this->client->request('GET', '/api/locales/'.$locales['locale_en']->getId(), [], [], static::$authorizedHeaderWithAccept);
$response = $this->client->getResponse();
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
}
}