Files
unsupervised-scheduler/tests/Unit/Payment/PaymentDueMailerTest.php
thatguygriffandClaude Opus 4.8 e8e66eef3c
CI / Tests (PHP 8.1) (pull_request) Successful in 47s
CI / Tests (PHP 8.2) (pull_request) Successful in 47s
CI / PHPStan (pull_request) Successful in 3m12s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m42s
CI / Build Plugin Zip (pull_request) Skipped
CI / No Debug Code (pull_request) Successful in 3s
CI / Coding Standards (pull_request) Successful in 2m52s
Credit students for cancelled paid lessons
Cancelling a lesson that was already paid for now credits the student
that money instead of leaving it as a manual refund, and the daily
scheduled-billing scan applies any available credit against their due
charges before emailing the notice.

- New us_credits ledger + us_payments.credit_applied column (Payment::netDue).
- PaymentService::creditForCancelledLesson issues a per-lesson share of the
  covering payment's total; wired into all three cancel paths (student
  self-cancel, instructor status update, admin student-detail cancel).
- PaymentService::applyCredits draws credit down FIFO across a run's charges,
  marking a fully-covered charge paid-by-credit; the notice shows the credit
  applied and reduced total, and the admin queue shows net due.
- Student detail page shows a student's credit balance and history.

Ships as part of the unreleased 1.2.0 (same release as scheduled billing).

Tests: composer test (585), composer lint, composer cs all pass.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-24 15:32:20 -03:00

133 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Payment;
use Brain\Monkey\Functions;
use Mockery;
use Unsupervised\Schedular\Payment\PaymentDueMailer;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class PaymentDueMailerTest extends TestCase
{
private function student(string $email): \WP_User
{
$student = Mockery::mock(\WP_User::class);
$student->user_email = $email;
return $student;
}
public function testReturnsFalseWithoutRecipient(): void
{
$items = [[ 'label' => 'x', 'amount' => 10.0, 'currency' => 'CAD', 'due_date' => '2026-07-14', 'etransfer_email' => null ]];
self::assertFalse((new PaymentDueMailer())->send($this->student(''), $items));
}
public function testReturnsFalseWithNoItems(): void
{
self::assertFalse((new PaymentDueMailer())->send($this->student('[email protected]'), []));
}
public function testConsolidatesItemsWithGrandTotal(): void
{
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
Mockery::type('string'),
Mockery::on(static function (string $body): bool {
return str_contains($body, 'Piano')
&& str_contains($body, 'Jul 15, 2026')
&& str_contains($body, 'Guitar')
&& str_contains($body, 'Jul 22, 2026')
&& str_contains($body, '35.00')
&& str_contains($body, '40.00')
// 35 + 40 grand total
&& str_contains($body, '75.00');
})
)
->andReturn(true);
$items = [
[ 'label' => 'Piano', 'amount' => 35.0, 'currency' => 'CAD', 'due_date' => '2026-07-15', 'etransfer_email' => null ],
[ 'label' => 'Guitar', 'amount' => 40.0, 'currency' => 'CAD', 'due_date' => '2026-07-22', 'etransfer_email' => null ],
];
self::assertTrue((new PaymentDueMailer())->send($this->student('[email protected]'), $items));
}
public function testIncludesReferenceWhenProvided(): void
{
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
Mockery::type('string'),
Mockery::on(static fn (string $body): bool => str_contains($body, 'REF12345'))
)
->andReturn(true);
$items = [[ 'label' => 'Piano', 'amount' => 35.0, 'currency' => 'CAD', 'due_date' => '2026-07-15', 'etransfer_email' => null ]];
self::assertTrue((new PaymentDueMailer())->send($this->student('[email protected]'), $items, 'REF12345'));
}
public function testCreditReducesTheTotalDue(): void
{
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
Mockery::type('string'),
Mockery::on(static function (string $body): bool {
// Line shows the full 35.00; credit line shows -20.00; total due 15.00.
return str_contains($body, '35.00')
&& str_contains($body, '-CAD 20.00')
&& str_contains($body, 'Total due: CAD 15.00');
})
)
->andReturn(true);
$items = [[ 'label' => 'Piano', 'amount' => 35.0, 'currency' => 'CAD', 'due_date' => '2026-07-15', 'etransfer_email' => '[email protected]' ]];
self::assertTrue((new PaymentDueMailer())->send($this->student('[email protected]'), $items, 'REF1', 20.0));
}
public function testCreditCoveringEverythingLeavesZeroDueAndNoEtransferLine(): void
{
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
Mockery::type('string'),
Mockery::on(static function (string $body): bool {
return str_contains($body, 'Total due: CAD 0.00')
&& ! str_contains($body, '[email protected]');
})
)
->andReturn(true);
$items = [[ 'label' => 'Piano', 'amount' => 35.0, 'currency' => 'CAD', 'due_date' => '2026-07-15', 'etransfer_email' => '[email protected]' ]];
self::assertTrue((new PaymentDueMailer())->send($this->student('[email protected]'), $items, '', 35.0));
}
public function testIncludesEtransferDestination(): void
{
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
Mockery::type('string'),
Mockery::on(static fn (string $body): bool => str_contains($body, '[email protected]'))
)
->andReturn(true);
$items = [[ 'label' => 'Piano', 'amount' => 35.0, 'currency' => 'CAD', 'due_date' => '2026-07-15', 'etransfer_email' => '[email protected]' ]];
self::assertTrue((new PaymentDueMailer())->send($this->student('[email protected]'), $items));
}
}