[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phortune/currency/__tests__/ -> PhortuneCurrencyTestCase.php (source)

   1  <?php
   2  
   3  final class PhortuneCurrencyTestCase extends PhabricatorTestCase {
   4  
   5    public function testCurrencyFormatForDisplay() {
   6      $map = array(
   7        '0' => '$0.00 USD',
   8        '.01' => '$0.01 USD',
   9        '1.00' => '$1.00 USD',
  10        '-1.23' => '$-1.23 USD',
  11        '50000.00' => '$50000.00 USD',
  12      );
  13  
  14      foreach ($map as $input => $expect) {
  15        $this->assertEqual(
  16          $expect,
  17          PhortuneCurrency::newFromString($input, 'USD')->formatForDisplay(),
  18          "newFromString({$input})->formatForDisplay()");
  19      }
  20    }
  21  
  22    public function testCurrencyFormatBareValue() {
  23  
  24      // NOTE: The PayPal API depends on the behavior of the bare value format!
  25  
  26      $map = array(
  27        '0' => '0.00',
  28        '.01' => '0.01',
  29        '1.00' => '1.00',
  30        '-1.23' => '-1.23',
  31        '50000.00' => '50000.00',
  32      );
  33  
  34      foreach ($map as $input => $expect) {
  35        $this->assertEqual(
  36          $expect,
  37          PhortuneCurrency::newFromString($input, 'USD')->formatBareValue(),
  38          "newFromString({$input})->formatBareValue()");
  39      }
  40    }
  41  
  42    public function testCurrencyFromString() {
  43  
  44      $map = array(
  45        '1.00' => 100,
  46        '1.00 USD' => 100,
  47        '$1.00' => 100,
  48        '$1.00 USD' => 100,
  49        '-$1.00 USD' => -100,
  50        '$-1.00 USD' => -100,
  51        '1' => 100,
  52        '.99' => 99,
  53        '$.99' => 99,
  54        '-$.99' => -99,
  55        '$-.99' => -99,
  56        '$.99 USD' => 99,
  57      );
  58  
  59      foreach ($map as $input => $expect) {
  60        $this->assertEqual(
  61          $expect,
  62          PhortuneCurrency::newFromString($input, 'USD')->getValue(),
  63          "newFromString({$input})->getValue()");
  64      }
  65    }
  66  
  67    public function testInvalidCurrencyFromString() {
  68      $map = array(
  69        '--1',
  70        '$$1',
  71        '1 JPY',
  72        'buck fiddy',
  73        '1.2.3',
  74        '1 dollar',
  75      );
  76  
  77      foreach ($map as $input) {
  78        $caught = null;
  79        try {
  80          PhortuneCurrency::newFromString($input, 'USD');
  81        } catch (Exception $ex) {
  82          $caught = $ex;
  83        }
  84        $this->assertTrue($caught instanceof Exception, "{$input}");
  85      }
  86    }
  87  
  88    public function testCurrencyRanges() {
  89      $value = PhortuneCurrency::newFromString('3.00 USD');
  90  
  91      $value->assertInRange('2.00 USD', '4.00 USD');
  92      $value->assertInRange('2.00 USD', null);
  93      $value->assertInRange(null, '4.00 USD');
  94      $value->assertInRange(null, null);
  95  
  96      $caught = null;
  97      try {
  98        $value->assertInRange('4.00 USD', null);
  99      } catch (Exception $ex) {
 100        $caught = $ex;
 101      }
 102      $this->assertTrue($caught instanceof Exception);
 103  
 104      $caught = null;
 105      try {
 106        $value->assertInRange(null, '2.00 USD');
 107      } catch (Exception $ex) {
 108        $caught = $ex;
 109      }
 110      $this->assertTrue($caught instanceof Exception);
 111  
 112      $caught = null;
 113      try {
 114        // Minimum and maximum are reversed here.
 115        $value->assertInRange('4.00 USD', '2.00 USD');
 116      } catch (Exception $ex) {
 117        $caught = $ex;
 118      }
 119      $this->assertTrue($caught instanceof Exception);
 120  
 121      $credit = PhortuneCurrency::newFromString('-3.00 USD');
 122      $credit->assertInRange('-4.00 USD', '-2.00 USD');
 123      $credit->assertInRange('-4.00 USD', null);
 124      $credit->assertInRange(null, '-2.00 USD');
 125      $credit->assertInRange(null, null);
 126  
 127      $caught = null;
 128      try {
 129        $credit->assertInRange('-2.00 USD', null);
 130      } catch (Exception $ex) {
 131        $caught = $ex;
 132      }
 133      $this->assertTrue($caught instanceof Exception);
 134  
 135      $caught = null;
 136      try {
 137        $credit->assertInRange(null, '-4.00 USD');
 138      } catch (Exception $ex) {
 139        $caught = $ex;
 140      }
 141      $this->assertTrue($caught instanceof Exception);
 142    }
 143  
 144    public function testAddCurrency() {
 145      $cases = array(
 146        array('0.00 USD', '0.00 USD', '$0.00 USD'),
 147        array('1.00 USD', '1.00 USD', '$2.00 USD'),
 148        array('1.23 USD', '9.77 USD', '$11.00 USD'),
 149      );
 150  
 151      foreach ($cases as $case) {
 152        list($l, $r, $expect) = $case;
 153  
 154        $l = PhortuneCurrency::newFromString($l);
 155        $r = PhortuneCurrency::newFromString($r);
 156        $sum = $l->add($r);
 157  
 158        $this->assertEqual($expect, $sum->formatForDisplay());
 159      }
 160    }
 161  
 162  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1