From 0cdf3cfa5f2ab64e5bcc2541ac40b166e1e15cb0 Mon Sep 17 00:00:00 2001 From: Rafikooo Date: Tue, 14 Mar 2023 23:22:15 +0100 Subject: [PATCH] [PriceHistory][Test] Add contract tests for ChannelPricingLogEntry entity --- .../Api/Admin/ChannelPricingLogEntryTest.php | 104 ++++++++++++++++++ .../product_variant_with_lowest_price.yaml | 98 +++++++++++++++++ tests/Api/JsonApiTestCase.php | 13 +++ ..._channel_pricing_log_entries_response.json | 71 ++++++++++++ ...et_channel_pricing_log_entry_response.json | 9 ++ ..._channel_pricing_log_entries_response.json | 51 +++++++++ 6 files changed, 346 insertions(+) create mode 100644 tests/Api/Admin/ChannelPricingLogEntryTest.php create mode 100644 tests/Api/DataFixtures/ORM/product/product_variant_with_lowest_price.yaml create mode 100644 tests/Api/Responses/Expected/admin/get_channel_pricing_log_entries_response.json create mode 100644 tests/Api/Responses/Expected/admin/get_channel_pricing_log_entry_response.json create mode 100644 tests/Api/Responses/Expected/admin/get_filtered_channel_pricing_log_entries_response.json diff --git a/tests/Api/Admin/ChannelPricingLogEntryTest.php b/tests/Api/Admin/ChannelPricingLogEntryTest.php new file mode 100644 index 0000000000..d0a8295895 --- /dev/null +++ b/tests/Api/Admin/ChannelPricingLogEntryTest.php @@ -0,0 +1,104 @@ +loadFixturesFromFiles(['product/product_variant_with_lowest_price.yaml']); + + $this->client->request( + method: 'GET', + uri: '/api/v2/admin/channel-pricing-log-entries', + server: $this->getUnloggedHeader(), + ); + + $response = $this->client->getResponse(); + $this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); + } + + /** @test */ + public function it_gets_single_channel_pricing_log_entry(): void + { + $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'product/product_variant_with_lowest_price.yaml']); + + /** @var ChannelPricingInterface $channelPricing */ + $channelPricing = $fixtures['channel_pricing_product_variant_mug_blue_home']; + + /** @var RepositoryInterface $channelPricingLogEntryRepository */ + $channelPricingLogEntryRepository = $this->getContainer()->get('sylius.repository.channel_pricing_log_entry'); + /** @var ChannelPricingLogEntryInterface $firstLogEntry */ + $channelPricingLogEntry = $channelPricingLogEntryRepository->findOneBy(['channelPricing' => $channelPricing]); + + $this->client->request( + method: 'GET', + uri: sprintf('/api/v2/admin/channel-pricing-log-entries/%d', $channelPricingLogEntry->getId()), + server: $this->getLoggedHeader(), + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/get_channel_pricing_log_entry_response', + Response::HTTP_OK + ); + } + + /** @test */ + public function it_gets_all_channel_pricing_log_entries(): void + { + $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'product/product_variant_with_lowest_price.yaml']); + + $this->client->request( + method: 'GET', + uri: '/api/v2/admin/channel-pricing-log-entries', + server: $this->getLoggedHeader(), + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/get_channel_pricing_log_entries_response', + Response::HTTP_OK + ); + } + + /** @test */ + public function it_gets_filtered_channel_pricing_log_entries(): void + { + $fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'product/product_variant_with_lowest_price.yaml']); + + $uri = '/api/v2/admin/channel-pricing-log-entries'; + $uri .= '?channelPricing.channelCode=' . $fixtures['channel_home']->getCode(); + $uri .= '&channelPricing.productVariant.code=' . $fixtures['product_variant_mug_blue']->getCode(); + + $this->client->request( + method: 'GET', + uri: $uri, + server: $this->getLoggedHeader(), + ); + + $this->assertResponse( + $this->client->getResponse(), + 'admin/get_filtered_channel_pricing_log_entries_response', + Response::HTTP_OK + ); + } +} diff --git a/tests/Api/DataFixtures/ORM/product/product_variant_with_lowest_price.yaml b/tests/Api/DataFixtures/ORM/product/product_variant_with_lowest_price.yaml new file mode 100644 index 0000000000..699d1053b5 --- /dev/null +++ b/tests/Api/DataFixtures/ORM/product/product_variant_with_lowest_price.yaml @@ -0,0 +1,98 @@ +Sylius\Component\Core\Model\Channel: + channel_home: + code: 'HOME' + name: 'Home Channel' + hostname: 'home.localhost' + description: 'Lorem ipsum' + baseCurrency: '@currency_usd' + defaultLocale: '@locale_en' + locales: ['@locale_en', '@locale_pl'] + color: 'black' + enabled: true + taxCalculationStrategy: 'order_items_based' + channel_fashion: + code: 'FASHION' + name: 'Fashion Channel' + hostname: 'fashion.localhost' + description: 'Lorem ipsum' + baseCurrency: '@currency_usd' + defaultLocale: '@locale_en' + locales: ['@locale_en', '@locale_pl'] + color: 'black' + enabled: true + taxCalculationStrategy: 'order_items_based' + +Sylius\Component\Currency\Model\Currency: + currency_usd: + code: 'USD' + +Sylius\Component\Locale\Model\Locale: + locale_en: + code: 'en_US' + locale_pl: + code: 'pl_PL' + locale_de: + code: 'de_DE' + +Sylius\Component\Core\Model\Product: + product_mug: + code: 'MUG' + channels: ['@channel_home', '@channel_fashion'] + currentLocale: 'en_US' + translations: + en_US: '@product_translation_mug' + +Sylius\Component\Core\Model\ProductTranslation: + product_translation_mug: + slug: 'mug' + locale: 'en_US' + name: 'Mug' + description: '' + translatable: '@product_mug' + +Sylius\Component\Core\Model\ProductVariant: + product_variant_mug_blue: + code: 'MUG_BLUE' + product: '@product_mug' + currentLocale: 'en_US' + translations: + en_US: '@product_variant_translation_mug_blue' + channelPricings: + HOME: '@channel_pricing_product_variant_mug_blue_home' + FASHION: '@channel_pricing_product_variant_mug_blue_fashion' + + product_variant_mug_red: + code: 'MUG_RED' + product: '@product_mug' + currentLocale: 'en_US' + translations: + en_US: '@product_variant_translation_mug_red' + channelPricings: + HOME: '@channel_pricing_product_variant_mug_red_home' + FASHION: '@channel_pricing_product_variant_mug_red_fashion' + +Sylius\Component\Product\Model\ProductVariantTranslation: + product_variant_translation_mug_blue: + locale: 'en_US' + name: 'Blue Mug' + translatable: '@product_variant_mug_blue' + product_variant_translation_mug_red: + locale: 'en_US' + name: 'Red Mug' + translatable: '@product_variant_mug_red' + +Sylius\Component\Core\Model\ChannelPricing: + channel_pricing_product_variant_mug_blue_home: + channelCode: 'HOME' + price: 1000 + originalPrice: 2000 + lowestPriceBeforeDiscount: 2100 + channel_pricing_product_variant_mug_blue_fashion: + channelCode: 'FASHION' + price: 2000 + channel_pricing_product_variant_mug_red_home: + channelCode: 'HOME' + price: 3000 + channel_pricing_product_variant_mug_red_fashion: + channelCode: 'FASHION' + price: 3000 diff --git a/tests/Api/JsonApiTestCase.php b/tests/Api/JsonApiTestCase.php index 64ad607cfc..9d25f420ea 100644 --- a/tests/Api/JsonApiTestCase.php +++ b/tests/Api/JsonApiTestCase.php @@ -14,9 +14,12 @@ declare(strict_types=1); namespace Sylius\Tests\Api; use ApiTestCase\JsonApiTestCase as BaseJsonApiTestCase; +use Sylius\Tests\Api\Utils\AdminUserLoginTrait; abstract class JsonApiTestCase extends BaseJsonApiTestCase { + use AdminUserLoginTrait; + public const CONTENT_TYPE_HEADER = ['CONTENT_TYPE' => 'application/ld+json', 'HTTP_ACCEPT' => 'application/ld+json']; public const PATCH_CONTENT_TYPE_HEADER = ['CONTENT_TYPE' => 'application/merge-patch+json', 'HTTP_ACCEPT' => 'application/ld+json']; @@ -37,4 +40,14 @@ abstract class JsonApiTestCase extends BaseJsonApiTestCase return parent::get($id); } + + protected function getUnloggedHeader(): array + { + return self::CONTENT_TYPE_HEADER; + } + + protected function getLoggedHeader(): array + { + return array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER); + } } diff --git a/tests/Api/Responses/Expected/admin/get_channel_pricing_log_entries_response.json b/tests/Api/Responses/Expected/admin/get_channel_pricing_log_entries_response.json new file mode 100644 index 0000000000..664738f6f4 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/get_channel_pricing_log_entries_response.json @@ -0,0 +1,71 @@ +{ + "@context": "\/api\/v2\/contexts\/ChannelPricingLogEntry", + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries", + "@type": "hydra:Collection", + "hydra:member": [ + { + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries\/@integer@", + "@type": "ChannelPricingLogEntry", + "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", + "price": 3000, + "originalPrice": null, + "loggedAt": "@date@" + }, + { + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries\/@integer@", + "@type": "ChannelPricingLogEntry", + "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", + "price": 1000, + "originalPrice": 2000, + "loggedAt": "@date@" + }, + { + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries\/@integer@", + "@type": "ChannelPricingLogEntry", + "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", + "price": 3000, + "originalPrice": null, + "loggedAt": "@date@" + }, + { + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries\/@integer@", + "@type": "ChannelPricingLogEntry", + "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", + "price": 2000, + "originalPrice": null, + "loggedAt": "@date@" + } + ], + "hydra:totalItems": 4, + "hydra:search": { + "@type": "hydra:IriTemplate", + "hydra:template": "\/api\/v2\/admin\/channel-pricing-log-entries{?channelPricing.channelCode,channelPricing.channelCode[],channelPricing.productVariant.code,channelPricing.productVariant.code[]}", + "hydra:variableRepresentation": "BasicRepresentation", + "hydra:mapping": [ + { + "@type": "IriTemplateMapping", + "variable": "channelPricing.channelCode", + "property": "channelPricing.channelCode", + "required": false + }, + { + "@type": "IriTemplateMapping", + "variable": "channelPricing.channelCode[]", + "property": "channelPricing.channelCode", + "required": false + }, + { + "@type": "IriTemplateMapping", + "variable": "channelPricing.productVariant.code", + "property": "channelPricing.productVariant.code", + "required": false + }, + { + "@type": "IriTemplateMapping", + "variable": "channelPricing.productVariant.code[]", + "property": "channelPricing.productVariant.code", + "required": false + } + ] + } +} diff --git a/tests/Api/Responses/Expected/admin/get_channel_pricing_log_entry_response.json b/tests/Api/Responses/Expected/admin/get_channel_pricing_log_entry_response.json new file mode 100644 index 0000000000..45ab8c17a5 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/get_channel_pricing_log_entry_response.json @@ -0,0 +1,9 @@ +{ + "@context": "\/api\/v2\/contexts\/ChannelPricingLogEntry", + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries/@integer@", + "@type": "ChannelPricingLogEntry", + "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", + "price": 1000, + "originalPrice": 2000, + "loggedAt": "@date@" +} diff --git a/tests/Api/Responses/Expected/admin/get_filtered_channel_pricing_log_entries_response.json b/tests/Api/Responses/Expected/admin/get_filtered_channel_pricing_log_entries_response.json new file mode 100644 index 0000000000..8e4f0b7210 --- /dev/null +++ b/tests/Api/Responses/Expected/admin/get_filtered_channel_pricing_log_entries_response.json @@ -0,0 +1,51 @@ +{ + "@context": "\/api\/v2\/contexts\/ChannelPricingLogEntry", + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries", + "@type": "hydra:Collection", + "hydra:member": [ + { + "@id": "\/api\/v2\/admin\/channel-pricing-log-entries\/@integer@", + "@type": "ChannelPricingLogEntry", + "channelPricing": "\/api\/v2\/admin\/channel-pricings\/@integer@", + "price": 1000, + "originalPrice": 2000, + "loggedAt": "@date@" + } + ], + "hydra:totalItems": 1, + "hydra:view": { + "@id": @string@, + "@type": "hydra:PartialCollectionView" + }, + "hydra:search": { + "@type": "hydra:IriTemplate", + "hydra:template": "\/api\/v2\/admin\/channel-pricing-log-entries{?channelPricing.channelCode,channelPricing.channelCode[],channelPricing.productVariant.code,channelPricing.productVariant.code[]}", + "hydra:variableRepresentation": "BasicRepresentation", + "hydra:mapping": [ + { + "@type": "IriTemplateMapping", + "variable": "channelPricing.channelCode", + "property": "channelPricing.channelCode", + "required": false + }, + { + "@type": "IriTemplateMapping", + "variable": "channelPricing.channelCode[]", + "property": "channelPricing.channelCode", + "required": false + }, + { + "@type": "IriTemplateMapping", + "variable": "channelPricing.productVariant.code", + "property": "channelPricing.productVariant.code", + "required": false + }, + { + "@type": "IriTemplateMapping", + "variable": "channelPricing.productVariant.code[]", + "property": "channelPricing.productVariant.code", + "required": false + } + ] + } +}