Skip to content

Commit 531f2d0

Browse files
author
coderswat
committed
unit tests added , phpcs fixed
1 parent 7948aec commit 531f2d0

File tree

3 files changed

+86
-21
lines changed

3 files changed

+86
-21
lines changed

src/BaseController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* For the full copyright and license information, please view the LICENSE
1111
* file that was distributed with this source code.
1212
*/
13-
13+
1414
namespace Coswat\Grapes;
1515

1616
class BaseController

src/Grape.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* For the full copyright and license information, please view the LICENSE
1111
* file that was distributed with this source code.
1212
*/
13-
13+
1414
namespace Coswat\Grapes;
1515

1616
use Coswat\Grapes\BaseController;
@@ -107,7 +107,8 @@ public static function timeAgo(): string
107107
*/
108108
public static function nextDay(bool $unix = false): string|int|float
109109
{
110-
$date = date("Y-m-d H:i:s", strtotime("+1 day"));
110+
$timestamp = strtotime("+1 day", self::$time);
111+
$date = date("Y-m-d H:i:s", $timestamp);
111112
if ($unix) {
112113
return strtotime($date);
113114
}
@@ -122,7 +123,8 @@ public static function nextDay(bool $unix = false): string|int|float
122123
*/
123124
public static function nextWeek(bool $unix = false): string|int|float
124125
{
125-
$date = date("Y-m-d H:i:s", strtotime("+1 week"));
126+
$timestamp = strtotime("+1 week", self::$time);
127+
$date = date("Y-m-d H:i:s", $timestamp);
126128
if ($unix) {
127129
return strtotime($date);
128130
}
@@ -137,7 +139,8 @@ public static function nextWeek(bool $unix = false): string|int|float
137139
*/
138140
public static function nextMonth(bool $unix = false): string|int|float
139141
{
140-
$date = date("Y-m-d H:i:s", strtotime("+1 month"));
142+
$timestamp = strtotime("+1 month", self::$time);
143+
$date = date("Y-m-d H:i:s", $timestamp);
141144
if ($unix) {
142145
return strtotime($date);
143146
}
@@ -153,7 +156,8 @@ public static function nextMonth(bool $unix = false): string|int|float
153156
*/
154157
public static function addDays(int $day, bool $unix = false): string|int|float
155158
{
156-
$date = date("Y-m-d H:i:s", strtotime("+{$day} day"));
159+
$timestamp = strtotime("+{$day} day", self::$time);
160+
$date = date("Y-m-d H:i:s", $timestamp);
157161
if ($unix) {
158162
return strtotime($date);
159163
}
@@ -169,7 +173,8 @@ public static function addDays(int $day, bool $unix = false): string|int|float
169173
*/
170174
public static function addWeeks(int $week, bool $unix = false): string|int|float
171175
{
172-
$date = date("Y-m-d H:i:s", strtotime("+{$week} week"));
176+
$timestamp = strtotime("+{$week} week", self::$time);
177+
$date = date("Y-m-d H:i:s", $timestamp);
173178
if ($unix) {
174179
return strtotime($date);
175180
}
@@ -185,7 +190,8 @@ public static function addWeeks(int $week, bool $unix = false): string|int|float
185190
*/
186191
public static function addMonths(int $month, bool $unix = false): string|int|float
187192
{
188-
$date = date("Y-m-d H:i:s", strtotime("+{$month} month"));
193+
$timestamp = strtotime("+{$month} month", self::$time);
194+
$date = date("Y-m-d H:i:s", $timestamp);
189195
if ($unix) {
190196
return strtotime($date);
191197
}

tests/GrapeTest.php

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,97 @@ public function test_toYear_function(): void
1313
{
1414
$year = Grape::time(1687350065)->toYear();
1515
$expected = 2023;
16-
$this->assertEquals($year, $expected);
16+
$this->assertEquals($expected, $year);
1717
}
1818
public function test_toMonth_function(): void
1919
{
2020
$month = Grape::time(1687350065)->toMonth();
2121
$expected = "June";
22-
$this->assertEquals($month, $expected);
22+
$this->assertEquals($expected, $month);
2323
}
2424
public function test_toDay_function(): void
2525
{
2626
$day = Grape::time(1687350065)->toDay();
2727
$expected = "Wednesday";
28-
$this->assertEquals($day, $expected);
28+
$this->assertEquals($expected, $day);
2929
}
3030
public function test_getTimezone_function(): void
3131
{
3232
$ip = "8.8.8.8";
3333
$timeZone = Grape::getTimezone($ip);
3434
$expected = "America/New_York";
35-
$this->assertEquals($timeZone, $expected);
35+
$this->assertEquals($expected, $timeZone);
3636
}
37-
public function test_toTime_function(): void
37+
public function test_toTime_function(): void
3838
{
39-
$time = Grape::time(1687350065)->toTime();
40-
$expected = '12:21:05';
41-
$this->assertEquals($time, $expected);
39+
$time = Grape::time(1687350065)->toTime();
40+
$expected = '12:21:05';
41+
$this->assertEquals($expected, $time);
4242
}
43-
public function test_toRaw_function(): void
43+
public function test_toRaw_function(): void
4444
{
45-
$time = Grape::time(1687350065)->toRaw();
46-
$expected = '2023-06-21 12:21:05';
47-
$this->assertEquals($time, $expected);
45+
$time = Grape::time(1687350065)->toRaw();
46+
$expected = '2023-06-21 12:21:05';
47+
$this->assertEquals($expected, $time);
48+
}
49+
public function test_nextDay_function(): void
50+
{
51+
$time = Grape::time(1687350065)->nextDay();
52+
$expected = '2023-06-22 12:21:05';
53+
$this->assertEquals($expected, $time);
54+
// check with unix timestamp also
55+
$time = Grape::time(1687350065)->nextDay(true);
56+
$expected = '1687436465';
57+
$this->assertEquals($expected, $time);
58+
}
59+
public function test_nextWeek_function(): void
60+
{
61+
$time = Grape::time(1687350065)->nextWeek();
62+
$expected = '2023-06-28 12:21:05';
63+
$this->assertEquals($expected, $time);
64+
// check with unix timestamp also
65+
$time = Grape::time(1687350065)->nextWeek(true);
66+
$expected = '1687954865';
67+
$this->assertEquals($expected, $time);
68+
}
69+
public function test_nextMonth_function(): void
70+
{
71+
$time = Grape::time(1687350065)->nextMonth();
72+
$expected = '2023-07-21 12:21:05';
73+
$this->assertEquals($expected, $time);
74+
// check with unix timestamp also
75+
$time = Grape::time(1687350065)->nextMonth(true);
76+
$expected = '1689942065';
77+
$this->assertEquals($expected, $time);
78+
}
79+
public function test_addDays_function(): void
80+
{
81+
$time = Grape::time(1687350065)->addDays(17);
82+
$expected = '2023-07-08 12:21:05';
83+
$this->assertEquals($expected, $time);
84+
// check with unix timestamp also
85+
$time = Grape::time(1687350065)->addDays(17, true);
86+
$expected = '1688818865';
87+
$this->assertEquals($expected, $time);
88+
}
89+
public function test_addWeeks_function(): void
90+
{
91+
$time = Grape::time(1687350065)->addWeeks(7);
92+
$expected = '2023-08-09 12:21:05';
93+
$this->assertEquals($expected, $time);
94+
// check with unix timestamp also
95+
$time = Grape::time(1687350065)->addWeeks(7, true);
96+
$expected = '1691583665';
97+
$this->assertEquals($expected, $time);
98+
}
99+
public function test_addMonths_function(): void
100+
{
101+
$time = Grape::time(1687350065)->addMonths(8);
102+
$expected = '2024-02-21 12:21:05';
103+
$this->assertEquals($expected, $time);
104+
// check with unix timestamp also
105+
$time = Grape::time(1687350065)->addMonths(8, true);
106+
$expected = '1708518065';
107+
$this->assertEquals($expected, $time);
48108
}
49-
50109
}

0 commit comments

Comments
 (0)