using JamesKovacs.IoCArticle.DependencyInjection.Domain;
using JamesKovacs.IoCArticle.DependencyInjection.Repositories;
using JamesKovacs.IoCArticle.DependencyInjection.Services;
using MbUnit.Framework;
using Rhino.Mocks;

namespace JamesKovacs.IoCArticle.DependencyInjection.Tests.Unit.Services {
    [TestFixture]
    public class InvoiceServiceTests {
        private MockRepository mockery;

        [SetUp]
        public void SetUp() {
            mockery = new MockRepository();
        }

        [Test]
        public void CanSubmitNewInvoice() {
            Invoice invoice = new Invoice();
            ValidationResults validationResults = new ValidationResults();
            IAuthorizationService authorizationService = mockery.CreateMock<IAuthorizationService>();
            IInvoiceValidator invoiceValidator = mockery.CreateMock<IInvoiceValidator>();
            IInvoiceRepository invoiceRepository = mockery.CreateMock<IInvoiceRepository>();

            using(mockery.Record()) {
                Expect.Call(authorizationService.IsActionAllowed(invoice, InvoiceAction.Submit)).Return(true);
                Expect.Call(invoiceValidator.Validate(invoice)).Return(validationResults);
                invoiceRepository.Save(invoice);
            }

            using(mockery.Playback()) {
                IInvoiceService service = new InvoiceService(authorizationService, invoiceValidator, invoiceRepository);
                service.Submit(invoice);
            }
        }
    }
}