Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/pod.git

Adds tests for money

AuthorDave Jarvis <email>
Date2026-01-25 12:48:55 GMT-0800
Commit21042721595ff01146506bcf64b945a137147be4
Parent07fd594
Order.php
private $statusCode = 0;
- private function escape( $value ) {
- return htmlspecialchars( $value ?? '', ENT_QUOTES, 'UTF-8' );
- }
-
public function process( array $response, int $statusCode ) {
$this->statusCode = $statusCode;
</form>
<?php
+ }
+
+ private function escape( $value ) {
+ return htmlspecialchars( $value ?? '', ENT_QUOTES, 'UTF-8' );
}
}
Publisher.php
return $response
- ? ( json_decode( $response, true )[ 'access_token' ] ?? null )
+ ? json_decode( $response, true )[ 'access_token' ] ?? null
: null;
}
}
-
tests/Main.php
require_once 'TestAddress.php';
require_once 'TestExchangeRate.php';
+require_once 'TestMoney.php';
+/**
+ * Runs the entire test suite.
+ *
+ * Usage: php Main.php
+ *
+ * Return: 0 means no tests failed; 1 means at least one test failed.
+ */
class Main {
- /**
- * Executes all registered test classes.
- */
public function run() {
$suite = new TestSuite();
- $tests = [ new AddressTest(), new ExchangeRateTest() ];
+ $tests = [ new AddressTest(), new ExchangeRateTest(), new MoneyTest() ];
+ $failed = false;
foreach( $tests as $test ) {
- $test->run( fn( $methods ) => $suite->execute( $methods, $test ) );
+ $failed = $failed || $test->run(
+ fn( $methods ) => $suite->execute( $methods, $test )
+ );
}
+
+ return $failed ? 1 : 0;
}
}
$main = new Main();
-$main->run();
+exit( $main->run() );
+
tests/TestMoney.php
+<?php
+require_once '../Money.php';
+require_once '../Currency.php';
+
+class MoneyTest {
+ /**
+ * Executes all test methods via callback.
+ *
+ * @param callable $callback Function to execute test methods
+ */
+ public function run( $callback ) {
+ $callback( [
+ 'test_Constructor_NegativeAmount_CreatesInstance',
+ 'test_Constructor_ZeroAmount_CreatesInstance',
+ 'test_Constructor_LargeAmount_CreatesInstance',
+ 'test_Constructor_ManyDecimals_CreatesInstance',
+ 'test_IsCurrency_MatchingCurrency_ReturnsTrue',
+ 'test_IsCurrency_DifferentCurrency_ReturnsFalse',
+ 'test_IsAmount_WithinTolerance_ReturnsTrue',
+ 'test_IsAmount_OutsideTolerance_ReturnsFalse'
+ ] );
+ }
+
+ public function test_Constructor_NegativeAmount_CreatesInstance() {
+ $money = new Money( -250.75, Currency::EUR );
+ return $money->isAmount( -250.75 ) && $money->isCurrency( Currency::EUR );
+ }
+
+ public function test_Constructor_ZeroAmount_CreatesInstance() {
+ $money = new Money( 0.0, Currency::GBP );
+ return $money->isAmount( 0.0 ) && $money->isCurrency( Currency::GBP );
+ }
+
+ public function test_Constructor_LargeAmount_CreatesInstance() {
+ $money = new Money( 999999999.99, Currency::CAD );
+ return $money->isAmount( 999999999.99 ) && $money->isCurrency( Currency::CAD );
+ }
+
+ public function test_Constructor_ManyDecimals_CreatesInstance() {
+ $money = new Money( 123.456789, Currency::AUD );
+ return $money->isAmount( 123.456789 ) && $money->isCurrency( Currency::AUD );
+ }
+
+ public function test_IsCurrency_MatchingCurrency_ReturnsTrue() {
+ $money = new Money( 50.0, Currency::USD );
+ return $money->isCurrency( Currency::USD );
+ }
+
+ public function test_IsCurrency_DifferentCurrency_ReturnsFalse() {
+ $money = new Money( 50.0, Currency::USD );
+ return !$money->isCurrency( Currency::EUR );
+ }
+
+ public function test_IsAmount_WithinTolerance_ReturnsTrue() {
+ $money = new Money( 100.005, Currency::USD );
+ return $money->isAmount( 100.00 );
+ }
+
+ public function test_IsAmount_OutsideTolerance_ReturnsFalse() {
+ $money = new Money( 100.00, Currency::USD );
+ return !$money->isAmount( 100.02 );
+ }
+}
tests/TestSuite.php
class TestSuite {
public function execute( $tests, $test ) {
+ $failed = true;
+
foreach( $tests as $method ) {
$result = $test->$method();
- echo "{$method}: " . ($result ? "PASS" : "FAIL") . "\n";
+ $failed = $failed && !$result;
+ echo ($result ? "PASS" : "FAIL") . ": {$method}\n";
}
+
+ return $failed ? 1 : 0;
}
}
Delta91 lines added, 13 lines removed, 78-line increase