You are here

private function ParserTest::weatherGetForecastsFromDatabase in Weather 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Functional/ParserTest.php \Drupal\Tests\weather\Functional\ParserTest::weatherGetForecastsFromDatabase()

Try to fetch forecasts from the database.

Parameters

string $geoid: GeoID of the place for which the weather is desired.

string $utc_offset: UTC offset of place in minutes.

int $days: Return weather for specified number of days (0 = all available days).

bool $detailed: Return detailed forecasts or just one forecast per day.

int $time: Timestamp for which the weather should be returned. This is only needed to enable proper testing of the module.

Return value

array Weather array with forecast information.

1 call to ParserTest::weatherGetForecastsFromDatabase()
ParserTest::weatherGetWeather in tests/src/Functional/ParserTest.php
Returns a weather object for the specified GeoID.

File

tests/src/Functional/ParserTest.php, line 50

Class

ParserTest
Tests parsing of XML weather forecasts.

Namespace

Drupal\Tests\weather\Functional

Code

private function weatherGetForecastsFromDatabase($geoid, $utc_offset, $days, $detailed, $time) {

  // Fetch the first forecast. This must be done separately, because
  // sometimes the first forecast is already on the next day (this can
  // happen e.g. late in the evenings). Otherwise, the calculation of
  // 'tomorrow' would fail.
  $current_local_time = gmdate('Y-m-d H:i:s', $time + $utc_offset * 60);
  $connection = \Drupal::database();
  $query = $connection
    ->select('weather_forecast', 'wfi');
  $query
    ->condition('wfi.geoid', $geoid, '=');
  $query
    ->condition('wfi.time_to', $current_local_time, '>=');
  $query
    ->fields('wfi', [
    'geoid',
    'time_from',
    'time_to',
    'period',
    'symbol',
    'precipitation',
    'wind_direction',
    'wind_speed',
    'temperature',
    'pressure',
  ]);
  $query
    ->range(0, 50);
  $result = $query
    ->execute();
  $first_forecast = $result
    ->fetchAll();

  // If there are no forecasts available, return an empty array.
  if ($first_forecast === FALSE) {
    return [];
  }
  $weather = $this
    ->weatherCreateWeatherArray([
    $first_forecast,
  ]);

  // Calculate tomorrow based on result.
  $first_forecast_day = explode('-', key($weather));
  $tomorrow_local_time = gmdate('Y-m-d H:i:s', gmmktime(0, 0, 0, $first_forecast_day[1], $first_forecast_day[2] + 1, $first_forecast_day[0]));
  $forecasts_until_local_time = gmdate('Y-m-d 23:59:59', gmmktime(23, 59, 59, $first_forecast_day[1], $first_forecast_day[2] + $days - 1, $first_forecast_day[0]));
  if ($detailed) {

    // Fetch all available forecasts.
    if ($days > 0) {
      $query = $connection
        ->select('weather_forecast', 'wfi');
      $query
        ->condition('wfi.geoid', $geoid, '=');
      $query
        ->condition('wfi.time_to', $forecasts_until_local_time, '>=');
      $query
        ->fields('wfi', [
        'geoid',
        'time_from',
        'time_to',
        'period',
        'symbol',
        'precipitation',
        'wind_direction',
        'wind_speed',
        'temperature',
        'pressure',
      ]);
      $query
        ->range(0, 50);
      $result = $query
        ->execute();
      $forecasts = $result
        ->fetchAll();
    }
    else {
      $query = $connection
        ->select('weather_forecast', 'wfi');
      $query
        ->condition('wfi.geoid', $geoid, '=');
      $query
        ->condition('wfi.time_to', $current_local_time, '>=');
      $query
        ->fields('wfi', [
        'geoid',
        'time_from',
        'time_to',
        'period',
        'symbol',
        'precipitation',
        'wind_direction',
        'wind_speed',
        'temperature',
        'pressure',
      ]);
      $query
        ->range(0, 50);
      $result = $query
        ->execute();
      $forecasts = $result
        ->fetchAll();
    }
    $weather = $this
      ->weatherCreateWeatherArray($forecasts);
  }
  else {
    if ($days > 1) {
      $query = $connection
        ->select('weather_forecast', 'wfi');
      $query
        ->condition('wfi.geoid', $geoid, '=');
      $query
        ->condition('wfi.time_from', $tomorrow_local_time, '>=');
      $query
        ->condition('wfi.time_to', $forecasts_until_local_time, '>=');
      $query
        ->fields('wfi', [
        'geoid',
        'time_from',
        'time_to',
        'period',
        'symbol',
        'precipitation',
        'wind_direction',
        'wind_speed',
        'temperature',
        'pressure',
      ]);
      $query
        ->range(0, 50);
      $result = $query
        ->execute();
      $forecasts = $result
        ->fetchAll();
      $weather = array_merge($weather, $this
        ->weatherCreateWeatherArray($forecasts));
    }
    elseif ($days == 0) {
      $query = $connection
        ->select('weather_forecast', 'wfi');
      $query
        ->condition('wfi.geoid', $geoid, '=');
      $query
        ->condition('wfi.time_from', $tomorrow_local_time, '>=');
      $query
        ->fields('wfi', [
        'geoid',
        'time_from',
        'time_to',
        'period',
        'symbol',
        'precipitation',
        'wind_direction',
        'wind_speed',
        'temperature',
        'pressure',
      ]);
      $query
        ->range(0, 50);
      $result = $query
        ->execute();
      $forecasts = $result
        ->fetchAll();
      $weather = array_merge($weather, $this
        ->weatherCreateWeatherArray($forecasts));
    }
  }
  return $weather;
}