Skip to content

Commit a649a79

Browse files
committed
Add tests for CurrencyFieldtype
1 parent 3c1d864 commit a649a79

File tree

4 files changed

+109
-13
lines changed

4 files changed

+109
-13
lines changed

src/Augmentables/AugmentedCurrency.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function symbol(): string
123123
*/
124124
public function append(): bool
125125
{
126-
return str_starts_with($this->fmt->getPattern(), '¤');
126+
return str_ends_with($this->fmt->getPattern(), '¤');
127127
}
128128

129129
/**

src/Fieldtypes/CurrencyFieldtype.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function preload(): array
109109

110110
return [
111111
'symbol' => $fmt->getSymbol(NumberFormatter::CURRENCY_SYMBOL),
112-
'append' => ends_with($fmt->getPattern(), '¤'),
112+
'append' => str_ends_with($fmt->getPattern(), '¤'),
113113
'group_separator' => $fmt->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL),
114114
'radix_point' => $fmt->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL),
115115
'digits' => $fmt->getAttribute(NumberFormatter::FRACTION_DIGITS),

tests/Feature/CurrencyFieldtypeTest.php

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,38 @@
33
namespace Feature;
44

55
use Doefom\CurrencyFieldtype\Fieldtypes\CurrencyFieldtype;
6+
use Doefom\CurrencyFieldtype\Models\Currency;
67
use Statamic\Facades\Site;
78
use Statamic\Fields\Field;
8-
use Statamic\Statamic;
9+
use Tests\TestCase;
910

10-
class CurrencyFieldtypeTest extends \Orchestra\Testbench\TestCase
11+
class CurrencyFieldtypeTest extends TestCase
1112
{
1213

1314
protected CurrencyFieldtype $currencyFieldtype;
15+
protected Currency $augmented;
1416

15-
protected function getPackageProviders($app)
16-
{
17-
return [
18-
'Doefom\CurrencyFieldtype\ServiceProvider',
19-
\Statamic\Providers\StatamicServiceProvider::class,
20-
];
21-
}
22-
17+
/**
18+
* Set up a field of currency fieldtype with the following configuration:
19+
* handle: price
20+
* iso: USD
21+
*
22+
* For NumberFormatter use the locale en_US.
23+
*
24+
* @return void
25+
*/
2326
protected function setUp(): void
2427
{
2528
parent::setUp();
29+
2630
Site::setCurrent('en_US');
2731

32+
$value = 1234.56;
33+
34+
// --------------------------------------------------------
35+
// SET UP FIELDTYPE
36+
// --------------------------------------------------------
37+
2838
$field = new Field('price', [
2939
'iso' => 'USD',
3040
'type' => 'currency',
@@ -35,12 +45,19 @@ protected function setUp(): void
3545
'visibility' => 'visible',
3646
'hide_display' => false,
3747
]);
38-
$field->setValue(1234.56);
48+
$field->setValue($value);
3949

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

4353
$this->currencyFieldtype = $currencyFieldtype;
54+
55+
// --------------------------------------------------------
56+
// SET UP AUGMENTED INSTANCE
57+
// --------------------------------------------------------
58+
59+
$this->augmented = $this->currencyFieldtype->augment($value);
60+
4461
}
4562

4663
public function test_pre_process()
@@ -49,4 +66,66 @@ public function test_pre_process()
4966
$this->assertEquals('1,234.56', $result);
5067
}
5168

69+
public function test_process()
70+
{
71+
$result = $this->currencyFieldtype->process('1,234.56');
72+
$this->assertEquals(1234.56, $result);
73+
}
74+
75+
public function test_pre_process_index()
76+
{
77+
$result = $this->currencyFieldtype->preProcessIndex(1234.56);
78+
$this->assertEquals('$1,234.56', $result);
79+
}
80+
81+
public function test_augmented_value()
82+
{
83+
$this->assertEquals(1234.56, $this->augmented->value);
84+
}
85+
86+
public function test_augmented_formatted()
87+
{
88+
$this->assertEquals('$1,234.56', $this->augmented->formatted);
89+
}
90+
91+
public function test_augmented_formatted_no_symbol()
92+
{
93+
$this->assertEquals('1,234.56', $this->augmented->formattedNoSymbol);
94+
}
95+
96+
public function test_augmented_iso()
97+
{
98+
$this->assertEquals('USD', $this->augmented->iso);
99+
}
100+
101+
public function test_augmented_numeric_code()
102+
{
103+
$this->assertEquals('840', $this->augmented->numericCode);
104+
}
105+
106+
public function test_augmented_symbol()
107+
{
108+
$this->assertEquals('$', $this->augmented->symbol);
109+
}
110+
111+
public function test_augmented_append()
112+
{
113+
$this->assertFalse($this->augmented->append);
114+
}
115+
116+
public function test_augmented_group_separator()
117+
{
118+
$this->assertEquals(',', $this->augmented->groupSeparator);
119+
}
120+
121+
public function test_augmented_radix_point()
122+
{
123+
$this->assertEquals('.', $this->augmented->radixPoint);
124+
}
125+
126+
public function test_augmented_digits()
127+
{
128+
$this->assertEquals(2, $this->augmented->digits);
129+
}
130+
52131
}

tests/TestCase.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use \PHPUnit\Framework\TestCase as BaseTestCase;
4+
use Statamic\Providers\StatamicServiceProvider;
5+
6+
class TestCase extends BaseTestCase
7+
{
8+
9+
protected function getPackageProviders($app)
10+
{
11+
return [
12+
'Doefom\CurrencyFieldtype\ServiceProvider',
13+
StatamicServiceProvider::class,
14+
];
15+
}
16+
17+
}

0 commit comments

Comments
 (0)