assertSame($result, simple_template($template, $keys));
}
public function providerSimpleTemplate()
{
$return = array(
// One line php-style comments
array(
'
{{title}}
// just something interesting... #or ^not...',
array('title' => 'A Comedy of Errors'),
'A Comedy of Errors
'
),
// Multiline php-style comments
array(
'/**
* just something interesting... #or ^not...
*/
{{title}}
/**
* just something interesting... #or ^not...
*/',
array('title' => 'A Comedy of Errors'),
' A Comedy of Errors
'.PHP_EOL
),
// Var not exist
array(
'{{title}}
',
array('non_exist' => 'A Comedy of Errors'),
''
),
);
$templates_dir = dirname(__FILE__) . '/templates';
foreach (scandir($templates_dir) as $dir)
{
$json = $templates_dir.'/'.$dir.'/'.$dir.'.json';
if ($dir != '.' && $dir != '..' && is_dir($templates_dir.'/'.$dir) && is_file($json))
{
$template = $templates_dir.'/'.$dir.'/'.$dir.'.mustache';
$result = $templates_dir.'/'.$dir.'/'.$dir.'.txt';
$return[] = array(
file_get_contents($template),
json_decode(file_get_contents($json), TRUE),
file_get_contents($result)
);
}
}
return $return;
}
/**
* @dataProvider providerArrayToXML
* @group templates
*/
public function testArrayToXML($array, $result)
{
$xml = new SimpleXMLElement('');
array_to_xml($array, $xml);
$this->assertSame($result, $xml->asXML());
}
public function providerArrayToXML()
{
return array(
array(
// Array
array('entity' => 'sensor',
'name' => 'temp',
'message' => 'temp alarm',
'severity' => 'crit',
'suppress_recovery' => 0,
'delay' => 0,
'conditions_and' => 0,
'conditions' => array('sensor_value > 30',
'sensor_value < 1'),
'conditions_complex' => 'sensor_value > 30 OR sensor_value < 1',
'associations' => array(array('device' => array('hostname match timos*'),
'entity' => array('sensor_class is temperature')),
array('device' => array('hostname match cisco*'),
'entity' => array('sensor_class is temperature')))),
// XML
'
sensortemptemp alarmcrit000sensor_value > 30sensor_value < 1sensor_value > 30 OR sensor_value < 1hostname match timos*sensor_class is temperaturehostname match cisco*sensor_class is temperature
'
),
);
}
/**
* @dataProvider providerGenerateTemplate
* @group templates
*/
public function testGenerateTemplate($type, $array, $result)
{
$template = generate_template($type, $array);
$template = preg_replace('//', '', $template);
$this->assertSame($result, $template);
}
public function providerGenerateTemplate()
{
$alert_array = array(
'alert_test_id' => '4',
'entity_type' => 'sensor',
'alert_name' => 'temp',
'alert_message' => 'temp alarm',
'conditions' => array(
array('value' => '30', 'condition' => '>', 'metric' => 'sensor_value'),
array('value' => '1', 'condition' => '<', 'metric' => 'sensor_value')),
'and' => '0',
'severity' => 'crit',
'delay' => '0',
'alerter' => 'default',
'enable' => '1',
'show_frontpage' => '1',
'suppress_recovery' => '0',
'ignore_until' => NULL,
'associations' => array(
array('entity_type' => 'sensor',
'entity_attribs' => array(
array('value' => 'temperature',
'condition' => 'is',
'attrib' => 'sensor_class')),
'device_attribs' => array(
array('value' => 'timos*',
'condition' => 'match',
'attrib' => 'hostname'))),
array('entity_type' => 'sensor',
'entity_attribs' => array(
array('value' => 'temperature',
'condition' => 'is',
'attrib' => 'sensor_class')),
'device_attribs' => array(
array('value' => 'cisco*',
'condition' => 'match',
'attrib' => 'hostname')))),
);
$alert_array1 = array(
'alert_test_id' => '7',
'entity_type' => 'storage',
'alert_name' => 'stor_perc_85',
'alert_message' => 'Storage exceeds 85% of disk capacity',
'conditions' => array(
array('value' => '85', 'condition' => 'ge', 'metric' => 'storage_perc')),
'and' => '1',
'severity' => 'crit',
'delay' => '0',
'alerter' => 'default',
'enable' => '1',
'show_frontpage' => '1',
'suppress_recovery' => '0',
'ignore_until' => NULL,
'associations' => array(
array('entity_type' => 'storage',
'entity_attribs' => array(
array('value' => 'hrStorageFixedDisk',
'condition' => 'equals',
'metric' => 'storage_perc',
'attrib' => 'storage_type')),
'device_attribs' => array(
array ('value' => NULL,
'condition' => NULL,
'metric' => 'storage_perc',
'attrib' => '*')))),
);
return array(
array(
'alert',
// Array
$alert_array,
// XML
'
sensortemptemp alarmcrit000sensor_value > 30sensor_value < 1sensor_value > 30 OR sensor_value < 1hostname match timos*sensor_class is temperaturehostname match cisco*sensor_class is temperature
'
),
array(
'alert',
// Array
$alert_array1,
// XML
'
storagestor_perc_85Storage exceeds 85% of disk capacitycrit001storage_perc ge 85storage_perc ge 85*storage_type equals hrStorageFixedDisk
'
),
);
}
}
// EOF