flamingo.me/flamingo-commerce/v3@v3.11.0/checkout/application/orderService_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"flamingo.me/flamingo/v3/framework/flamingo"
     9  	"flamingo.me/flamingo/v3/framework/web"
    10  
    11  	"flamingo.me/flamingo-commerce/v3/checkout/application"
    12  	"flamingo.me/flamingo-commerce/v3/payment/interfaces"
    13  )
    14  
    15  func TestOrderService_LastPlacedOrder(t *testing.T) {
    16  	type args struct {
    17  		ctx context.Context
    18  	}
    19  	tests := []struct {
    20  		name    string
    21  		args    args
    22  		want    *application.PlaceOrderInfo
    23  		wantErr bool
    24  	}{
    25  		{
    26  			name: "",
    27  			args: args{
    28  				ctx: contextWithPlaceOrderInfoInSession(application.PlaceOrderInfo{
    29  					PaymentInfos: nil,
    30  					PlacedOrders: nil,
    31  					ContactEmail: "test@test.de",
    32  				}),
    33  			},
    34  			want: &application.PlaceOrderInfo{
    35  				PaymentInfos: nil,
    36  				PlacedOrders: nil,
    37  				ContactEmail: "test@test.de",
    38  			},
    39  			wantErr: false,
    40  		},
    41  	}
    42  
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			fakeGatewayProvider := func() map[string]interfaces.WebCartPaymentGateway {
    46  				return map[string]interfaces.WebCartPaymentGateway{}
    47  			}
    48  
    49  			os := &application.OrderService{}
    50  
    51  			os.Inject(flamingo.NullLogger{}, nil, nil, nil, fakeGatewayProvider, nil)
    52  
    53  			got, err := os.LastPlacedOrder(tt.args.ctx)
    54  			if (err != nil) != tt.wantErr {
    55  				t.Errorf("LastPlacedOrder() error = %v, wantErr %v", err, tt.wantErr)
    56  				return
    57  			}
    58  			if !reflect.DeepEqual(got, tt.want) {
    59  				t.Errorf("LastPlacedOrder() got = %v, want %v", got, tt.want)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestOrderService_ClearLastPlacedOrder(t *testing.T) {
    66  	fakeGatewayProvider := func() map[string]interfaces.WebCartPaymentGateway {
    67  		return map[string]interfaces.WebCartPaymentGateway{}
    68  	}
    69  
    70  	os := &application.OrderService{}
    71  
    72  	os.Inject(flamingo.NullLogger{}, nil, nil, nil, fakeGatewayProvider, nil)
    73  
    74  	want := &application.PlaceOrderInfo{
    75  		PaymentInfos: nil,
    76  		PlacedOrders: nil,
    77  		ContactEmail: "test@test.de",
    78  	}
    79  
    80  	ctx := contextWithPlaceOrderInfoInSession(application.PlaceOrderInfo{
    81  		PaymentInfos: nil,
    82  		PlacedOrders: nil,
    83  		ContactEmail: "test@test.de",
    84  	})
    85  
    86  	got, err := os.LastPlacedOrder(ctx)
    87  	if err != nil {
    88  		t.Errorf("LastPlacedOrder() shouldn't return an error: %v", err)
    89  	}
    90  
    91  	if !reflect.DeepEqual(got, want) {
    92  		t.Errorf("LastPlacedOrder() got = %v, want %v", got, want)
    93  	}
    94  
    95  	os.ClearLastPlacedOrder(ctx)
    96  
    97  	got, err = os.LastPlacedOrder(ctx)
    98  	if err != nil {
    99  		t.Errorf("LastPlacedOrder() shouldn't return an error: %v", err)
   100  	}
   101  
   102  	if got != nil {
   103  		t.Errorf("LastPlacedOrder() shouldn't return an order")
   104  	}
   105  
   106  }
   107  
   108  func contextWithPlaceOrderInfoInSession(info application.PlaceOrderInfo) context.Context {
   109  	session := web.EmptySession()
   110  	session.Store(application.LastPlacedOrderSessionKey, info)
   111  	return web.ContextWithSession(context.Background(), session)
   112  }