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

CurrencyInfoSourceTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 46
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testReturnsInstanceWithGivenJson() 0 14 1
A testThrowsExceptionIfInputIsNotString() 0 6 1
A testReturnsCurrencyInfoSourceInstanceFromGivenJsonString() 0 7 1
A testReturnsCurrencyInfoInstanceBasedOnFileName() 0 13 1
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