Sylius/tests/Api/Admin/ProvincesTest.php
2023-01-12 12:41:07 +01:00

85 lines
2.3 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.
*/
declare(strict_types=1);
namespace Sylius\Tests\Api\Admin;
use Sylius\Component\Addressing\Model\ProvinceInterface;
use Sylius\Tests\Api\JsonApiTestCase;
use Sylius\Tests\Api\Utils\AdminUserLoginTrait;
use Symfony\Component\HttpFoundation\Response;
final class ProvincesTest extends JsonApiTestCase
{
use AdminUserLoginTrait;
/** @test */
public function it_gets_a_country(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'country.yaml']);
$header = $this->getLoggedHeader();
/** @var ProvinceInterface $province */
$province = $fixtures['province_US_WY'];
$this->client->request(
'GET',
sprintf('/api/v2/admin/provinces/%s', $province->getCode()),
[],
[],
$header,
);
$this->assertResponse(
$this->client->getResponse(),
'admin/province/get_province_response',
Response::HTTP_OK,
);
}
/** @test */
public function it_updates_an_existing_province(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'country.yaml']);
/** @var ProvinceInterface $province */
$province = $fixtures['province_US_MI'];
$header = $this->getLoggedHeader();
$this->client->request(
'PUT',
'/api/v2/admin/provinces/' . $province->getCode(),
[],
[],
$header,
json_encode([
'abbreviation' => 'Minn.',
], JSON_THROW_ON_ERROR),
);
$this->assertResponse(
$this->client->getResponse(),
'admin/province/put_province_response',
Response::HTTP_OK,
);
}
private function getLoggedHeader(): array
{
$token = $this->logInAdminUser('api@example.com');
$authorizationHeader = self::$kernel->getContainer()->getParameter('sylius.api.authorization_header');
$header['HTTP_' . $authorizationHeader] = 'Bearer ' . $token;
return array_merge($header, self::CONTENT_TYPE_HEADER);
}
}