|
3 | 3 | namespace ProgrammatorDev\OpenWeatherMap\Test\Unit\Resource; |
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\Attributes\DataProvider; |
| 6 | +use ProgrammatorDev\OpenWeatherMap\Entity\Stations\MeasurementAggregate; |
6 | 7 | use ProgrammatorDev\OpenWeatherMap\Entity\Stations\Station; |
| 8 | +use ProgrammatorDev\OpenWeatherMap\Enum\AggregationInterval; |
7 | 9 | use ProgrammatorDev\OpenWeatherMap\Request\Stations\CloudLayer; |
8 | 10 | use ProgrammatorDev\OpenWeatherMap\Request\Stations\Measurement; |
9 | 11 | use ProgrammatorDev\OpenWeatherMap\Request\Stations\Weather; |
@@ -305,6 +307,100 @@ public function testRejectsAnInvalidMeasurementBatchItem(): void |
305 | 307 | ]); |
306 | 308 | } |
307 | 309 |
|
| 310 | + public function testAggregatesMeasurements(): void |
| 311 | + { |
| 312 | + $this->respondWithFixture( |
| 313 | + 'stations/measurements/aggregate-hour-success.json', |
| 314 | + ); |
| 315 | + |
| 316 | + $aggregates = $this->api->stations()->measurements( |
| 317 | + stationId: ' 6a77ba36adde3b0001343e09 ', |
| 318 | + interval: AggregationInterval::HOUR, |
| 319 | + startAt: new \DateTimeImmutable('@1786231349'), |
| 320 | + endAt: new \DateTimeImmutable('@1786345863'), |
| 321 | + limit: 100, |
| 322 | + ); |
| 323 | + $request = $this->client->getLastRequest(); |
| 324 | + |
| 325 | + self::assertCount(1, $aggregates); |
| 326 | + self::assertContainsOnlyInstancesOf(MeasurementAggregate::class, $aggregates); |
| 327 | + self::assertSame(AggregationInterval::HOUR, $aggregates[0]->interval()); |
| 328 | + self::assertSame(20.5, $aggregates[0]->temperature()?->average()); |
| 329 | + self::assertSame(0.6, $aggregates[0]->precipitation()?->rain()); |
| 330 | + self::assertSame('GET', $request->getMethod()); |
| 331 | + self::assertSame('/data/3.0/measurements', $request->getUri()->getPath()); |
| 332 | + self::assertSame([ |
| 333 | + 'station_id' => '6a77ba36adde3b0001343e09', |
| 334 | + 'type' => 'h', |
| 335 | + 'limit' => '100', |
| 336 | + 'from' => '1786231349', |
| 337 | + 'to' => '1786345863', |
| 338 | + 'appid' => 'api-key', |
| 339 | + ], $this->query($request)); |
| 340 | + } |
| 341 | + |
| 342 | + public function testReturnsAnEmptyMeasurementAggregateCollection(): void |
| 343 | + { |
| 344 | + $this->respondWithFixture( |
| 345 | + 'stations/measurements/aggregate-minute-empty.json', |
| 346 | + ); |
| 347 | + |
| 348 | + $aggregates = $this->api->stations()->measurements( |
| 349 | + stationId: 'station-id', |
| 350 | + interval: AggregationInterval::MINUTE, |
| 351 | + startAt: new \DateTimeImmutable('@1786143599'), |
| 352 | + endAt: new \DateTimeImmutable('@1786230780'), |
| 353 | + limit: 10, |
| 354 | + ); |
| 355 | + |
| 356 | + self::assertSame([], $aggregates); |
| 357 | + } |
| 358 | + |
| 359 | + #[DataProvider('invalidAggregationArguments')] |
| 360 | + public function testRejectsInvalidAggregationArguments( |
| 361 | + string $stationId, |
| 362 | + \DateTimeInterface $startAt, |
| 363 | + \DateTimeInterface $endAt, |
| 364 | + int $limit, |
| 365 | + string $message, |
| 366 | + ): void { |
| 367 | + $this->expectException(\InvalidArgumentException::class); |
| 368 | + $this->expectExceptionMessage($message); |
| 369 | + |
| 370 | + $this->api->stations()->measurements( |
| 371 | + $stationId, |
| 372 | + AggregationInterval::HOUR, |
| 373 | + $startAt, |
| 374 | + $endAt, |
| 375 | + $limit, |
| 376 | + ); |
| 377 | + } |
| 378 | + |
| 379 | + public static function invalidAggregationArguments(): iterable |
| 380 | + { |
| 381 | + yield 'blank station identifier' => [ |
| 382 | + ' ', |
| 383 | + new \DateTimeImmutable('@100'), |
| 384 | + new \DateTimeImmutable('@200'), |
| 385 | + 1, |
| 386 | + 'The station ID must be a non-empty string.', |
| 387 | + ]; |
| 388 | + yield 'reversed date range' => [ |
| 389 | + 'station-id', |
| 390 | + new \DateTimeImmutable('@200'), |
| 391 | + new \DateTimeImmutable('@100'), |
| 392 | + 1, |
| 393 | + 'The end date must be after or equal to the start date.', |
| 394 | + ]; |
| 395 | + yield 'non-positive result limit' => [ |
| 396 | + 'station-id', |
| 397 | + new \DateTimeImmutable('@100'), |
| 398 | + new \DateTimeImmutable('@200'), |
| 399 | + 0, |
| 400 | + 'The result limit must be at least 1.', |
| 401 | + ]; |
| 402 | + } |
| 403 | + |
308 | 404 | #[DataProvider('invalidCreationArguments')] |
309 | 405 | public function testRejectsInvalidCreationArguments( |
310 | 406 | string $externalId, |
|
0 commit comments