|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ogone\Tests\Subscription; |
|
4
|
|
|
|
|
5
|
|
|
use Ogone\Subscription\SubscriptionPeriod; |
|
6
|
|
|
|
|
7
|
|
|
class SubscriptionPeriodTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** @test */ |
|
11
|
|
|
public function UnitMustBeValid() |
|
12
|
|
|
{ |
|
13
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
|
14
|
|
|
$this->createPeriod('not an actual unit'); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** @test */ |
|
18
|
|
|
public function SettingUnitToWeeklyChecksMoment() |
|
19
|
|
|
{ |
|
20
|
|
|
$period = $this->createPeriod(SubscriptionPeriod::UNIT_DAILY, 12, 8); |
|
21
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
|
22
|
|
|
$period->setUnit(SubscriptionPeriod::UNIT_WEEKLY); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** @test */ |
|
26
|
|
|
public function SettingUnitToMonthlyChecksMoment() |
|
27
|
|
|
{ |
|
28
|
|
|
$period = $this->createPeriod(SubscriptionPeriod::UNIT_DAILY, 12, 29); |
|
29
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
|
30
|
|
|
$period->setUnit(SubscriptionPeriod::UNIT_MONTHLY); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @test |
|
35
|
|
|
* @dataProvider unitProvider |
|
36
|
|
|
*/ |
|
37
|
|
|
public function UnitCanBeSetRight($unit) |
|
38
|
|
|
{ |
|
39
|
|
|
$period = $this->createPeriod($unit); |
|
40
|
|
|
$this->assertEquals($unit, $period->getUnit()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** @test */ |
|
44
|
|
|
public function IntervalMustBeInteger() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
|
47
|
|
|
$this->createPeriod(SubscriptionPeriod::UNIT_DAILY, 'not an int'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** @test */ |
|
51
|
|
|
public function IntervalMustBePositive() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
|
54
|
|
|
$this->createPeriod(SubscriptionPeriod::UNIT_DAILY, -12); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** @test */ |
|
58
|
|
|
public function IntervalMustNotBeTooBig() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
|
61
|
|
|
$this->createPeriod(SubscriptionPeriod::UNIT_DAILY, 150000000000000000); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @test |
|
66
|
|
|
* @dataProvider intProvider |
|
67
|
|
|
*/ |
|
68
|
|
|
public function IntervalCanBeSetRight($interval) |
|
69
|
|
|
{ |
|
70
|
|
|
$period = $this->createPeriod(SubscriptionPeriod::UNIT_DAILY, $interval); |
|
71
|
|
|
$this->assertEquals($interval, $period->getInterval()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** @test */ |
|
75
|
|
|
public function MomentMustBeInt() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
|
78
|
|
|
$this->createPeriod(SubscriptionPeriod::UNIT_DAILY, 12, 'not an int'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** @test */ |
|
82
|
|
|
public function MomentMustBePositive() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
|
85
|
|
|
$this->createPeriod(SubscriptionPeriod::UNIT_DAILY, 12, -12); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** @test */ |
|
89
|
|
|
public function MomentMustNotBeTooBig() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
|
92
|
|
|
$this->createPeriod(SubscriptionPeriod::UNIT_DAILY, 12, 150000000000000000); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @test |
|
97
|
|
|
* @dataProvider badUnitMomentComboProvider |
|
98
|
|
|
*/ |
|
99
|
|
|
public function MomentChecksUnit($unit, $interval) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
|
102
|
|
|
$this->createPeriod($unit, 12, $interval); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @test |
|
107
|
|
|
* @dataProvider intProvider |
|
108
|
|
|
*/ |
|
109
|
|
|
public function MomentCanBeSetRight($moment) |
|
110
|
|
|
{ |
|
111
|
|
|
$period = $this->createPeriod(SubscriptionPeriod::UNIT_DAILY, 12, $moment); |
|
112
|
|
|
$this->assertEquals($moment, $period->getMoment()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
View Code Duplication |
public function unitProvider() |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
return array( |
|
118
|
|
|
array(SubscriptionPeriod::UNIT_DAILY), |
|
119
|
|
|
array(SubscriptionPeriod::UNIT_WEEKLY), |
|
120
|
|
|
array(SubscriptionPeriod::UNIT_MONTHLY) |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function intProvider() |
|
125
|
|
|
{ |
|
126
|
|
|
return array( |
|
127
|
|
|
array(1), |
|
128
|
|
|
array(5), |
|
129
|
|
|
array(32), |
|
130
|
|
|
array(123546) |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
View Code Duplication |
public function badUnitMomentComboProvider() |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
|
|
return array( |
|
137
|
|
|
array(SubscriptionPeriod::UNIT_WEEKLY, 8), |
|
138
|
|
|
array(SubscriptionPeriod::UNIT_MONTHLY, 29) |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
protected function createPeriod($unit = SubscriptionPeriod::UNIT_DAILY, $interval = 12, $moment = 6) |
|
143
|
|
|
{ |
|
144
|
|
|
return new SubscriptionPeriod($unit, $interval, $moment); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
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.