BuiltInFunctionsExtensionTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 145
Duplicated Lines 68.97 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 100
loc 145
rs 10
c 8
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testNow() 0 23 1
B testStartOfDay() 25 25 1
B testStartOfWeek() 25 25 1
B testStartOfMonth() 25 25 1
B testStartOfYear() 25 25 1
A getExtension() 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
namespace Netdudes\DataSourceryBundle\Tests\Extension;
3
4
use Netdudes\DataSourceryBundle\Extension\BuiltInFunctionsExtension;
5
use Netdudes\DataSourceryBundle\Util\CurrentDateTimeProvider;
6
use PHPUnit\Framework\TestCase;
7
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
8
9
class BuiltInFunctionsExtensionTest extends TestCase
10
{
11
    public function testNow()
12
    {
13
        $extension = $this->getExtension();
14
15
        $todayResult = $extension->now(null);
16
        $this->assertSame("2012-06-03T22:22:22+0200", $todayResult, 'The today function result did not produce the expected  with no offset');
17
18
        $offset = "+5 days";
19
        $todayResult = $extension->now($offset);
20
        $this->assertSame("2012-06-08T22:22:22+0200", $todayResult, 'The today function result did not produce the expected result with ofset ' . $offset);
21
22
        $offset = "-3 days";
23
        $todayResult = $extension->now($offset);
24
        $this->assertSame("2012-05-31T22:22:22+0200", $todayResult, 'The today function result did not produce the expected result with ofset ' . $offset);
25
26
        $offset = "-6 days - 3 minutes";
27
        $todayResult = $extension->now($offset);
28
        $this->assertSame("2012-05-28T22:19:22+0200", $todayResult, 'The today function result did not produce the expected result with ofset ' . $offset);
29
30
        $offset = "-30 minutes";
31
        $todayResult = $extension->now($offset);
32
        $this->assertSame("2012-06-03T21:52:22+0200", $todayResult, 'The today function result did not produce the expected result with ofset ' . $offset);
33
    }
34
35 View Code Duplication
    public function testStartOfDay()
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...
36
    {
37
        $extension = $this->getExtension();
38
39
        $startOfDayResult = $extension->startOfDay();
40
        $this->assertSame('2012-06-03T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result');
41
42
        $startOfDayResult = $extension->startOfDay('+2 hours');
43
        $this->assertSame('2012-06-04T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result');
44
45
        $startOfDayResult = $extension->startOfDay('-5 days');
46
        $this->assertSame('2012-05-29T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result');
47
48
        $startOfDayResult = $extension->startOfDay('+1 month');
49
        $this->assertSame('2012-07-03T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result');
50
51
        $startOfDayResult = $extension->startOfDay('15-05-2012');
52
        $this->assertSame('2012-05-15T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result');
53
54
        $startOfDayResult = $extension->startOfDay('2012-05-15');
55
        $this->assertSame('2012-05-15T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result');
56
57
        $startOfDayResult = $extension->startOfDay('15.05.2012');
58
        $this->assertSame('2012-05-15T00:00:00+0200', $startOfDayResult, 'The startOfDay function result did not produce the expected result');
59
    }
60
61 View Code Duplication
    public function testStartOfWeek()
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...
62
    {
63
        $extension = $this->getExtension();
64
65
        $startOfWeekResult = $extension->startOfWeek();
66
        $this->assertSame('2012-05-28T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result');
67
68
        $startOfWeekResult = $extension->startOfWeek('+2 hours');
69
        $this->assertSame('2012-06-04T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result');
70
71
        $startOfWeekResult = $extension->startOfWeek('-5 days');
72
        $this->assertSame('2012-05-28T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result');
73
74
        $startOfWeekResult = $extension->startOfWeek('+1 month');
75
        $this->assertSame('2012-07-02T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result');
76
77
        $startOfWeekResult = $extension->startOfWeek('15-05-2012');
78
        $this->assertSame('2012-05-14T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result');
79
80
        $startOfWeekResult = $extension->startOfWeek('2012-05-15');
81
        $this->assertSame('2012-05-14T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result');
82
83
        $startOfWeekResult = $extension->startOfWeek('15.05.2012');
84
        $this->assertSame('2012-05-14T00:00:00+0200', $startOfWeekResult, 'The startOfWeek function result did not produce the expected result');
85
    }
86
87 View Code Duplication
    public function testStartOfMonth()
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...
88
    {
89
        $extension = $this->getExtension();
90
91
        $startOfMonthResult = $extension->startOfMonth();
92
        $this->assertSame('2012-06-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result');
93
94
        $startOfMonthResult = $extension->startOfMonth('+2 hours');
95
        $this->assertSame('2012-06-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result');
96
97
        $startOfMonthResult = $extension->startOfMonth('-5 days');
98
        $this->assertSame('2012-05-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result');
99
100
        $startOfMonthResult = $extension->startOfMonth('+1 month');
101
        $this->assertSame('2012-07-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result');
102
103
        $startOfMonthResult = $extension->startOfMonth('15-05-2012');
104
        $this->assertSame('2012-05-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result');
105
106
        $startOfMonthResult = $extension->startOfMonth('2012-05-15');
107
        $this->assertSame('2012-05-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result');
108
109
        $startOfMonthResult = $extension->startOfMonth('15.05.2012');
110
        $this->assertSame('2012-05-01T00:00:00+0200', $startOfMonthResult, 'The startOfMonth function result did not produce the expected result');
111
    }
112
113 View Code Duplication
    public function testStartOfYear()
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...
114
    {
115
        $extension = $this->getExtension();
116
117
        $startOfYearResult = $extension->startOfYear();
118
        $this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result');
119
120
        $startOfYearResult = $extension->startOfYear('+2 hours');
121
        $this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result');
122
123
        $startOfYearResult = $extension->startOfYear('-5 days');
124
        $this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result');
125
126
        $startOfYearResult = $extension->startOfYear('+1 month');
127
        $this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result');
128
129
        $startOfYearResult = $extension->startOfYear('15-05-2012');
130
        $this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result');
131
132
        $startOfYearResult = $extension->startOfYear('2012-05-15');
133
        $this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result');
134
135
        $startOfYearResult = $extension->startOfYear('15.05.2012');
136
        $this->assertSame('2012-01-01T00:00:00+0200', $startOfYearResult, 'The startOfYear function result did not produce the expected result');
137
    }
138
139
    /**
140
     * @return BuiltInFunctionsExtension
141
     */
142
    private function getExtension()
143
    {
144
        $now = new \DateTime("2012-06-03T22:22:22+0200");
145
146
        $tokenStorage = $this->prophesize(TokenStorageInterface::class);
147
        $provider = $this->prophesize(CurrentDateTimeProvider::class);
148
        $provider->get()->willReturn($now);
149
        $extension = new BuiltInFunctionsExtension($tokenStorage->reveal(), $provider->reveal());
150
151
        return $extension;
152
    }
153
}
154