[Unit] Refine promotion tests

This commit is contained in:
Rafikooo 2024-09-11 11:39:23 +02:00
parent 6304d13a52
commit c83a2642a8
No known key found for this signature in database
GPG key ID: 4A26D8327BC2442B
3 changed files with 59 additions and 207 deletions

View file

@ -28,71 +28,44 @@ use Sylius\Component\Core\Promotion\Checker\Rule\NthOrderRuleChecker;
use Sylius\Component\Core\Promotion\Checker\Rule\ShippingCountryRuleChecker; use Sylius\Component\Core\Promotion\Checker\Rule\ShippingCountryRuleChecker;
use Sylius\Component\Core\Promotion\Checker\Rule\TotalOfItemsFromTaxonRuleChecker; use Sylius\Component\Core\Promotion\Checker\Rule\TotalOfItemsFromTaxonRuleChecker;
use Sylius\Tests\Api\JsonApiTestCase; use Sylius\Tests\Api\JsonApiTestCase;
use Sylius\Tests\Api\Utils\AdminUserLoginTrait;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
final class PromotionsTest extends JsonApiTestCase final class PromotionsTest extends JsonApiTestCase
{ {
use AdminUserLoginTrait; protected function setUp(): void
{
$this->setUpAdminContext();
$this->setUpDefaultPostHeaders();
$this->setUpDefaultGetHeaders();
$this->setUpDefaultPutHeaders();
$this->setUpDefaultPatchHeaders();
$this->setUpDefaultDeleteHeaders();
parent::setUp();
}
/** @test */ /** @test */
public function it_gets_promotions(): void public function it_gets_promotions(): void
{ {
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']); $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']);
$this->setUpAdminContext('api@example.com');
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
$this->client->request(method: 'GET', uri: '/api/v2/admin/promotions', server: $header); $this->requestGet('/api/v2/admin/promotions');
$this->assertResponse( $this->assertResponseSuccessful('admin/promotion/get_promotions_response');
$this->client->getResponse(),
'admin/promotion/get_promotions_response',
Response::HTTP_OK,
);
} }
/** @test */ /** @test */
public function it_gets_a_promotion(): void public function it_gets_a_promotion(): void
{ {
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']); $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var PromotionInterface $promotion */ /** @var PromotionInterface $promotion */
$promotion = $fixtures['promotion_50_off']; $promotion = $fixtures['promotion_50_off'];
$this->client->request( $this->requestGet(sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()));
method: 'GET',
uri: sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()),
server: $header,
);
$this->assertResponse( $this->assertResponseSuccessful('admin/promotion/get_promotion_response');
$this->client->getResponse(),
'admin/promotion/get_promotion_response',
Response::HTTP_OK,
);
}
/** @test */
public function it_gets_promotion_coupons(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var PromotionInterface $promotion */
$promotion = $fixtures['promotion_1_off'];
$this->client->request(
method: 'GET',
uri: sprintf('/api/v2/admin/promotions/%s/coupons', $promotion->getCode()),
server: $header,
);
$this->assertResponse(
$this->client->getResponse(),
'admin/promotion/get_promotion_coupons_response',
Response::HTTP_OK,
);
} }
/** @test */ /** @test */
@ -106,13 +79,10 @@ final class PromotionsTest extends JsonApiTestCase
'promotion/product.yaml', 'promotion/product.yaml',
'promotion/taxon.yaml', 'promotion/taxon.yaml',
]); ]);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
$this->client->request( $this->requestPost(
method: 'POST',
uri: '/api/v2/admin/promotions', uri: '/api/v2/admin/promotions',
server: $header, body: [
content: json_encode([
'name' => 'T-Shirts discount', 'name' => 'T-Shirts discount',
'code' => 'tshirts_discount', 'code' => 'tshirts_discount',
'channels' => [ 'channels' => [
@ -244,95 +214,67 @@ final class PromotionsTest extends JsonApiTestCase
], ],
], ],
], ],
], \JSON_THROW_ON_ERROR), ],
); );
$this->assertResponse( $this->assertResponseCreated('admin/promotion/post_promotion_response');
$this->client->getResponse(),
'admin/promotion/post_promotion_response',
Response::HTTP_CREATED,
);
} }
/** @test */ /** @test */
public function it_does_not_create_a_promotion_without_required_data(): void public function it_does_not_create_a_promotion_without_required_data(): void
{ {
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml']); $this->loadFixturesFromFiles(['authentication/api_administrator.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
$this->client->request( $this->requestPost(
method: 'POST',
uri: '/api/v2/admin/promotions', uri: '/api/v2/admin/promotions',
server: $header, body: [],
content: json_encode([], \JSON_THROW_ON_ERROR),
); );
$this->assertResponse( $this->assertResponseUnprocessableEntity('admin/promotion/post_promotion_without_required_data_response');
$this->client->getResponse(),
'admin/promotion/post_promotion_without_required_data_response',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
} }
/** @test */ /** @test */
public function it_does_not_create_a_promotion_with_taken_code(): void public function it_does_not_create_a_promotion_with_taken_code(): void
{ {
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']); $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
$this->client->request( $this->requestPost(
method: 'POST',
uri: '/api/v2/admin/promotions', uri: '/api/v2/admin/promotions',
server: $header, body: [
content: json_encode([
'name' => '50% Off on your first order', 'name' => '50% Off on your first order',
'code' => '50_off', 'code' => '50_off',
], \JSON_THROW_ON_ERROR), ],
); );
$this->assertResponse( $this->assertResponseUnprocessableEntity('admin/promotion/post_promotion_with_taken_code_response');
$this->client->getResponse(),
'admin/promotion/post_promotion_with_taken_code_response',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
} }
/** @test */ /** @test */
public function it_does_not_create_a_promotion_with_end_date_earlier_than_start_date(): void public function it_does_not_create_a_promotion_with_end_date_earlier_than_start_date(): void
{ {
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml']); $this->loadFixturesFromFiles(['authentication/api_administrator.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
$this->client->request( $this->requestPost(
method: 'POST',
uri: '/api/v2/admin/promotions', uri: '/api/v2/admin/promotions',
server: $header, body: [
content: json_encode([
'name' => 'T-Shirts discount', 'name' => 'T-Shirts discount',
'code' => 'tshirts_discount', 'code' => 'tshirts_discount',
'startsAt' => '2023-12-04 12:30:00', 'startsAt' => '2023-12-04 12:30:00',
'endsAt' => '2023-11-04 12:30:00', 'endsAt' => '2023-11-04 12:30:00',
], \JSON_THROW_ON_ERROR), ],
); );
$this->assertResponse( $this->assertResponseUnprocessableEntity('admin/promotion/post_promotion_with_invalid_dates_response');
$this->client->getResponse(),
'admin/promotion/post_promotion_with_invalid_dates_response',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
} }
/** @test */ /** @test */
public function it_does_not_create_a_promotion_with_invalid_rules(): void public function it_does_not_create_a_promotion_with_invalid_rules(): void
{ {
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'promotion/channel.yaml']); $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'promotion/channel.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
$this->client->request( $this->requestPost(
method: 'POST',
uri: '/api/v2/admin/promotions', uri: '/api/v2/admin/promotions',
server: $header, body: [
content: json_encode([
'name' => 'T-Shirts discount', 'name' => 'T-Shirts discount',
'code' => 'tshirts_discount', 'code' => 'tshirts_discount',
'rules' => [ 'rules' => [
@ -390,27 +332,20 @@ final class PromotionsTest extends JsonApiTestCase
'configuration' => [], 'configuration' => [],
], ],
], ],
], \JSON_THROW_ON_ERROR), ],
); );
$this->assertResponse( $this->assertResponseUnprocessableEntity('admin/promotion/post_promotion_with_invalid_rules_response');
$this->client->getResponse(),
'admin/promotion/post_promotion_with_invalid_rules_response',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
} }
/** @test */ /** @test */
public function it_does_not_create_a_promotion_with_invalid_actions(): void public function it_does_not_create_a_promotion_with_invalid_actions(): void
{ {
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'promotion/channel.yaml']); $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'promotion/channel.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
$this->client->request( $this->requestPost(
method: 'POST',
uri: '/api/v2/admin/promotions', uri: '/api/v2/admin/promotions',
server: $header, body: [
content: json_encode([
'name' => 'T-Shirts discount', 'name' => 'T-Shirts discount',
'code' => 'tshirts_discount', 'code' => 'tshirts_discount',
'actions' => [ 'actions' => [
@ -453,30 +388,23 @@ final class PromotionsTest extends JsonApiTestCase
'configuration' => [], 'configuration' => [],
], ],
], ],
], \JSON_THROW_ON_ERROR), ],
); );
$this->assertResponse( $this->assertResponseUnprocessableEntity('admin/promotion/post_promotion_with_invalid_actions_response');
$this->client->getResponse(),
'admin/promotion/post_promotion_with_invalid_actions_response',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
} }
/** @test */ /** @test */
public function it_updates_promotion(): void public function it_updates_promotion(): void
{ {
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']); $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var PromotionInterface $promotion */ /** @var PromotionInterface $promotion */
$promotion = $fixtures['promotion_50_off']; $promotion = $fixtures['promotion_50_off'];
$this->client->request( $this->requestPut(
method: 'PUT',
uri: sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()), uri: sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()),
server: $header, body: [
content: json_encode([
'name' => 'Christmas', 'name' => 'Christmas',
'code' => 'new_code', 'code' => 'new_code',
'appliesToDiscounted' => true, 'appliesToDiscounted' => true,
@ -511,82 +439,59 @@ final class PromotionsTest extends JsonApiTestCase
'@id' => '/api/v2/admin/promotions/50_off/translations/en_US', '@id' => '/api/v2/admin/promotions/50_off/translations/en_US',
'label' => 'Christmas', 'label' => 'Christmas',
]], ]],
], \JSON_THROW_ON_ERROR), ],
); );
$this->assertResponse( $this->assertResponseSuccessful('admin/promotion/put_promotion_response');
$this->client->getResponse(),
'admin/promotion/put_promotion_response',
Response::HTTP_OK,
);
} }
/** @test */ /** @test */
public function it_updates_promotion_to_last_priority_when_priority_is_minus_one(): void public function it_updates_promotion_to_last_priority_when_priority_is_minus_one(): void
{ {
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']); $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var PromotionInterface $promotion */ /** @var PromotionInterface $promotion */
$promotion = $fixtures['promotion_50_off']; $promotion = $fixtures['promotion_50_off'];
$this->client->request( $this->requestPut(
method: 'PUT',
uri: sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()), uri: sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()),
server: $header, body: [
content: json_encode([
'priority' => -1, 'priority' => -1,
], \JSON_THROW_ON_ERROR), ],
); );
$this->assertResponse( $this->assertResponseSuccessful('admin/promotion/put_promotion_to_last_priority_when_priority_is_minus_one_response');
$this->client->getResponse(),
'admin/promotion/put_promotion_to_last_priority_when_priority_is_minus_one_response',
Response::HTTP_OK,
);
} }
/** @test */ /** @test */
public function it_does_not_update_a_promotion_with_duplicate_locale_translation(): void public function it_does_not_update_a_promotion_with_duplicate_locale_translation(): void
{ {
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']); $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var PromotionInterface $promotion */ /** @var PromotionInterface $promotion */
$promotion = $fixtures['promotion_50_off']; $promotion = $fixtures['promotion_50_off'];
$this->client->request( $this->requestPut(
method: 'PUT',
uri: sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()), uri: sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()),
server: $header, body: [
content: json_encode([
'translations' => ['en_US' => [ 'translations' => ['en_US' => [
'label' => 'Christmas', 'label' => 'Christmas',
]], ]],
], \JSON_THROW_ON_ERROR), ],
); );
$this->assertResponse( $this->assertResponseUnprocessableEntity('admin/promotion/put_promotion_with_duplicate_locale_translation');
$this->client->getResponse(),
'admin/promotion/put_promotion_with_duplicate_locale_translation',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
} }
/** @test */ /** @test */
public function it_deletes_a_promotion(): void public function it_deletes_a_promotion(): void
{ {
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']); $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'channel/channel.yaml', 'promotion/promotion.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var PromotionInterface $promotion */ /** @var PromotionInterface $promotion */
$promotion = $fixtures['promotion_50_off']; $promotion = $fixtures['promotion_50_off'];
$this->client->request( $this->requestDelete(sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()));
method: 'DELETE',
uri: sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()),
server: $header,
);
$this->assertResponseCode($this->client->getResponse(), Response::HTTP_NO_CONTENT); $this->assertResponseCode($this->client->getResponse(), Response::HTTP_NO_CONTENT);
} }
@ -600,16 +505,11 @@ final class PromotionsTest extends JsonApiTestCase
'promotion/promotion.yaml', 'promotion/promotion.yaml',
'promotion/promotion_order.yaml', 'promotion/promotion_order.yaml',
]); ]);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var PromotionInterface $promotion */ /** @var PromotionInterface $promotion */
$promotion = $fixtures['promotion_50_off']; $promotion = $fixtures['promotion_50_off'];
$this->client->request( $this->requestDelete(sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()));
method: 'DELETE',
uri: sprintf('/api/v2/admin/promotions/%s', $promotion->getCode()),
server: $header,
);
$this->assertResponseCode($this->client->getResponse(), Response::HTTP_UNPROCESSABLE_ENTITY); $this->assertResponseCode($this->client->getResponse(), Response::HTTP_UNPROCESSABLE_ENTITY);
} }
@ -626,17 +526,11 @@ final class PromotionsTest extends JsonApiTestCase
/** @var PromotionInterface $promotion */ /** @var PromotionInterface $promotion */
$promotion = $fixtures['promotion_50_off']; $promotion = $fixtures['promotion_50_off'];
$this->client->request( $this->requestPatch(
method: 'PATCH',
uri: sprintf('/api/v2/admin/promotions/%s/archive', $promotion->getCode()), uri: sprintf('/api/v2/admin/promotions/%s/archive', $promotion->getCode()),
server: $this->headerBuilder()->withJsonLdAccept()->withAdminUserAuthorization('api@example.com')->build(),
); );
$this->assertResponse( $this->assertResponseSuccessful('admin/promotion/archive_promotion');
$this->client->getResponse(),
'admin/promotion/archive_promotion',
Response::HTTP_OK,
);
} }
/** @test */ /** @test */
@ -651,16 +545,8 @@ final class PromotionsTest extends JsonApiTestCase
/** @var PromotionInterface $promotion */ /** @var PromotionInterface $promotion */
$promotion = $fixtures['promotion_back_to_school']; $promotion = $fixtures['promotion_back_to_school'];
$this->client->request( $this->requestPatch(sprintf('/api/v2/admin/promotions/%s/restore', $promotion->getCode()));
method: 'PATCH',
uri: sprintf('/api/v2/admin/promotions/%s/restore', $promotion->getCode()),
server: $this->headerBuilder()->withJsonLdAccept()->withAdminUserAuthorization('api@example.com')->build(),
);
$this->assertResponse( $this->assertResponseSuccessful('admin/promotion/restore_promotion');
$this->client->getResponse(),
'admin/promotion/restore_promotion',
Response::HTTP_OK,
);
} }
} }

