PlaylistSchedulerTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 163
Duplicated Lines 12.27 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 1
dl 20
loc 163
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testScheduleFirstTimeButNoEpg() 13 13 1
B testSchedule() 0 39 1
A prophesizeMediaSegmentManager() 0 13 3
A prophesizeChannel() 0 6 1
A prophesizeEpgManager() 0 20 3
A prophesizeLastScheduledMediaSegment() 0 8 1
A prophesizeNewScheduledMediaSegment() 0 6 1
A prophesizeMediaSegment() 0 8 1
A prophesizeParser() 0 11 2
A prophesizeProgram() 0 8 1
A prophesizeVideo() 0 7 1
A prophesizeM3u8() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\PlaylistScheduler;
15
use Prophecy\Prophecy\ObjectProphecy;
16
17
class PlaylistSchedulerTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @expectedException UnexpectedValueException
21
     */
22 View Code Duplication
    public function testScheduleFirstTimeButNoEpg() // no last media segment
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
23
    {
24
        $startsAt = new \DateTime();
25
        $endsAt = new \DateTime('+30 seconds');
26
        $channel = $this->prophesizeChannel();
27
28
        $parser = $this->prophesizeParser();
29
        $msManager = $this->prophesizeMediaSegmentManager();
30
        $epgManager = $this->prophesizeEpgManager();
31
32
        $scheduler = new PlaylistScheduler($parser->reveal(), $epgManager->reveal(), $msManager->reveal());
33
        $scheduler->schedule($channel->reveal(), $startsAt, $endsAt);
34
    }
35
36
    public function testSchedule()
37
    {
38
        $startsAt = new \DateTime();
39
        $endsAt = new \DateTime('+30 seconds');
40
        $channel = $this->prophesizeChannel();
41
42
        $videoUri = 'http://www.example.com/foo.m3u8';
43
        $video = $this->prophesizeVideo($videoUri);
44
45
        $programStartsAt = new \DateTime('+10 seconds');
46
        $programEndsAt = new \DateTime('+40 seconds');
47
        $program = $this->prophesizeProgram($programStartsAt, $programEndsAt, $video);
48
49
        $epgManager = $this->prophesizeEpgManager($channel, $startsAt, $endsAt, $program);
50
51
        $lastScheduledMsSequence = 10;
52
        $lastScheduledMsEndsAt = new \DateTime('+10 seconds');
53
        $lastScheduledMs = $this->prophesizeLastScheduledMediaSegment($lastScheduledMsEndsAt, $lastScheduledMsSequence);
54
55
        $msDuration = 30;
56
        $msUri = 'http://example.com/foo1.ts';
57
        $mediaSegment = $this->prophesizeMediaSegment($msUri, $msDuration);
58
59
        $m3u8 = $this->prophesizeM3u8($mediaSegment);
60
61
        $parser = $this->prophesizeParser($videoUri, $m3u8);
62
63
        $newScheduledMs = $this->prophesizeNewScheduledMediaSegment();
64
65
        $msManager = $this->prophesizeMediaSegmentManager($channel, $programStartsAt, $lastScheduledMs);
66
        $msManager->create($program->reveal(), $programStartsAt, $programEndsAt, $msUri, $msDuration, $lastScheduledMsSequence + 1, true)
67
            ->shouldBeCalledTimes(1)->willReturn($newScheduledMs->reveal());
68
69
        $msManager->saveDeferred($newScheduledMs->reveal())->shouldBeCalledTimes(1);
70
        $msManager->commit()->shouldBeCalledTimes(1);
71
72
        $scheduler = new PlaylistScheduler($parser->reveal(), $epgManager->reveal(), $msManager->reveal());
73
        $scheduler->schedule($channel->reveal(), $startsAt, $endsAt);
74
    }
75
76
    private function prophesizeMediaSegmentManager(ObjectProphecy $channel = null, $programStartsAt = null, ObjectProphecy $last = null)
