Completed
Push — master ( a69b0a...cda368 )
by Tomasz
01:10
created

TestTrait::assertCurrency()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 22
nc 7
nop 6
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 39.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types=1);
3
namespace Thunder\Currenz\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Thunder\Currenz\CurrencyInterface;
7
8
trait TestTrait
9
{
10
    private function assertCurrency(CurrencyInterface $currency, string $code, string $name, array $countries, ?int $digits, string $number)
11
    {
12
        /** @var TestCase $this */
13
        $this->assertSame($number, $currency->getNumber(), 'ID ('.$code.')');
14
        $this->assertSame($code, $currency->getCode(), 'Code');
15
        $this->assertSame($digits, $currency->getDigits(), 'Digits');
16
        $this->assertSame($name, $currency->getName(), 'Name');
17
        $this->assertSame($countries, $currency->getCountries(), 'Country');
18
19
        $this->assertTrue($currency->hasCode($code));
20
        $this->assertTrue($currency->hasCountry($countries[0]));
21
        $this->assertTrue($currency->hasName($name));
22
        $this->assertTrue($currency->hasNumber($number));
23
24
        $this->assertFalse($currency->hasCode('INVALID'));
25
        $this->assertFalse($currency->hasCountry('INVALID'));
26
        $this->assertFalse($currency->hasName('INVALID'));
27
        $this->assertFalse($currency->hasNumber('INVALID'));
28
29
        switch($currency->getDigits()) {
30
            case null: { $this->assertSame(0, $currency->getUnits()); break; }
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $currency->getDigits() of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
31
            case 0: { $this->assertSame(0, $currency->getUnits()); break; }
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $currency->getDigits() of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
32
            case 1: { $this->assertSame(10, $currency->getUnits()); break; }
33
            case 2: { $this->assertSame(100, $currency->getUnits()); break; }
34
            case 3: { $this->assertSame(1000, $currency->getUnits()); break; }
35
            case 4: { $this->assertSame(10000, $currency->getUnits()); break; }
36
            default: { throw new \OutOfRangeException(sprintf('Invalid number of currency digits: %s!', $currency->getDigits())); }
37
        }
38
    }
39
}
40