1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spiral\Tests\Statistics; |
4
|
|
|
|
5
|
|
|
use Spiral\Statistics\Database\Event; |
6
|
|
|
use Spiral\Statistics\Database\Occurrence; |
7
|
|
|
use Spiral\Tests\BaseTest; |
8
|
|
|
|
9
|
|
|
class TrackTest extends BaseTest |
10
|
|
|
{ |
11
|
|
View Code Duplication |
public function testEvent() |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
$track = $this->getTrack(); |
14
|
|
|
|
15
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
16
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
17
|
|
|
|
18
|
|
|
$track->event('some-event', 1.23); |
19
|
|
|
|
20
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
21
|
|
|
$this->assertCount(1, $this->orm->source(Event::class)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
View Code Duplication |
public function testEvents() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$track = $this->getTrack(); |
27
|
|
|
|
28
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
29
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
30
|
|
|
|
31
|
|
|
$track->events([ |
32
|
|
|
'some-event' => 1.23, |
33
|
|
|
'some-event2' => 2.34 |
34
|
|
|
]); |
35
|
|
|
|
36
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
37
|
|
|
$this->assertCount(2, $this->orm->source(Event::class)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function testEmptyEvents() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$track = $this->getTrack(); |
43
|
|
|
|
44
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
45
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
46
|
|
|
|
47
|
|
|
$track->events([]); |
48
|
|
|
|
49
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
50
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testEventInc() |
54
|
|
|
{ |
55
|
|
|
$track = $this->getTrack(); |
56
|
|
|
|
57
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
58
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
59
|
|
|
|
60
|
|
|
$datetime = new \DateTime('now'); |
61
|
|
|
$track->event('some-event', 1.23, $datetime); |
62
|
|
|
$track->event('some-event', 2.34, $datetime); |
63
|
|
|
|
64
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
65
|
|
|
$this->assertCount(1, $this->orm->source(Event::class)); |
66
|
|
|
|
67
|
|
|
/** @var Event $event */ |
68
|
|
|
$event = $this->orm->source(Event::class)->findOne(); |
69
|
|
|
|
70
|
|
|
$this->assertNotEmpty($event); |
71
|
|
|
$this->assertSame(3.57, $event->value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testEventsInc() |
75
|
|
|
{ |
76
|
|
|
$track = $this->getTrack(); |
77
|
|
|
|
78
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
79
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
80
|
|
|
|
81
|
|
|
$datetime = new \DateTime('now'); |
82
|
|
|
$track->events([ |
83
|
|
|
'some-event' => 1.23, |
84
|
|
|
], $datetime); |
85
|
|
|
$track->events([ |
86
|
|
|
'some-event' => 2.34, |
87
|
|
|
], $datetime); |
88
|
|
|
|
89
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
90
|
|
|
$this->assertCount(1, $this->orm->source(Event::class)); |
91
|
|
|
|
92
|
|
|
/** @var Event $event */ |
93
|
|
|
$event = $this->orm->source(Event::class)->findOne(); |
94
|
|
|
|
95
|
|
|
$this->assertNotEmpty($event); |
96
|
|
|
$this->assertSame(3.57, $event->value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function testSignEventInc() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$track = $this->getTrack(); |
102
|
|
|
|
103
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
104
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
105
|
|
|
|
106
|
|
|
$datetime = new \DateTime('now'); |
107
|
|
|
$track->event('some-event', 1.23, $datetime); |
108
|
|
|
$track->event('some-event', -2.34, $datetime); |
109
|
|
|
|
110
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
111
|
|
|
$this->assertCount(1, $this->orm->source(Event::class)); |
112
|
|
|
|
113
|
|
|
/** @var Event $event */ |
114
|
|
|
$event = $this->orm->source(Event::class)->findOne(); |
115
|
|
|
|
116
|
|
|
$this->assertNotEmpty($event); |
117
|
|
|
$this->assertSame(-1.11, $event->value); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function testSignEventsInc() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$track = $this->getTrack(); |
123
|
|
|
|
124
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
125
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
126
|
|
|
|
127
|
|
|
$datetime = new \DateTime('now'); |
128
|
|
|
$track->events([ |
129
|
|
|
'some-event' => 1.23, |
130
|
|
|
], $datetime); |
131
|
|
|
$track->events([ |
132
|
|
|
'some-event' => -2.34, |
133
|
|
|
], $datetime); |
134
|
|
|
|
135
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
136
|
|
|
$this->assertCount(1, $this->orm->source(Event::class)); |
137
|
|
|
|
138
|
|
|
/** @var Event $event */ |
139
|
|
|
$event = $this->orm->source(Event::class)->findOne(); |
140
|
|
|
|
141
|
|
|
$this->assertNotEmpty($event); |
142
|
|
|
$this->assertSame(-1.11, $event->value); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testZeroEventInc() |
146
|
|
|
{ |
147
|
|
|
$track = $this->getTrack(); |
148
|
|
|
|
149
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
150
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
151
|
|
|
|
152
|
|
|
$datetime = new \DateTime('now'); |
153
|
|
|
$track->event('some-event', 1.23, $datetime); |
154
|
|
|
$track->event('some-event', 0, $datetime); |
155
|
|
|
|
156
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
157
|
|
|
$this->assertCount(1, $this->orm->source(Event::class)); |
158
|
|
|
|
159
|
|
|
/** @var Event $event */ |
160
|
|
|
$event = $this->orm->source(Event::class)->findOne(); |
161
|
|
|
|
162
|
|
|
$this->assertNotEmpty($event); |
163
|
|
|
$this->assertSame(1.23, $event->value); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function testZeroEventsInc() |
167
|
|
|
{ |
168
|
|
|
$track = $this->getTrack(); |
169
|
|
|
|
170
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
171
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
172
|
|
|
|
173
|
|
|
$datetime = new \DateTime('now'); |
174
|
|
|
$track->events([ |
175
|
|
|
'some-event' => 1.23, |
176
|
|
|
], $datetime); |
177
|
|
|
$track->events([ |
178
|
|
|
'some-event' => 0, |
179
|
|
|
], $datetime); |
180
|
|
|
|
181
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
182
|
|
|
$this->assertCount(1, $this->orm->source(Event::class)); |
183
|
|
|
|
184
|
|
|
/** @var Event $event */ |
185
|
|
|
$event = $this->orm->source(Event::class)->findOne(); |
186
|
|
|
|
187
|
|
|
$this->assertNotEmpty($event); |
188
|
|
|
$this->assertSame(1.23, $event->value); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
View Code Duplication |
public function testNewEventRelation() |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$track = $this->getTrack(); |
194
|
|
|
|
195
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
196
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
197
|
|
|
|
198
|
|
|
$datetime = new \DateTime('now'); |
199
|
|
|
$track->event('some-event', 1.23, $datetime); |
200
|
|
|
|
201
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
202
|
|
|
$this->assertCount(1, $this->orm->source(Event::class)); |
203
|
|
|
|
204
|
|
|
/** @var Occurrence $occurrence */ |
205
|
|
|
$occurrence = $this->orm->source(Occurrence::class)->findOne(); |
206
|
|
|
$this->assertCount(1, $occurrence->events); |
207
|
|
|
|
208
|
|
|
$track->event('some-event2', 2.34, $datetime); |
209
|
|
|
|
210
|
|
|
/** @var Occurrence $occurrence */ |
211
|
|
|
$occurrence = $this->orm->source(Occurrence::class)->findOne(); |
212
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
213
|
|
|
$this->assertCount(2, $this->orm->source(Event::class)); |
214
|
|
|
$this->assertCount(2, $occurrence->events); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
View Code Duplication |
public function testNewEventsRelation() |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$track = $this->getTrack(); |
220
|
|
|
|
221
|
|
|
$this->assertCount(0, $this->orm->source(Occurrence::class)); |
222
|
|
|
$this->assertCount(0, $this->orm->source(Event::class)); |
223
|
|
|
|
224
|
|
|
$datetime = new \DateTime('now'); |
225
|
|
|
$track->events([ |
226
|
|
|
'some-event' => 1.23, |
227
|
|
|
], $datetime); |
228
|
|
|
|
229
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
230
|
|
|
$this->assertCount(1, $this->orm->source(Event::class)); |
231
|
|
|
|
232
|
|
|
/** @var Occurrence $occurrence */ |
233
|
|
|
$occurrence = $this->orm->source(Occurrence::class)->findOne(); |
234
|
|
|
$this->assertCount(1, $occurrence->events); |
235
|
|
|
$track->events([ |
236
|
|
|
'some-event2' => 2.34, |
237
|
|
|
], $datetime); |
238
|
|
|
|
239
|
|
|
/** @var Occurrence $occurrence */ |
240
|
|
|
$occurrence = $this->orm->source(Occurrence::class)->findOne(); |
241
|
|
|
$this->assertCount(1, $this->orm->source(Occurrence::class)); |
242
|
|
|
$this->assertCount(2, $this->orm->source(Event::class)); |
243
|
|
|
$this->assertCount(2, $occurrence->events); |
244
|
|
|
} |
245
|
|
|
} |
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.