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
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
class BnDateTimeTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function flagDataProvider() |
22
|
|
|
{ |
23
|
|
|
return new CsvFileIterator(__DIR__ . '/../Resources/bn_flag_data.csv'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function enFlagDataProvider() |
27
|
|
|
{ |
28
|
|
|
return new CsvFileIterator(__DIR__ . '/../Resources/bn_flag_data_in_en.csv'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function dataProviderForFlag_t() |
32
|
|
|
{ |
33
|
|
|
return new CsvFileIterator(__DIR__ . '/../Resources/bn_flag_t_data.csv'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function dataProviderEnSuffix() { |
37
|
|
|
$ret = array(); |
38
|
|
|
$date = new \DateTime('2014-01-01'); |
39
|
|
|
for($i=0; $i < 31; $i++) { |
40
|
|
|
$ret[] = array($i+1, $date->format('S')); |
41
|
|
|
$date->modify('+1 day'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $ret; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @dataProvider flagDataProvider |
49
|
|
|
* @param $time |
50
|
|
|
* @param $flag |
51
|
|
|
* @param $expected |
52
|
|
|
*/ |
53
|
|
|
public function testFormat($time, $flag, $expected) |
54
|
|
|
{ |
55
|
|
|
$object = new BnDateTime($time, new \DateTimeZone('Asia/Dhaka')); |
56
|
|
|
$this->assertEquals($expected, $object->format($flag)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testFactoryShouldReturnNullForNullValue() |
60
|
|
|
{ |
61
|
|
|
$time = BnDateTime::create(null); |
62
|
|
|
$this->assertNull($time); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testFactoryShouldCreateBnDateTimeObjectFromString() |
66
|
|
|
{ |
67
|
|
|
$timeStr = '2015-01-01 05:00:00'; |
68
|
|
|
$time = BnDateTime::create($timeStr); |
69
|
|
|
$this->assertEquals(new BnDateTime($timeStr), $time); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testFactoryShouldReturnSameObjectIfBnDateObjectGiven() |
73
|
|
|
{ |
74
|
|
|
$bnDateTime = new BnDateTime('now'); |
75
|
|
|
$this->assertEquals(BnDateTime::create($bnDateTime), $bnDateTime); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function testFactoryShouldReturnBnDateTimeObjectWithSameNativeDateTime() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$dateTime = new \DateTime('now'); |
81
|
|
|
$bnDateTime = BnDateTime::create($dateTime); |
82
|
|
|
$this->assertInstanceOf('EasyBanglaDate\Types\BnDateTime', $bnDateTime); |
83
|
|
|
$this->assertEquals($bnDateTime->getTimestamp(), $dateTime->getTimestamp()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
public function testFactoryShouldReturnBnDateTimeObjectFromTimeStamp() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$time = time(); |
89
|
|
|
$bnDateTime = BnDateTime::create($time); |
90
|
|
|
$this->assertInstanceOf('EasyBanglaDate\Types\BnDateTime', $bnDateTime); |
91
|
|
|
$this->assertEquals($bnDateTime->getTimestamp(), $time); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @expectedException \Exception |
96
|
|
|
*/ |
97
|
|
|
public function testFactoryForInvalidInputShouldThrowException() |
98
|
|
|
{ |
99
|
|
|
$invalidTimeString = 'invalid'; |
100
|
|
|
$bnDateTime = BnDateTime::create($invalidTimeString); |
101
|
|
|
$this->assertInstanceOf('EasyBanglaDate\Types\BnDateTime', $bnDateTime); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @dataProvider enFlagDataProvider |
106
|
|
|
* @param $time |
107
|
|
|
* @param $flag |
108
|
|
|
* @param $expected |
109
|
|
|
*/ |
110
|
|
|
public function testEnFormat($time, $flag, $expected) |
111
|
|
|
{ |
112
|
|
|
$object = new BnDateTime($time, new \DateTimeZone('Asia/Dhaka')); |
113
|
|
|
$this->assertEquals($expected, $object->enFormat($flag), "$time, $flag, $expected"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @dataProvider dataProviderEnSuffix |
118
|
|
|
* @param $day |
119
|
|
|
* @param $suffix |
120
|
|
|
*/ |
121
|
|
|
public function testEnSuffix($day, $suffix) { |
122
|
|
|
$object = $this->createObject('now')->setDate('1422', 1, $day); |
123
|
|
|
$this->assertEquals($suffix, $object->enFormat('S'), "$day, $suffix"); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @dataProvider dataProviderForFlag_t |
128
|
|
|
* @param $time |
129
|
|
|
* @param $expected |
130
|
|
|
*/ |
131
|
|
|
public function testDayInMonth($time, $expected) |
132
|
|
|
{ |
133
|
|
|
$object = new BnDateTime($time, new \DateTimeZone('Asia/Dhaka')); |
134
|
|
|
$this->assertEquals($expected, $object->format('t')); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testCustomMorningTest() |
138
|
|
|
{ |
139
|
|
|
$object = $this->createObject("2015-01-01 05:00:00"); |
140
|
|
|
$this->assertEquals("১৭", $object->format('d')); |
141
|
|
|
$object->setMorning(4); |
142
|
|
|
$this->assertEquals("১৮", $object->format('d')); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testBanglaDateSetting() |
146
|
|
|
{ |
147
|
|
|
$object = $this->createObjectAndSetBanglaDate("2015-01-01 08:00:00", 1405,9,21); |
148
|
|
|
$this->assertEquals("২১-০৯-১৪০৫ ০৮:০০:০০", $object->format('d-m-Y H:i:s')); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testDateTimeObject() |
152
|
|
|
{ |
153
|
|
|
$object = $this->createObjectAndSetBanglaDate("2015-01-01 08:00:00", 1421,1,1); |
154
|
|
|
$this->assertEquals("১৪-০৪-২০১৪ ০৮:০০:০০", $object->getDateTime()->format('d-m-Y H:i:s')); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
protected function createObjectAndSetBanglaDate($time, $year, $month, $day) |
158
|
|
|
{ |
159
|
|
|
return $this->createObject($time)->setDate($year, $month, $day); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param $time |
164
|
|
|
* @return BnDateTime |
165
|
|
|
*/ |
166
|
|
|
protected function createObject($time) |
167
|
|
|
{ |
168
|
|
|
return new BnDateTime($time, new \DateTimeZone('Asia/Dhaka')); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.