-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7966 from pamil/product-association-type-admin-api
Add API for managing product association types
- Loading branch information
Showing
12 changed files
with
575 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Sylius/Bundle/AdminApiBundle/Resources/config/routing/product_association_type.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This file is part of the Sylius package. | ||
# (c) Paweł Jędrzejewski | ||
|
||
sylius_admin_api_product_association_type: | ||
resource: | | ||
identifier: code | ||
alias: sylius.product_association_type | ||
section: admin_api | ||
serialization_version: $version | ||
criteria: | ||
code: $code | ||
type: sylius.resource_api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,327 @@ | ||
<?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\Tests\Controller; | ||
|
||
use Lakion\ApiTestCase\JsonApiTestCase; | ||
use Sylius\Component\Product\Model\ProductAssociationTypeInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* @author Kamil Kokot <kamil@kokot.me> | ||
*/ | ||
final class ProductAssociationTypeApiTest extends JsonApiTestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function it_denies_creating_product_association_type_for_non_authenticated_user() | ||
{ | ||
$this->client->request('POST', '/api/v1/product-association-types/'); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_does_not_allow_to_create_product_association_type_without_specifying_required_data() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
|
||
$this->client->request('POST', '/api/v1/product-association-types/', [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'CONTENT_TYPE' => 'application/json', | ||
]); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'product_association_type/create_validation_fail_response', Response::HTTP_BAD_REQUEST); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_allows_to_create_product_association_type() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
$this->loadFixturesFromFile('resources/locales.yml'); | ||
|
||
$data = | ||
<<<EOT | ||
{ | ||
"code": "cross_sell", | ||
"translations": { | ||
"en_US": { | ||
"name": "Cross sell" | ||
} | ||
} | ||
} | ||
EOT; | ||
|
||
$this->client->request('POST', '/api/v1/product-association-types/', [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'CONTENT_TYPE' => 'application/json', | ||
], $data); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'product_association_type/create_response', Response::HTTP_CREATED); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_denies_getting_product_association_types_for_non_authenticated_user() | ||
{ | ||
$this->client->request('GET', '/api/v1/product-association-types/'); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_allows_to_get_product_association_types() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
$this->loadFixturesFromFile('resources/product_association_types.yml'); | ||
|
||
$this->client->request('GET', '/api/v1/product-association-types/', [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
]); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'product_association_type/index_response', Response::HTTP_OK); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_denies_getting_product_association_type_for_non_authenticated_user() | ||
{ | ||
$this->client->request('GET', '/api/v1/product-association-types/1'); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_returns_not_found_response_when_requesting_details_of_a_product_association_type_which_does_not_exist() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
|
||
$this->client->request('GET', '/api/v1/product-association-types/-1', [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'ACCEPT' => 'application/json', | ||
]); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_allows_to_get_product_association_type() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
$productAssociationTypes = $this->loadFixturesFromFile('resources/product_association_types.yml'); | ||
$productAssociationType = $productAssociationTypes['productAssociationType1']; | ||
|
||
$this->client->request('GET', $this->getAssociationTypeUrl($productAssociationType), [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'ACCEPT' => 'application/json', | ||
]); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'product_association_type/show_response', Response::HTTP_OK); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_denies_updating_product_association_type_for_non_authenticated_user() | ||
{ | ||
$this->client->request('PUT', '/api/v1/product-association-types/1'); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_returns_not_found_response_when_trying_to_update_product_association_type_which_does_not_exist() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
|
||
$this->client->request('PUT', '/api/v1/product-association-types/-1', [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'ACCEPT' => 'application/json', | ||
]); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_does_not_allow_to_update_product_association_type_without_specifying_required_data() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
$this->loadFixturesFromFile('resources/locales.yml'); | ||
$productAssociationTypes = $this->loadFixturesFromFile('resources/product_association_types.yml'); | ||
$productAssociationType = $productAssociationTypes['productAssociationType1']; | ||
|
||
$this->client->request('PUT', $this->getAssociationTypeUrl($productAssociationType), [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'CONTENT_TYPE' => 'application/json', | ||
]); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'product_association_type/update_validation_fail_response', Response::HTTP_BAD_REQUEST); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_allows_to_update_product_association_type() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
$this->loadFixturesFromFile('resources/locales.yml'); | ||
$productAssociationTypes = $this->loadFixturesFromFile('resources/product_association_types.yml'); | ||
$productAssociationType = $productAssociationTypes['productAssociationType1']; | ||
|
||
$data = | ||
<<<EOT | ||
{ | ||
"translations": { | ||
"en_US": { | ||
"name": "Cross sell (old)" | ||
}, | ||
"nl_NL": { | ||
"name": "Cross sell (old, nl_NL)" | ||
} | ||
} | ||
} | ||
EOT; | ||
|
||
$this->client->request('PUT', $this->getAssociationTypeUrl($productAssociationType), [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'CONTENT_TYPE' => 'application/json', | ||
], $data); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_returns_not_found_response_when_trying_to_partially_update_product_association_type_which_does_not_exist() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
|
||
$this->client->request('PATCH', '/api/v1/product-association-types/-1', [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'ACCEPT' => 'application/json', | ||
]); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_allows_to_partially_update_product_association_type() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
$this->loadFixturesFromFile('resources/locales.yml'); | ||
$productAssociationTypes = $this->loadFixturesFromFile('resources/product_association_types.yml'); | ||
$productAssociationType = $productAssociationTypes['productAssociationType1']; | ||
|
||
$data = | ||
<<<EOT | ||
{ | ||
"translations": { | ||
"en_US": { | ||
"name": "Cross sell (old)" | ||
} | ||
} | ||
} | ||
EOT; | ||
|
||
$this->client->request('PATCH', $this->getAssociationTypeUrl($productAssociationType), [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'CONTENT_TYPE' => 'application/json', | ||
], $data); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_returns_not_found_response_when_trying_to_delete_product_association_type_which_does_not_exist() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
|
||
$this->client->request('DELETE', '/api/v1/product-association-types/-1', [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'ACCEPT' => 'application/json', | ||
]); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_allows_to_delete_product_association_type() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
$productAssociationTypes = $this->loadFixturesFromFile('resources/product_association_types.yml'); | ||
$productAssociationType = $productAssociationTypes['productAssociationType1']; | ||
|
||
$this->client->request('DELETE', $this->getAssociationTypeUrl($productAssociationType), [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'CONTENT_TYPE' => 'application/json', | ||
], []); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); | ||
|
||
$this->client->request('GET', $this->getAssociationTypeUrl($productAssociationType), [], [], [ | ||
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', | ||
'ACCEPT' => 'application/json', | ||
]); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); | ||
} | ||
|
||
/** | ||
* @param ProductAssociationTypeInterface $productAssociationType | ||
* | ||
* @return string | ||
*/ | ||
private function getAssociationTypeUrl(ProductAssociationTypeInterface $productAssociationType) | ||
{ | ||
return 'api/v1/product-association-types/' . $productAssociationType->getCode(); | ||
} | ||
} |
Oops, something went wrong.