Completed
Pull Request — master (#144)
by Markus
01:01
created

CalendarFactoryTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 24.49 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 24
loc 98
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testMinimalEvent() 0 21 1
A testEventWithSummaryAndDescription() 0 11 1
A testEventWithLocation() 0 14 1
A testSingleDayEvent() 0 8 1
A testMultiDayEvent() 12 12 1
A testTimespanEvent() 12 12 1
A assertEventRendersCorrect() 0 11 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 eluceo/iCal package.
5
 *
6
 * (c) 2019 Markus Poerschke <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Unit\Presentation\Factory;
13
14
use DateTimeImmutable;
15
use DateTimeZone;
16
use Eluceo\iCal\Domain\Entity\Event;
17
use Eluceo\iCal\Domain\ValueObject\Date;
18
use Eluceo\iCal\Domain\ValueObject\DateTime;
19
use Eluceo\iCal\Domain\ValueObject\GeographicPosition;
20
use Eluceo\iCal\Domain\ValueObject\Location;
21
use Eluceo\iCal\Domain\ValueObject\MultiDay;
22
use Eluceo\iCal\Domain\ValueObject\SingleDay;
23
use Eluceo\iCal\Domain\ValueObject\TimeSpan;
24
use Eluceo\iCal\Domain\ValueObject\Timestamp;
25
use Eluceo\iCal\Domain\ValueObject\UniqueIdentifier;
26
use Eluceo\iCal\Presentation\Component;
27
use Eluceo\iCal\Presentation\Factory\EventFactory;
28
use PHPUnit\Framework\TestCase;
29
30
class CalendarFactoryTest extends TestCase
31
{
32
    public function testMinimalEvent()
33
    {
34
        $currentTime = Timestamp::fromDateTimeInterface(
35
            DateTimeImmutable::createFromFormat(
36
                'Y-m-d H:i:s',
37
                '2019-11-10 11:22:33',
38
                new DateTimeZone('UTC')
39
            )
40
        );
41
42
        $event = Event::create(UniqueIdentifier::fromString('event1'))->touch($currentTime);
43
44
        $expected = implode(Component::LINE_SEPARATOR, [
45
            'BEGIN:VEVENT',
46
            'UID:event1',
47
            'DTSTAMP:20191110T112233Z',
48
            'END:VEVENT',
49
        ]);
50
51
        self::assertSame($expected, (string) (new EventFactory())->createComponent($event));
52
    }
53
54
    public function testEventWithSummaryAndDescription()
55
    {
56
        $event = Event::create()
57
            ->setSummary('Lorem Summary')
58
            ->setDescription('Lorem Description');
59
60
        $this->assertEventRendersCorrect($event, [
61
            'SUMMARY:Lorem Summary',
62
            'DESCRIPTION:Lorem Description',
63
        ]);
64
    }
65
66
    public function testEventWithLocation()
67
    {
68
        $geographicalPosition = GeographicPosition::fromLatitudeAndLongitude(51.333333333333, 7.05);
69
        $location = Location::fromString('Location Name')->withGeographicPosition($geographicalPosition);
70
        $event = Event::create()->setLocation($location);
71
72
        $this->assertEventRendersCorrect(
73
            $event,
74
            [
75
                'LOCATION:Location Name',
76
                'GEO:51.333333;7.050000',
77
            ]
78
        );
79
    }
80
81
    public function testSingleDayEvent()
82
    {
83
        $event = Event::create()->setOccurrence(SingleDay::fromDate(Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24'))));
84
85
        $this->assertEventRendersCorrect($event, [
86
            'DTSTART:20301224',
87
        ]);
88
    }
89
90 View Code Duplication
    public function testMultiDayEvent()
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...
91
    {
92
        $firstDay = Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24'));
93
        $lastDay = Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-26'));
94
        $occurrence = MultiDay::fromDates($firstDay, $lastDay);
95
        $event = Event::create()->setOccurrence($occurrence);
96
97
        $this->assertEventRendersCorrect($event, [
98
            'DTSTART:20301224',
99
            'DTEND:20301227',
100
        ]);
101
    }
102
103 View Code Duplication
    public function testTimespanEvent()
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...
104
    {
105
        $begin = DateTime::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d H:i', '2030-12-24 12:15'));
106
        $end = DateTime::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d H:i', '2030-12-24 13:45'));
107
        $occurrence = TimeSpan::create($begin, $end);
108
        $event = Event::create()->setOccurrence($occurrence);
109
110
        $this->assertEventRendersCorrect($event, [
111
            'DTSTART:20301224T121500',
112
            'DTEND:20301224T134500',
113
        ]);
114
    }
115
116
    private function assertEventRendersCorrect(Event $event, array $expected)
117
    {
118
        $resultAsString = (string) (new EventFactory())->createComponent($event);
119
120
        $resultAsArray = explode(Component::LINE_SEPARATOR, $resultAsString);
121
122
        self::assertGreaterThan(4, count($resultAsArray), 'No additional content lines were produced.');
123
124
        $resultAsArray = array_slice($resultAsArray, 3, -1);
125
        self::assertSame($expected, $resultAsArray);
126
    }
127
}
128