Challenge 2 — Solution Task: Using the Chapter 3 InsufficientFundsException-style BankAccount class from Intermediate Chapter 3, write a PHPUnit test that uses expectException() to confirm withdrawing more than the balance throws InsufficientFundsException, and a second test confirming a valid withdrawal correctly reduces the balance. ---- src/BankAccount.php (from Intermediate Chapter 3) ---- balance += $amount; } public function withdraw(float $amount) { if ($amount > $this->balance) { throw new InsufficientFundsException("Cannot withdraw £$amount, balance is only £{$this->balance}"); } $this->balance -= $amount; } public function getBalance(): float { return $this->balance; } } ---- tests/BankAccountTest.php ---- deposit(50); $this->expectException(InsufficientFundsException::class); $account->withdraw(100); } public function testValidWithdrawalReducesBalance() { $account = new BankAccount(); $account->deposit(100); $account->withdraw(30); $this->assertEquals(70, $account->getBalance()); } } Expected test run output: PHPUnit 10.x .. 2 / 2 (100%) OK (2 tests, 2 assertions) Notes: - The first test deliberately deposits 50 before attempting to withdraw 100, ensuring the InsufficientFundsException genuinely comes from the amount exceeding the balance, not from some unrelated problem with an empty account. - expectException() must be called BEFORE the line that's actually expected to throw - PHPUnit records the expectation first, then verifies it was met once the throwing line executes. - The second test verifies the withdrawal actually changed state correctly (100 - 30 = 70), using getBalance() as the only way to inspect the account's own private $balance property from outside the class - exactly the encapsulation pattern from Intermediate Chapter 2, now being exercised by an automated test rather than a manual echo.