Completed
Push — master ( 9a2061...9f070b )
by Andrii
06:46
created

LeasingTest::testTill()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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 DateTimeImmutable;
14
use hiqdev\php\billing\charge\modifiers\addons\MonthPeriod;
15
use hiqdev\php\billing\charge\modifiers\addons\YearPeriod;
16
use hiqdev\php\billing\charge\modifiers\Leasing;
17
use hiqdev\php\billing\tests\unit\action\ActionTest;
18
19
/**
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class LeasingTest extends ActionTest
23
{
24
    protected $reason = 'test reason string';
25
26
    protected function buildLeasing($term)
27
    {
28
        $month = (new DateTimeImmutable())->modify('first day of this month midnight');
29
30
        return (new Leasing())->since($month)->lasts($term);
31
    }
32
33 View Code Duplication
    public function testCreateMonth()
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...
34
    {
35
        $leasing = $this->buildLeasing('12 months');
36
        $period = $leasing->getTerm();
37
        $this->assertInstanceOf(MonthPeriod::class, $period);
38
        $this->assertSame(12, $period->getValue());
39
    }
40
41
    public function testTill()
42
    {
43
        $this->expectException(\Exception::class);
44
        $this->buildLeasing('month')->till('08.2018');
45
    }
46
47
    public function testReason()
48
    {
49
        $leasing = $this->buildLeasing('12 months');
50
        $this->assertSame($leasing, $leasing->reason($this->reason));
51
        $this->assertSame($this->reason, $leasing->getReason()->getValue());
52
    }
53
54 View Code Duplication
    public function testCreateYear()
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...
55
    {
56
        $leasing = $this->buildLeasing('1 year');
57
        $period = $leasing->getTerm();
58
        $this->assertInstanceOf(YearPeriod::class, $period);
59
        $this->assertSame(1, $period->getValue());
60
    }
61
62
    public function testModifyCharge()
63
    {
64
        $leasing = $this->buildLeasing('6 months');
65
        $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...
66
        $charge = $action->calculateCharge($this->price);
67
        $charges = $leasing->modifyCharge($charge, $action);
0 ignored issues
show
Bug introduced by
It seems like $charge defined by $action->calculateCharge($this->price) on line 66 can be null; however, hiqdev\php\billing\charg...Leasing::modifyCharge() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
68
        $this->assertInternalType('array', $charges);
69
        $this->assertSame(1, count($charges));
70
        $this->assertEquals($charge, $charges[0]);
71
    }
72
}
73