Skip to content

Commit 8abc825

Browse files
committed
chore: moved endpoints access to properties
1 parent d8eae65 commit 8abc825

12 files changed

+60
-79
lines changed

src/Endpoint/AbstractEndpoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class AbstractEndpoint
4343

4444
public function __construct(protected OpenWeatherMap $api)
4545
{
46-
$this->config = $this->api->getConfig();
46+
$this->config = $this->api->config;
4747

4848
$this->httpClientBuilder = $this->config->getHttpClientBuilder();
4949
$this->cache = $this->config->getCache();

src/OpenWeatherMap.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,19 @@
99

1010
class OpenWeatherMap
1111
{
12-
public function __construct(
13-
private readonly Config $config
14-
) {}
12+
public OneCallEndpoint $oneCall;
1513

16-
public function getConfig(): Config
17-
{
18-
return $this->config;
19-
}
14+
public WeatherEndpoint $weather;
2015

21-
public function getOneCall(): OneCallEndpoint
22-
{
23-
return new OneCallEndpoint($this);
24-
}
16+
public AirPollutionEndpoint $airPollution;
2517

26-
public function getWeather(): WeatherEndpoint
27-
{
28-
return new WeatherEndpoint($this);
29-
}
30-
31-
public function getAirPollution(): AirPollutionEndpoint
32-
{
33-
return new AirPollutionEndpoint($this);
34-
}
18+
public GeocodingEndpoint $geocoding;
3519

36-
public function getGeocoding(): GeocodingEndpoint
20+
public function __construct(public readonly Config $config)
3721
{
38-
return new GeocodingEndpoint($this);
22+
$this->oneCall = new OneCallEndpoint($this);
23+
$this->weather = new WeatherEndpoint($this);
24+
$this->airPollution = new AirPollutionEndpoint($this);
25+
$this->geocoding = new GeocodingEndpoint($this);
3926
}
4027
}

tests/AbstractEndpointTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testAbstractEndpointWithCache()
2020
$cache->expects($this->once())->method('save');
2121

2222
$api = $this->givenApi();
23-
$api->getConfig()->setCache($cache);
23+
$api->config->setCache($cache);
2424

2525
$this->mockSendRequest($api);
2626
}
@@ -35,7 +35,7 @@ public function testAbstractEndpointWithLogger()
3535
$logger->expects($this->atLeastOnce())->method('info');
3636

3737
$api = $this->givenApi();
38-
$api->getConfig()->setLogger($logger);
38+
$api->config->setLogger($logger);
3939

4040
$this->mockSendRequest($api);
4141
}

tests/AirPollutionEndpointTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public function testAirPollutionGetCurrent()
2525
)
2626
);
2727

28-
$response = $this->givenApi()->getAirPollution()->getCurrent(50, 50);
28+
$response = $this->givenApi()->airPollution->getCurrent(50, 50);
2929
$this->assertCurrentResponse($response);
3030
}
3131

3232
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
3333
public function testAirPollutionGetCurrentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
3434
{
3535
$this->expectException($expectedException);
36-
$this->givenApi()->getAirPollution()->getCurrent($latitude, $longitude);
36+
$this->givenApi()->airPollution->getCurrent($latitude, $longitude);
3737
}
3838

3939
// --- FORECAST ---
@@ -47,15 +47,15 @@ public function testAirPollutionGetForecast()
4747
)
4848
);
4949

50-
$response = $this->givenApi()->getAirPollution()->getForecast(50, 50);
50+
$response = $this->givenApi()->airPollution->getForecast(50, 50);
5151
$this->assertForecastResponse($response);
5252
}
5353

5454
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
5555
public function testAirPollutionGetForecastWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
5656
{
5757
$this->expectException($expectedException);
58-
$this->givenApi()->getAirPollution()->getForecast($latitude, $longitude);
58+
$this->givenApi()->airPollution->getForecast($latitude, $longitude);
5959
}
6060

6161
// --- HISTORY ---
@@ -71,7 +71,7 @@ public function testAirPollutionGetHistory()
7171

7272
$utcTimezone = new \DateTimeZone('UTC');
7373

