1717use Sylius \Behat \Client \ApiClientInterface ;
1818use Sylius \Behat \Client \Request ;
1919use Sylius \Behat \Client \ResponseCheckerInterface ;
20+ use Sylius \Behat \Service \SharedStorageInterface ;
21+ use Sylius \Component \Core \Model \ChannelInterface ;
2022use Sylius \Component \Core \Model \ProductInterface ;
2123use Sylius \Component \Taxonomy \Model \TaxonInterface ;
2224use Symfony \Component \HttpFoundation \Request as HttpRequest ;
@@ -31,10 +33,17 @@ final class ProductContext implements Context
3133 /** @var ResponseCheckerInterface */
3234 private $ responseChecker ;
3335
34- public function __construct (ApiClientInterface $ client , ResponseCheckerInterface $ responseChecker )
35- {
36+ /** @var SharedStorageInterface */
37+ private $ sharedStorage ;
38+
39+ public function __construct (
40+ ApiClientInterface $ client ,
41+ ResponseCheckerInterface $ responseChecker ,
42+ SharedStorageInterface $ sharedStorage
43+ ) {
3644 $ this ->client = $ client ;
3745 $ this ->responseChecker = $ responseChecker ;
46+ $ this ->sharedStorage = $ sharedStorage ;
3847 }
3948
4049 /**
@@ -47,33 +56,43 @@ public function iOpenProductPage(ProductInterface $product): void
4756 }
4857
4958 /**
50- * @When /^ I browse products from ( taxon "([^"]+)")$/
59+ * @When I browse products from taxon :taxon
5160 */
52- public function iCheckListOfProductsForTaxon (TaxonInterface $ taxon ): void
61+ public function iBrowseProductsFromTaxon (TaxonInterface $ taxon ): void
5362 {
5463 $ this ->client ->index ();
5564 $ this ->client ->addFilter ('productTaxons.taxon.code ' , $ taxon ->getCode ());
5665 $ this ->client ->filter ();
5766 }
5867
5968 /**
60- * @Then I should see the product price :productPrice
69+ * @Then /^ I should see the product price ("[^"]+")$/
6170 */
62- public function iShouldSeeTheProductPrice (string $ productPrice ): void
71+ public function iShouldSeeTheProductPrice (int $ price ): void
6372 {
64- $ price = (int ) filter_var ($ productPrice , FILTER_SANITIZE_NUMBER_INT );
65-
66- Assert::true ($ this ->hasProductWithPrice ([$ this ->responseChecker ->getResponseContent ($ this ->client ->getLastResponse ())], $ price ));
73+ Assert::true (
74+ $ this ->hasProductWithPriceInChannel (
75+ [$ this ->responseChecker ->getResponseContent ($ this ->client ->getLastResponse ())],
76+ $ price ,
77+ $ this ->sharedStorage ->get ('channel ' ),
78+ )
79+ );
6780 }
6881
6982 /**
70- * @Then I should see the product :product with price :productPrice
83+ * @Then /^ I should see the ( product "[^"]+") with price ("[^"]+")$/
7184 */
72- public function iShouldSeeTheProductWithPrice (ProductInterface $ product , string $ productPrice ): void
85+ public function iShouldSeeTheProductWithPrice (ProductInterface $ product , int $ price ): void
7386 {
74- $ price = (int ) filter_var ($ productPrice , FILTER_SANITIZE_NUMBER_INT );
75-
76- Assert::true ($ this ->hasProductWithPrice ($ this ->responseChecker ->getCollection ($ this ->client ->getLastResponse ()), $ price , $ product ->getCode ()));
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+ );
7796 }
7897
7998 /**
@@ -113,18 +132,16 @@ public function itsCurrentVariantShouldBeNamed(string $variantName): void
113132 );
114133 }
115134
116- private function hasProductWithPrice (array $ resource , int $ price , ?string $ productCode = null ): bool
135+ private function hasProductWithPriceInChannel (array $ products , int $ price, ChannelInterface $ channel , ?string $ productCode = null ): bool
117136 {
118- foreach ($ resource as $ product ) {
119- if ($ productCode && $ product ['code ' ] !== $ productCode ) {
137+ foreach ($ products as $ product ) {
138+ if ($ productCode !== null && $ product ['code ' ] !== $ productCode ) {
120139 continue ;
121140 }
122141
123142 foreach ($ product ['variants ' ] as $ variant ) {
124- foreach ($ variant ['channelPricings ' ] as $ channelPricing ) {
125- if ($ channelPricing ['price ' ] === $ price ) {
126- return true ;
127- }
143+ if ($ variant ['channelPricings ' ][$ channel ->getCode ()]['price ' ] === $ price ) {
144+ return true ;
128145 }
129146 }
130147 }
0 commit comments