public function WeatherCommonTestTrait::weatherFillWeatherSchema in Weather 8
Same name and namespace in other branches
- 2.0.x tests/src/Functional/WeatherCommonTestTrait.php \Drupal\Tests\weather\Functional\WeatherCommonTestTrait::weatherFillWeatherSchema()
Provides functionality for filling database tables from source XML file.
Parameters
string $source: Source filename.
Throws
\ReflectionException
3 calls to WeatherCommonTestTrait::weatherFillWeatherSchema()
- ConfigurationTest::testConfiguration in tests/
src/ Functional/ ConfigurationTest.php - Tests configuration of weather block.
- FunctionsTest::testFunctionWeatherGetLinkForGeoId in tests/
src/ Functional/ FunctionsTest.php - Test _weather_get_link_for_geoid().
- PermissionsTest::testPermissions in tests/
src/ Functional/ PermissionsTest.php - Permissions of weather block.
File
- tests/
src/ Functional/ WeatherCommonTestTrait.php, line 21
Class
- WeatherCommonTestTrait
- Provides a helper method for testing Weather module.
Namespace
Drupal\Tests\weather\FunctionalCode
public function weatherFillWeatherSchema($source = 'geonames_2911298.xml') {
// Fetch forecast data from xml file.
$xml_source = dirname((new ReflectionClass(static::class))
->getFileName()) . '/data/' . $source;
$xml_source_stream_content = stream_get_contents(fopen($xml_source, 'rb'));
$element_data = new \SimpleXMLElement($xml_source_stream_content);
// Prepare data for DB compatible format.
$geobase = $element_data->location->location
->attributes()->{'geobase'};
$geobase_id = $element_data->location->location
->attributes()->{'geobaseid'};
$latitude = $element_data->location->location
->attributes()->{'latitude'};
$longitude = $element_data->location->location
->attributes()->{'longitude'};
$name = $element_data->location->name;
$country = $element_data->location->country;
$last_update = $element_data->meta->lastupdate;
$next_update = $element_data->meta->nextupdate;
if ($source !== 'geonames_2911298.xml') {
$link = 'Kiev/Kyiv';
}
else {
$link = 'Hamburg/Hamburg';
}
// Get forecast from XML source.
$forecasts = $element_data->forecast->tabular;
$forecats_list = [];
foreach ($forecasts->time as $key => $forecast) {
$from = $forecast
->attributes()->{'from'};
$to = $forecast
->attributes()->{'to'};
$symbol = (string) $forecast->symbol
->attributes()->{'var'};
// Remove moon phases, which are not supported.
// This is in the format "mf/03n.56", where 56 would be the
// percentage of the moon phase.
if (strlen($symbol) > 3) {
$symbol = substr($symbol, 3, 3);
}
$period = $forecast
->attributes()->{'period'};
$precipitation = $forecast->precipitation
->attributes()->{'value'};
$wind_direction = $forecast->windDirection
->attributes()->{'deg'};
$wind_speed = $forecast->windSpeed
->attributes()->{'mps'};
$temperature = $forecast->temperature
->attributes()->{'value'};
$pressure = $forecast->pressure
->attributes()->{'value'};
$forecats_list[] = [
'time_from' => (string) $from,
'time_to' => (string) $to,
'period' => (string) $period,
'symbol' => $symbol,
'precipitation' => (string) $precipitation,
'wind_direction' => (string) $wind_direction,
'wind_speed' => (string) $wind_speed,
'temperature' => (string) $temperature,
'pressure' => (string) $pressure,
];
}
// Fill places table from XML source.
$connection = \Drupal::database();
$query = $connection
->insert('weather_place');
$query
->fields([
'geoid' => (string) $geobase . '_' . (string) $geobase_id,
'latitude' => (string) $latitude,
'longitude' => (string) $longitude,
'name' => (string) $name,
'country' => (string) $country,
'link' => $link,
'status' => WeatherPlaceInterface::STATUS_ORIGINAL,
]);
$query
->execute();
// Fill forecast table from XML source.
foreach ($forecats_list as $forecast_item) {
$query = $connection
->insert('weather_forecast');
$query
->fields([
'id' => 0,
'geoid' => (string) $geobase . '_' . (string) $geobase_id,
'time_from' => $forecast_item['time_from'],
'time_to' => $forecast_item['time_to'],
'period' => $forecast_item['period'],
'symbol' => $forecast_item['symbol'],
'precipitation' => $forecast_item['precipitation'],
'wind_direction' => $forecast_item['wind_direction'],
'wind_speed' => $forecast_item['wind_speed'],
'temperature' => $forecast_item['temperature'],
'pressure' => $forecast_item['pressure'],
]);
$query
->execute();
}
// Fill forecast_information table from XML source.
$query = $connection
->insert('weather_forecast_information');
$query
->fields([
'geoid' => (string) $geobase . '_' . (string) $geobase_id,
'last_update' => (string) $last_update,
'next_update' => (string) $next_update,
'next_download_attempt' => (string) $next_update,
'utc_offset' => '180',
]);
$query
->execute();
}