74-
$response = $this->givenApi()->getAirPollution()->getHistory(
74+
$response = $this->givenApi()->airPollution->getHistory(
7575
50,
7676
50,
7777
new \DateTimeImmutable('-5 days', $utcTimezone),
@@ -88,7 +88,7 @@ public function testAirPollutionGetHistoryWithInvalidCoordinate(float $latitude,
8888
$startDate = new \DateTimeImmutable('-5 days');
8989
$endDate = new \DateTimeImmutable('-4 days');
9090

91-
$this->givenApi()->getAirPollution()->getHistory($latitude, $longitude, $startDate, $endDate);
91+
$this->givenApi()->airPollution->getHistory($latitude, $longitude, $startDate, $endDate);
9292
}
9393

9494
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidPastDateData')]
@@ -98,7 +98,7 @@ public function testAirPollutionGetHistoryWithInvalidPastStartDate(
9898
)
9999
{
100100
$this->expectException($expectedException);
101-
$this->givenApi()->getAirPollution()->getHistory(
101+
$this->givenApi()->airPollution->getHistory(
102102
50,
103103
50,
104104
$startDate,
@@ -113,7 +113,7 @@ public function testAirPollutionGetHistoryWithInvalidPastEndDate(
113113
)
114114
{
115115
$this->expectException($expectedException);
116-
$this->givenApi()->getAirPollution()->getHistory(
116+
$this->givenApi()->airPollution->getHistory(
117117
50,
118118
50,
119119
new \DateTimeImmutable('-5 days', new \DateTimeZone('UTC')),
@@ -129,7 +129,7 @@ public function testAirPollutionGetHistoryWithInvalidDateRange(
129129
)
130130
{
131131
$this->expectException($expectedException);
132-
$this->givenApi()->getAirPollution()->getHistory(50, 50, $startDate, $endDate);
132+
$this->givenApi()->airPollution->getHistory(50, 50, $startDate, $endDate);
133133
}
134134

135135
// --- ASSERT METHODS EXIST ---

tests/ApiErrorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testApiError(int $statusCode, string $expectedException)
2424
);
2525

2626
$this->expectException($expectedException);
27-
$this->givenApi()->getWeather()->getCurrent(38.7077507, -9.1365919);
27+
$this->givenApi()->weather->getCurrent(38.7077507, -9.1365919);
2828
}
2929

3030
public static function provideApiErrorData(): \Generator

tests/GeocodingEndpointTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public function testGeocodingGetByLocationName()
2525
)
2626
);
2727

28-
$response = $this->givenApi()->getGeocoding()->getByLocationName('lisbon, pt');
28+
$response = $this->givenApi()->geocoding->getByLocationName('lisbon, pt');
2929
$this->assertLocationListResponse($response);
3030
}
3131

3232
public function testGeocodingGetByLocationNameWithBlankValue()
3333
{
3434
$this->expectException(ValidationException::class);
35-
$this->givenApi()->getGeocoding()->getByLocationName('');
35+
$this->givenApi()->geocoding->getByLocationName('');
3636
}
3737

3838
// --- BY ZIP CODE ---
@@ -46,7 +46,7 @@ public function testGeocodingGetByZipCode()
4646
)
4747
);
4848

49-
$response = $this->givenApi()->getGeocoding()->getByZipCode('1000-001', 'pt');
49+
$response = $this->givenApi()->geocoding->getByZipCode('1000-001', 'pt');
5050
$this->assertInstanceOf(ZipCodeLocation::class, $response);
5151

5252
$this->assertSame('1000-001', $response->getZipCode());
@@ -63,7 +63,7 @@ public function testGeocodingGetByZipCode()
6363
public function testGeocodingGetByZipCodeWithInvalidValue(string $zipCode, string $countryCode)
6464
{
6565
$this->expectException(ValidationException::class);
66-
$this->givenApi()->getGeocoding()->getByZipCode($zipCode, $countryCode);
66+
$this->givenApi()->geocoding->getByZipCode($zipCode, $countryCode);
6767
}
6868

6969
public static function provideGeocodingGetByZipCodeWithInvalidValueData(): \Generator
@@ -84,7 +84,7 @@ public function testGeocodingGetByCoordinate()
8484
)
8585
);
8686

87-
$response = $this->givenApi()->getGeocoding()->getByCoordinate(50, 50);
87+
$response = $this->givenApi()->geocoding->getByCoordinate(50, 50);
8888
$this->assertLocationListResponse($response);
8989
}
9090

