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

EpgSchedulerTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 136
Duplicated Lines 15.44 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 1
dl 21
loc 136
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testScheduleWithNoLastProgramAndNoVideos() 14 14 1
A testScheduleWithNoLastProgram() 0 20 1
A testScheduleWithLastProgramButNoVideos() 0 15 1
B testScheduleWithLastProgramWithForce() 0 29 1
A epgManagerShouldCreateProgram() 0 13 1
A prophesizeEpgManager() 0 8 2
A prophesizeVideoManager() 7 7 1
A prophesizeChannel() 0 4 1
A prophesizeProgram() 0 7 1
A prophesizeVideo() 0 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\EpgScheduler;
15
use Prophecy\Prophecy\ObjectProphecy;
16
17
class EpgSchedulerTest extends \PHPUnit_Framework_TestCase
18
{
19 View Code Duplication
    public function testScheduleWithNoLastProgramAndNoVideos()
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...
20
    {
21
        $startsAt = new \DateTime();
22
        $endsAt = new \DateTime('+30 seconds');
23
24
        $channel = $this->prophesizeChannel();
25
26
        $epgManager = $this->prophesizeEpgManager($channel);
27
        $videoManager = $this->prophesizeVideoManager($channel);
28
29
        $epgScheduler = new EpgScheduler($epgManager->reveal(), $videoManager->reveal());
30
31
        $epgScheduler->schedule($channel->reveal(), $startsAt, $endsAt);
32
    }
33
34
    public function testScheduleWithNoLastProgram()
35
    {
36
        $startsAt = new \DateTime();
37
        $endsAt = new \DateTime('+30 seconds');
0 ignored issues
show
Unused Code introduced by
$endsAt 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...
38
        $videoDuration = 40;
39
40
        $channel = $this->prophesizeChannel();
41
        $video = $this->prophesizeVideo($videoDuration);
42
        $videos = array($video);
43
44
        $epgManager = $this->prophesizeEpgManager($channel, null, true);
45
        $videoManager = $this->prophesizeVideoManager($channel, $videos);
46
47
        $epgScheduler = new EpgScheduler($epgManager->reveal(), $videoManager->reveal());
48
        $endsAt = clone $startsAt;
49
        $endsAt->modify(sprintf('+%d seconds', $videoDuration - 1)); // duration = [startsAt, endsAt]
50
        $this->epgManagerShouldCreateProgram($epgManager, $channel, $video, 1, $startsAt, $endsAt); // the first program sequence should be 1
51
52
        $epgScheduler->schedule($channel->reveal(), $startsAt, $endsAt);
53
    }
54
55
    public function testScheduleWithLastProgramButNoVideos()
56
    {
57
        $startsAt = new \DateTime();
58
        $endsAt = new \DateTime('+30 seconds');
59
        $lastProgramEndsAt = new \DateTime('+2 seconds');
60
61
        $channel = $this->prophesizeChannel();
62
        $program = $this->prophesizeProgram($lastProgramEndsAt);
63
64
        $epgManager = $this->prophesizeEpgManager($channel, $program);
65
        $videoManager = $this->prophesizeVideoManager($channel);
66
        $epgScheduler = new EpgScheduler($epgManager->reveal(), $videoManager->reveal());
67
68
        $epgScheduler->schedule($channel->reveal(), $startsAt, $endsAt);
69
    }
70
71
    public function testScheduleWithLastProgramWithForce()
72
    {
73
        $startsAt = new \DateTime();
74
        $endsAt = new \DateTime('+30 seconds');
75
        $lastProgramEndsAt = new \DateTime('+2 seconds');
76
        $lastProgramSequence = rand(1, 999);
77
        $videoDuration = 40;
78
79
        $channel = $this->prophesizeChannel($lastProgramEndsAt);
80
        $program = $this->prophesizeProgram($lastProgramEndsAt);
81
        $video = $this->prophesizeVideo($videoDuration);
82
        $videos = array($video);
83
84
        $epgManager = $this->prophesizeEpgManager($channel, $program);
85
        $epgManager->clear($channel->reveal(), $startsAt)->shouldBeCalledTimes(1);
86
87
        $videoManager = $this->prophesizeVideoManager($channel, $videos);
88
89
        $program->getSequence()->shouldBeCalledTimes(1)->willReturn($lastProgramSequence);
90
91
        $epgScheduler = new EpgScheduler($epgManager->reveal(), $videoManager->reveal());
92
        $programEndsAt = clone $lastProgramEndsAt;
93
        $programEndsAt->modify(sprintf('+%d seconds', $videoDuration)); // duration = (lastProgramEndsAt, $endsAt]
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
        $programStartsAt = clone $lastProgramEndsAt;
95
        $programStartsAt->modify('+1 second');
96
        $this->epgManagerShouldCreateProgram($epgManager, $channel, $video, $lastProgramSequence + 1, $programStartsAt, $programEndsAt);
97
98
        $epgScheduler->schedule($channel->reveal(), $startsAt, $endsAt, true);
99
    }
100
101
    private function epgManagerShouldCreateProgram(
102
        ObjectProphecy $epgManager,
103
        ObjectProphecy $channel,
104
        ObjectProphecy $video,
105
        $sequence, $videoStartsAt, $videoEndsAt
106
    ) {
107
        $newProgram = $this->prophesize('Chrisyue\Mala\Model\ProgramInterface');
108
        $epgManager->createProgram($channel->reveal(), $video->reveal(), $sequence, $videoStartsAt, $videoEndsAt)
109
            ->willReturn($newProgram->reveal());
110
111
        $epgManager->saveDeferred($newProgram->reveal())->shouldBeCalled();
112
        $epgManager->commit()->shouldBeCalledTimes(1);
113
    }
114
115
    private function prophesizeEpgManager(ObjectProphecy $channel, ObjectProphecy $lastProgram = null, $shouldCreateProgram = false)
116
    {
117
        $epgManager = $this->prophesize('Chrisyue\Mala\Manager\EpgManagerInterface');
118
        $epgManager->findLastProgram($channel->reveal())->shouldBeCalledTimes(1)
119
            ->willReturn(null === $lastProgram ? null : $lastProgram->reveal());
120
121
        return $epgManager;
122
    }
123
124 View Code Duplication
    private function prophesizeVideoManager(ObjectProphecy $channel, array $videos = array())
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...
125
    {
126
        $videoManager = $this->prophesize('Chrisyue\Mala\Manager\VideoManagerInterface');
127
        $videoManager->findByChannel($channel->reveal())->shouldBeCalledTimes(1)->willReturn($videos);
128
129
        return $videoManager;
130
    }
131
132
    private function prophesizeChannel()
133
    {
134
        return $this->prophesize('Chrisyue\Mala\Model\ChannelInterface');
135
    }
136
137
    private function prophesizeProgram(\DateTime $endsAt)
138
    {
139
        $program = $this->prophesize('Chrisyue\Mala\Model\ProgramInterface');
140
        $program->getEndsAt()->shouldBeCalledTimes(1)->willReturn($endsAt);
141
142
        return $program;
143
    }
144
145
    private function prophesizeVideo($duration)
146
    {
147
        $video = $this->prophesize('Chrisyue\Mala\Model\VideoInterface');
148
        $video->getDuration()->shouldBeCalledTimes(1)->willReturn($duration);
149
150
        return $video;
151
    }
152
}
153