Completed
Push — master ( 1b4132...cb7491 )
by Roni
01:15
created

Tests/Types/BnDateTimeTest.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the EasyBanglaDate package.
5
 *
6
 * Copyright (c) 2015 Roni Saha
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
13
namespace EasyBanglaDateTests\Types;
14
15
use EasyBanglaDateTests\Utils\CsvFileIterator;
16
use EasyBanglaDate\Types\BnDateTime;
17
18
class BnDateTimeTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function flagDataProvider()
21
    {
22
        return new CsvFileIterator(__DIR__ . '/../Resources/bn_flag_data.csv');
23
    }
24
25
    public function enFlagDataProvider()
26
    {
27
        return new CsvFileIterator(__DIR__ . '/../Resources/bn_flag_data_in_en.csv');
28
    }
29
30
    public function dataProviderForFlag_t()
31
    {
32
        return new CsvFileIterator(__DIR__ . '/../Resources/bn_flag_t_data.csv');
33
    }
34
35
    public function dataProviderEnSuffix() {
36
        $ret = array();
37
        $date = new \DateTime('2014-01-01');
38
        for($i=0; $i < 31; $i++) {
39
            $ret[] = array($i+1, $date->format('S'));
40
            $date->modify('+1 day');
41
        }
42
43
        return $ret;
44
    }
45
46
    /**
47
     * @dataProvider flagDataProvider
48
     * @param $time
49
     * @param $flag
50
     * @param $expected
51
     */
52
    public function testFormat($time, $flag, $expected)
53
    {
54
        $object = new BnDateTime($time, new \DateTimeZone('Asia/Dhaka'));
55
        $this->assertEquals($expected, $object->format($flag));
56
    }
57
58
    public function testFactoryShouldCreateBnDateTimeObjectFromString()
59
    {
60
        $timeStr = '2015-01-01 05:00:00';
61
        $time = BnDateTime::create($timeStr);
62
        $this->assertEquals(new BnDateTime($timeStr), $time);
63
    }
64
65
    public function testFactoryShouldReturnSameObjectIfBnDateObjectGiven()
66
    {
67
        $bnDateTime = new BnDateTime('now');
68
        $this->assertEquals(BnDateTime::create($bnDateTime), $bnDateTime);
0 ignored issues
show
$bnDateTime is of type object<EasyBanglaDate\Types\BnDateTime>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
    }
70
71 View Code Duplication
    public function testFactoryShouldReturnBnDateTimeObjectWithSameNativeDateTime()
72
    {
73
        $dateTime = new \DateTime('now');
74
        $bnDateTime = BnDateTime::create($dateTime);
0 ignored issues
show
$dateTime is of type object<DateTime>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
        $this->assertInstanceOf(BnDateTime::class, $bnDateTime);
76
        $this->assertEquals($bnDateTime->getTimestamp(), $dateTime->getTimestamp());
77
    }
78
79 View Code Duplication
    public function testFactoryShouldReturnBnDateTimeObjectFromTimeStamp()
80
    {
81
        $time = time();
82
        $bnDateTime = BnDateTime::create($time);
83
        $this->assertInstanceOf(BnDateTime::class, $bnDateTime);
84
        $this->assertEquals($bnDateTime->getTimestamp(), $time);
85
    }
86
87
    /**
88
     * @expectedException \Exception
89
     */
90
    public function testFactoryForInvalidInputShouldThrowException()
91
    {
92
        $invalidTimeString = 'invalid';
93
        $bnDateTime = BnDateTime::create($invalidTimeString);
94
        $this->assertInstanceOf(BnDateTime::class, $bnDateTime);
95
    }
96
97
    /**
98
     * @dataProvider enFlagDataProvider
99
     * @param $time
100
     * @param $flag
101
     * @param $expected
102
     */
103
    public function testEnFormat($time, $flag, $expected)
104
    {
105
        $object = new BnDateTime($time, new \DateTimeZone('Asia/Dhaka'));
106
        $this->assertEquals($expected, $object->enFormat($flag), "$time, $flag, $expected");
107
    }
108
109
    /**
110
     * @dataProvider dataProviderEnSuffix
111
     * @param $day
112
     * @param $suffix
113
     */
114
    public function testEnSuffix($day, $suffix) {
115
        $object = $this->createObject('now')->setDate('1422', 1, $day);
116
        $this->assertEquals($suffix, $object->enFormat('S'), "$day, $suffix");
117
    }
118
119
    /**
120
     * @dataProvider dataProviderForFlag_t
121
     * @param $time
122
     * @param $expected
123
     */
124
    public function testDayInMonth($time, $expected)
125
    {
126
        $object = new BnDateTime($time, new \DateTimeZone('Asia/Dhaka'));
127
        $this->assertEquals($expected, $object->format('t'));
128
    }
129
130
    public function testCustomMorningTest()
131
    {
132
        $object = $this->createObject("2015-01-01 05:00:00");
133
        $this->assertEquals("১৭", $object->format('d'));
134
        $object->setMorning(4);
135
        $this->assertEquals("১৮", $object->format('d'));
136
    }
137
138
    public function testBanglaDateSetting()
139
    {
140
        $object = $this->createObjectAndSetBanglaDate("2015-01-01 08:00:00", 1405,9,21);
141
        $this->assertEquals("২১-০৯-১৪০৫ ০৮:০০:০০", $object->format('d-m-Y H:i:s'));
142
    }
143
144
    public function testDateTimeObject()
145
    {
146
        $object = $this->createObjectAndSetBanglaDate("2015-01-01 08:00:00", 1421,1,1);
147
        $this->assertEquals("১৪-০৪-২০১৪ ০৮:০০:০০", $object->getDateTime()->format('d-m-Y H:i:s'));
148
    }
149
150
    protected function createObjectAndSetBanglaDate($time, $year, $month, $day)
151
    {
152
        return $this->createObject($time)->setDate($year, $month, $day);
153
    }
154
155
    /**
156
     * @param $time
157
     * @return BnDateTime
158
     */
159
    protected function createObject($time)
160
    {
161
        return new BnDateTime($time, new \DateTimeZone('Asia/Dhaka'));
162
    }
163
}
164