@@ -96,7 +96,7 @@ public function testGeocodingGetByCoordinateWithInvalidCoordinate(
9696
)
9797
{
9898
$this->expectException($expectedException);
99-
$this->givenApi()->getGeocoding()->getByCoordinate($latitude, $longitude);
99+
$this->givenApi()->geocoding->getByCoordinate($latitude, $longitude);
100100
}
101101

102102
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidNumResultsData')]
@@ -106,7 +106,7 @@ public function testGeocodingGetByCoordinateWithInvalidNumResults(
106106
)
107107
{
108108
$this->expectException($expectedException);
109-
$this->givenApi()->getGeocoding()->getByCoordinate(50, 50, $numResults);
109+
$this->givenApi()->geocoding->getByCoordinate(50, 50, $numResults);
110110
}
111111

112112
// --- ASSERT METHODS EXIST ---

tests/OneCallEndpointTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public function testOneCallGetWeather()
3333
)
3434
);
3535

36-
$response = $this->givenApi()->getOneCall()->getWeather(50, 50);
36+
$response = $this->givenApi()->oneCall->getWeather(50, 50);
3737
$this->assertWeatherResponse($response);
3838
}
3939

4040
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
4141
public function testOneCallGetWeatherWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
4242
{
4343
$this->expectException($expectedException);
44-
$this->givenApi()->getOneCall()->getWeather($latitude, $longitude);
44+
$this->givenApi()->oneCall->getWeather($latitude, $longitude);
4545
}
4646

4747
// --- HISTORY MOMENT ---
@@ -55,7 +55,7 @@ public function testOneCallGetHistoryMoment()
5555
)
5656
);
5757

