Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b1508e4

Browse files
authored
feature #12279 [API] show product price (SirDomin)
This PR was merged into the 1.9-dev branch. Discussion ---------- | Q | A | --------------- | ----- | Branch? |master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT <!-- - Bug fixes must be submitted against the 1.7 or 1.8 branch (the lowest possible) - Features and deprecations must be submitted against the master branch - Make sure that the correct base branch is set To be sure you are not breaking any Backward Compatibilities, check the documentation: https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html --> Commits ------- b89f37a [API] show product price 92c5773 pr-fix
2 parents a1bba4f + 92c5773 commit b1508e4

9 files changed

Lines changed: 153 additions & 5 deletions

File tree

features/product/viewing_products/viewing_product_price_on_products_list.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Feature: Viewing a product price on products list
77
Background:
88
Given the store operates on a single channel in "United States"
99

10-
@ui
10+
@ui @api
1111
Scenario: Viewing a products with price on list
1212
Given the store has a product "T-shirt watermelon" priced at "$19.00"
1313
And the store classifies its products as "T-Shirts"

features/product/viewing_products/viewing_products_price.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Feature: Viewing a product price
77
Background:
88
Given the store operates on a single channel in "United States"
99

10-
@ui
10+
@ui @api
1111
Scenario: Viewing a detailed page with product's price
1212
Given the store has a product "T-shirt banana" priced at "$39.00"
1313
When I check this product's details

src/Sylius/Behat/Context/Api/Shop/ProductContext.php

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
use Sylius\Behat\Client\ApiClientInterface;
1818
use Sylius\Behat\Client\Request;
1919
use Sylius\Behat\Client\ResponseCheckerInterface;
20+
use Sylius\Behat\Service\SharedStorageInterface;
21+
use Sylius\Component\Core\Model\ChannelInterface;
2022
use Sylius\Component\Core\Model\ProductInterface;
23+
use Sylius\Component\Taxonomy\Model\TaxonInterface;
2124
use Symfony\Component\HttpFoundation\Request as HttpRequest;
25+
use Symfony\Component\HttpFoundation\Response;
2226
use Webmozart\Assert\Assert;
2327

2428
final class ProductContext implements Context
@@ -29,10 +33,17 @@ final class ProductContext implements Context
2933
/** @var ResponseCheckerInterface */
3034
private $responseChecker;
3135

32-
public function __construct(ApiClientInterface $client, ResponseCheckerInterface $responseChecker)
33-
{
36+
/** @var SharedStorageInterface */
37+
private $sharedStorage;
38+
39+
public function __construct(
40+
ApiClientInterface $client,
41+
ResponseCheckerInterface $responseChecker,
42+
SharedStorageInterface $sharedStorage
43+
) {
3444
$this->client = $client;
3545
$this->responseChecker = $responseChecker;
46+
$this->sharedStorage = $sharedStorage;
3647
}
3748

3849
/**
@@ -44,6 +55,46 @@ public function iOpenProductPage(ProductInterface $product): void
4455
$this->client->show($product->getCode());
4556
}
4657

58+
/**
59+
* @When I browse products from taxon :taxon
60+
*/
61+
public function iBrowseProductsFromTaxon(TaxonInterface $taxon): void
62+
{
63+
$this->client->index();
64+
$this->client->addFilter('productTaxons.taxon.code', $taxon->getCode());
65+
$this->client->filter();
66+
}
67+
68+
/**
69+
* @Then /^I should see the product price ("[^"]+")$/
70+
*/
71+
public function iShouldSeeTheProductPrice(int $price): void
72+
{
73+
Assert::true(
74+
$this->hasProductWithPriceInChannel(
75+
[$this->responseChecker->getResponseContent($this->client->getLastResponse())],
76+
$price,
77+
$this->sharedStorage->get('channel'),
78+
)
79+
);
80+
}
81+
82+
/**
83+
* @Then /^I should see the (product "[^"]+") with price ("[^"]+")$/
84+
*/
85+
public function iShouldSeeTheProductWithPrice(ProductInterface $product, int $price): void
86+
{
87+
Assert::true(
88+
$this->hasProductWithPriceInChannel(
89+
$this->responseChecker->getCollection($this->client->getLastResponse()),
90+
$price,
91+
$this->sharedStorage->get('channel'),
92+
$product->getCode()
93+
),
94+
sprintf("There is no product with %s code and %s price", $product->getCode(), $price)
95+
);
96+
}
97+
4798
/**
4899
* @Then I should see the product name :name
49100
*/
@@ -69,7 +120,7 @@ public function itsCurrentVariantShouldBeNamed(string $variantName): void
69120
$response = $this->client->getLastResponse();
70121

71122
$productVariant = $this->responseChecker->getValue($response, 'variants');
72-
$this->client->executeCustomRequest(Request::custom($productVariant[0], HttpRequest::METHOD_GET));
123+
$this->client->executeCustomRequest(Request::custom($productVariant[0]['@id'], HttpRequest::METHOD_GET));
73124

74125
Assert::true(
75126
$this->responseChecker->hasTranslation(
@@ -80,4 +131,21 @@ public function itsCurrentVariantShouldBeNamed(string $variantName): void
80131
)
81132
);
82133
}
134+
135+
private function hasProductWithPriceInChannel(array $products, int $price, ChannelInterface $channel, ?string $productCode = null): bool
136+
{
137+
foreach ($products as $product) {
138+
if ($productCode !== null && $product['code'] !== $productCode) {
139+
continue;
140+
}
141+
142+
foreach ($product['variants'] as $variant) {
143+
if ($variant['channelPricings'][$channel->getCode()]['price'] === $price) {
144+
return true;
145+
}
146+
}
147+
}
148+
149+
return false;
150+
}
83151
}

