IcingaTimePeriod: Update test for include/exclude

This commit is contained in:
Markus Frosch 2018-09-13 14:58:21 +02:00
parent c86c168e43
commit 282d78c877
1 changed files with 82 additions and 11 deletions

View File

@ -9,6 +9,8 @@ class IcingaTimePeriodTest extends BaseTestCase
{
protected $testPeriodName = '___TEST___timeperiod';
protected $createdNames = [];
public function testWhetherUpdatedTimeperiodsAreCorrectlyStored()
{
if ($this->skipForMissingDb()) {
@ -54,12 +56,15 @@ class IcingaTimePeriodTest extends BaseTestCase
);
}
protected function createTestPeriod()
protected function createTestPeriod($suffix = '', $testRanges = [])
{
$db = $this->getDb();
$name = $this->testPeriodName . $suffix;
$this->createdNames[] = $name;
$object = IcingaTimePeriod::create(
array(
'object_name' => $this->testPeriodName,
'object_name' => $name,
'object_type' => 'object'
),
$db
@ -67,11 +72,13 @@ class IcingaTimePeriodTest extends BaseTestCase
$object->store();
$ranges = $object->ranges();
$testRanges = array(
'monday' => '00:00-24:00',
'tuesday' => '00:00-24:00',
'wednesday' => '00:00-24:00',
);
if (empty($testRanges)) {
$testRanges = array(
'monday' => '00:00-24:00',
'tuesday' => '00:00-24:00',
'wednesday' => '00:00-24:00',
);
}
$ranges->set($testRanges);
$object->store();
@ -98,16 +105,80 @@ class IcingaTimePeriodTest extends BaseTestCase
$this->assertFalse($period->isActive(strtotime('2016-05-19 10:00:00')));
}
protected function loadTestPeriod()
public function testPeriodWithIncludes()
{
return IcingaTimePeriod::load($this->testPeriodName, $this->getDb());
$period = $this->createTestPeriod();
$include = $this->createTestPeriod('include', ['thursday' => '00:00-24:00']);
$period->set('includes', $include->object_name);
$period->store();
// Wednesday:
$this->assertTrue($period->isActive(strtotime('2016-05-18 10:00:00')));
// Thursday:
$this->assertTrue($period->isActive(strtotime('2016-05-19 10:00:00')));
}
public function testPeriodWithExcludes()
{
$period = $this->createTestPeriod();
$exclude = $this->createTestPeriod('exclude', ['wednesday' => '00:00-24:00']);
$period->set('excludes', $exclude->object_name);
$period->store();
// Wednesday:
$this->assertFalse($period->isActive(strtotime('2016-05-18 10:00:00')));
// Thursday:
$this->assertFalse($period->isActive(strtotime('2016-05-19 10:00:00')));
}
public function testPeriodPreferingIncludes()
{
$period = $this->createTestPeriod();
$include = $this->createTestPeriod('include', ['thursday' => '00:00-24:00']);
$exclude = $this->createTestPeriod('exclude', ['thursday' => '00:00-24:00']);
$period->set('includes', $include->object_name);
$period->set('excludes', $exclude->object_name);
$period->store();
// Wednesday:
$this->assertTrue($period->isActive(strtotime('2016-05-18 10:00:00')));
// Thursday:
$this->assertTrue($period->isActive(strtotime('2016-05-19 10:00:00')));
}
public function testPeriodPreferingExcludes()
{
$period = $this->createTestPeriod();
$include = $this->createTestPeriod('include', ['thursday' => '00:00-24:00']);
$exclude = $this->createTestPeriod('exclude', ['thursday' => '00:00-24:00']);
$period->set('prefer_includes', false);
$period->set('includes', $include->object_name);
$period->set('excludes', $exclude->object_name);
$period->store();
// Wednesday:
$this->assertTrue($period->isActive(strtotime('2016-05-18 10:00:00')));
// Thursday:
$this->assertFalse($period->isActive(strtotime('2016-05-19 10:00:00')));
}
protected function loadTestPeriod($suffix = '')
{
return IcingaTimePeriod::load($this->testPeriodName . $suffix, $this->getDb());
}
public function tearDown()
{
$db = $this->getDb();
if (IcingaTimePeriod::exists($this->testPeriodName, $db)) {
IcingaTimePeriod::load($this->testPeriodName, $db)->delete();
foreach ($this->createdNames as $name) {
if (IcingaTimePeriod::exists($name, $db)) {
IcingaTimePeriod::load($name, $db)->delete();
}
}
}
}