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, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\tests\unit; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\action\Action; |
14
|
|
|
use hiqdev\php\billing\charge\Charge; |
15
|
|
|
use hiqdev\php\billing\price\SinglePrice; |
16
|
|
|
use hiqdev\php\billing\target\Target; |
17
|
|
|
use hiqdev\php\billing\type\Type; |
18
|
|
|
use hiqdev\php\units\Quantity; |
19
|
|
|
use Money\Money; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Andrii Vasyliev <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ActionTest extends \PHPUnit\Framework\TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var SinglePrice |
28
|
|
|
*/ |
29
|
|
|
protected $price; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Action |
33
|
|
|
*/ |
34
|
|
|
protected $action; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Money |
38
|
|
|
*/ |
39
|
|
|
protected $money; |
40
|
|
|
|
41
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->type = new Type('server_traf'); |
44
|
|
|
$this->target = new Target('server', 2); |
45
|
|
|
$this->prepaid = Quantity::gigabyte(10); |
46
|
|
|
$this->money = Money::USD(15); |
47
|
|
|
$this->price = new SinglePrice(5, $this->type, $this->target, null, $this->prepaid, $this->money); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function createAction($quantity) |
51
|
|
|
{ |
52
|
|
|
return new Action(null, $this->type, $this->target, $quantity); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function tearDown() |
56
|
|
|
{ |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testCalculateCharge() |
60
|
|
|
{ |
61
|
|
|
$action = $this->createAction($this->prepaid->multiply(2)); |
62
|
|
|
$charge = $action->calculateCharge($this->price); |
63
|
|
|
$this->assertInstanceOf(Charge::class, $charge); |
64
|
|
|
$this->assertSame($action, $charge->getAction()); |
65
|
|
|
$this->assertSame($this->target, $charge->getTarget()); |
66
|
|
|
$this->assertSame($this->type, $charge->getPrice()->getType()); |
67
|
|
|
$this->assertEquals($this->prepaid, $charge->getUsage()); |
68
|
|
|
$this->assertEquals($this->money->multiply($this->prepaid->getQuantity()), $charge->getSum()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testCalculateChargeNull() |
72
|
|
|
{ |
73
|
|
|
$action = $this->createAction($this->prepaid); |
74
|
|
|
$charge = $action->calculateCharge($this->price); |
75
|
|
|
$this->assertNull($charge); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
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.