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

PaymentTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 43.1 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 25
loc 58
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateObject() 9 9 1
A testCreateIncompleteObject() 0 9 1
A testCreateObjectWithInvalidFeeType() 0 6 1
A testFixedFee() 8 8 1
A testPercentageFee() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Plane\Shop\Tests;
4
5
use Money\Money;
6
use Plane\Shop\Payment;
7
use InvalidArgumentException;
8
9
/**
10
 * Payment test suite
11
 *
12
 * @author Dariusz Korsak <[email protected]>
13
 * @package Plane\Shop
14
 */
15
class PaymentTest extends \PHPUnit\Framework\TestCase
16
{
17
    use MoneyTrait;
18
19
    const CURRENCY = 'USD';
20
21
    const PAYMENT_INPUT = [
22
       'id'             => 1,
23
       'name'           => 'PayPal',
24
       'description'    => 'Payment with Paypal',
25
       'fee'            => 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
        $payment = new Payment(self::PAYMENT_INPUT);
31
32
        $this->assertSame(self::PAYMENT_INPUT['id'], $payment->getId());
33
        $this->assertSame(self::PAYMENT_INPUT['name'], $payment->getName());
34
        $this->assertSame(self::PAYMENT_INPUT['description'], $payment->getDescription());
35
        $this->assertInstanceOf(Money::class, $payment->getFee($this->getMoney(10), self::CURRENCY));
36
    }
37
38
    public function testCreateIncompleteObject()
39
    {
40
        $input = self::PAYMENT_INPUT;
41
        unset($input['fee']);
42
43
        $this->expectException(InvalidArgumentException::class);
44
45
        $payment = new Payment($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...
46
    }
47
48
    public function testCreateObjectWithInvalidFeeType()
49
    {
50
        $this->expectException(InvalidArgumentException::class);
51
52
        $payment = new Payment(self::PAYMENT_INPUT, 'newfeetype');
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...
53
    }
54
55 View Code Duplication
    public function testFixedFee()
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...
56
    {
57
        $payment = new Payment(self::PAYMENT_INPUT, Payment::FEE_FIXED);
58
        
59
        $fee = $payment->getFee($this->getMoney(10), self::CURRENCY);
60
 
61
        $this->assertEquals(4.00, $this->getAmount($fee));
62
    }
63
64 View Code Duplication
    public function testPercentageFee()
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...
65
    {
66
        $payment = new Payment(self::PAYMENT_INPUT, Payment::FEE_PERCENTAGE);
67
68
        $fee = $payment->getFee($this->getMoney(10), self::CURRENCY);
69
70
        $this->assertEquals(0.40, $this->getAmount($fee));
71
    }
72
}
73