Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 80 additions & 10 deletions RefactorThis.Domain.Tests/InvoicePaymentProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace RefactorThis.Domain.Tests
public class InvoicePaymentProcessorTests
{
[Test]
public void ProcessPayment_Should_ThrowException_When_NoInoiceFoundForPaymentReference( )
public void ProcessPayment_Should_ThrowException_When_NoInvoiceFoundForPaymentReference()
{
var repo = new InvoiceRepository( );

Expand All @@ -32,7 +32,7 @@ public void ProcessPayment_Should_ThrowException_When_NoInoiceFoundForPaymentRef
}

[Test]
public void ProcessPayment_Should_ReturnFailureMessage_When_NoPaymentNeeded( )
public void ProcessPayment_Should_ReturnFailureMessage_When_NoPaymentNeeded()
{
var repo = new InvoiceRepository( );

Expand All @@ -55,7 +55,7 @@ public void ProcessPayment_Should_ReturnFailureMessage_When_NoPaymentNeeded( )
}

[Test]
public void ProcessPayment_Should_ReturnFailureMessage_When_InvoiceAlreadyFullyPaid( )
public void ProcessPayment_Should_ReturnFailureMessage_When_InvoiceAlreadyFullyPaid()
{
var repo = new InvoiceRepository( );

Expand Down Expand Up @@ -83,7 +83,7 @@ public void ProcessPayment_Should_ReturnFailureMessage_When_InvoiceAlreadyFullyP
}

[Test]
public void ProcessPayment_Should_ReturnFailureMessage_When_PartialPaymentExistsAndAmountPaidExceedsAmountDue( )
public void ProcessPayment_Should_ReturnFailureMessage_When_PartialPaymentExistsAndAmountPaidExceedsAmountDue()
{
var repo = new InvoiceRepository( );
var invoice = new Invoice( repo )
Expand Down Expand Up @@ -113,7 +113,7 @@ public void ProcessPayment_Should_ReturnFailureMessage_When_PartialPaymentExists
}

[Test]
public void ProcessPayment_Should_ReturnFailureMessage_When_NoPartialPaymentExistsAndAmountPaidExceedsInvoiceAmount( )
public void ProcessPayment_Should_ReturnFailureMessage_When_NoPartialPaymentExistsAndAmountPaidExceedsInvoiceAmount()
{
var repo = new InvoiceRepository( );
var invoice = new Invoice( repo )
Expand All @@ -137,7 +137,7 @@ public void ProcessPayment_Should_ReturnFailureMessage_When_NoPartialPaymentExis
}

[Test]
public void ProcessPayment_Should_ReturnFullyPaidMessage_When_PartialPaymentExistsAndAmountPaidEqualsAmountDue( )
public void ProcessPayment_Should_ReturnFullyPaidMessage_When_PartialPaymentExistsAndAmountPaidEqualsAmountDue()
{
var repo = new InvoiceRepository( );
var invoice = new Invoice( repo )
Expand Down Expand Up @@ -167,7 +167,7 @@ public void ProcessPayment_Should_ReturnFullyPaidMessage_When_PartialPaymentExis
}

[Test]
public void ProcessPayment_Should_ReturnFullyPaidMessage_When_NoPartialPaymentExistsAndAmountPaidEqualsInvoiceAmount( )
public void ProcessPayment_Should_ReturnFullyPaidMessage_When_NoPartialPaymentExistsAndAmountPaidEqualsInvoiceAmount()
{
var repo = new InvoiceRepository( );
var invoice = new Invoice( repo )
Expand All @@ -191,7 +191,7 @@ public void ProcessPayment_Should_ReturnFullyPaidMessage_When_NoPartialPaymentEx
}

[Test]
public void ProcessPayment_Should_ReturnPartiallyPaidMessage_When_PartialPaymentExistsAndAmountPaidIsLessThanAmountDue( )
public void ProcessPayment_Should_ReturnPartiallyPaidMessage_When_PartialPaymentExistsAndAmountPaidIsLessThanAmountDue()
{
var repo = new InvoiceRepository( );
var invoice = new Invoice( repo )
Expand Down Expand Up @@ -221,7 +221,7 @@ public void ProcessPayment_Should_ReturnPartiallyPaidMessage_When_PartialPayment
}

[Test]
public void ProcessPayment_Should_ReturnPartiallyPaidMessage_When_NoPartialPaymentExistsAndAmountPaidIsLessThanInvoiceAmount( )
public void ProcessPayment_Should_ReturnPartiallyPaidMessage_When_NoPartialPaymentExistsAndAmountPaidIsLessThanInvoiceAmount()
{
var repo = new InvoiceRepository( );
var invoice = new Invoice( repo )
Expand All @@ -243,5 +243,75 @@ public void ProcessPayment_Should_ReturnPartiallyPaidMessage_When_NoPartialPayme

Assert.AreEqual( "invoice is now partially paid", result );
}
}

[Test]
public void ProcessPayment_Should_TransitionToFullyPaid_When_MultiplePartialPaymentsAddUpToInvoiceAmount()
{
var repo = new InvoiceRepository();
var invoice = new Invoice(repo)
{
Amount = 10,
AmountPaid = 0,
Payments = new List<Payment>()
};
repo.Add(invoice);

var paymentProcessor = new InvoiceService(repo);

var firstPayment = new Payment { Amount = 4 };
var firstResult = paymentProcessor.ProcessPayment(firstPayment);
Assert.AreEqual("invoice is now partially paid", firstResult);

var secondPayment = new Payment { Amount = 6 };
var secondResult = paymentProcessor.ProcessPayment(secondPayment);
Assert.AreEqual("final partial payment received, invoice is now fully paid", secondResult);
}

[Test]
public void ProcessPayment_Should_AddPaymentToInvoice_When_Successful()
{
var repo = new InvoiceRepository();
var invoice = new Invoice(repo)
{
Amount = 10,
AmountPaid = 0,
Payments = new List<Payment>()
};
repo.Add(invoice);

var paymentProcessor = new InvoiceService(repo);

var payment = new Payment { Amount = 5 };

var result = paymentProcessor.ProcessPayment(payment);

Assert.AreEqual("invoice is now partially paid", result);
Assert.AreEqual(5, invoice.AmountPaid);
Assert.AreEqual(1, invoice.Payments.Count);
Assert.AreEqual(5, invoice.Payments[0].Amount);
}

[Test]
public void ProcessPayment_Should_HandleNullPaymentsList()
{
var repo = new InvoiceRepository();
var invoice = new Invoice(repo)
{
Amount = 10,
AmountPaid = 0,
Payments = null // deliberately null
};
repo.Add(invoice);

var paymentProcessor = new InvoiceService(repo);

var payment = new Payment { Amount = 3 };
var result = paymentProcessor.ProcessPayment(payment);

Assert.AreEqual("invoice is now partially paid", result);
Assert.AreEqual(3, invoice.AmountPaid);
Assert.IsNotNull(invoice.Payments);
Assert.AreEqual(1, invoice.Payments.Count);
}
}
}
Loading