|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Terox\SubscriptionBundle\Tests\Strategy; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Terox\SubscriptionBundle\Exception\PermanentSubscriptionException; |
|
7
|
|
|
use Terox\SubscriptionBundle\Model\ProductInterface; |
|
8
|
|
|
use Terox\SubscriptionBundle\Model\SubscriptionInterface; |
|
9
|
|
|
use Terox\SubscriptionBundle\Strategy\ProductDefaultStrategy; |
|
10
|
|
|
use Terox\SubscriptionBundle\Strategy\SubscriptionEndLastStrategy; |
|
11
|
|
|
use Terox\SubscriptionBundle\Tests\AbstractTestCaseBase; |
|
12
|
|
|
use Terox\SubscriptionBundle\Tests\Mock\SubscriptionMock; |
|
13
|
|
|
|
|
14
|
|
|
class EndLastSubscriptionStrategyTest extends AbstractTestCaseBase |
|
15
|
|
|
{ |
|
16
|
|
|
public function setUp() |
|
17
|
|
|
{ |
|
18
|
|
|
parent::setUp(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testExpiredSubscription() |
|
22
|
|
|
{ |
|
23
|
|
|
$currentDate = new \DateTimeImmutable(); |
|
24
|
|
|
$period = 864000; // 10 days |
|
25
|
|
|
|
|
26
|
|
|
// Product |
|
27
|
|
|
$product = \Mockery::mock(ProductInterface::class); |
|
28
|
|
|
$product->shouldReceive('isAutoRenewal')->andReturn(false); |
|
29
|
|
|
$product->shouldReceive('getDuration')->andReturn($period); |
|
30
|
|
|
|
|
31
|
|
|
// Active subscriptions |
|
32
|
|
|
$subscription1 = \Mockery::mock(SubscriptionInterface::class); |
|
33
|
|
|
$subscription1->shouldReceive('getEndDate')->andReturn(new \DateTimeImmutable('2017-04-15 16:05:10')); |
|
34
|
|
|
|
|
35
|
|
|
// Strategy |
|
36
|
|
|
$strategy = new SubscriptionEndLastStrategy(SubscriptionMock::class, $this->defaultProductStrategy); |
|
37
|
|
|
$subscription = $strategy->createSubscription($product, [ $subscription1 ]); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertEquals($currentDate->getTimestamp(), $subscription->getStartDate()->getTimestamp()); |
|
40
|
|
|
$this->assertEquals( |
|
41
|
|
|
$currentDate->modify('+'.$period.' seconds')->getTimestamp(), |
|
42
|
|
|
$subscription->getEndDate()->getTimestamp() |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testNonExpiredSubscription() |
|
47
|
|
|
{ |
|
48
|
|
|
$currentDate = new \DateTimeImmutable(); |
|
49
|
|
|
$period = 864000; // 10 days |
|
50
|
|
|
|
|
51
|
|
|
// Product |
|
52
|
|
|
$product = \Mockery::mock(ProductInterface::class); |
|
53
|
|
|
$product->shouldReceive('isAutoRenewal')->andReturn(false); |
|
54
|
|
|
$product->shouldReceive('getDuration')->andReturn($period); |
|
55
|
|
|
|
|
56
|
|
|
// Active subscriptions |
|
57
|
|
|
$subscription1 = \Mockery::mock(SubscriptionInterface::class); |
|
58
|
|
|
$subscription1->shouldReceive('getEndDate')->andReturn($currentDate->modify('+5 days')); |
|
59
|
|
|
|
|
60
|
|
|
// Strategy |
|
61
|
|
|
$strategy = new SubscriptionEndLastStrategy(SubscriptionMock::class, $this->defaultProductStrategy); |
|
62
|
|
|
$subscription = $strategy->createSubscription($product, [ $subscription1 ]); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertEquals($currentDate->modify('+5 days')->getTimestamp(), $subscription->getStartDate()->getTimestamp()); |
|
65
|
|
|
$this->assertEquals( |
|
66
|
|
|
$currentDate->modify('+15 days')->getTimestamp(), |
|
67
|
|
|
$subscription->getEndDate()->getTimestamp() |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testCreatePermanentSubscriptionOnNoActiveSubscriptions() |
|
72
|
|
|
{ |
|
73
|
|
|
// Product |
|
74
|
|
|
$product = \Mockery::mock(ProductInterface::class); |
|
75
|
|
|
$product->shouldReceive('isAutoRenewal')->andReturn(false); |
|
76
|
|
|
$product->shouldReceive('getDuration')->andReturn(null); |
|
77
|
|
|
|
|
78
|
|
|
$strategy = new SubscriptionEndLastStrategy(SubscriptionMock::class, $this->defaultProductStrategy); |
|
79
|
|
|
$subscription = $strategy->createSubscription($product); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertEquals(null, $subscription->getEndDate()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testFailOnMoreThanOnePermanentSubscriptionByProduct() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->expectException(PermanentSubscriptionException::class); |
|
87
|
|
|
|
|
88
|
|
|
// Active subscriptions |
|
89
|
|
|
$subscription1 = \Mockery::mock(SubscriptionInterface::class); |
|
90
|
|
|
$subscription1->shouldReceive('getName')->andReturn('Subscription one with product A'); |
|
91
|
|
|
$subscription1->shouldReceive('getProduct')->andReturn($this->product); |
|
92
|
|
|
$subscription1->shouldReceive('getEndDate')->andReturn(null); |
|
93
|
|
|
|
|
94
|
|
|
$subscription2 = \Mockery::mock(SubscriptionInterface::class); |
|
95
|
|
|
$subscription2->shouldReceive('getName')->andReturn('Subscription two with product A'); |
|
96
|
|
|
$subscription2->shouldReceive('getProduct')->andReturn($this->product); |
|
97
|
|
|
$subscription2->shouldReceive('getEndDate')->andReturn(null); |
|
98
|
|
|
|
|
99
|
|
|
$strategy = new SubscriptionEndLastStrategy(SubscriptionMock::class, $this->defaultProductStrategy); |
|
100
|
|
|
$strategy->createSubscription($this->product, [ $subscription1, $subscription2 ]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testReturnSameSubscriptionInstanceOnPermanentSubscription() |
|
104
|
|
|
{ |
|
105
|
|
|
// Product X |
|
106
|
|
|
$productX = \Mockery::mock(ProductInterface::class); |
|
107
|
|
|
|
|
108
|
|
|
// Active subscriptions |
|
109
|
|
|
$subscription1 = \Mockery::mock(SubscriptionInterface::class); |
|
110
|
|
|
$subscription1->shouldReceive('getName')->andReturn('Subscription two with product X'); |
|
111
|
|
|
$subscription1->shouldReceive('getProduct')->andReturn($productX); |
|
112
|
|
|
$subscription1->shouldReceive('getEndDate')->andReturn(null); |
|
113
|
|
|
|
|
114
|
|
|
$strategy = new SubscriptionEndLastStrategy(SubscriptionMock::class, $this->defaultProductStrategy); |
|
115
|
|
|
$subscription = $strategy->createSubscription($this->product, [ $subscription1 ]); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertEquals($subscription1, $subscription); |
|
118
|
|
|
} |
|
119
|
|
|
} |