|
161 | 161 | 'updated_at' => $date->toJSON() |
162 | 162 | ]); |
163 | 163 | }); |
| 164 | + |
| 165 | +test('a model with a timezone date cast can parse ISO-formatted values properly', function () { |
| 166 | + setupFacade(); |
| 167 | + |
| 168 | + Config::shouldReceive('get') |
| 169 | + ->with('app.timezone') |
| 170 | + ->andReturn('UTC'); |
| 171 | + |
| 172 | + $date = new Carbon('2022-12-15 09:00:00', 'UTC'); |
| 173 | + $model = fakeModelWithCast(); |
| 174 | + |
| 175 | + $model->test_at = $date->toIso8601String(); |
| 176 | + $model->updated_at = $date->toIso8601String(); |
| 177 | + |
| 178 | + expect($model->jsonSerialize()) |
| 179 | + ->toBe([ |
| 180 | + 'test_at' => $date->toJSON(), |
| 181 | + 'updated_at' => $date->toJSON() |
| 182 | + ]); |
| 183 | +}); |
| 184 | + |
| 185 | +test('a model with a timezone date cast can parse datetime values properly', function () { |
| 186 | + setupFacade(); |
| 187 | + |
| 188 | + Config::shouldReceive('get') |
| 189 | + ->with('app.timezone') |
| 190 | + ->andReturn('UTC'); |
| 191 | + |
| 192 | + $date = new DateTime('2022-12-15 09:00:00'); |
| 193 | + $model = fakeModelWithCast(); |
| 194 | + |
| 195 | + $model->test_at = $date; |
| 196 | + $model->updated_at = $date; |
| 197 | + |
| 198 | + expect($model->jsonSerialize()) |
| 199 | + ->toBe([ |
| 200 | + 'test_at' => '2022-12-15T09:00:00.000000Z', |
| 201 | + 'updated_at' => '2022-12-15T09:00:00.000000Z' |
| 202 | + ]); |
| 203 | +}); |
| 204 | + |
| 205 | +test('a model with a timezone date cast can parse datetime values with a defined timezone properly', function () { |
| 206 | + setupFacade(); |
| 207 | + |
| 208 | + Config::shouldReceive('get') |
| 209 | + ->with('app.timezone') |
| 210 | + ->andReturn('UTC'); |
| 211 | + |
| 212 | + $date = new DateTime('2022-12-15 09:00:00', new DateTimeZone('Asia/Taipei')); |
| 213 | + $model = fakeModelWithCast(); |
| 214 | + |
| 215 | + $model->test_at = $date; |
| 216 | + $model->updated_at = $date; |
| 217 | + |
| 218 | + expect($model->jsonSerialize()) |
| 219 | + ->toBe([ |
| 220 | + 'test_at' => '2022-12-15T01:00:00.000000Z', |
| 221 | + 'updated_at' => '2022-12-15T01:00:00.000000Z' |
| 222 | + ]); |
| 223 | +}); |
0 commit comments