Completed
Push — master ( 7d972a...3baf5c )
by Andrii
02:11
created

FixedDiscountTest::assertAbsolute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\tests\unit\charge\modifiers;
12
13
use hiqdev\php\billing\charge\Charge;
14
use hiqdev\php\billing\charge\modifiers\FixedDiscount;
15
use hiqdev\php\billing\tests\unit\action\ActionTest;
16
use hiqdev\php\units\Quantity;
17
use Money\Money;
18
19
/**
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class FixedDiscountTest extends ActionTest
23
{
24
    protected function setUp()
25
    {
26
        parent::setUp();
27
        $this->value = Money::USD(1000);
0 ignored issues
show
Bug introduced by
The property value does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
        $this->rate = '10';
0 ignored issues
show
Bug introduced by
The property rate does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
    }
30
31
    protected function buildDiscount($value)
32
    {
33
        return new FixedDiscount($value);
34
    }
35
36
    public function testCreateAbsolute()
37
    {
38
        $abs = $this->buildDiscount($this->value);
39
        $this->assertAbsolute($this->value, $abs);
40
        $abs = $this->buildDiscount('10 USD');
41
        $this->assertAbsolute($this->value, $abs);
42
        $abs = $this->buildDiscount('10.00 USD');
43
        $this->assertAbsolute($this->value, $abs);
44
    }
45
46
    public function assertAbsolute($value, $abs)
47
    {
48
        $this->assertEquals($value, $abs->getValue());
49
        $this->assertTrue($abs->isAbsolute());
50
        $this->assertFalse($abs->isRelative());
51
    }
52
53
    public function testCreateRelative()
54
    {
55
        $rel = $this->buildDiscount($this->rate);
56
        $this->assertRelative($this->rate, $rel);
57
        $rel = $this->buildDiscount($this->rate . '%');
58
        $this->assertRelative($this->rate, $rel);
59
    }
60
61
    protected function assertRelative($rate, $rel)
62
    {
63
        $this->assertSame($rate, $rel->getValue());
64
        $this->assertTrue($rel->isRelative());
65
        $this->assertFalse($rel->isAbsolute());
66
    }
67
68
    public function testAbsoluteModifyCharge()
69
    {
70
        $abs = $this->buildDiscount($this->value);
71
        $this->assertCharges($abs, $this->value);
72
    }
73
74
    public function testRelativeModifyCharge()
75
    {
76
        $rel = $this->buildDiscount($this->rate);
77
        $this->assertCharges($rel, $this->value);
78
    }
79
80 View Code Duplication
    public function assertCharges($fd, $sum)
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...
81
    {
82
        $action = $this->createAction($this->prepaid->multiply(2));
0 ignored issues
show
Bug introduced by
The property prepaid does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
83
        $charge = $action->calculateCharge($this->price);
84
        $charges = $fd->modifyCharge($charge, $action);
85
        $this->assertInternalType('array', $charges);
86
        $this->assertSame(2, count($charges));
87
        $this->assertSame($charge, $charges[0]);
88
        $discount = $charges[1];
89
        $this->assertInstanceOf(Charge::class, $discount);
90
        $this->assertEquals(Quantity::items(1), $discount->getUsage());
91
        $this->assertEquals($sum, $discount->getSum());
92
    }
93
}
94