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() |
|
|
|
|
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); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testCreateObjectWithInvalidFeeType() |
49
|
|
|
{ |
50
|
|
|
$this->expectException(InvalidArgumentException::class); |
51
|
|
|
|
52
|
|
|
$payment = new Payment(self::PAYMENT_INPUT, 'newfeetype'); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function testFixedFee() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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.