@@ -42,21 +42,6 @@ public static function dataProviderForUsernameChangeCost()
42
42
];
43
43
}
44
44
45
- public static function dataProviderForUsernameChangeCostLastChange ()
46
- {
47
- // assume there are 6 changes (max tier + 1)
48
- return [
49
- [0 , 100 ],
50
- [1 , 64 ],
51
- [2 , 32 ],
52
- [3 , 16 ],
53
- [4 , 8 ],
54
- [5 , 8 ],
55
- [6 , 8 ],
56
- [10 , 8 ],
57
- ];
58
- }
59
-
60
45
public static function dataProviderForUsernameChangeCostType ()
61
46
{
62
47
return [
@@ -68,14 +53,16 @@ public static function dataProviderForUsernameChangeCostType()
68
53
];
69
54
}
70
55
71
- public static function dataProviderForUsernameChangeCostTypeLastChange ()
56
+ public static function dataProviderForUsernameChangeCostWindow ()
72
57
{
58
+ // years with no name changes, cost
59
+ // test is setup with name change every 6 months.
73
60
return [
74
- [' admin ' , 8 ],
75
- [' inactive ' , 8 ],
76
- [' paid ' , 32 ],
77
- [' revert ' , 8 ],
78
- [' support ' , 32 ],
61
+ [0 , 100 ],
62
+ [1 , 32 ],
63
+ [2 , 8 ],
64
+ [3 , 8 ],
65
+ [4 , 8 ],
79
66
];
80
67
}
81
68
@@ -185,20 +172,50 @@ public function testUsernameChangeCost(int $changes, int $cost)
185
172
$ this ->assertSame ($ cost , $ user ->usernameChangeCost ());
186
173
}
187
174
188
- /**
189
- * @dataProvider dataProviderForUsernameChangeCostLastChange
190
- */
191
- public function testUsernameChangeCostLastChange (int $ years , int $ cost )
175
+ public function testUsernameChangeCostMultiple ()
192
176
{
193
- $ this -> travelTo (CarbonImmutable:: now ()->subYears ( $ years ) );
177
+ $ user = User:: factory ()->create ( );
194
178
195
- $ user = User::factory ()
196
- ->has (UsernameChangeHistory::factory ()->count (6 )) // 6 = max tier + 1
197
- ->create ();
179
+ $ this ->assertSame (0 , $ user ->usernameChangeCost ());
180
+
181
+ $ user ->usernameChangeHistory ()->create ([
182
+ 'timestamp ' => CarbonImmutable::now (),
183
+ 'type ' => 'paid ' ,
184
+ 'username ' => 'marty ' ,
185
+ ]);
198
186
199
- $ this ->travelBack ();
187
+ // 1 change in last 3 years
188
+ $ this ->travelTo (CarbonImmutable::now ()->addYears (3 ));
189
+ $ this ->assertSame (8 , $ user ->usernameChangeCost ());
200
190
201
- $ this ->assertSame ($ cost , $ user ->usernameChangeCost ());
191
+ // 0 changes in last 3 years
192
+ $ this ->travelTo (CarbonImmutable::now ()->addYears (1 ));
193
+ $ this ->assertSame (8 , $ user ->usernameChangeCost ());
194
+
195
+ $ user ->usernameChangeHistory ()->create ([
196
+ 'timestamp ' => CarbonImmutable::now (),
197
+ 'type ' => 'paid ' ,
198
+ 'username ' => 'mcfly ' ,
199
+ ]);
200
+
201
+ // 1 change in last 3 years
202
+ $ this ->assertSame (8 , $ user ->usernameChangeCost ());
203
+
204
+ $ user ->usernameChangeHistory ()->create ([
205
+ 'timestamp ' => CarbonImmutable::now (),
206
+ 'type ' => 'paid ' ,
207
+ 'username ' => 'futuremarty ' ,
208
+ ]);
209
+
210
+ // 2 changes in last 3 years
211
+ $ this ->assertSame (16 , $ user ->usernameChangeCost ());
212
+
213
+ // 1 changes in last 3 years
214
+ $ this ->travelTo (CarbonImmutable::now ()->addYears (3 ));
215
+ $ this ->assertSame (8 , $ user ->usernameChangeCost ());
216
+ // 0 changes in last 3 years
217
+ $ this ->travelTo (CarbonImmutable::now ()->addYears (1 ));
218
+ $ this ->assertSame (8 , $ user ->usernameChangeCost ());
202
219
}
203
220
204
221
/**
@@ -214,22 +231,25 @@ public function testUsernameChangeCostType(string $type, int $cost)
214
231
}
215
232
216
233
/**
217
- * This tests the correct last UsernameChangeHistory is used when applying the cost changes.
218
- *
219
- * @dataProvider dataProviderForUsernameChangeCostTypeLastChange
234
+ * @dataProvider dataProviderForUsernameChangeCostWindow
220
235
*/
221
- public function testUsernameChangeCostTypeLastChange ( string $ type , int $ cost )
236
+ public function testUsernameChangeCostWindow ( int $ years , int $ cost )
222
237
{
223
- $ this ->travelTo (CarbonImmutable::now ()->subYears (1 ));
224
-
225
- $ user = User::factory ()
226
- ->has (UsernameChangeHistory::factory ()->count (2 ))
227
- ->create ();
238
+ $ now = CarbonImmutable::now ();
239
+ $ this ->travelTo (CarbonImmutable::now ()->subYears (3 ));
228
240
229
- $ this ->travelBack ();
230
-
231
- UsernameChangeHistory::factory ()->state (['type ' => $ type , 'user_id ' => $ user ])->create ();
241
+ $ user = User::factory ()->create ();
242
+ while (CarbonImmutable::now ()->isBefore ($ now )) {
243
+ $ user ->usernameChangeHistory ()->create ([
244
+ 'timestamp ' => CarbonImmutable::now (),
245
+ 'type ' => 'paid ' ,
246
+ 'username ' => 'marty ' ,
247
+ ]);
248
+
249
+ $ this ->travelTo (CarbonImmutable::now ()->addMonths (6 ));
250
+ }
232
251
252
+ $ this ->travelTo ($ now ->addYears ($ years ));
233
253
$ this ->assertSame ($ cost , $ user ->usernameChangeCost ());
234
254
}
235
255
0 commit comments