public void Execute_03_NoCartLines( CartShippingOptionAmountOffAction action, IRuleValue <string> fulfillmentOptionName, IRuleValue <decimal> amountOff, Cart cart, CommerceContext commerceContext, IRuleExecutionContext context) { /********************************************** * Arrange **********************************************/ cart.Adjustments.Clear(); cart.Lines.Clear(); context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); commerceContext.AddObject(cart); action.FulfillmentOptionName = fulfillmentOptionName; action.AmountOff = amountOff; /********************************************** * Act **********************************************/ Action executeAction = () => action.Execute(context); /********************************************** * Assert **********************************************/ executeAction.Should().NotThrow <Exception>(); cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty(); cart.Adjustments.Should().BeEmpty(); }
public void Execute_12_NoFulfillmentFee( IRuleValue <string> fulfillmentOptionName, IRuleValue <decimal> amountOff, Cart cart, CommerceContext commerceContext, IRuleExecutionContext context, string fulfillmentOptionNameValue, FulfillmentComponent fulfillmentComponent) { /********************************************** * Arrange **********************************************/ cart.Adjustments.Clear(); cart.Lines.ForEach(l => l.Adjustments.Clear()); cart.SetComponent(fulfillmentComponent); context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); commerceContext.AddObject(cart); fulfillmentOptionName.Yield(context).ReturnsForAnyArgs(fulfillmentOptionNameValue); amountOff.Yield(context).ReturnsForAnyArgs(MINIMUM_AMOUNT_OFF); var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>()); var command = Substitute.For <GetFulfillmentMethodsCommand>( Substitute.For <IFindEntityPipeline>(), Substitute.For <IGetCartFulfillmentMethodsPipeline>(), Substitute.For <IGetCartLineFulfillmentMethodsPipeline>(), Substitute.For <IGetFulfillmentMethodsPipeline>(), Substitute.For <IServiceProvider>()); var fulfillmentMethod = new FulfillmentMethod() { FulfillmentType = fulfillmentOptionNameValue }; command.Process(commerceContext).ReturnsForAnyArgs(new List <FulfillmentMethod>() { fulfillmentMethod }.AsEnumerable()); commander.When(x => x.Command <GetFulfillmentMethodsCommand>()).DoNotCallBase(); commander.Command <GetFulfillmentMethodsCommand>().Returns(command); var action = new CartShippingOptionAmountOffAction(commander) { FulfillmentOptionName = fulfillmentOptionName, AmountOff = amountOff }; /********************************************** * Act **********************************************/ Action executeAction = () => action.Execute(context); /********************************************** * Assert **********************************************/ executeAction.Should().NotThrow <Exception>(); command.Received().Process(commerceContext); cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty(); cart.Adjustments.Should().BeEmpty(); }
public void Execute_02_NoCart( CartShippingOptionAmountOffAction action, IRuleValue <string> fulfillmentOptionName, IRuleValue <decimal> amountOff, CommerceContext commerceContext, IRuleExecutionContext context) { /********************************************** * Arrange **********************************************/ context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); action.FulfillmentOptionName = fulfillmentOptionName; action.AmountOff = amountOff; /********************************************** * Act **********************************************/ Action executeAction = () => action.Execute(context); /********************************************** * Assert **********************************************/ executeAction.Should().NotThrow <Exception>(); }
public void Execute_WithProperties( IRuleValue <string> fulfillmentOptionName, IRuleValue <decimal> amountOff, Cart cart, CommerceContext commerceContext, IRuleExecutionContext context, string fulfillmentOptionNameValue, FulfillmentComponent fulfillmentComponent) { /********************************************** * Arrange **********************************************/ const decimal FULFILLMENT_FEE = MINIMUM_AMOUNT_OFF + 10m; cart.Adjustments.Clear(); var awardedAdjustment = new AwardedAdjustment() { Name = "FulfillmentFee" }; awardedAdjustment.Adjustment = new Money(FULFILLMENT_FEE); cart.Adjustments.Add(awardedAdjustment); cart.Lines.ForEach(l => l.Adjustments.Clear()); cart.SetComponent(fulfillmentComponent); context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); commerceContext.AddObject(cart); var globalPricingPolicy = commerceContext.GetPolicy <GlobalPricingPolicy>(); globalPricingPolicy.ShouldRoundPriceCalc = false; fulfillmentOptionName.Yield(context).ReturnsForAnyArgs(fulfillmentOptionNameValue); amountOff.Yield(context).ReturnsForAnyArgs(MINIMUM_AMOUNT_OFF); var propertiesModel = new PropertiesModel(); propertiesModel.Properties.Add("PromotionId", "id"); propertiesModel.Properties.Add("PromotionCartText", "carttext"); propertiesModel.Properties.Add("PromotionText", "text"); commerceContext.AddObject(propertiesModel); var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>()); var command = Substitute.For <GetFulfillmentMethodsCommand>( Substitute.For <IFindEntityPipeline>(), Substitute.For <IGetCartFulfillmentMethodsPipeline>(), Substitute.For <IGetCartLineFulfillmentMethodsPipeline>(), Substitute.For <IGetFulfillmentMethodsPipeline>(), Substitute.For <IServiceProvider>()); var fulfillmentMethod = new FulfillmentMethod() { FulfillmentType = fulfillmentOptionNameValue }; command.Process(commerceContext).ReturnsForAnyArgs(new List <FulfillmentMethod>() { fulfillmentMethod }.AsEnumerable()); commander.When(x => x.Command <GetFulfillmentMethodsCommand>()).DoNotCallBase(); commander.Command <GetFulfillmentMethodsCommand>().Returns(command); var action = new CartShippingOptionAmountOffAction(commander) { FulfillmentOptionName = fulfillmentOptionName, AmountOff = amountOff }; /********************************************** * Act **********************************************/ Action executeAction = () => action.Execute(context); /********************************************** * Assert **********************************************/ executeAction.Should().NotThrow <Exception>(); cart.Lines.SelectMany(l => l.Adjustments).Should().HaveCount(0); cart.Adjustments.Should().NotBeEmpty(); cart.Adjustments.LastOrDefault().Should().NotBeNull(); cart.Adjustments.LastOrDefault().Should().BeOfType <CartLevelAwardedAdjustment>(); cart.Adjustments.LastOrDefault().Name.Should().Be("text"); cart.Adjustments.LastOrDefault().DisplayName.Should().Be("carttext"); cart.Adjustments.LastOrDefault().AdjustmentType.Should().Be(commerceContext.GetPolicy <KnownCartAdjustmentTypesPolicy>().Discount); cart.Adjustments.LastOrDefault().AwardingBlock.Should().Contain(nameof(CartShippingOptionAmountOffAction)); cart.Adjustments.LastOrDefault().IsTaxable.Should().BeFalse(); cart.Adjustments.LastOrDefault().Adjustment.CurrencyCode.Should().Be(commerceContext.CurrentCurrency()); cart.Adjustments.LastOrDefault().Adjustment.Amount.Should().Be(-MINIMUM_AMOUNT_OFF); cart.HasComponent <MessagesComponent>().Should().BeTrue(); }
public void Execute_AmountOffLessThanFulfillmentFee_False( IRuleValue <string> fulfillmentOptionName, IRuleValue <decimal> amountOff, Cart cart, CommerceContext commerceContext, IRuleExecutionContext context, string fulfillmentOptionNameValue, FulfillmentComponent fulfillmentComponent) { /********************************************** * Arrange **********************************************/ const decimal FULFILLMENT_FEE = 10m; const decimal AMOUNT_OFF = FULFILLMENT_FEE + 1m; cart.Adjustments.Clear(); var awardedAdjustment = new AwardedAdjustment() { Name = "FulfillmentFee" }; awardedAdjustment.Adjustment = new Money(FULFILLMENT_FEE); cart.Adjustments.Add(awardedAdjustment); cart.Lines.ForEach(l => l.Adjustments.Clear()); cart.SetComponent(fulfillmentComponent); context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext); commerceContext.AddObject(cart); fulfillmentOptionName.Yield(context).ReturnsForAnyArgs(fulfillmentOptionNameValue); amountOff.Yield(context).ReturnsForAnyArgs(AMOUNT_OFF); var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>()); var command = Substitute.For <GetFulfillmentMethodsCommand>( Substitute.For <IFindEntityPipeline>(), Substitute.For <IGetCartFulfillmentMethodsPipeline>(), Substitute.For <IGetCartLineFulfillmentMethodsPipeline>(), Substitute.For <IGetFulfillmentMethodsPipeline>(), Substitute.For <IServiceProvider>()); var fulfillmentMethod = new FulfillmentMethod() { FulfillmentType = fulfillmentOptionNameValue }; command.Process(commerceContext).ReturnsForAnyArgs(new List <FulfillmentMethod>() { fulfillmentMethod }.AsEnumerable()); commander.When(x => x.Command <GetFulfillmentMethodsCommand>()).DoNotCallBase(); commander.Command <GetFulfillmentMethodsCommand>().Returns(command); var action = new CartShippingOptionAmountOffAction(commander) { FulfillmentOptionName = fulfillmentOptionName, AmountOff = amountOff }; /********************************************** * Act **********************************************/ Action executeAction = () => action.Execute(context); /********************************************** * Assert **********************************************/ executeAction.Should().NotThrow <Exception>(); command.Received().Process(commerceContext); cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty(); cart.Adjustments.Should().HaveCount(2, "The cart adjustments should only contain the fulfillment fee and shipping discount adjustments."); var adjustment = cart.Adjustments.LastOrDefault(); adjustment.Adjustment.Amount.Should().Be(-FULFILLMENT_FEE, "Adjustment amount should match the AmountOff rule."); }