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

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,
"endsAt": null,
"coupons": [
"\/api\/v2\/admin\/promotion-coupons\/XYZ1",
"\/api\/v2\/admin\/promotion-coupons\/XYZ2"
"\/api\/v2\/admin\/promotions\/dollar_off\/coupons\/XYZ1",
"\/api\/v2\/admin\/promotions\/dollar_off\/coupons\/XYZ2"
],
"rules": [],
"actions": [