10
10
use Setono \SyliusCatalogPromotionPlugin \Calculator \ProductVariantPricesCalculator ;
11
11
use Setono \SyliusCatalogPromotionPlugin \Model \ProductInterface ;
12
12
use Sylius \Component \Core \Model \Channel ;
13
+ use Sylius \Component \Core \Model \ChannelInterface ;
14
+ use Sylius \Component \Core \Model \ChannelPricing ;
13
15
use Sylius \Component \Core \Model \ChannelPricingInterface ;
14
16
use Sylius \Component \Core \Model \ProductVariantInterface ;
15
17
@@ -33,6 +35,7 @@ public function it_delegates_to_decorated(): void
33
35
$ product ->hasPreQualifiedCatalogPromotions ()->willReturn (false );
34
36
35
37
$ productVariant = $ this ->prophesize (ProductVariantInterface::class);
38
+ $ productVariant ->getCode ()->willReturn ('product_variant_1 ' );
36
39
$ productVariant ->getChannelPricingForChannel ($ channel )->willReturn ($ channelPricing ->reveal ());
37
40
$ productVariant ->getProduct ()->willReturn ($ product ->reveal ());
38
41
@@ -65,6 +68,7 @@ public function it_calculates_catalog_promotion(): void
65
68
$ product ->getPreQualifiedCatalogPromotions ()->willReturn (['promo1 ' , 'promo2 ' ]);
66
69
67
70
$ productVariant = $ this ->prophesize (ProductVariantInterface::class);
71
+ $ productVariant ->getCode ()->willReturn ('product_variant_1 ' );
68
72
$ productVariant ->getChannelPricingForChannel ($ channel )->willReturn ($ channelPricing ->reveal ());
69
73
$ productVariant ->getProduct ()->willReturn ($ product ->reveal ());
70
74
@@ -99,6 +103,7 @@ public function it_respects_minimum_price(): void
99
103
$ product ->getPreQualifiedCatalogPromotions ()->willReturn (['promo1 ' ]);
100
104
101
105
$ productVariant = $ this ->prophesize (ProductVariantInterface::class);
106
+ $ productVariant ->getCode ()->willReturn ('product_variant_1 ' );
102
107
$ productVariant ->getChannelPricingForChannel ($ channel )->willReturn ($ channelPricing ->reveal ());
103
108
$ productVariant ->getProduct ()->willReturn ($ product ->reveal ());
104
109
@@ -111,4 +116,61 @@ public function it_respects_minimum_price(): void
111
116
'channel ' => $ channel ,
112
117
]));
113
118
}
119
+
120
+ /**
121
+ * @test
122
+ */
123
+ public function it_works_with_different_channels (): void
124
+ {
125
+ $ channel1 = self ::createChannel ('channel1 ' );
126
+ $ channel2 = self ::createChannel ('channel2 ' );
127
+
128
+ $ channelPricing1 = self ::createChannelPricing (100 );
129
+ $ channelPricing2 = self ::createChannelPricing (200 );
130
+
131
+ $ product = $ this ->prophesize (ProductInterface::class);
132
+ $ product ->hasPreQualifiedCatalogPromotions ()->willReturn (true );
133
+ $ product ->getPreQualifiedCatalogPromotions ()->willReturn (['promo1 ' ]);
134
+
135
+ $ productVariant = $ this ->prophesize (ProductVariantInterface::class);
136
+ $ productVariant ->getCode ()->willReturn ('product_variant_1 ' );
137
+ $ productVariant ->getChannelPricingForChannel ($ channel1 )->willReturn ($ channelPricing1 );
138
+ $ productVariant ->getChannelPricingForChannel ($ channel2 )->willReturn ($ channelPricing2 );
139
+ $ productVariant ->getProduct ()->willReturn ($ product ->reveal ());
140
+
141
+ $ runtimePromotionsApplicator = $ this ->prophesize (RuntimePromotionsApplicatorInterface::class);
142
+ $ runtimePromotionsApplicator ->apply ($ product ->reveal (), 100 , 100 )->willReturn (80 );
143
+ $ runtimePromotionsApplicator ->apply ($ product ->reveal (), 200 , 200 )->willReturn (160 );
144
+
145
+ $ calculator = new ProductVariantPricesCalculator ($ runtimePromotionsApplicator ->reveal ());
146
+
147
+ $ this ->assertSame (80 , $ calculator ->calculate ($ productVariant ->reveal (), [
148
+ 'channel ' => $ channel1 ,
149
+ ]));
150
+
151
+ $ this ->assertSame (160 , $ calculator ->calculate ($ productVariant ->reveal (), [
152
+ 'channel ' => $ channel2 ,
153
+ ]));
154
+ }
155
+
156
+ private static function createChannel (string $ code ): ChannelInterface
157
+ {
158
+ $ channel = new Channel ();
159
+ $ channel ->setCode ($ code );
160
+
161
+ return $ channel ;
162
+ }
163
+
164
+ private static function createChannelPricing (int $ price , int $ originalPrice = null , int $ minimumPrice = null ): ChannelPricingInterface
165
+ {
166
+ $ channelPricing = new ChannelPricing ();
167
+ $ channelPricing ->setPrice ($ price );
168
+ $ channelPricing ->setOriginalPrice ($ originalPrice );
169
+
170
+ if (method_exists ($ channelPricing , 'setMinimumPrice ' )) {
171
+ $ channelPricing ->setMinimumPrice ($ minimumPrice );
172
+ }
173
+
174
+ return $ channelPricing ;
175
+ }
114
176
}
0 commit comments