<?php

namespace SilverStripe\ORM\Tests;

use SilverStripe\Forms\CurrencyField;
use SilverStripe\ORM\FieldType\DBCurrency;
use SilverStripe\Dev\SapphireTest;

class DBCurrencyTest extends SapphireTest
{
    public function testNiceFormatting()
    {
        // Test a bunch of different data values and results in Nice() and Whole()
        $tests = [
            // Test basic operation
            '$50.00' => ['$50.00', '$50'],

            // Test removal of junk text
            'this is -50.29 dollars' => ['($50.29)', '($50)'],
            'this is -50.79 dollars' => ['($50.79)', '($51)'],
            'this is 50.79 dollars' => ['$50.79', '$51'],

            // Test negative numbers
            '-1000' => ['($1,000.00)','($1,000)'],
            '-$2,000' => ['($2,000.00)', '($2,000)'],

            // Test thousands comma
            '5000' => ['$5,000.00', '$5,000'],

            // Test scientific notation
            '5.68434188608E-14' => ['$0.00', '$0'],
            '5.68434188608E7' => ['$56,843,418.86', '$56,843,419'],
            "Sometimes Es are still bad: 51 dollars, even though they\'re used in scientific notation"
                => ['$51.00', '$51'],
            "What about 5.68434188608E7 in the middle of a string" => ['$56,843,418.86', '$56,843,419'],
        ];

        foreach ($tests as $value => $niceValues) {
            $c = new DBCurrency('MyField');
            $c->setValue($value);
            $this->assertEquals($niceValues[0], $c->Nice());
            $this->assertEquals($niceValues[1], $c->Whole());
        }
    }

    public function testScaffoldedField()
    {
        // Test DBCurrency scaffolds a CurrencyField
        $currencyDbField = DBCurrency::create('Currency');
        $scaffoldedField = $currencyDbField->scaffoldFormField();

        $this->assertInstanceOf(CurrencyField::class, $scaffoldedField);
    }
}