Browse Source

cleanup tests

Kevin Pfeifer 3 years ago
parent
commit
f9e308a69a

+ 51 - 72
tests/TestCase/Database/Type/DateTimeTypeTest.php

@@ -46,6 +46,11 @@ class DateTimeTypeTest extends TestCase
     protected $_originalMap = [];
 
     /**
+     * @var string
+     */
+    protected $originalTimeZone;
+
+    /**
      * Setup
      */
     public function setUp(): void
@@ -59,6 +64,18 @@ class DateTimeTypeTest extends TestCase
             'src/I18n/Time.php',
             'tests/TestCase/Database/Type/DateTimeTypeTest.php',
         ]);
+        $this->originalTimeZone = date_default_timezone_get();
+    }
+
+    /**
+     * Reset timezone to its initial value
+     *
+     * @return void
+     */
+    protected function tearDown(): void
+    {
+        parent::tearDown();
+        date_default_timezone_set($this->originalTimeZone);
     }
 
     /**
@@ -203,78 +220,7 @@ class DateTimeTypeTest extends TestCase
             'src/I18n/Time.php',
         ]);
 
-        $data = $this->dateTimeArray();
-
-        Configure::delete('Error.ignoredDeprecationPaths');
-
-        return $data;
-    }
-
-    /**
-     * Data provider for marshal()
-     *
-     * @return array
-     */
-    public function marshalWithTimezoneProvider(): array
-    {
-        Configure::write('Error.ignoredDeprecationPaths', [
-            'src/I18n/Time.php',
-        ]);
-
-        $defaultTimezone = date_default_timezone_get();
-        date_default_timezone_set('Europe/Vienna');
-        $data = $this->dateTimeArray();
-        Configure::delete('Error.ignoredDeprecationPaths');
-        date_default_timezone_set($defaultTimezone);
-
-        return $data;
-    }
-
-    /**
-     * test marshalling data.
-     *
-     * @dataProvider marshalProvider
-     * @param mixed $value
-     * @param mixed $expected
-     */
-    public function testMarshal($value, $expected): void
-    {
-        $result = $this->type->marshal($value);
-        if (is_object($expected)) {
-            $this->assertEquals($expected, $result);
-        } else {
-            $this->assertSame($expected, $result);
-        }
-    }
-
-    /**
-     * test marshalling data with different timezone
-     *
-     * @dataProvider marshalWithTimezoneProvider
-     * @param mixed $value
-     * @param mixed $expected
-     */
-    public function testMarshalWithTimezone($value, $expected): void
-    {
-        $defaultTimezone = date_default_timezone_get();
-        date_default_timezone_set('Europe/Vienna');
-        $result = $this->type->marshal($value);
-        if (is_object($expected)) {
-            $this->assertEquals($expected, $result);
-        } else {
-            $this->assertSame($expected, $result);
-        }
-        date_default_timezone_set($defaultTimezone);
-    }
-
-    /**
-     * Helper method which is used by both marshalProvider and marshalWithTimezoneProvider
-     *
-     * @return array
-     */
-    protected function dateTimeArray(): array
-    {
-        return [
+        $data = [
             // invalid types.
             [null, null],
             [false, null],
@@ -360,6 +306,39 @@ class DateTimeTypeTest extends TestCase
                 Time::now(),
             ],
         ];
+
+        Configure::delete('Error.ignoredDeprecationPaths');
+
+        return $data;
+    }
+
+    /**
+     * test marshalling data.
+     *
+     * @dataProvider marshalProvider
+     * @param mixed $value
+     * @param mixed $expected
+     */
+    public function testMarshal($value, $expected): void
+    {
+        $result = $this->type->marshal($value);
+        if (is_object($expected)) {
+            $this->assertEquals($expected, $result);
+        } else {
+            $this->assertSame($expected, $result);
+        }
+    }
+
+    /**
+     * test marshalling data with different timezone
+     */
+    public function testMarshalWithTimezone(): void
+    {
+        date_default_timezone_set('Europe/Vienna');
+        $value = Time::now();
+        $expected = Time::now();
+        $result = $this->type->marshal($value);
+        $this->assertEquals($expected, $result);
     }
 
     /**

+ 15 - 42
tests/TestCase/Database/Type/DateTypeTest.php

@@ -40,6 +40,11 @@ class DateTypeTest extends TestCase
     protected $driver;
 
     /**
+     * @var string
+     */
+    protected $originalTimeZone;
+
+    /**
      * Setup
      */
     public function setUp(): void
@@ -54,6 +59,7 @@ class DateTypeTest extends TestCase
             'src/I18n/Time.php',
             'tests/TestCase/Database/Type/DateTypeTest.php',
         ]);
+        $this->originalTimeZone = date_default_timezone_get();
     }
 
     /**
@@ -63,6 +69,7 @@ class DateTypeTest extends TestCase
     {
         parent::tearDown();
         $this->type->useLocaleParser(false)->setLocaleFormat(null);
+        date_default_timezone_set($this->originalTimeZone);
     }
 
     /**
@@ -131,42 +138,8 @@ class DateTypeTest extends TestCase
         Configure::write('Error.ignoredDeprecationPaths', [
             'src/I18n/Date.php',
         ]);
-        $data = $this->dateArray();
-        Configure::delete('Error.ignoredDeprecationPaths');
-
-        return $data;
-    }
-
-    /**
-     * Data provider for marshalWithTimezone()
-     *
-     * @return array
-     */
-    public function marshalWithTimezoneProvider(): array
-    {
-        Configure::write('Error.ignoredDeprecationPaths', [
-            'src/I18n/Date.php',
-        ]);
-
-        $defaultTimezone = date_default_timezone_get();
-        date_default_timezone_set('Europe/Vienna');
-        $data = $this->dateArray();
-        Configure::delete('Error.ignoredDeprecationPaths');
-        date_default_timezone_set($defaultTimezone);
-
-        return $data;
-    }
-
-    /**
-     * Helper method which is used by both marshalProvider and marshalWithTimezoneProvider
-     *
-     * @return array
-     */
-    protected function dateArray(): array
-    {
         $date = new ChronosDate('@1392387900');
-
-        return [
+        $data = [
             // invalid types.
             [null, null],
             [false, null],
@@ -234,6 +207,9 @@ class DateTypeTest extends TestCase
                 new ChronosDate('2014-02-14'),
             ],
         ];
+        Configure::delete('Error.ignoredDeprecationPaths');
+
+        return $data;
     }
 
     /**
@@ -255,22 +231,19 @@ class DateTypeTest extends TestCase
 
     /**
      * test marshaling data with different timezone
-     *
-     * @dataProvider marshalWithTimezoneProvider
-     * @param mixed $value
-     * @param mixed $expected
      */
-    public function testMarshalWithTimezone($value, $expected): void
+    public function testMarshalWithTimezone(): void
     {
-        $defaultTimezone = date_default_timezone_get();
         date_default_timezone_set('Europe/Vienna');
+        $value = new FrozenDate('2023-04-26');
+        $expected = new ChronosDate('2023-04-26');
+
         $result = $this->type->marshal($value);
         $this->assertEquals($expected, $result);
 
         $this->type->useMutable();
         $result = $this->type->marshal($value);
         $this->assertEquals($expected, $result);
-        date_default_timezone_set($defaultTimezone);
     }
 
     /**