M3u8GeneratorTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 6
c 3
b 2
f 0
lcom 1
cbo 1
dl 0
loc 71
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGenerateWithNoMediaSegments() 0 8 1
A testGenerateWithMediaSegments() 0 22 1
A prophesizeMediaSegmentManager() 0 8 1
A prophesizeChannel() 0 6 1
A prophesizeMediaSegment() 0 12 1
A prophesizeProgram() 0 7 1
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\M3u8Generator;
15
use Prophecy\Argument;
16
17
class M3u8GeneratorTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testGenerateWithNoMediaSegments()
20
    {
21
        $targetDuration = 10;
22
        $msManager = $this->prophesizeMediaSegmentManager(new \DateTime(), $targetDuration, array());
23
24
        $m3u8Generator = new M3u8Generator($msManager->reveal(), array('target_duration' => $targetDuration));
25
        $m3u8Generator->generate($this->prophesizeChannel()->reveal());
26
    }
27
28
    public function testGenerateWithMediaSegments()
29
    {
30
        $startsAt = new \DateTime();
31
32
        $targetDuration = rand(5, 15);
33
        $programSequence = rand(1, 9999);
34
        $version = rand(2, 3);
35
        $segmentEndsAt = clone $startsAt;
36
        $segment = $this->prophesizeMediaSegment($segmentEndsAt, $programSequence, true);
37
38
        $msManager = $this->prophesizeMediaSegmentManager($startsAt, $targetDuration, array($segment->reveal()));
39
40
        $m3u8Generator = new M3u8Generator($msManager->reveal(), array('target_duration' => $targetDuration, 'version' => $version));
41
        $m3u8 = $m3u8Generator->generate($this->prophesizeChannel()->reveal(), $startsAt);
42
43
        $this->assertEquals($m3u8->getTargetDuration(), $targetDuration);
44
        $this->assertEquals($m3u8->getVersion(), $version);
45
        // only when `discontinuity` tag is removed (which the first segment is not discontinuity segment), the media sequence should +1
46
        $this->assertEquals($m3u8->getDiscontinuitySequence(), --$programSequence);
47
        // even if first media segment will end at once, the age should be one second
48
        $this->assertEquals($m3u8->getAge(), 1);
49
    }
50
51
    private function prophesizeMediaSegmentManager(\DateTime $startsAt, $targetDuration, array $segments)
52
    {
53
        $msManager = $this->prophesize('Chrisyue\Mala\Manager\MediaSegmentManagerInterface');
54
        $msManager->findPlaying(Argument::type('Chrisyue\Mala\Model\ChannelInterface'), $startsAt, $targetDuration * 3)
55
            ->shouldBeCalledTimes(1)->willReturn($segments);
56
57
        return $msManager;
58
    }
59
60
    private function prophesizeChannel()
61
    {
62
        $channel = $this->prophesize('Chrisyue\Mala\Model\ChannelInterface');
63
64
        return $channel;
65
    }
66
67
    private function prophesizeMediaSegment(\DateTime $endsAt, $programSequence, $isDiscontinuity = false)
68
    {
69
        $mediaSegment = $this->prophesize('Chrisyue\Mala\Model\ScheduledMediaSegment');
70
        $mediaSegment->getEndsAt()->shouldBeCalledTimes(1)->willReturn($endsAt);
71
72
        $program = $this->prophesizeProgram($programSequence);
73
        $mediaSegment->getProgram()->shouldBeCalledTimes(1)->willReturn($program->reveal());
74
75
        $mediaSegment->isDiscontinuity()->shouldBeCalledTimes(1)->willReturn($isDiscontinuity);
76
77
        return $mediaSegment;
78
    }
79
80
    private function prophesizeProgram($sequence)
81
    {
82
        $program = $this->prophesize('Chrisyue\Mala\Model\ProgramInterface');
83
        $program->getSequence()->willReturn($sequence);
84
85
        return $program;
86
    }
87
}
88