[icinga-checkins] icinga.org: icingaweb2-module-director/master: TimePeriodRangesTest: restructure test

git at icinga.org git at icinga.org
Thu Feb 25 18:56:50 CET 2016


Module: icingaweb2-module-director
Branch: master
Commit: f9c694d39d0b9bca63d11fde81aae402aa5db4cf
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=f9c694d39d0b9bca63d11fde81aae402aa5db4cf

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu Feb 25 18:56:39 2016 +0100

TimePeriodRangesTest: restructure test

---

 .../Director/Objects/TimePeriodRangesTest.php      |   77 ++++++++++----------
 1 file changed, 40 insertions(+), 37 deletions(-)

diff --git a/test/php/library/Director/Objects/TimePeriodRangesTest.php b/test/php/library/Director/Objects/TimePeriodRangesTest.php
index bf3666f..3b8dfd6 100644
--- a/test/php/library/Director/Objects/TimePeriodRangesTest.php
+++ b/test/php/library/Director/Objects/TimePeriodRangesTest.php
@@ -1,75 +1,78 @@
 <?php
 
+namespace Tests\Icinga\Modules\Director\Objects;
+
+use Icinga\Application\Config;
+use Icinga\Module\Director\Db;
 use Icinga\Module\Director\Objects\IcingaTimePeriodRange;
 use Icinga\Module\Director\Objects\IcingaTimePeriodRanges;
 use Icinga\Module\Director\Objects\IcingaTimePeriod;
-use Icinga\Test\BaseTestCase;
-use Icinga\Application\Config;
-use Icinga\Module\Director\Db;
-
-Icinga\Application\Config::$configDir = '/etc/icingaweb2';
+use Icinga\Module\Director\Test\BaseTestCase;
 
-/**
- * $ icingacli test php unit /usr/local/icingaweb-modules/director
- */
 class TimePeriodRangesTest extends BaseTestCase
 {
-    public function testFoo()
-    {
-        $this->assertEquals('foo', 'foo');
-    }
+    protected $testPeriodName = '___TEST___timerange';
 
     public function getDb()
     {
         $resourceName = Config::module('director')->get('db', 'resource');
         $db = Db::fromResourceName($resourceName);
-
         return $db;
     }
 
-    public function prepare()
+    public function testUpdate()
     {
-        $db = $this->getDb();
-        $object = IcingaTimePeriod::load(1, $db);
-        $ranges = $object->ranges();
+        $period = $this->createTestPeriod();
 
         $newRanges = array(
             'monday'    => '00:00-24:00',
-            'tuesday'   => '00:00-24:00',
+            'tuesday'   => '18:00-24:00',
             'wednesday' => '00:00-24:00',
         );
+        $period->ranges()->set($newRanges)->store();
 
-        $ranges->set($newRanges);
-        $ranges->store();
-
-        return $ranges;
+        $period = $this->loadTestPeriod();
+        $this->assertEquals(
+            '18:00-24:00',
+            $period->ranges()->get('tuesday')->timeperiod_value
+        );
     }
 
-    public function reload()
+    protected function createTestPeriod()
     {
         $db = $this->getDb();
-        $object = IcingaTimePeriod::load(1, $db);
+        $object = IcingaTimePeriod::create(
+            array(
+                'object_name' => $this->testPeriodName,
+                'object_type' => 'object'
+            ),
+            $db
+        );
+        $object->store();
         $ranges = $object->ranges();
 
-        return $ranges;
-    }
-
-    public function testUpdate()
-    {
-        $ranges = $this->prepare();
-
-        $newRanges = array(
+        $testRanges = array(
             'monday'    => '00:00-24:00',
-            'tuesday'   => '18:00-24:00',
+            'tuesday'   => '00:00-24:00',
             'wednesday' => '00:00-24:00',
         );
 
-        $ranges->set($newRanges);
+        $ranges->set($testRanges);
         $ranges->store();
 
-        $reloaded = $this->reload();
-        $newValue = $reloaded->get('tuesday')->timeperiod_value;
+        return $object;
+    }
+
+    protected function loadTestPeriod()
+    {
+        return IcingaTimePeriod::load($this->testPeriodName, $this->getDb());
+    }
 
-        $this->assertEquals('18:00-24:00', $newValue);
+    public function tearDown()
+    {
+        $db = $this->getDb();
+        if (IcingaTimePeriod::exists($this->testPeriodName, $db)) {
+            IcingaTimePeriod::load($this->testPeriodName, $db)->delete();
+        }
     }
 }



More information about the icinga-checkins mailing list