77
    {
78
        $msManager = $this->prophesize('Chrisyue\Mala\Manager\MediaSegmentManagerInterface');
79
80
        if (null === $channel) {
81
            return $msManager;
82
        }
83
84
        $msManager->clear($channel->reveal(), $programStartsAt)->shouldBeCalledTimes(1);
85
        $msManager->findLast($channel->reveal())->shouldBeCalledTimes(1)->willReturn(null === $last ? null : $last->reveal());
86
87
        return $msManager;
88
    }
89
90
    private function prophesizeChannel()
91
    {
92
        $channel = $this->prophesize('Chrisyue\Mala\Model\ChannelInterface');
93
94
        return $channel;
95
    }
96
97
    private function prophesizeEpgManager(
98
        ObjectProphecy $channel = null,
99
        \DateTime $startsAt = null,
100
        \DateTime $endsAt = null,
101
        ObjectProphecy $program = null
102
    ) {
103
        $epgManager = $this->prophesize('Chrisyue\Mala\Manager\EpgManagerInterface');
104
        if (null === $channel) {
105
            return $epgManager;
106
        }
107
108
        $findResult = null;
109
        if (null !== $program) {
110
            $findResult = array($program->reveal());
111
        }
112
113
        $epgManager->find($channel->reveal(), $startsAt, $endsAt)->willReturn($findResult);
114
115
        return $epgManager;
116
    }
117
118
    private function prophesizeLastScheduledMediaSegment(\DateTime $endsAt, $sequence)
119
    {
120
        $mediaSegment = $this->prophesize('Chrisyue\Mala\Model\ScheduledMediaSegment');
121
        $mediaSegment->getEndsAt()->shouldBeCalledTimes(1)->willReturn($endsAt);
122
        $mediaSegment->getSequence()->shouldBeCalledTimes(1)->willReturn($sequence);
123
124
        return $mediaSegment;
125
    }
126
127
    private function prophesizeNewScheduledMediaSegment()
128
    {
129
        $mediaSegment = $this->prophesize('Chrisyue\Mala\Model\ScheduledMediaSegment');
130
131
        return $mediaSegment;
132
    }
133
134
    private function prophesizeMediaSegment($uri, $duration)
135
    {
136
        $mediaSegment = $this->prophesize('Chrisyue\Mala\Model\ScheduledMediaSegment');
137
        $mediaSegment->getUri()->shouldBeCalledTimes(1)->willReturn($uri);
138
        $mediaSegment->getDuration()->shouldBeCalledTimes(2)->willReturn($duration);
139
140
        return $mediaSegment;
141
    }
142
143
    private function prophesizeParser($m3u8Uri = null, ObjectProphecy $m3u8 = null)
144
    {
145
        $parser = $this->prophesize('Chrisyue\PhpM3u8\Parser');
146
        if (null === $m3u8Uri) {
147
            return $parser;
148
        }
149
150
        $parser->parseFromUri($m3u8Uri)->shouldBeCalledTimes(1)->willReturn($m3u8->reveal());
151
152
        return $parser;
153
    }
154
155
    private function prophesizeProgram(\DateTime $startsAt, \DateTime $endsAt, ObjectProphecy $video)
156
    {
157
        $program = $this->prophesize('Chrisyue\Mala\Model\ProgramInterface');
158
        $program->getVideo()->shouldBeCalledTimes(1)->willReturn($video->reveal());
159
        $program->getStartsAt()->shouldBeCalled()->willReturn($startsAt);
160
161
        return $program;
162
    }
163
164
    private function prophesizeVideo($uri)
165
    {
166
        $video = $this->prophesize('Chrisyue\Mala\Model\VideoInterface');
167
        $video->getUri()->shouldBeCalledTimes(1)->willReturn($uri);
168
169
        return $video;
170
    }
171
172 View Code Duplication
    private function prophesizeM3u8(ObjectProphecy $mediaSegment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
173
    {
174
        $m3u8 = $this->prophesize('Chrisyue\PhpM3u8\M3u8\M3u8');
175
        $m3u8->getPlaylist()->shouldBeCalledTimes(1)->willReturn(array($mediaSegment->reveal()));
176
177
        return $m3u8;
178
    }
179
}
180