flamingo.me/flamingo-commerce/v3@v3.11.0/cart/infrastructure/placeorder/logger_test.go (about)

     1  package logger_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
     8  	"flamingo.me/flamingo-commerce/v3/cart/domain/placeorder"
     9  	logger "flamingo.me/flamingo-commerce/v3/cart/infrastructure/placeorder"
    10  	"flamingo.me/flamingo-commerce/v3/price/domain"
    11  	"flamingo.me/flamingo/v3/framework/flamingo"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  type (
    16  	stubLogger struct {
    17  		flamingo.NullLogger
    18  		loggedcart interface{}
    19  	}
    20  )
    21  
    22  func (l *stubLogger) WithField(key flamingo.LogKey, value interface{}) flamingo.Logger {
    23  	if key == "cart" {
    24  		l.loggedcart = value
    25  	}
    26  	return l
    27  }
    28  
    29  func TestPlaceOrderLoggerAdapter_PlaceGuestCart(t *testing.T) {
    30  	stubLogger := &stubLogger{}
    31  	placeOrderAdapter := &logger.PlaceOrderLoggerAdapter{}
    32  	placeOrderAdapter.Inject(stubLogger, &struct {
    33  		UseFlamingoLog bool   `inject:"config:commerce.cart.placeOrderLogger.useFlamingoLog,optional"`
    34  		LogAsFile      bool   `inject:"config:commerce.cart.placeOrderLogger.logAsFile,optional"`
    35  		LogDirectory   string `inject:"config:commerce.cart.placeOrderLogger.logDirectory,optional"`
    36  	}{
    37  		UseFlamingoLog: true,
    38  		LogAsFile:      false,
    39  		LogDirectory:   "",
    40  	})
    41  	exampleCart := &cart.Cart{
    42  		ID:       "testid",
    43  		EntityID: "",
    44  		BillingAddress: &cart.Address{
    45  			Vat:                    "",
    46  			Firstname:              "Adrianna",
    47  			Lastname:               "Mustermann",
    48  			MiddleName:             "",
    49  			Title:                  "",
    50  			Salutation:             "Mr",
    51  			Street:                 "Musterstraße",
    52  			StreetNr:               "7",
    53  			AdditionalAddressLines: nil,
    54  			Company:                "AOE",
    55  			City:                   "Wiesbaden",
    56  			PostCode:               "65200",
    57  			State:                  "",
    58  			RegionCode:             "",
    59  			Country:                "Germany",
    60  			CountryCode:            "",
    61  			Telephone:              "",
    62  			Email:                  "adrianna@mail.de",
    63  		},
    64  		Purchaser: &cart.Person{
    65  			Address: &cart.Address{
    66  				Vat:                    "",
    67  				Firstname:              "Max",
    68  				Lastname:               "Mustermann",
    69  				MiddleName:             "",
    70  				Title:                  "",
    71  				Salutation:             "Mr",
    72  				Street:                 "Musterstraße",
    73  				StreetNr:               "7",
    74  				AdditionalAddressLines: nil,
    75  				Company:                "AOE",
    76  				City:                   "Wiesbaden",
    77  				PostCode:               "65200",
    78  				State:                  "",
    79  				RegionCode:             "",
    80  				Country:                "Germany",
    81  				CountryCode:            "",
    82  				Telephone:              "",
    83  				Email:                  "mail@mail.de",
    84  			},
    85  			PersonalDetails:      cart.PersonalDetails{},
    86  			ExistingCustomerData: nil,
    87  		},
    88  		Deliveries: []cart.Delivery{
    89  			{
    90  				DeliveryInfo: cart.DeliveryInfo{
    91  					Code:     "delivery",
    92  					Workflow: "",
    93  					Method:   "",
    94  					Carrier:  "",
    95  					DeliveryLocation: cart.DeliveryLocation{
    96  						Type: "",
    97  						Address: &cart.Address{
    98  							Vat:                    "",
    99  							Firstname:              "Opa",
   100  							Lastname:               "Mustermann",
   101  							MiddleName:             "",
   102  							Title:                  "",
   103  							Salutation:             "Mr",
   104  							Street:                 "Musterstraße",
   105  							StreetNr:               "7",
   106  							AdditionalAddressLines: nil,
   107  							Company:                "AOE",
   108  							City:                   "Wiesbaden",
   109  							PostCode:               "65200",
   110  							State:                  "",
   111  							RegionCode:             "",
   112  							Country:                "Germany",
   113  							CountryCode:            "",
   114  							Telephone:              "",
   115  							Email:                  "mail@mail.de",
   116  						},
   117  						UseBillingAddress: false,
   118  						Code:              "",
   119  					},
   120  					AdditionalData:          nil,
   121  					AdditionalDeliveryInfos: nil,
   122  				},
   123  				Cartitems: []cart.Item{
   124  					{
   125  						ID:                     "1",
   126  						ExternalReference:      "",
   127  						MarketplaceCode:        "",
   128  						VariantMarketPlaceCode: "",
   129  						ProductName:            "ProductName",
   130  						SourceID:               "",
   131  						Qty:                    1,
   132  						AdditionalData:         nil,
   133  						SinglePriceGross:       domain.NewFromInt(1190, 100, "EUR"),
   134  						SinglePriceNet:         domain.NewFromInt(1000, 100, "EUR"),
   135  						RowPriceGross:          domain.NewFromInt(2380, 100, "EUR"),
   136  						RowPriceNet:            domain.NewFromInt(2000, 100, "EUR"),
   137  						RowTaxes:               nil,
   138  						AppliedDiscounts:       nil,
   139  					},
   140  				},
   141  				ShippingItem: cart.ShippingItem{
   142  					Title:            "Express",
   143  					PriceNet:         domain.NewFromInt(1000, 100, "EUR"),
   144  					TaxAmount:        domain.NewFromInt(190, 100, "EUR"),
   145  					AppliedDiscounts: nil,
   146  				},
   147  			},
   148  			{
   149  				DeliveryInfo: cart.DeliveryInfo{
   150  					Code:     "pickup",
   151  					Workflow: "pickup",
   152  					Method:   "",
   153  					Carrier:  "",
   154  					DeliveryLocation: cart.DeliveryLocation{
   155  						Type:              "pickup",
   156  						UseBillingAddress: false,
   157  						Code:              "location1",
   158  					},
   159  					AdditionalData:          nil,
   160  					AdditionalDeliveryInfos: nil,
   161  				},
   162  				Cartitems: []cart.Item{
   163  					{
   164  						ID:                     "2",
   165  						ExternalReference:      "",
   166  						MarketplaceCode:        "",
   167  						VariantMarketPlaceCode: "",
   168  						ProductName:            "ProductName 2",
   169  						SourceID:               "",
   170  						Qty:                    1,
   171  						AdditionalData:         nil,
   172  						SinglePriceGross:       domain.NewFromInt(1190, 100, "EUR"),
   173  						SinglePriceNet:         domain.NewFromInt(1000, 100, "EUR"),
   174  						RowPriceGross:          domain.NewFromInt(2380, 100, "EUR"),
   175  						RowPriceNet:            domain.NewFromInt(2000, 100, "EUR"),
   176  						RowTaxes:               nil,
   177  						AppliedDiscounts:       nil,
   178  					},
   179  					{
   180  						ID:                     "3",
   181  						ExternalReference:      "",
   182  						MarketplaceCode:        "",
   183  						VariantMarketPlaceCode: "",
   184  						ProductName:            "ProductName 3",
   185  						SourceID:               "",
   186  						Qty:                    1,
   187  						AdditionalData:         nil,
   188  						SinglePriceGross:       domain.NewFromInt(1190, 100, "EUR"),
   189  						SinglePriceNet:         domain.NewFromInt(1000, 100, "EUR"),
   190  						RowPriceGross:          domain.NewFromInt(2380, 100, "EUR"),
   191  						RowPriceNet:            domain.NewFromInt(2000, 100, "EUR"),
   192  						RowTaxes:               nil,
   193  						AppliedDiscounts:       nil,
   194  					},
   195  				},
   196  			},
   197  		},
   198  		AdditionalData:             cart.AdditionalData{},
   199  		PaymentSelection:           nil,
   200  		BelongsToAuthenticatedUser: false,
   201  		AuthenticatedUserID:        "",
   202  		AppliedCouponCodes:         nil,
   203  		DefaultCurrency:            "",
   204  		Totalitems:                 nil,
   205  		AppliedGiftCards:           nil,
   206  	}
   207  
   208  	payment := &placeorder.Payment{
   209  		Gateway: "test",
   210  		Transactions: []placeorder.Transaction{
   211  			placeorder.Transaction{
   212  				Method:            "testmethod",
   213  				Status:            placeorder.PaymentStatusOpen,
   214  				ValuedAmountPayed: exampleCart.GrandTotal,
   215  				AmountPayed:       exampleCart.GrandTotal,
   216  				TransactionID:     "t1",
   217  			},
   218  		},
   219  		RawTransactionData: nil,
   220  		PaymentID:          "p1",
   221  	}
   222  	poi, err := placeOrderAdapter.PlaceGuestCart(context.Background(), exampleCart, payment)
   223  	assert.NoError(t, err)
   224  	assert.Equal(t, poi.GetOrderNumberForDeliveryCode("delivery"), "testid")
   225  	assert.NotNil(t, stubLogger.loggedcart)
   226  	assert.IsType(t, stubLogger.loggedcart, &cart.Cart{})
   227  }