1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Mala package. |
5
|
|
|
* |
6
|
|
|
* (c) Chrisyue <http://chrisyue.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tests; |
13
|
|
|
|
14
|
|
|
use Chrisyue\Mala\EpgScheduler; |
15
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
16
|
|
|
|
17
|
|
|
class EpgSchedulerTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
View Code Duplication |
public function testScheduleWithNoLastProgramAndNoVideos() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$startsAt = new \DateTime(); |
22
|
|
|
$endsAt = new \DateTime('+30 seconds'); |
23
|
|
|
|
24
|
|
|
$channel = $this->prophesizeChannel(); |
25
|
|
|
$epgManager = $this->prophesizeEpgManager($channel); |
26
|
|
|
$videoManager = $this->prophesizeVideoManager($channel); |
27
|
|
|
|
28
|
|
|
$epgScheduler = new EpgScheduler($epgManager->reveal(), $videoManager->reveal()); |
29
|
|
|
$epgScheduler->schedule($channel->reveal(), $startsAt, $endsAt); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testScheduleWithNoLastProgram() |
33
|
|
|
{ |
34
|
|
|
$startsAt = new \DateTime(); |
35
|
|
|
$videoDuration = 40; |
36
|
|
|
|
37
|
|
|
$channel = $this->prophesizeChannel(); |
38
|
|
|
$video = $this->prophesizeVideo($videoDuration); |
39
|
|
|
$videos = array($video); |
40
|
|
|
|
41
|
|
|
$epgManager = $this->prophesizeEpgManager($channel, null, true); |
42
|
|
|
$videoManager = $this->prophesizeVideoManager($channel, $videos); |
43
|
|
|
|
44
|
|
|
$epgScheduler = new EpgScheduler($epgManager->reveal(), $videoManager->reveal()); |
45
|
|
|
$endsAt = clone $startsAt; |
46
|
|
|
$endsAt->modify(sprintf('+%d seconds', $videoDuration - 1)); // duration is [startsAt, endsAt] |
47
|
|
|
$this->epgManagerShouldCreateProgram($epgManager, $channel, $video, 1, $startsAt, $endsAt); // the first program sequence should be 1 |
48
|
|
|
|
49
|
|
|
$epgScheduler->schedule($channel->reveal(), $startsAt, $endsAt); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testScheduleWithLastProgramButNoVideos() |
53
|
|
|
{ |
54
|
|
|
$startsAt = new \DateTime(); |
55
|
|
|
$endsAt = new \DateTime('+30 seconds'); |
56
|
|
|
$lastProgramEndsAt = new \DateTime('+2 seconds'); |
57
|
|
|
|
58
|
|
|
$channel = $this->prophesizeChannel(); |
59
|
|
|
$program = $this->prophesizeProgram($lastProgramEndsAt); |
60
|
|
|
|
61
|
|
|
$epgManager = $this->prophesizeEpgManager($channel, $program); |
62
|
|
|
$videoManager = $this->prophesizeVideoManager($channel); |
63
|
|
|
$epgScheduler = new EpgScheduler($epgManager->reveal(), $videoManager->reveal()); |
64
|
|
|
|
65
|
|
|
$epgScheduler->schedule($channel->reveal(), $startsAt, $endsAt); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testScheduleWithLastProgramWithForce() |
69
|
|
|
{ |
70
|
|
|
$startsAt = new \DateTime(); |
71
|
|
|
$endsAt = new \DateTime('+30 seconds'); |
72
|
|
|
$lastProgramEndsAt = new \DateTime('+2 seconds'); |
73
|
|
|
$lastProgramSequence = rand(1, 999); |
74
|
|
|
$videoDuration = 40; |
75
|
|
|
|
76
|
|
|
$channel = $this->prophesizeChannel($lastProgramEndsAt); |
77
|
|
|
$program = $this->prophesizeProgram($lastProgramEndsAt); |
78
|
|
|
$video = $this->prophesizeVideo($videoDuration); |
79
|
|
|
$videos = array($video); |
80
|
|
|
|
81
|
|
|
$epgManager = $this->prophesizeEpgManager($channel, $program); |
82
|
|
|
$epgManager->clear($channel->reveal(), $startsAt)->shouldBeCalledTimes(1); |
83
|
|
|
|
84
|
|
|
$videoManager = $this->prophesizeVideoManager($channel, $videos); |
85
|
|
|
|
86
|
|
|
$program->getSequence()->shouldBeCalledTimes(1)->willReturn($lastProgramSequence); |
87
|
|
|
|
88
|
|
|
$epgScheduler = new EpgScheduler($epgManager->reveal(), $videoManager->reveal()); |
89
|
|
|
$programEndsAt = clone $lastProgramEndsAt; |
90
|
|
|
$programEndsAt->modify(sprintf('+%d seconds', $videoDuration)); // duration is (lastProgramEndsAt, $endsAt] |
91
|
|
|
$programStartsAt = clone $lastProgramEndsAt; |
92
|
|
|
$programStartsAt->modify('+1 second'); |
93
|
|
|
$this->epgManagerShouldCreateProgram($epgManager, $channel, $video, $lastProgramSequence + 1, $programStartsAt, $programEndsAt); |
94
|
|
|
|
95
|
|
|
$epgScheduler->schedule($channel->reveal(), $startsAt, $endsAt, true); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function epgManagerShouldCreateProgram( |
99
|
|
|
ObjectProphecy $epgManager, |
100
|
|
|
ObjectProphecy $channel, |
101
|
|
|
ObjectProphecy $video, |
102
|
|
|
$sequence, $videoStartsAt, $videoEndsAt |
103
|
|
|
) { |
104
|
|
|
$newProgram = $this->prophesize('Chrisyue\Mala\Model\ProgramInterface'); |
105
|
|
|
$epgManager->createProgram($channel->reveal(), $video->reveal(), $sequence, $videoStartsAt, $videoEndsAt) |
106
|
|
|
->willReturn($newProgram->reveal()); |
107
|
|
|
|
108
|
|
|
$epgManager->saveDeferred($newProgram->reveal())->shouldBeCalled(); |
109
|
|
|
$epgManager->commit()->shouldBeCalledTimes(1); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function prophesizeEpgManager(ObjectProphecy $channel, ObjectProphecy $lastProgram = null, $shouldCreateProgram = false) |
113
|
|
|
{ |
114
|
|
|
$epgManager = $this->prophesize('Chrisyue\Mala\Manager\EpgManagerInterface'); |
115
|
|
|
$epgManager->findLastProgram($channel->reveal())->shouldBeCalledTimes(1) |
116
|
|
|
->willReturn(null === $lastProgram ? null : $lastProgram->reveal()); |
117
|
|
|
|
118
|
|
|
return $epgManager; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
private function prophesizeVideoManager(ObjectProphecy $channel, array $videos = array()) |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$videoManager = $this->prophesize('Chrisyue\Mala\Manager\VideoManagerInterface'); |
124
|
|
|
$videoManager->findByChannel($channel->reveal())->shouldBeCalledTimes(1)->willReturn($videos); |
125
|
|
|
|
126
|
|
|
return $videoManager; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function prophesizeChannel() |
130
|
|
|
{ |
131
|
|
|
return $this->prophesize('Chrisyue\Mala\Model\ChannelInterface'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function prophesizeProgram(\DateTime $endsAt) |
135
|
|
|
{ |
136
|
|
|
$program = $this->prophesize('Chrisyue\Mala\Model\ProgramInterface'); |
137
|
|
|
$program->getEndsAt()->shouldBeCalledTimes(1)->willReturn($endsAt); |
138
|
|
|
|
139
|
|
|
return $program; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function prophesizeVideo($duration) |
143
|
|
|
{ |
144
|
|
|
$video = $this->prophesize('Chrisyue\Mala\Model\VideoInterface'); |
145
|
|
|
$video->getDuration()->shouldBeCalledTimes(1)->willReturn($duration); |
146
|
|
|
|
147
|
|
|
return $video; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.