public void CartUpdatedEventRaised_WhenUserChanged()
        {
            var shoppingCartUpdatedRaised = false;
            var accountService            = new MockAccountService();
            var shoppingCartUpdatedEvent  = new MockShoppingCartUpdatedEvent()
            {
                PublishDelegate = () => shoppingCartUpdatedRaised = true
            };
            var eventAggregator = new MockEventAggregator()
            {
                GetEventDelegate = (a) => shoppingCartUpdatedEvent
            };
            var shoppingCartService = new MockShoppingCartService()
            {
                MergeShoppingCartsAsyncDelegate = (s, s1) => Task.FromResult(false)
            };

            var target = new ShoppingCartRepository(shoppingCartService, accountService, eventAggregator, new MockSessionStateService());

            accountService.RaiseUserChanged(new UserInfo {
                UserName = "******"
            }, null);

            Assert.IsTrue(shoppingCartUpdatedRaised);
        }
        public async Task CartItemUpdatedEventRaised_WhenItemRemoved()
        {
            var shoppingCartItemUpdatedRaised = false;
            var shoppingCartService           = new MockShoppingCartService()
            {
                RemoveShoppingCartItemDelegate = (s, s1) =>
                {
                    Assert.AreEqual("TestShoppingCartItemId", s1);
                    return(Task.FromResult(string.Empty));
                }
            };
            var shoppingCartItemUpdatedEvent = new ShoppingCartItemUpdatedEvent();

            shoppingCartItemUpdatedEvent.Subscribe((a) => shoppingCartItemUpdatedRaised = true);

            var eventAggregator = new MockEventAggregator()
            {
                GetEventDelegate = (a) => shoppingCartItemUpdatedEvent
            };

            var target = new ShoppingCartRepository(shoppingCartService, new MockAccountService(), eventAggregator, new MockSessionStateService());
            await target.RemoveShoppingCartItemAsync("TestShoppingCartItemId");

            Assert.IsTrue(shoppingCartItemUpdatedRaised);
        }
        public async Task Remove_InvalidatesCachedCart()
        {
            var shoppingCartService = new MockShoppingCartService
            {
                RemoveShoppingCartItemDelegate = (s, s1) => Task.FromResult(string.Empty),
                GetShoppingCartAsyncDelegate   = s => Task.FromResult(new ShoppingCart(new Collection <ShoppingCartItem>())
                {
                    Id = "first"
                })
            };
            var eventAggregator = new MockEventAggregator
            {
                GetEventDelegate = type => new ShoppingCartItemUpdatedEvent()
            };
            var target            = new ShoppingCartRepository(shoppingCartService, null, eventAggregator, new MockSessionStateService());
            var firstCartReturned = await target.GetShoppingCartAsync();

            await target.RemoveShoppingCartItemAsync("TestShoppingCartItemId");

            shoppingCartService.GetShoppingCartAsyncDelegate = s => Task.FromResult(new ShoppingCart(new Collection <ShoppingCartItem>())
            {
                Id = "second"
            });
            var secondCartReturned = await target.GetShoppingCartAsync();

            Assert.IsNotNull(firstCartReturned);
            Assert.IsNotNull(secondCartReturned);
            Assert.AreNotSame(firstCartReturned, secondCartReturned);
        }
        public async void CartItemUpdatedEventRaised_WhenProductRemoved()
        {
            var shoppingCartItemUpdatedRaised = false;
            var shoppingCartService           = new MockShoppingCartService();

            shoppingCartService.RemoveProductFromShoppingCartAsyncDelegate =
                (s, s1) => Task.FromResult(string.Empty);
            var shoppingCartItemUpdatedEvent = new ShoppingCartItemUpdatedEvent();

            shoppingCartItemUpdatedEvent.Subscribe((_) =>
            {
                shoppingCartItemUpdatedRaised = true;
            });
            var eventAggregator = new MockEventAggregator();

            eventAggregator.GetEventDelegate = type => shoppingCartItemUpdatedEvent;
            var target = new ShoppingCartRepository(shoppingCartService, new MockAccountService(), eventAggregator, new MockSessionStateService());

            await target.RemoveProductFromShoppingCartAsync("TestProductId");

            Assert.IsTrue(shoppingCartItemUpdatedRaised);
        }
        public async Task GetShoppingCartAsync_CachesCart()
        {
            var shoppingCart        = new ShoppingCart(new Collection <ShoppingCartItem>());
            var shoppingCartService = new MockShoppingCartService()
            {
                GetShoppingCartAsyncDelegate = s => Task.FromResult(shoppingCart)
            };

            var target            = new ShoppingCartRepository(shoppingCartService, null, null, new MockSessionStateService());
            var firstCartReturned = await target.GetShoppingCartAsync();

            shoppingCartService.GetShoppingCartAsyncDelegate = s =>
            {
                Assert.Fail("Should not have called proxy second time.");
                return(Task.FromResult((ShoppingCart)null));
            };

            var secondCartReturned = await target.GetShoppingCartAsync();

            Assert.AreSame(shoppingCart, firstCartReturned);
            Assert.AreSame(shoppingCart, secondCartReturned);
        }
        public void ShoppingCartMerged_WhenAnonymousUserLogsIn()
        {
            bool mergeShoppingCartsCalled  = false;
            bool alertMessageServiceCalled = false;
            var  anonymousCartItems        = new List <ShoppingCartItem>
            {
                new ShoppingCartItem
                {
                    Quantity = 1, Product = new Product {
                        ProductNumber = "123"
                    }
                }
            };
            var testUserCartItems = new List <ShoppingCartItem>
            {
                new ShoppingCartItem
                {
                    Quantity = 2, Product = new Product {
                        ProductNumber = "123"
                    }
                }
            };

            var shoppingCartService = new MockShoppingCartService()
            {
                GetShoppingCartAsyncDelegate = s =>
                {
                    switch (s)
                    {
                    case "AnonymousId":
                        return(Task.FromResult(new ShoppingCart(anonymousCartItems)));

                    default:
                        return(Task.FromResult(new ShoppingCart(testUserCartItems)));
                    }
                },
                MergeShoppingCartsAsyncDelegate = (s, s1) =>
                {
                    mergeShoppingCartsCalled = true;
                    Assert.AreEqual("AnonymousId", s);
                    Assert.AreEqual("TestUserName", s1);
                    return(Task.FromResult(true));
                }
            };
            var accountService           = new MockAccountService();
            var shoppingCartUpdatedEvent = new MockShoppingCartUpdatedEvent
            {
                PublishDelegate = () => { }
            };

            var eventAggregator = new MockEventAggregator()
            {
                GetEventDelegate = (a) => shoppingCartUpdatedEvent
            };
            var sessionStateService = new MockSessionStateService();

            sessionStateService.SessionState[ShoppingCartRepository.ShoppingCartIdKey] = "AnonymousId";

            var target = new ShoppingCartRepository(shoppingCartService, accountService, eventAggregator, sessionStateService);

            accountService.RaiseUserChanged(new UserInfo {
                UserName = "******"
            }, null);

            Assert.IsTrue(mergeShoppingCartsCalled);
        }