[ApiBundle] Add contract tests for ProductTaxon resource

This commit is contained in:
Wojdylak 2023-12-13 15:02:43 +01:00
parent 4036f1f132
commit e8f1a0a831
No known key found for this signature in database
GPG key ID: 7509E560A6821ABE
6 changed files with 242 additions and 0 deletions

View file

@ -0,0 +1,135 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductTaxonInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Tests\Api\JsonApiTestCase;
use Sylius\Tests\Api\Utils\AdminUserLoginTrait;
use Symfony\Component\HttpFoundation\Response;
final class ProductTaxonsTest extends JsonApiTestCase
{
use AdminUserLoginTrait;
/** @test */
public function it_gets_a_product_taxon(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel.yaml', 'product/product_taxon.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var ProductTaxonInterface $productTaxon */
$productTaxon = $fixtures['product_mug_taxon_mugs'];
$this->client->request(
method: 'GET',
uri: sprintf('/api/v2/admin/product-taxons/%s', $productTaxon->getId()),
server: $header,
);
$this->assertResponse(
$this->client->getResponse(),
'admin/product_taxon/get_product_taxon_response',
Response::HTTP_OK,
);
}
/** @test */
public function it_gets_product_taxons(): void
{
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel.yaml', 'product/product_taxon.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
$this->client->request(method: 'GET', uri: '/api/v2/admin/product-taxons', server: $header);
$this->assertResponse(
$this->client->getResponse(),
'admin/product_taxon/get_product_taxons_response',
Response::HTTP_OK,
);
}
/** @test */
public function it_creates_a_product_taxon(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel.yaml', 'product/product_taxon.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var ProductInterface $product */
$product = $fixtures['product_mug'];
/** @var TaxonInterface $taxon */
$taxon = $fixtures['taxon_caps'];
$this->client->request(
method: 'POST',
uri: '/api/v2/admin/product-taxons',
server: $header,
content: json_encode([
'product' => sprintf('/api/v2/admin/products/%s', $product->getCode()),
'taxon' => sprintf('/api/v2/admin/taxons/%s', $taxon->getCode()),
'position' => 10,
], \JSON_THROW_ON_ERROR),
);
$this->assertResponse(
$this->client->getResponse(),
'admin/product_taxon/post_product_taxon_response',
Response::HTTP_CREATED,
);
}
/** @test */
public function it_updates_a_product_taxon(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel.yaml', 'product/product_taxon.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var ProductTaxonInterface $productTaxon */
$productTaxon = $fixtures['product_cap_taxon_caps'];
$this->client->request(
method: 'PUT',
uri: sprintf('/api/v2/admin/product-taxons/%s', $productTaxon->getId()),
server: $header,
content: json_encode([
'position' => 1,
], \JSON_THROW_ON_ERROR),
);
$this->assertResponse(
$this->client->getResponse(),
'admin/product_taxon/put_product_taxon_response',
Response::HTTP_OK,
);
}
/** @test */
public function it_deletes_a_product_taxon(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel.yaml', 'product/product_taxon.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var ProductTaxonInterface $productTaxon */
$productTaxon = $fixtures['product_cap_taxon_caps'];
$this->client->request(
method: 'DELETE',
uri: sprintf('/api/v2/admin/product-taxons/%s', $productTaxon->getId()),
server: $header,
);
$this->assertResponseCode($this->client->getResponse(), Response::HTTP_NO_CONTENT);
}
}

View file

@ -0,0 +1,25 @@
Sylius\Component\Core\Model\Product:
product_mug:
code: 'MUG'
channels: ['@channel_web']
currentLocale: 'en_US'
product_cap:
code: 'CAP'
channels: ['@channel_web']
currentLocale: 'en_US'
Sylius\Component\Core\Model\Taxon:
taxon_mugs:
code: 'MUGS'
taxon_caps:
code: 'CAPS'
Sylius\Component\Core\Model\ProductTaxon:
product_mug_taxon_mugs:
product: '@product_mug'
taxon: '@taxon_mugs'
position: 1
product_cap_taxon_caps:
product: '@product_cap'
taxon: '@taxon_caps'
position: 2

View file

@ -0,0 +1,9 @@
{
"@context": "\/api\/v2\/contexts\/ProductTaxon",
"@id": "\/api\/v2\/admin\/product-taxons\/@integer@",
"@type": "ProductTaxon",
"id": @integer@,
"product": "\/api\/v2\/admin\/products\/MUG",
"taxon": "\/api\/v2\/admin\/taxons\/MUGS",
"position": 1
}

View file

@ -0,0 +1,55 @@
{
"@context": "\/api\/v2\/contexts\/ProductTaxon",
"@id": "\/api\/v2\/admin\/product-taxons",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "\/api\/v2\/admin\/product-taxons\/@integer@",
"@type": "ProductTaxon",
"id": @integer@,
"product": "\/api\/v2\/admin\/products\/MUG",
"taxon": "\/api\/v2\/admin\/taxons\/MUGS",
"position": 1
},
{
"@id": "\/api\/v2\/admin\/product-taxons\/@integer@",
"@type": "ProductTaxon",
"id": @integer@,
"product": "\/api\/v2\/admin\/products\/CAP",
"taxon": "\/api\/v2\/admin\/taxons\/CAPS",
"position": 2
}
],
"hydra:totalItems": 2,
"hydra:search": {
"@type": "hydra:IriTemplate",
"hydra:template": "\/api\/v2\/admin\/product-taxons{?product.code,product.code[],taxon.code,taxon.code[]}",
"hydra:variableRepresentation": "BasicRepresentation",
"hydra:mapping": [
{
"@type": "IriTemplateMapping",
"variable": "product.code",
"property": "product.code",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "product.code[]",
"property": "product.code",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "taxon.code",
"property": "taxon.code",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "taxon.code[]",
"property": "taxon.code",
"required": false
}
]
}
}

View file

@ -0,0 +1,9 @@
{
"@context": "\/api\/v2\/contexts\/ProductTaxon",
"@id": "\/api\/v2\/admin\/product-taxons\/@integer@",
"@type": "ProductTaxon",
"id": @integer@,
"product": "\/api\/v2\/admin\/products\/MUG",
"taxon": "\/api\/v2\/admin\/taxons\/CAPS",
"position": 10
}

View file

@ -0,0 +1,9 @@
{
"@context": "\/api\/v2\/contexts\/ProductTaxon",
"@id": "\/api\/v2\/admin\/product-taxons\/@integer@",
"@type": "ProductTaxon",
"id": @integer@,
"product": "\/api\/v2\/admin\/products\/CAP",
"taxon": "\/api\/v2\/admin\/taxons\/CAPS",
"position": 1
}