testNewDateTimeZone()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2021 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Tests\Form\DataTransformer;
13
14
use DateTime;
15
use DateTimeZone;
16
use Throwable;
17
use WBW\Bundle\CoreBundle\Tests\AbstractTestCase;
18
use WBW\Bundle\CoreBundle\Tests\Fixtures\Form\DataTransformer\TestDateTimeDataTransformer;
19
20
/**
21
 * Abstract date/time data transformer test.
22
 *
23
 * @author webeweb <https://github.com/webeweb>
24
 * @package WBW\Bundle\CoreBundle\Tests\Form\DataTransformer
25
 */
26
class AbstractDateTimeDataTransformerTest extends AbstractTestCase {
27
28
    /**
29
     * Test newDateTimeZone()
30
     *
31
     * @return void
32
     */
33
    public function testNewDateTimeZone(): void {
34
35
        $obj = new TestDateTimeDataTransformer("Y-m-d H:i:s", "UTC");
36
37
        $res = $obj->newDateTimeZone();
38
        $this->assertEquals("UTC", $res->getName());
39
    }
40
41
    /**
42
     * Test newDateTimeZone()
43
     *
44
     * @return void
45
     */
46
    public function testNewDateTimeZoneWithoutTimeZone(): void {
47
48
        $obj = new TestDateTimeDataTransformer("Y-m-d H:i:s", null);
49
50
        $this->assertNull($obj->newDateTimeZone());
51
    }
52
53
    /**
54
     * Test reverseTransform()
55
     *
56
     * @return void
57
     * @throws Throwable Throws an exception if an error occurs.
58
     */
59
    public function testReverseTransform(): void {
60
61
        // Set a date/time mock.
62
        $dateTime = new DateTime("2021-03-23 19:00:00", new DateTimeZone("UTC"));
63
64
        $obj = new TestDateTimeDataTransformer("Y-m-d H:i:s", "UTC");
65
66
        $fmt = $obj->getFormat();
67
        $arg = $dateTime->format($fmt);
68
69
        $this->assertEquals(null, $obj->reverseTransform(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->reverseTransform(null) targeting WBW\Bundle\CoreBundle\Fo...mer::reverseTransform() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
70
        $this->assertEquals(null, $obj->reverseTransform(""));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $obj->reverseTransform('') targeting WBW\Bundle\CoreBundle\Fo...mer::reverseTransform() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
        $this->assertEquals(null, $obj->reverseTransform("exception"));
72
        $this->assertEquals($dateTime, $obj->reverseTransform($arg));
73
    }
74
75
    /**
76
     * Test setFormat()
77
     *
78
     * @return void
79
     */
80
    public function testSetFormat(): void {
81
82
        $obj = new TestDateTimeDataTransformer("Y-m-d H:i:s", "UTC");
83
84
        $obj->setFormat("format");
85
        $this->assertEquals("format", $obj->getFormat());
86
    }
87
88
    /**
89
     * Test setTimezone()
90
     *
91
     * @return void
92
     */
93
    public function testSetTimezone(): void {
94
95
        $obj = new TestDateTimeDataTransformer("Y-m-d H:i:s", "UTC");
96
97
        $obj->setTimezone("timezone");
98
        $this->assertEquals("timezone", $obj->getTimezone());
99
    }
100
101
    /**
102
     * Test transform()
103
     *
104
     * @return void
105
     * @throws Throwable Throws an exception if an error occurs.
106
     */
107
    public function testTransform(): void {
108
109
        // Set a date/time mock.
110
        $dateTime = new DateTime("2021-03-23 19:00:00", new DateTimeZone("UTC"));
111
112
        $obj = new TestDateTimeDataTransformer("Y-m-d H:i:s", "UTC");
113
114
        $fmt = $obj->getFormat();
115
        $exp = $dateTime->format($fmt);
116
117
        $this->assertEquals(null, $obj->transform(null));
118
        $this->assertEquals($exp, $obj->transform($dateTime));
119
    }
120
121
    /**
122
     * Test __construct()
123
     *
124
     * @return void
125
     */
126
    public function test__construct(): void {
127
128
        $obj = new TestDateTimeDataTransformer("Y-m-d H:i:s", "UTC");
129
130
        $this->assertEquals("Y-m-d H:i:s", $obj->getFormat());
131
        $this->assertEquals("UTC", $obj->getTimezone());
132
    }
133
}
134