Completed
Push — develop ( af7229...125d61 )
by Chris
8s
created

M3u8GeneratorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGenerateWithNoMediaSegments() 0 8 1
A testGenerateWithMediaSegments() 0 16 1
A prophesizeMediaSegmentManager() 0 10 1
A prophesizeChannel() 0 6 1
A prophesizeMediaSegment() 0 10 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
        $targetDuration = rand(5, 15);
31
        $programSequence = rand(1, 9999);
32
        $version = rand(2, 3);
33
        $segment = $this->prophesizeMediaSegment(new \DateTime(), $programSequence);
34
        $msManager = $this->prophesizeMediaSegmentManager(new \DateTime(), $targetDuration, array($segment->reveal()));
35
36
        $m3u8Generator = new M3u8Generator($msManager->reveal(), array('target_duration' => $targetDuration, 'version' => $version));
37
        $m3u8 = $m3u8Generator->generate($this->prophesizeChannel()->reveal());
38
39
        $this->assertEquals($m3u8->getTargetDuration(), $targetDuration);
40
        $this->assertEquals($m3u8->getVersion(), $version);
41
        $this->assertEquals($m3u8->getDiscontinuitySequence(), $programSequence);
42
        $this->assertEquals($m3u8->getAge(), 1); // even if program will end at once, the age should be one second
43
    }
44
45
    private function prophesizeMediaSegmentManager(\DateTime $startsAt, $targetDuration, array $segments)
46
    {
47
        $msManager = $this->prophesize('Chrisyue\Mala\Manager\MediaSegmentManagerInterface');
48
49
        $now = new \DateTime();
0 ignored issues
show
Unused Code introduced by
$now is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
        $msManager->findPlaying(Argument::type('Chrisyue\Mala\Model\ChannelInterface'), $startsAt, $targetDuration)
51
            ->shouldBeCalledTimes(1)->willReturn($segments);
52
53
        return $msManager;
54
    }
55
56
    private function prophesizeChannel()
57
    {
58
        $channel = $this->prophesize('Chrisyue\Mala\Model\ChannelInterface');
59
60
        return $channel;
61
    }
62
63
    private function prophesizeMediaSegment(\DateTime $endsAt, $programSequence)
64
    {
65
        $mediaSegment = $this->prophesize('Chrisyue\Mala\Model\ScheduledMediaSegment');
66
        $mediaSegment->getEndsAt()->shouldBeCalledTimes(1)->willReturn($endsAt);
67
68
        $program = $this->prophesizeProgram($programSequence);
69
        $mediaSegment->getProgram()->shouldBeCalledTimes(1)->willReturn($program->reveal());
70
71
        return $mediaSegment;
72
    }
73
74
    private function prophesizeProgram($sequence)
75
    {
76
        $program = $this->prophesize('Chrisyue\Mala\Model\ProgramInterface');
77
        $program->getSequence()->willReturn($sequence);
78
79
        return $program;
80
    }
81
}
82