Passed
Push — master ( 403b97...3809dd )
by Vinai
02:33
created

testReturnsCurrencyInfoInstanceBasedOnFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
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);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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