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