testReturnsCurrencyInfoSourceInstanceFromGivenJsonString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace VinaiKopp\CurrencyInfo\Build;
4
5
use VinaiKopp\CurrencyInfo\StaticAccess\CurrencyInfo;
6
7
/**
8
 * @covers \VinaiKopp\CurrencyInfo\Build\CurrencyInfoSource
9
 */
10
class CurrencyInfoSourceTest extends \PHPUnit_Framework_TestCase
11
{
12
    public function testReturnsInstanceWithGivenJson()
13
    {
14
        $testSource = [
15
            'EUR' => [
16
                CurrencyInfo::SYMBOL          => 'EUR',
17
                CurrencyInfo::SYMBOL_NATIVE   => '€',
18
                CurrencyInfo::FRACTION_DIGITS => 2,
19
                CurrencyInfo::ROUNDING        => 0,
20
                CurrencyInfo::CODE            => 'EUR',
21
            ],
22
        ];
23
        $currencyInfoSource = new CurrencyInfoSource($testSource);
24
        $this->assertSame($testSource, $currencyInfoSource->get());
25
    }
26
27
    public function testThrowsExceptionIfInputIsNotString()
28
    {
29
        $this->expectException(\InvalidArgumentException::class);
30
        $this->expectExceptionMessage('Input has to be a JSON encoded currency info object');
31
        CurrencyInfoSource::fromJson('foo');
32
    }
33
34
    public function testReturnsCurrencyInfoSourceInstanceFromGivenJsonString()
35
    {
36
        $testSource = ['foo' => ['bar' => 'buz']];
37
        $currencyInfoSource = CurrencyInfoSource::fromJson(json_encode($testSource));
38
        $this->assertInstanceOf(CurrencyInfoSource::class, $currencyInfoSource);
39
        $this->assertSame($testSource, $currencyInfoSource->get());
40
    }
41
42
    public function testReturnsCurrencyInfoInstanceBasedOnFileName()
43
    {
44
        $testSource = ['foo' => ['bar' => 'buz']];
45
        $fileName = tempnam(sys_get_temp_dir(), 'test-');
46
        register_shutdown_function(function () use ($fileName) {
47
            @unlink($fileName);
48
        });
49
        file_put_contents($fileName, json_encode($testSource));
50
51
        $currencyInfoSource = CurrencyInfoSource::fromFile($fileName);
52
        $this->assertInstanceOf(CurrencyInfoSource::class, $currencyInfoSource);
53
        $this->assertSame($testSource, $currencyInfoSource->get());
54
    }
55
}
56