1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chrisyue\PhpM3u8\Tests\M3u8\Lines; |
4
|
|
|
|
5
|
|
|
use Chrisyue\PhpM3u8\M3u8\Lines\Lines; |
6
|
|
|
use Chrisyue\PhpM3u8\M3u8\Transformer\TransformerInterface; |
7
|
|
|
use Chrisyue\PhpM3u8\Stream\StreamInterface; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class LinesTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testGoNext() |
13
|
|
|
{ |
14
|
|
|
$prophecy = $this->prophesize(StreamInterface::class); |
15
|
|
|
$prophecy->goNext()->shouldBeCalledTimes(1); |
16
|
|
|
$prophecy->isValid()->shouldBeCalledTimes(1)->willReturn(false); |
17
|
|
|
|
18
|
|
|
$lines = new Lines($prophecy->reveal(), $this->createMock(TransformerInterface::class)); |
19
|
|
|
$lines->goNext(); |
20
|
|
|
|
21
|
|
|
// if stream get an empty line, goNext should auto go next next. |
22
|
|
|
$prophecy = $this->prophesize(StreamInterface::class); |
23
|
|
|
$prophecy->goNext()->shouldBeCalledTimes(2); |
24
|
|
|
$prophecy->isValid()->shouldBeCalledTimes(2)->willReturn(true); |
25
|
|
|
$prophecy->getLine()->shouldBeCalledTimes(2)->willReturn(' ', '#TAG'); |
26
|
|
|
|
27
|
|
|
$lines = new Lines($prophecy->reveal(), $this->createMock(TransformerInterface::class)); |
28
|
|
|
$lines->goNext(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testIsValid() |
32
|
|
|
{ |
33
|
|
|
$valid = (bool) rand(0, 1); |
34
|
|
|
|
35
|
|
|
$prophecy = $this->prophesize(StreamInterface::class); |
36
|
|
|
$prophecy->isValid()->shouldBeCalledTimes(1)->willReturn($valid); |
37
|
|
|
|
38
|
|
|
$lines = new Lines($prophecy->reveal(), $this->createMock(TransformerInterface::class)); |
39
|
|
|
$this->assertSame($valid, $lines->isValid()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testRead() |
43
|
|
|
{ |
44
|
|
|
$line = '#TAG'; |
45
|
|
|
$transformed = ['tag' => '#TAG', 'value' => '']; |
46
|
|
|
|
47
|
|
|
$streamProphecy = $this->prophesize(StreamInterface::class); |
48
|
|
|
$streamProphecy->getLine()->willReturn($line); |
49
|
|
|
|
50
|
|
|
$transformerProphecy = $this->prophesize(TransformerInterface::class); |
51
|
|
|
$transformerProphecy->transform($line)->shouldBeCalledTimes(1)->willReturn($transformed); |
52
|
|
|
|
53
|
|
|
$lines = new Lines($streamProphecy->reveal(), $transformerProphecy->reveal()); |
54
|
|
|
|
55
|
|
|
$this->assertSame($transformed, $lines->read()); |
56
|
|
|
$this->assertSame($transformed, $lines->read()); // test read cache, should only transform once |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function testWrite() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$line = 'value'; |
62
|
|
|
$transformed = ['value' => 'value']; |
63
|
|
|
|
64
|
|
|
$transformerProphecy = $this->prophesize(TransformerInterface::class); |
65
|
|
|
$transformerProphecy->reverse($transformed)->shouldBeCalledTimes(1)->willReturn($line); |
66
|
|
|
|
67
|
|
|
$streamProphecy = $this->prophesize(StreamInterface::class); |
68
|
|
|
$streamProphecy->putLine($line)->shouldBeCalled(); |
69
|
|
|
|
70
|
|
|
$lines = new Lines($streamProphecy->reveal(), $transformerProphecy->reveal()); |
71
|
|
|
$lines->write($transformed); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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.