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

ProductTest::testSetPriceFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
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 14
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Plane\Shop\Tests;
4
5
use Plane\Shop\Product;
6
use InvalidArgumentException;
7
8
/**
9
 * Product test suite
10
 *
11
 * @author Dariusz Korsak <[email protected]>
12
 * @package Plane\Shop
13
 */
14
class ProductTest extends \PHPUnit\Framework\TestCase
15
{
16
    use MoneyTrait;
17
    
18
    const CURRENCY = 'USD';
19
    
20
    const PRODUCT_INPUT = [
21
        'id'            => 'someID',
22
        'name'          => 'Test product',
23
        'price'         => 10.5665,
24
        'stock'         => 4,
25
        'taxRate'       => 0.22,
26
        'weight'        => 5.1,
27
        'imagePath'     => '/path_to_file/file.jpg',
28
    ];
29
    
30
    const PRODUCT_OUTPUT = [
31
        'id'            => 'someID',
32
        'name'          => 'Test product',
33
        'price'         => '10.57',
34
        'stock'         => 4,
35
        'taxRate'       => 0.22,
36
        'tax'           => '2.33',
37
        'priceWithTax'  => '12.90',
38
        'weight'        => 5.1,
39
        'imagePath'     => '/path_to_file/file.jpg',
40
    ];
41
    
42
    public function testCreateObject()
43
    {
44
        $product = new Product(self::PRODUCT_INPUT);
45
        
46
        $this->assertSame(self::PRODUCT_OUTPUT, [
47
            'id'                => $product->getId(),
48
            'name'              => $product->getName(),
49
            'price'             => $this->getAmount($product->getPrice(self::CURRENCY)),
50
            'stock'             => $product->getStock(),
51
            'taxRate'           => $product->getTaxRate(),
52
            'tax'               => $this->getAmount($product->getTax(self::CURRENCY)),
53
            'priceWithTax'      => $this->getAmount($product->getPriceWithTax(self::CURRENCY)),
54
            'weight'            => $product->getWeight(),
55
            'imagePath'         => $product->getImagePath(),
56
        ]);
57
    }
58
59
    public function testCreateIncompleteObject()
60
    {
61
        $input = self::PRODUCT_INPUT;
62
        unset($input['price']);
63
64
        $this->expectException(InvalidArgumentException::class);
65
66
        $payment = new Product($input);
0 ignored issues
show
Unused Code introduced by
$payment 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...
67
    }
68
    
69
    public function testToArray()
70
    {
71
        $product = new Product(self::PRODUCT_INPUT);
72
73
        $array = $product->toArray(self::CURRENCY);
74
        
75
        $this->assertSame(self::PRODUCT_OUTPUT['id'],           $array['id']);
76
        $this->assertSame(self::PRODUCT_OUTPUT['name'],         $array['name']);
77
        $this->assertSame(self::PRODUCT_OUTPUT['price'],        $this->getAmount($array['price']));
78
        $this->assertSame(self::PRODUCT_OUTPUT['stock'],        $array['stock']);
79
        $this->assertSame(self::PRODUCT_OUTPUT['taxRate'],      $array['taxRate']);
80
        $this->assertSame(self::PRODUCT_OUTPUT['tax'],          $this->getAmount($array['tax']));
81
        $this->assertSame(self::PRODUCT_OUTPUT['priceWithTax'], $this->getAmount($array['priceWithTax']));
82
        $this->assertSame(self::PRODUCT_OUTPUT['weight'],       $array['weight']);
83
        $this->assertSame(self::PRODUCT_OUTPUT['imagePath'],    $array['imagePath']);
84
        
85
    }
86
}
87