public function testRetrievesTemperatureFromApiIfNotFoundInCache() { $city = 'Miami, FL'; $expectedTemperature = '90F'; $expectedCacheKey = md5($city); $temperatureData = ['temperature' => $expectedTemperature]; $this->cache->shouldReceive('get')->once()->with($expectedCacheKey)->andReturn(null); $this->cache->shouldReceive('set')->once()->with($expectedCacheKey, $temperatureData, 900); $this->httpClient->shouldReceive('get')->once()->with('https://some-weather-api.com/temperature/' . urlencode($city))->andReturn(new Response(200, [], Stream::factory(json_encode($temperatureData)))); $this->assertSame($expectedTemperature, $this->weatherService->getTemperature($city)); }
public function testRetrievesTemperatureFromApiIfNotFoundInCache() { $expectedTemperature = '90F'; $expectedCacheKey = md5('Miami, FL'); $temperatureData = ['temperature' => '90F']; // Use Mockery for our mocks this time. $cache = m::mock('Memcached'); $cache->shouldReceive('get')->once()->with($expectedCacheKey)->andReturn(null); $cache->shouldReceive('set')->once()->with($expectedCacheKey, $temperatureData, 900); $httpClient = m::mock('GuzzleHttp\\Client'); $httpClient->shouldReceive('get')->once()->with('https://some-weather-api.com/temperature/' . urlencode('Miami, FL'))->andReturn(new Response(200, [], Stream::factory(json_encode($temperatureData)))); $weatherService = new WeatherService($cache, $httpClient); $currentTemperature = $weatherService->getTemperature('Miami, FL'); $this->assertSame($expectedTemperature, $currentTemperature); }