flamingo.me/flamingo-commerce/v3@v3.11.0/checkout/domain/placeorder/states/validate_payment_selection_test.go (about) 1 package states_test 2 3 import ( 4 "context" 5 "errors" 6 "net/url" 7 "testing" 8 9 "flamingo.me/flamingo/v3/framework/flamingo" 10 "github.com/stretchr/testify/assert" 11 12 "flamingo.me/flamingo-commerce/v3/cart/domain/cart" 13 "flamingo.me/flamingo-commerce/v3/cart/domain/decorator" 14 "flamingo.me/flamingo-commerce/v3/cart/domain/validation" 15 "flamingo.me/flamingo-commerce/v3/checkout/domain/placeorder/process" 16 "flamingo.me/flamingo-commerce/v3/checkout/domain/placeorder/states" 17 ) 18 19 type paymentSelectionValidator struct { 20 t *testing.T 21 isCalled bool 22 expectedPaymentSelection cart.PaymentSelection 23 returnedError error 24 } 25 26 func (p *paymentSelectionValidator) Validate(_ context.Context, _ *decorator.DecoratedCart, selection cart.PaymentSelection) error { 27 p.isCalled = true 28 assert.Equal(p.t, p.expectedPaymentSelection, selection) 29 30 return p.returnedError 31 } 32 33 func TestValidatePaymentSelection_IsFinal(t *testing.T) { 34 assert.False(t, states.ValidatePaymentSelection{}.IsFinal()) 35 } 36 37 func TestValidatePaymentSelection_Name(t *testing.T) { 38 assert.Equal(t, "ValidatePaymentSelection", states.ValidatePaymentSelection{}.Name()) 39 } 40 41 func TestValidatePaymentSelection_Rollback(t *testing.T) { 42 s := states.ValidatePaymentSelection{} 43 assert.Nil(t, s.Rollback(context.Background(), nil)) 44 } 45 46 func TestValidatePaymentSelection_Run(t *testing.T) { 47 cartWithPaymentSelection := provideCartWithPaymentSelection(t) 48 tests := []struct { 49 name string 50 cart cart.Cart 51 validator validation.PaymentSelectionValidator 52 expectedResult process.RunResult 53 expectedValidatorCalled bool 54 expectedState string 55 }{ 56 { 57 name: "no payment selection", 58 cart: cart.Cart{}, 59 validator: nil, 60 expectedResult: process.RunResult{ 61 Failed: process.PaymentErrorOccurredReason{Error: cart.ErrPaymentSelectionNotSet.Error()}, 62 }, 63 expectedValidatorCalled: false, 64 expectedState: states.New{}.Name(), 65 }, 66 { 67 name: "no validator", 68 cart: cartWithPaymentSelection, 69 validator: nil, 70 expectedResult: process.RunResult{}, 71 expectedValidatorCalled: false, 72 expectedState: states.CreatePayment{}.Name(), 73 }, 74 { 75 name: "call validator", 76 cart: cartWithPaymentSelection, 77 validator: &paymentSelectionValidator{ 78 t: t, 79 expectedPaymentSelection: cartWithPaymentSelection.PaymentSelection, 80 returnedError: nil, 81 }, 82 expectedResult: process.RunResult{}, 83 expectedValidatorCalled: true, 84 expectedState: states.CreatePayment{}.Name(), 85 }, 86 { 87 name: "call validator with error", 88 cart: cartWithPaymentSelection, 89 validator: &paymentSelectionValidator{ 90 t: t, 91 expectedPaymentSelection: cartWithPaymentSelection.PaymentSelection, 92 returnedError: errors.New("validator error"), 93 }, 94 expectedResult: process.RunResult{ 95 Failed: process.PaymentErrorOccurredReason{Error: "validator error"}, 96 }, 97 expectedValidatorCalled: true, 98 expectedState: states.New{}.Name(), 99 }, 100 } 101 for _, tt := range tests { 102 t.Run(tt.name, func(t *testing.T) { 103 factory := provideProcessFactory(t) 104 p, _ := factory.New(&url.URL{}, tt.cart) 105 106 s := states.ValidatePaymentSelection{} 107 s.Inject( 108 func() *decorator.DecoratedCartFactory { 109 result := &decorator.DecoratedCartFactory{} 110 result.Inject( 111 nil, 112 flamingo.NullLogger{}, 113 ) 114 115 return result 116 }(), 117 &struct { 118 Validator validation.PaymentSelectionValidator `inject:",optional"` 119 }{Validator: tt.validator}) 120 121 result := s.Run(context.Background(), p) 122 assert.Equal(t, result, tt.expectedResult) 123 assert.Equal(t, p.Context().CurrentStateName, tt.expectedState) 124 if tt.validator != nil { 125 assert.Equal(t, tt.expectedValidatorCalled, tt.validator.(*paymentSelectionValidator).isCalled) 126 } 127 }) 128 } 129 }