58-
$response = $this->givenApi()->getOneCall()->getHistoryMoment(
58+
$response = $this->givenApi()->oneCall->getHistoryMoment(
5959
50,
6060
50,
6161
new \DateTimeImmutable('2023-01-01 00:00:00')
@@ -67,7 +67,7 @@ public function testOneCallGetHistoryMoment()
6767
public function testOneCallGetHistoryMomentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
6868
{
6969
$this->expectException($expectedException);
70-
$this->givenApi()->getOneCall()->getHistoryMoment(
70+
$this->givenApi()->oneCall->getHistoryMoment(
7171
$latitude,
7272
$longitude,
7373
new \DateTimeImmutable('2023-01-01 00:00:00')
@@ -78,7 +78,7 @@ public function testOneCallGetHistoryMomentWithInvalidCoordinate(float $latitude
7878
public function testOneCallGetHistoryMomentWithInvalidPastDate(\DateTimeImmutable $date, string $expectedException)
7979
{
8080
$this->expectException($expectedException);
81-
$this->givenApi()->getOneCall()->getHistoryMoment(50, 50, $date);
81+
$this->givenApi()->oneCall->getHistoryMoment(50, 50, $date);
8282
}
8383

8484
// --- HISTORY AGGREGATE ---
@@ -92,7 +92,7 @@ public function testOneCallGetHistoryAggregate()
9292
)
9393
);
9494

95-
$response = $this->givenApi()->getOneCall()->getHistoryAggregate(
95+
$response = $this->givenApi()->oneCall->getHistoryAggregate(
9696
50,
9797
50,
9898
new \DateTimeImmutable('2023-01-01')
@@ -104,7 +104,7 @@ public function testOneCallGetHistoryAggregate()
104104
public function testOneCallGetHistoryAggregateWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
105105
{
106106
$this->expectException($expectedException);
107-
$this->givenApi()->getOneCall()->getHistoryAggregate(
107+
$this->givenApi()->oneCall->getHistoryAggregate(
108108
$latitude,
109109
$longitude,
110110
new \DateTimeImmutable('2023-01-01')
@@ -115,7 +115,7 @@ public function testOneCallGetHistoryAggregateWithInvalidCoordinate(float $latit
115115
public function testOneCallGetHistoryAggregateWithInvalidPastDate(\DateTimeImmutable $date, string $expectedException)
116116
{
117117
$this->expectException($expectedException);
118-
$this->givenApi()->getOneCall()->getHistoryAggregate(50, 50, $date);
118+
$this->givenApi()->oneCall->getHistoryAggregate(50, 50, $date);
119119
}
120120

121121
// --- ASSERT METHODS EXIST ---

tests/OpenWeatherMapTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@
1010

1111
class OpenWeatherMapTest extends AbstractTest
1212
{
13-
public function testOpenWeatherMapGetConfig()
13+
public function testOpenWeatherMapConfig()
1414
{
15-
$this->assertInstanceOf(Config::class, $this->givenApi()->getConfig());
15+
$this->assertInstanceOf(Config::class, $this->givenApi()->config);
1616
}
1717

18-
public function testOpenWeatherMapGetOneCall()
18+
public function testOpenWeatherMapOneCall()
1919
{
20-
$this->assertInstanceOf(OneCallEndpoint::class, $this->givenApi()->getOneCall());
20+
$this->assertInstanceOf(OneCallEndpoint::class, $this->givenApi()->oneCall);
2121
}
2222

23-
public function testOpenWeatherMapGetWeather()
23+
public function testOpenWeatherMapWeather()
2424
{
25-
$this->assertInstanceOf(WeatherEndpoint::class, $this->givenApi()->getWeather());
25+
$this->assertInstanceOf(WeatherEndpoint::class, $this->givenApi()->weather);
2626
}
2727

28-
public function testOpenWeatherMapGetAirPollution()
28+
public function testOpenWeatherMapAirPollution()
2929
{
30-
$this->assertInstanceOf(AirPollutionEndpoint::class, $this->givenApi()->getAirPollution());
30+
$this->assertInstanceOf(AirPollutionEndpoint::class, $this->givenApi()->airPollution);
3131
}
3232

33-
public function testOpenWeatherMapGetGeocoding()
33+
public function testOpenWeatherMapGeocoding()
3434
{
35-
$this->assertInstanceOf(GeocodingEndpoint::class, $this->givenApi()->getGeocoding());
35+
$this->assertInstanceOf(GeocodingEndpoint::class, $this->givenApi()->geocoding);
3636
}
3737
}

tests/WeatherEndpointTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ public function testWeatherGetCurrent()
3232
)
3333
);
3434

35-
$response = $this->givenApi()->getWeather()->getCurrent(50, 50);
35+
$response = $this->givenApi()->weather->getCurrent(50, 50);
3636
$this->assertCurrentResponse($response);
3737
}
3838

3939
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
4040
public function testWeatherGetCurrentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
4141
{
4242
$this->expectException($expectedException);
43-
$this->givenApi()->getWeather()->getCurrent($latitude, $longitude);
43+
$this->givenApi()->weather->getCurrent($latitude, $longitude);
4444
}
4545

4646
// --- FORECAST ---
@@ -54,22 +54,22 @@ public function testWeatherGetForecast()
5454
)
5555
);
5656

57-
$response = $this->givenApi()->getWeather()->getForecast(50, 50, 1);
57+
$response = $this->givenApi()->weather->getForecast(50, 50, 1);
5858
$this->assertForecastResponse($response);
5959
}
6060

6161
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
6262
public function testWeatherGetForecastWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
6363
{
6464
$this->expectException($expectedException);
65-
$this->givenApi()->getWeather()->getForecast($latitude, $longitude, 10);
65+
$this->givenApi()->weather->getForecast($latitude, $longitude, 10);
6666
}
6767

6868
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidNumResultsData')]
6969
public function testWeatherGetForecastWithInvalidNumResults(int $numResults, string $expectedException)
7070
{
7171
$this->expectException($expectedException);
72-
$this->givenApi()->getWeather()->getForecast(50, 50, $numResults);
72+
$this->givenApi()->weather->getForecast(50, 50, $numResults);
7373
}
7474

7575
// --- ASSERT METHODS EXIST ---

tests/WithCacheTtl.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ public function testWithCacheTtl()
88
{
99
$this->assertSame(
1010
60 * 60,
11-
$this->givenApi()->getWeather()
12-
->withCacheTtl(60 * 60)
13-
->getCacheTtl()
11+
$this->givenApi()->weather->withCacheTtl(60 * 60)->getCacheTtl()
1412
);
1513
}
1614

1715
public function testWithCacheTtlGetCacheTtl()
1816
{
19-
$this->assertSame(60 * 10, $this->givenApi()->getWeather()->getCacheTtl());
17+
$this->assertSame(60 * 10, $this->givenApi()->weather->getCacheTtl());
2018
}
2119
}

0 commit comments

Comments
 (0)