1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JhFlexiTimeTest\Entity; |
4
|
|
|
|
5
|
|
|
use JhFlexiTime\Entity\Booking; |
6
|
|
|
use JhFlexiTime\DateTime\DateTime; |
7
|
|
|
|
8
|
|
|
class BookingTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var Booking |
13
|
|
|
*/ |
14
|
|
|
protected $booking; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* SetUp |
18
|
|
|
*/ |
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->booking = new Booking; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Test The Date/Time setter/getters and default values |
26
|
|
|
* |
27
|
|
|
* @param string $name |
28
|
|
|
* @param DateTime $default |
29
|
|
|
* @param DateTime $newValue |
30
|
|
|
* |
31
|
|
|
* @dataProvider dateSetterGetterProvider |
32
|
|
|
*/ |
33
|
|
|
public function testDateSetterGetter($name, $default, $newValue) |
34
|
|
|
{ |
35
|
|
|
$getMethod = 'get' . ucfirst($name); |
36
|
|
|
$setMethod = 'set' . ucfirst($name); |
37
|
|
|
|
38
|
|
|
$this->assertInstanceOf('DateTime', $this->booking->$getMethod()); |
39
|
|
|
$this->assertEquals($default, $this->booking->$getMethod()); |
40
|
|
|
|
41
|
|
|
$this->booking->$setMethod($newValue); |
42
|
|
|
$this->assertSame($newValue, $this->booking->$getMethod()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function dateSetterGetterProvider() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
['date', new DateTime("today"), new DateTime("24 March 2014")], |
52
|
|
|
['startTime', new DateTime("today 09:00"), new DateTime("24 March 2014 10:00")], |
53
|
|
|
['endTime', new DateTime("today 17:30"), new DateTime("24 March 2014 18:30")], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* test the Number setter/getters and default values |
59
|
|
|
* |
60
|
|
|
* @param $name |
61
|
|
|
* @param $default |
62
|
|
|
* @param $newValue |
63
|
|
|
* |
64
|
|
|
* @dataProvider numberSetterGetterProvider |
65
|
|
|
*/ |
66
|
|
|
public function testNumberSetterGetter($name, $default, $newValue) |
67
|
|
|
{ |
68
|
|
|
$getMethod = 'get' . ucfirst($name); |
69
|
|
|
$setMethod = 'set' . ucfirst($name); |
70
|
|
|
|
71
|
|
|
$this->assertEquals($default, $this->booking->$getMethod()); |
72
|
|
|
|
73
|
|
|
$this->booking->$setMethod($newValue); |
74
|
|
|
$this->assertEquals($newValue, $this->booking->$getMethod()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function numberSetterGetterProvider() |
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
|
|
['total', 0, 7.5], |
84
|
|
|
['balance', 0, 0], |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Test the other setter/getters which have no default values |
90
|
|
|
* |
91
|
|
|
* @param string $name |
92
|
|
|
* @param mixed $value |
93
|
|
|
* |
94
|
|
|
* @dataProvider setterGetterProvider |
95
|
|
|
*/ |
96
|
|
|
public function testSetterGetter($name, $value) |
97
|
|
|
{ |
98
|
|
|
$getMethod = 'get' . ucfirst($name); |
99
|
|
|
$setMethod = 'set' . ucfirst($name); |
100
|
|
|
|
101
|
|
|
$this->assertNull($this->booking->$getMethod()); |
102
|
|
|
$this->booking->$setMethod($value); |
103
|
|
|
$this->assertSame($value, $this->booking->$getMethod()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function setterGetterProvider() |
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
|
|
['user', $this->getMock('ZfcUser\Entity\UserInterface')], |
113
|
|
|
['notes', 'Why You even Unit Testing bro?'], |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testJsonSerializeWithModifiedValues() |
118
|
|
|
{ |
119
|
|
|
$date = new DateTime("24 March 2014"); |
120
|
|
|
|
121
|
|
|
$expected = [ |
122
|
|
|
'id' => $date->getTimestamp() . "-2", |
123
|
|
|
'date' => $date->format('d-m-Y'), |
124
|
|
|
'startTime' => '11:00', |
125
|
|
|
'endTime' => '19:30', |
126
|
|
|
'total' => 10, |
127
|
|
|
'balance' => 2.5, |
128
|
|
|
'notes' => 'Just point and click and see if it works. deploy?!', |
129
|
|
|
'user' => 2 |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
$user = $this->getMock('ZfcUser\Entity\UserInterface'); |
133
|
|
|
$user->expects($this->exactly(2)) |
134
|
|
|
->method('getId') |
135
|
|
|
->will($this->returnValue(2)); |
136
|
|
|
|
137
|
|
|
$this->booking |
138
|
|
|
->setDate($date) |
139
|
|
|
->setStartTime(new DateTime("11:00")) |
140
|
|
|
->setEndTime(new DateTime("19:30")) |
141
|
|
|
->setTotal(10) |
142
|
|
|
->setBalance(2.5) |
143
|
|
|
->setNotes('Just point and click and see if it works. deploy?!') |
144
|
|
|
->setUser($user); |
145
|
|
|
|
146
|
|
|
$this->assertEquals($expected, $this->booking->jsonSerialize()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testGetIdThrowsExceptionIfUserNotSet() |
150
|
|
|
{ |
151
|
|
|
$this->setExpectedException('\RuntimeException', 'No User is set. Needed to generate ID'); |
152
|
|
|
$this->booking->getId(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testGetIdContainsUSerIdAndDate() |
156
|
|
|
{ |
157
|
|
|
$user = $this->getMock('ZfcUser\Entity\UserInterface'); |
158
|
|
|
$user->expects($this->once()) |
159
|
|
|
->method('getId') |
160
|
|
|
->will($this->returnValue(2)); |
161
|
|
|
$date = new DateTime("24 March 2014"); |
162
|
|
|
|
163
|
|
|
$this->booking->setUser($user); |
164
|
|
|
$this->booking->setDate($date); |
165
|
|
|
$id = $this->booking->getId(); |
166
|
|
|
$this->assertEquals($date->getTimestamp() . "-2", $id); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|