Skip to content

Commit

Permalink
Add tests for CurrencyFieldtype
Browse files Browse the repository at this point in the history
  • Loading branch information
esdoefom committed Jul 6, 2023
1 parent 3c1d864 commit a649a79
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Augmentables/AugmentedCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function symbol(): string
*/
public function append(): bool
{
return str_starts_with($this->fmt->getPattern(), '¤');
return str_ends_with($this->fmt->getPattern(), '¤');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Fieldtypes/CurrencyFieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function preload(): array

return [
'symbol' => $fmt->getSymbol(NumberFormatter::CURRENCY_SYMBOL),
'append' => ends_with($fmt->getPattern(), '¤'),
'append' => str_ends_with($fmt->getPattern(), '¤'),
'group_separator' => $fmt->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL),
'radix_point' => $fmt->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL),
'digits' => $fmt->getAttribute(NumberFormatter::FRACTION_DIGITS),
Expand Down
101 changes: 90 additions & 11 deletions tests/Feature/CurrencyFieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,38 @@
namespace Feature;

use Doefom\CurrencyFieldtype\Fieldtypes\CurrencyFieldtype;
use Doefom\CurrencyFieldtype\Models\Currency;
use Statamic\Facades\Site;
use Statamic\Fields\Field;
use Statamic\Statamic;
use Tests\TestCase;

class CurrencyFieldtypeTest extends \Orchestra\Testbench\TestCase
class CurrencyFieldtypeTest extends TestCase
{

protected CurrencyFieldtype $currencyFieldtype;
protected Currency $augmented;

protected function getPackageProviders($app)
{
return [
'Doefom\CurrencyFieldtype\ServiceProvider',
\Statamic\Providers\StatamicServiceProvider::class,
];
}

/**
* Set up a field of currency fieldtype with the following configuration:
* handle: price
* iso: USD
*
* For NumberFormatter use the locale en_US.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();

Site::setCurrent('en_US');

$value = 1234.56;

// --------------------------------------------------------
// SET UP FIELDTYPE
// --------------------------------------------------------

$field = new Field('price', [
'iso' => 'USD',
'type' => 'currency',
Expand All @@ -35,12 +45,19 @@ protected function setUp(): void
'visibility' => 'visible',
'hide_display' => false,
]);
$field->setValue(1234.56);
$field->setValue($value);

$currencyFieldtype = new CurrencyFieldtype();
$currencyFieldtype->setField($field);

$this->currencyFieldtype = $currencyFieldtype;

// --------------------------------------------------------
// SET UP AUGMENTED INSTANCE
// --------------------------------------------------------

$this->augmented = $this->currencyFieldtype->augment($value);

}

public function test_pre_process()
Expand All @@ -49,4 +66,66 @@ public function test_pre_process()
$this->assertEquals('1,234.56', $result);
}

public function test_process()
{
$result = $this->currencyFieldtype->process('1,234.56');
$this->assertEquals(1234.56, $result);
}

public function test_pre_process_index()
{
$result = $this->currencyFieldtype->preProcessIndex(1234.56);
$this->assertEquals('$1,234.56', $result);
}

public function test_augmented_value()
{
$this->assertEquals(1234.56, $this->augmented->value);
}

public function test_augmented_formatted()
{
$this->assertEquals('$1,234.56', $this->augmented->formatted);
}

public function test_augmented_formatted_no_symbol()
{
$this->assertEquals('1,234.56', $this->augmented->formattedNoSymbol);
}

public function test_augmented_iso()
{
$this->assertEquals('USD', $this->augmented->iso);
}

public function test_augmented_numeric_code()
{
$this->assertEquals('840', $this->augmented->numericCode);
}

public function test_augmented_symbol()
{
$this->assertEquals('$', $this->augmented->symbol);
}

public function test_augmented_append()
{
$this->assertFalse($this->augmented->append);
}

public function test_augmented_group_separator()
{
$this->assertEquals(',', $this->augmented->groupSeparator);
}

public function test_augmented_radix_point()
{
$this->assertEquals('.', $this->augmented->radixPoint);
}

public function test_augmented_digits()
{
$this->assertEquals(2, $this->augmented->digits);
}

}
17 changes: 17 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use \PHPUnit\Framework\TestCase as BaseTestCase;
use Statamic\Providers\StatamicServiceProvider;

class TestCase extends BaseTestCase
{

protected function getPackageProviders($app)
{
return [
'Doefom\CurrencyFieldtype\ServiceProvider',
StatamicServiceProvider::class,
];
}

}

0 comments on commit a649a79

Please sign in to comment.