src/Sylius/Behat/Resources/config/services/contexts/api/shop.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<service id="sylius.behat.context.api.shop.product" class="Sylius\Behat\Context\Api\Shop\ProductContext">
8080
<argument type="service" id="sylius.behat.api_platform_client.shop.product" />
8181
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
82+
<argument type="service" id="sylius.behat.shared_storage" />
8283
</service>
8384

8485
<service id="sylius.behat.context.api.shop.registration" class="Sylius\Behat\Context\Api\Shop\RegistrationContext">

src/Sylius/Behat/Resources/config/suites/api/product/viewing_products.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ default:
1111
- sylius.behat.context.transform.lexical
1212
- sylius.behat.context.transform.product
1313
- sylius.behat.context.transform.shared_storage
14+
- sylius.behat.context.transform.taxon
1415

1516
- sylius.behat.context.setup.channel
1617
- sylius.behat.context.setup.locale
1718
- sylius.behat.context.setup.product
19+
- sylius.behat.context.setup.product_taxon
20+
- sylius.behat.context.setup.taxonomy
1821

1922
- sylius.behat.context.api.shop.product
2023

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" ?>
2+
3+
<!--
4+
5+
This file is part of the Sylius package.
6+
7+
(c) Paweł Jędrzejewski
8+
9+
For the full copyright and license information, please view the LICENSE
10+
file that was distributed with this source code.
11+
12+
-->
13+
14+
<resources xmlns="https://api-platform.com/schema/metadata"
15+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
17+
>
18+
<resource class="%sylius.model.channel_pricing.class%" shortName="ChannelPricing">
19+
<attribute name="validation_groups">sylius</attribute>
20+
21+
<attribute name="normalization_context">
22+
<attribute name="groups">
23+
<attribute>shop:channel_pricing:read</attribute>
24+
</attribute>
25+
</attribute>
26+
27+
<itemOperations>
28+
<itemOperation name="shop_get">
29+
<attribute name="method">GET</attribute>
30+
<attribute name="path">/shop/channel-pricings/{id}</attribute>
31+
</itemOperation>
32+
</itemOperations>
33+
34+
<collectionOperations>
35+
</collectionOperations>
36+
37+
<property name="id" writable="false" readable="true" identifier="true" />
38+
<property name="price" writable="false" readable="true" />
39+
<property name="originalPrice" writable="false" readable="true" />
40+
</resource>
41+
</resources>

src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/ProductVariant.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@
6464
</attribute>
6565
</attribute>
6666
</property>
67+
<property name="channelPricings" writable="true" readable="true" />
6768
</resource>
6869
</resources>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" ?>
2+
3+
<!--
4+
5+
This file is part of the Sylius package.
6+
7+
(c) Paweł Jędrzejewski
8+
9+
For the full copyright and license information, please view the LICENSE
10+
file that was distributed with this source code.
11+
12+
-->
13+
14+
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
15+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
17+
>
18+
<class name="Sylius\Component\Core\Model\ChannelPricing">
19+
<attribute name="price">
20+
<group>shop:channel_pricing:read</group>
21+
<group>shop:product:read</group>
22+
</attribute>
23+
<attribute name="originalPrice">
24+
<group>shop:channel_pricing:read</group>
25+
<group>shop:product:read</group>
26+
</attribute>
27+
</class>
28+
</serializer>

src/Sylius/Bundle/ApiBundle/Resources/config/serialization/ProductVariant.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
<class name="Sylius\Component\Core\Model\ProductVariant">
1919
<attribute name="id">
2020
<group>product_variant:read</group>
21+
<group>shop:product:read</group>
2122
</attribute>
2223
<attribute name="code">
2324
<group>product_variant:read</group>
25+
<group>shop:product:read</group>
2426
</attribute>
2527
<attribute name="product">
2628
<group>product_variant:read</group>
@@ -29,6 +31,10 @@
2931
<group>shop:order:account:read</group>
3032
<group>product_variant:read</group>
3133
<group>shop:cart:read</group>
34+
<group>shop:product:read</group>
35+
</attribute>
36+
<attribute name="channelPricings">
37+
<group>shop:product:read</group>
3238
</attribute>
3339
</class>
3440
</serializer>

0 commit comments

Comments
 (0)