1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
<?php
declare(strict_types=1);
/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\CarbonInterval;
use BadMethodCallException;
use Carbon\CarbonInterval;
use InvalidArgumentException;
use Tests\AbstractTestCase;
class SettersTest extends AbstractTestCase
{
public function testSet()
{
$ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
$ci->set('seconds', 34);
$this->assertSame(34, $ci->seconds);
$ci->set([
'seconds' => 59,
'minutes' => 33,
]);
$this->assertSame(59, $ci->seconds);
$this->assertSame(33, $ci->minutes);
}
public function testYearsSetter()
{
$ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
$ci->years = 2;
$this->assertSame(2, $ci->years);
$ci->years(5);
$this->assertSame(5, $ci->years);
}
public function testMonthsSetter()
{
$ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
$ci->months = 11;
$this->assertSame(11, $ci->months);
$ci->months(8);
$this->assertSame(8, $ci->months);
}
public function testWeeksSetter()
{
$ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
$ci->weeks = 11;
$this->assertSame(11, $ci->weeks);
$this->assertSame(7 * 11, $ci->dayz);
$ci->weeks(4);
$this->assertSame(4, $ci->weeks);
}
public function testDayzSetter()
{
$ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
$ci->dayz = 11;
$this->assertSame(11, $ci->dayz);
$this->assertSame(1, $ci->weeks);
$this->assertSame(4, $ci->dayzExcludeWeeks);
$ci->days(1);
$this->assertSame(1, $ci->dayz);
$ci->day(3);
$this->assertSame(3, $ci->dayz);
}
public function testHoursSetter()
{
$ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
$ci->hours = 12;
$this->assertSame(12, $ci->hours);
$ci->hours(0);
$this->assertSame(0, $ci->hours);
}
public function testMinutesSetter()
{
$ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
$ci->minutes = 11;
$this->assertSame(11, $ci->minutes);
$ci->minutes(9);
$this->assertSame(9, $ci->minutes);
}
public function testSecondsSetter()
{
$ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
$ci->seconds = 34;
$this->assertSame(34, $ci->seconds);
$ci->seconds(59);
$this->assertSame(59, $ci->seconds);
$ci->second(1);
$this->assertSame(1, $ci->seconds);
}
public function testMillisecondsSetter()
{
$ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
$ci->milliseconds = 34;
$this->assertSame(34, $ci->milliseconds);
$ci->milliseconds(59);
$this->assertSame(59, $ci->milliseconds);
$ci->millisecond(1);
$this->assertSame(1, $ci->milliseconds);
}
public function testMicrosecondsSetter()
{
$ci = CarbonInterval::create(4, 5, 6, 5, 8, 9, 10);
$ci->microseconds = 34;
$this->assertSame(34, $ci->microseconds);
$ci->microseconds(59);
$this->assertSame(59, $ci->microseconds);
$ci->microsecond(1);
$this->assertSame(1, $ci->microseconds);
}
public function testFluentSetters()
{
$ci = CarbonInterval::years(4)->months(2)->dayz(5)->hours(3)->minute()->seconds(59);
$this->assertInstanceOfCarbonInterval($ci);
$this->assertCarbonInterval($ci, 4, 2, 5, 3, 1, 59);
$ci = CarbonInterval::years(4)->months(2)->weeks(2)->hours(3)->minute()->seconds(59);
$this->assertInstanceOfCarbonInterval($ci);
$this->assertCarbonInterval($ci, 4, 2, 14, 3, 1, 59);
}
public function testFluentSettersDaysOverwritesWeeks()
{
$ci = CarbonInterval::weeks(3)->days(5);
$this->assertCarbonInterval($ci, 0, 0, 5, 0, 0, 0);
}
public function testFluentSettersWeeksOverwritesDays()
{
$ci = CarbonInterval::days(5)->weeks(3);
$this->assertCarbonInterval($ci, 0, 0, 3 * 7, 0, 0, 0);
}
public function testFluentSettersWeeksAndDaysIsCumulative()
{
$ci = CarbonInterval::year(5)->weeksAndDays(2, 6);
$this->assertCarbonInterval($ci, 5, 0, 20, 0, 0, 0);
$this->assertSame(20, $ci->dayz);
$this->assertSame(2, $ci->weeks);
$this->assertSame(6, $ci->dayzExcludeWeeks);
}
public function testInvert()
{
$ci = new CarbonInterval();
$this->assertSame($ci, $ci->invert());
$this->assertSame(1, $ci->invert);
$this->assertSame($ci, $ci->invert());
$this->assertSame(0, $ci->invert);
$this->assertSame($ci, $ci->invert(true));
$this->assertSame(1, $ci->invert);
$this->assertSame($ci, $ci->invert(true));
$this->assertSame(1, $ci->invert);
$this->assertSame($ci, $ci->invert(false));
$this->assertSame(0, $ci->invert);
$this->assertSame($ci, $ci->invert(false));
$this->assertSame(0, $ci->invert);
}
public function testInvalidSetter()
{
$this->expectExceptionObject(new InvalidArgumentException(
'Unknown setter \'doesNotExit\''
));
/** @var mixed $ci */
$ci = new CarbonInterval();
$ci->doesNotExit = 123;
}
public function testInvalidFluentSetter()
{
$this->expectExceptionObject(new BadMethodCallException(
'Unknown fluent setter \'doesNotExit\''
));
/** @var mixed $ci */
$ci = new CarbonInterval();
$ci->doesNotExit(123);
}
public function testInvalidStaticFluentSetter()
{
$this->expectExceptionObject(new BadMethodCallException(
'Unknown fluent constructor \'doesNotExit\''
));
CarbonInterval::doesNotExit(123);
}
public function testLocale()
{
/** @var CarbonInterval $interval */
$interval = CarbonInterval::hour()->locale('de');
$this->assertSame('de', $interval->locale);
}
public function testTimezone()
{
$interval = CarbonInterval::hour()->shiftTimezone('America/Toronto');
$this->assertSame('America/Toronto', $interval->getSettings()['timezone']);
}
}
|