1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Thunder\Currenz\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Thunder\Currenz\Currency; |
7
|
|
|
use Thunder\Currenz\Currenz; |
8
|
|
|
use Thunder\Currenz\Utility\Utility; |
9
|
|
|
|
10
|
|
|
final class CurrenzTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
use TestTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @dataProvider provideCurrencies |
16
|
|
|
*/ |
17
|
|
|
public function testCurrencies(string $code, string $name, array $countries, ?int $digits, string $id) |
18
|
|
|
{ |
19
|
|
|
$this->assertCurrency(Currenz::{$code === 'TRY' ? $code.'_' : $code}(), $code, $name, $countries, $digits, $id); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @dataProvider provideCurrencies |
24
|
|
|
*/ |
25
|
|
|
public function testClasses(string $code, string $name, array $countries, ?int $digits, string $id) |
26
|
|
|
{ |
27
|
|
|
$class = 'Thunder\\Currenz\\Currency\\'.($code === 'TRY' ? $code.'_' : $code); |
28
|
|
|
|
29
|
|
|
$this->assertCurrency(new $class(), $code, $name, $countries, $digits, $id); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider provideCurrencies |
34
|
|
|
*/ |
35
|
|
|
public function testFromCode(string $code, string $name, array $countries, ?int $digits, string $id) |
36
|
|
|
{ |
37
|
|
|
$this->assertCurrency(Currenz::createFromCode($code), $code, $name, $countries, $digits, $id); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function provideCurrencies() |
41
|
|
|
{ |
42
|
|
|
return array_filter(array_map(function(array $item) { |
43
|
|
|
return $item['code'] ? [$item['code'], $item['name'], $item['countries'], $item['units'] === 'N.A.' ? null : (int)$item['units'], $item['id']] : null; |
44
|
|
|
}, Utility::xmlToArray(file_get_contents(__DIR__.'/../data/list_one.xml')))); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testSpecificCurrency() |
48
|
|
|
{ |
49
|
|
|
$this->assertCurrency(new Currency\PLN(), 'PLN', 'Zloty', ['POLAND'], 2, '985'); |
50
|
|
|
$this->assertCurrency(Currenz::PLN(), 'PLN', 'Zloty', ['POLAND'], 2, '985'); |
51
|
|
|
$this->assertCurrency(Currenz::createFromCode('PLN'), 'PLN', 'Zloty', ['POLAND'], 2, '985'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testInvalidCodeException() |
55
|
|
|
{ |
56
|
|
|
$this->expectException(\OutOfRangeException::class); |
57
|
|
|
Currenz::ZZZ(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testInvalidCodeInNamedConstructorException() |
61
|
|
|
{ |
62
|
|
|
$this->expectException(\OutOfRangeException::class); |
63
|
|
|
Currenz::createFromCode('ZZZ'); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|