|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Tests\Util; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Util\Period; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use LogicException; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
class PeriodTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
public function testConstructor() |
|
13
|
|
|
{ |
|
14
|
|
|
$start = '2016-08-01 00:00:01'; |
|
15
|
|
|
$end = '2016-08-02 00:00:01'; |
|
16
|
|
|
$period = new Period(new DateTime($start), new DateTime($end)); |
|
17
|
|
|
|
|
18
|
|
|
$this->assertEquals($start, $period->getStartDate()->format('Y-m-d H:i:s')); |
|
19
|
|
|
$this->assertEquals($end, $period->getEndDate()->format('Y-m-d H:i:s')); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testConstructorEqualDate() |
|
23
|
|
|
{ |
|
24
|
|
|
$start = '2016-08-01 00:00:01'; |
|
25
|
|
|
$end = '2016-08-01 00:00:01'; |
|
26
|
|
|
$period = new Period(new DateTime($start), new DateTime($end)); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertEquals($start, $period->getStartDate()->format('Y-m-d H:i:s')); |
|
29
|
|
|
$this->assertEquals($end, $period->getEndDate()->format('Y-m-d H:i:s')); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testConstructorWrongStartEndDate() |
|
33
|
|
|
{ |
|
34
|
|
|
$start = '2016-08-02 00:00:01'; |
|
35
|
|
|
$end = '2016-08-01 00:00:01'; |
|
36
|
|
|
|
|
37
|
|
|
$this->expectException(LogicException::class); |
|
38
|
|
|
$period = new Period(new DateTime($start), new DateTime($end)); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testGetters() |
|
42
|
|
|
{ |
|
43
|
|
|
$start = '2016-08-01 00:00:01'; |
|
44
|
|
|
$end = '2016-08-02 00:00:01'; |
|
45
|
|
|
$period = new Period(new DateTime($start), new DateTime($end)); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertEquals($start, $period->getStartDate()->format('Y-m-d H:i:s')); |
|
48
|
|
|
$this->assertEquals($end, $period->getEndDate()->format('Y-m-d H:i:s')); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testSetters() |
|
52
|
|
|
{ |
|
53
|
|
|
$start = '2016-08-01 00:00:01'; |
|
54
|
|
|
$end = '2016-08-02 00:00:01'; |
|
55
|
|
|
$period = new Period(new DateTime($start), new DateTime($end)); |
|
56
|
|
|
|
|
57
|
|
|
$startSet = '2016-08-01 01:00:01'; |
|
58
|
|
|
$endSet = '2016-08-04 00:00:01'; |
|
59
|
|
|
$period->setStartDate(new DateTime($startSet)); |
|
60
|
|
|
$period->setEndDate(new DateTime($endSet)); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertEquals($startSet, $period->getStartDate()->format('Y-m-d H:i:s')); |
|
63
|
|
|
$this->assertEquals($endSet, $period->getEndDate()->format('Y-m-d H:i:s')); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testWrongSetStartDate() |
|
67
|
|
|
{ |
|
68
|
|
|
$start = '2016-08-01 00:00:01'; |
|
69
|
|
|
$end = '2016-08-02 00:00:01'; |
|
70
|
|
|
$period = new Period(new DateTime($start), new DateTime($end)); |
|
71
|
|
|
|
|
72
|
|
|
$startSet = '2016-08-03 01:00:01'; |
|
73
|
|
|
$this->expectException(LogicException::class); |
|
74
|
|
|
$period->setStartDate(new DateTime($startSet)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testWrongSetEndDate() |
|
78
|
|
|
{ |
|
79
|
|
|
$start = '2016-08-01 00:00:01'; |
|
80
|
|
|
$end = '2016-08-02 00:00:01'; |
|
81
|
|
|
$period = new Period(new DateTime($start), new DateTime($end)); |
|
82
|
|
|
|
|
83
|
|
|
$endSet = '2016-07-31 00:00:01'; |
|
84
|
|
|
$this->expectException(LogicException::class); |
|
85
|
|
|
$period->setEndDate(new DateTime($endSet)); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.