View file

@ -1,34 +0,0 @@
{
"@context": "\/api\/v2\/contexts\/PromotionCoupon",
"@id": "\/api\/v2\/admin\/promotions/dollar_off/coupons",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "\/api\/v2\/admin\/promotion-coupons\/XYZ1",
"@type": "PromotionCoupon",
"perCustomerUsageLimit": 1,
"reusableFromCancelledOrders": true,
"code": "XYZ1",
"usageLimit": 2,
"used": 1,
"promotion": "\/api\/v2\/admin\/promotions\/dollar_off",
"expiresAt": null,
"createdAt": "@date@",
"updatedAt": "@date@"
},
{
"@id": "\/api\/v2\/admin\/promotion-coupons\/XYZ2",
"@type": "PromotionCoupon",
"perCustomerUsageLimit": null,
"reusableFromCancelledOrders": false,
"code": "XYZ2",
"usageLimit": null,
"used": 0,
"promotion": "\/api\/v2\/admin\/promotions\/dollar_off",
"expiresAt": "@date@",
"createdAt": "@date@",
"updatedAt": "@date@"
}
],
"hydra:totalItems": 2
}

View file

@ -23,8 +23,8 @@
"startsAt": null, "startsAt": null,
"endsAt": null, "endsAt": null,
"coupons": [ "coupons": [
"\/api\/v2\/admin\/promotion-coupons\/XYZ1", "\/api\/v2\/admin\/promotions\/dollar_off\/coupons\/XYZ1",
"\/api\/v2\/admin\/promotion-coupons\/XYZ2" "\/api\/v2\/admin\/promotions\/dollar_off\/coupons\/XYZ2"
], ],
"rules": [], "rules": [],
"actions": [ "actions": [