Passed
Branch master (168fd2)
by Dariusz
06:34
created

ShippingTest::testAmount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Plane\Shop\Tests;
4
5
use Money\Money;
6
use Plane\Shop\Shipping;
7
use InvalidArgumentException;
8
9
/**
10
 * Shipping test suite
11
 *
12
 * @author Dariusz Korsak <[email protected]>
13
 * @package Plane\Shop
14
 */
15
class ShippingTest extends \PHPUnit\Framework\TestCase
16
{
17
    use MoneyTrait;
18
    
19
    const CURRENCY = 'USD';
20
    
21
    const SHIPPING_INPUT = [
22
       'id'             => 1,
23
       'name'           => 'National postal services',
24
       'description'    => '',
25
       'cost'           => 3.4
26
    ];
27
28 View Code Duplication
    public function testCreateObject()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        $shipping = new Shipping(self::SHIPPING_INPUT);
31
32
        $this->assertSame(self::SHIPPING_INPUT['id'], $shipping->getId());
33
        $this->assertSame(self::SHIPPING_INPUT['name'], $shipping->getName());
34
        $this->assertSame(self::SHIPPING_INPUT['description'], $shipping->getDescription());
35
36
        $this->assertInstanceOf(Money::class, $shipping->getCost(self::CURRENCY));
37
        $this->assertEquals(3.40, $this->getAmount($shipping->getCost(self::CURRENCY)));
38
    }
39
40
    public function testCreateIncompleteObject()
41
    {
42
        $input =self::SHIPPING_INPUT;
43
        unset($input['cost']);
44
45
        $this->expectException(InvalidArgumentException::class);
46
47
        $shipping = new Shipping($input);
0 ignored issues
show
Unused Code introduced by
$shipping is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
    }
49
50
    public function testAmount()
51
    {
52
        $input = self::SHIPPING_INPUT;
53
54
        $sample = [
55
            '1.11'      => 1.11411,
56
            '5.58'      => 5.5811,
57
            '2.51'      => 2.509,
58
        ];
59
60
        foreach ($sample as $expected => $cost) {
61
            $input['cost'] = $cost;
62
63
            $shipping = new Shipping($input);
64
65
            $this->assertSame($expected, $this->getAmount($shipping->getCost(self::CURRENCY)));
66
        }
67
    }
68
}
69