Completed
Pull Request — develop (#27)
by Chris
11:56
created

ParserTest::testRead()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 56
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 56
rs 9.7251
c 1
b 0
f 0
cc 3
eloc 45
nc 4
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the PhpM3u8 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 Chrisyue\PhpM3u8\tests;
13
14
use PHPUnit\Framework\TestCase;
15
use Chrisyue\PhpM3u8\Parser\Parser;
16
use Chrisyue\PhpM3u8\Parser\PlaylistBuilder;
17
use Chrisyue\PhpM3u8\Parser\PlaylistComponentFactory;
18
use Chrisyue\PhpM3u8\Dumper\Dumper;
19
20
class ParserTest extends TestCase
21
{
22
    public function testRead()
23
    {
24
        $content = DummyM3u8Factory::createMediaPlaylistContent(2);
25
26
        $parser = new Parser(new PlaylistBuilder(new PlaylistComponentFactory()));
27
        foreach (explode("\n", $content) as $line) {
28
            $parser->parseLine($line);
29
        }
30
31
        $playlist = $parser->getPlaylist();
32
        $this->assertEquals(2, $playlist->getVersion());
33
        $this->assertEquals(12, $playlist->getTargetduration());
34
        $this->assertEquals(33, $playlist->getMediaSequence());
35
        $this->assertEquals(3, $playlist->getDiscontinuitySequence());
36
37
        $segment = $playlist->getSegments()->offsetGet(0);
38
        $this->assertEquals('AES-128', $segment->getKeys()[0]->getMethod());
39
        $this->assertEquals('com.apple', $segment->getKeys()[1]->getKeyformat());
40
        $this->assertEquals('1', $segment->getKeys()[1]->getKeyformatversions());
41
        $this->assertEquals(12, $segment->getInf()->getDuration());
42
        $this->assertEquals('hello world', $segment->getInf()->getTitle());
43
        $this->assertEquals('stream33.ts', $segment->getUri());
44
        $this->assertEquals(10000, $segment->getByterange()->getLength());
45
        $this->assertEquals(100, $segment->getByterange()->getOffset());
46
        $this->assertFalse($segment->isDiscontinuity());
47
48
        $segment = $playlist->getSegments()->offsetGet(1);
49
        $this->assertEquals(10, $segment->getInf()->getDuration());
50
        $this->assertEquals('video01.ts', $segment->getUri());
51
        $this->assertTrue($segment->isDiscontinuity());
52
53
        $this->assertEquals(22, $playlist->getDuration());
54
55
        $content = DummyM3u8Factory::createMasterPlaylistContent();
56
        $parser = new Parser(new PlaylistBuilder(new PlaylistComponentFactory()));
57
        foreach (explode("\n", $content) as $line) {
58
            $parser->parseLine($line);
59
        }
60
61
        $playlist = $parser->getPlaylist();
62
        $this->assertEquals(true, $playlist->isIndependentSegments());
63
        $this->assertEquals(1.1, $playlist->getStart()->getTimeOffset());
64
        $this->assertEquals('YES', $playlist->getStart()->getPrecise());
65
        $this->assertEquals('AUDIO', $playlist->getMedias()[0]->getType());
66
        $this->assertEquals('media.uri', $playlist->getMedias()[0]->getUri());
67
        $this->assertEquals('group', $playlist->getMedias()[0]->getGroupId());
68
        $this->assertEquals('zh-Hans', $playlist->getMedias()[0]->getLanguage());
69
        $this->assertEquals('zh-Hans', $playlist->getMedias()[0]->getAssocLanguage());
70
        $this->assertEquals('name', $playlist->getMedias()[0]->getName());
71
        $this->assertEquals('YES', $playlist->getMedias()[0]->getDefault());
72
        $this->assertEquals('YES', $playlist->getMedias()[0]->getAutoselect());
73
        $this->assertEquals('NO', $playlist->getMedias()[0]->getForced());
74
        $this->assertEquals('CC1', $playlist->getMedias()[0]->getInstreamId());
75
        $this->assertEquals('public.easy-to-read', $playlist->getMedias()[0]->getCharacteristics());
76
        $this->assertEquals('6', $playlist->getMedias()[0]->getChannels());
77
    }
78
79
    public function testDump()
80
    {
81
        $playlist = DummyM3u8Factory::createMediaPlaylist(3);
82
        $dumper = new Dumper();
83
84
        $this->assertEquals(DummyM3u8Factory::createMediaPlaylistContent(3), $dumper->dump($playlist));
85
    }
86
}
87
88