FormatterTest.php 48.8 KB
Newer Older
Qiang Xue committed
1
<?php
2

3
namespace yiiunit\framework\i18n;
4

5
use NumberFormatter;
6
use yii\i18n\Formatter;
7
use Yii;
Qiang Xue committed
8
use yiiunit\TestCase;
9 10
use DateTime;
use DateInterval;
Qiang Xue committed
11 12

/**
13
 * @group i18n
Qiang Xue committed
14 15 16
 */
class FormatterTest extends TestCase
{
David Renty committed
17 18 19 20 21 22 23 24
    /**
     * @var Formatter
     */
    protected $formatter;

    protected function setUp()
    {
        parent::setUp();
25

26
        IntlTestHelper::setIntlStatus($this);
27 28

        $this->mockApplication([
29 30
            'timeZone' => 'UTC',
            'language' => 'ru-RU',
31 32
        ]);
        $this->formatter = new Formatter(['locale' => 'en-US']);
David Renty committed
33 34 35 36 37
    }

    protected function tearDown()
    {
        parent::tearDown();
38
        IntlTestHelper::resetIntlStatus();
David Renty committed
39 40 41
        $this->formatter = null;
    }

42 43 44 45 46 47

    public function testFormat()
    {
        $value = time();
        $this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'date'));
        $this->assertSame(date('M j, Y', $value), $this->formatter->format($value, 'DATE'));
48
        $this->assertSame(date('Y/m/d', $value), $this->formatter->format($value, ['date', 'php:Y/m/d']));
49 50 51 52
        $this->setExpectedException('\yii\base\InvalidParamException');
        $this->assertSame(date('Y-m-d', $value), $this->formatter->format($value, 'data'));
    }

53 54 55 56 57 58 59 60 61 62 63
    public function testLocale()
    {
        // locale is configured explicitly
        $f = new Formatter(['locale' => 'en-US']);
        $this->assertEquals('en-US', $f->locale);

        // if not, take from application
        $f = new Formatter();
        $this->assertEquals('ru-RU', $f->locale);
    }

64

David Renty committed
65 66 67 68 69 70 71 72
    public function testAsRaw()
    {
        $value = '123';
        $this->assertSame($value, $this->formatter->asRaw($value));
        $value = 123;
        $this->assertSame($value, $this->formatter->asRaw($value));
        $value = '<>';
        $this->assertSame($value, $this->formatter->asRaw($value));
73 74

        // null display
David Renty committed
75 76 77 78 79 80 81 82 83 84 85
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asRaw(null));
    }

    public function testAsText()
    {
        $value = '123';
        $this->assertSame($value, $this->formatter->asText($value));
        $value = 123;
        $this->assertSame("$value", $this->formatter->asText($value));
        $value = '<>';
        $this->assertSame('&lt;&gt;', $this->formatter->asText($value));
86 87

        // null display
David Renty committed
88 89 90 91 92 93 94 95 96 97 98 99 100
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asText(null));
    }

    public function testAsNtext()
    {
        $value = '123';
        $this->assertSame($value, $this->formatter->asNtext($value));
        $value = 123;
        $this->assertSame("$value", $this->formatter->asNtext($value));
        $value = '<>';
        $this->assertSame('&lt;&gt;', $this->formatter->asNtext($value));
        $value = "123\n456";
        $this->assertSame("123<br />\n456", $this->formatter->asNtext($value));
101 102

        // null display
David Renty committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asNtext(null));
    }

    public function testAsParagraphs()
    {
        $value = '123';
        $this->assertSame("<p>$value</p>", $this->formatter->asParagraphs($value));
        $value = 123;
        $this->assertSame("<p>$value</p>", $this->formatter->asParagraphs($value));
        $value = '<>';
        $this->assertSame('<p>&lt;&gt;</p>', $this->formatter->asParagraphs($value));
        $value = "123\n456";
        $this->assertSame("<p>123\n456</p>", $this->formatter->asParagraphs($value));
        $value = "123\n\n456";
        $this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
        $value = "123\n\n\n456";
        $this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
120 121 122 123 124 125 126 127 128 129 130 131
        $value = "123\r\n456";
        $this->assertSame("<p>123\r\n456</p>", $this->formatter->asParagraphs($value));
        $value = "123\r\n\r\n456";
        $this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
        $value = "123\r\n\r\n\r\n456";
        $this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
        $value = "123\r456";
        $this->assertSame("<p>123\r456</p>", $this->formatter->asParagraphs($value));
        $value = "123\r\r456";
        $this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
        $value = "123\r\r\r456";
        $this->assertSame("<p>123</p>\n<p>456</p>", $this->formatter->asParagraphs($value));
132 133

        // null display
David Renty committed
134 135 136 137 138 139 140 141 142 143 144 145
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asParagraphs(null));
    }

    public function testAsHtml()
    {
        // todo: dependency on HtmlPurifier
    }

    public function testAsEmail()
    {
        $value = 'test@sample.com';
        $this->assertSame("<a href=\"mailto:$value\">$value</a>", $this->formatter->asEmail($value));
146 147
        $value = 'test@sample.com';
        $this->assertSame("<a href=\"mailto:$value\" target=\"_blank\">$value</a>", $this->formatter->asEmail($value, ['target' => '_blank']));
148 149

        // null display
David Renty committed
150 151 152
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asEmail(null));
    }

153 154 155 156 157 158 159 160 161 162
    public function testAsUrl()
    {
        $value = 'http://www.yiiframework.com/';
        $this->assertSame("<a href=\"$value\">$value</a>", $this->formatter->asUrl($value));
        $value = 'https://www.yiiframework.com/';
        $this->assertSame("<a href=\"$value\">$value</a>", $this->formatter->asUrl($value));
        $value = 'www.yiiframework.com/';
        $this->assertSame("<a href=\"http://$value\">$value</a>", $this->formatter->asUrl($value));
        $value = 'https://www.yiiframework.com/?name=test&value=5"';
        $this->assertSame("<a href=\"https://www.yiiframework.com/?name=test&amp;value=5&quot;\">https://www.yiiframework.com/?name=test&amp;value=5&quot;</a>", $this->formatter->asUrl($value));
163 164
        $value = 'http://www.yiiframework.com/';
        $this->assertSame("<a href=\"$value\" target=\"_blank\">$value</a>", $this->formatter->asUrl($value, ['target' => '_blank']));
165 166 167 168 169

        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asUrl(null));
    }

David Renty committed
170 171 172 173
    public function testAsImage()
    {
        $value = 'http://sample.com/img.jpg';
        $this->assertSame("<img src=\"$value\" alt=\"\">", $this->formatter->asImage($value));
174 175 176
        $value = 'http://sample.com/img.jpg';
        $alt = "Hello!";
        $this->assertSame("<img src=\"$value\" alt=\"$alt\">", $this->formatter->asImage($value, ['alt' => $alt]));
177 178

        // null display
David Renty committed
179 180 181 182 183
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asImage(null));
    }

    public function testAsBoolean()
    {
184 185 186 187 188
        $this->assertSame('Yes', $this->formatter->asBoolean(true));
        $this->assertSame('No', $this->formatter->asBoolean(false));
        $this->assertSame('Yes', $this->formatter->asBoolean("111"));
        $this->assertSame('No', $this->formatter->asBoolean(""));
        $this->assertSame('No', $this->formatter->asBoolean(0));
189 190

        // null display
David Renty committed
191 192 193
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asBoolean(null));
    }

194 195 196 197 198 199 200 201 202

    // date format


    public function testIntlAsDate()
    {
        $this->testAsDate();
    }

David Renty committed
203 204 205
    public function testAsDate()
    {
        $value = time();
206 207
        $this->assertSame(date('M j, Y', $value), $this->formatter->asDate($value));
        $this->assertSame(date('Y/m/d', $value), $this->formatter->asDate($value, 'php:Y/m/d'));
208 209
        $this->assertSame(date('m/d/Y', $value), $this->formatter->asDate($value, 'MM/dd/yyyy'));
        $this->assertSame(date('n/j/y', $value), $this->formatter->asDate($value, 'short'));
210
        $this->assertSame(date('F j, Y', $value), $this->formatter->asDate($value, 'long'));
211

212 213 214 215
        // empty input
        $this->assertSame('Jan 1, 1970', $this->formatter->asDate(''));
        $this->assertSame('Jan 1, 1970', $this->formatter->asDate(0));
        $this->assertSame('Jan 1, 1970', $this->formatter->asDate(false));
216

217
        // null display
David Renty committed
218 219 220
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asDate(null));
    }

221 222 223
    public function testIntlAsTime()
    {
        $this->testAsTime();
224 225 226 227 228 229

        // empty input
        $this->formatter->locale = 'de-DE';
        $this->assertSame('00:00:00', $this->formatter->asTime(''));
        $this->assertSame('00:00:00', $this->formatter->asTime(0));
        $this->assertSame('00:00:00', $this->formatter->asTime(false));
230 231
    }

David Renty committed
232 233 234
    public function testAsTime()
    {
        $value = time();
235
        $this->assertSame(date('g:i:s A', $value), $this->formatter->asTime($value));
236
        $this->assertSame(date('h:i:s A', $value), $this->formatter->asTime($value, 'php:h:i:s A'));
237

238 239 240 241
        // empty input
        $this->assertSame('12:00:00 AM', $this->formatter->asTime(''));
        $this->assertSame('12:00:00 AM', $this->formatter->asTime(0));
        $this->assertSame('12:00:00 AM', $this->formatter->asTime(false));
242
        // null display
David Renty committed
243 244 245
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null));
    }

246 247 248
    public function testIntlAsDatetime()
    {
        $this->testAsDatetime();
249 250 251 252 253 254

        // empty input
        $this->formatter->locale = 'de-DE';
        $this->assertSame('01.01.1970 00:00:00', $this->formatter->asDatetime(''));
        $this->assertSame('01.01.1970 00:00:00', $this->formatter->asDatetime(0));
        $this->assertSame('01.01.1970 00:00:00', $this->formatter->asDatetime(false));
255 256
    }

David Renty committed
257 258 259
    public function testAsDatetime()
    {
        $value = time();
260
        $this->assertSame(date('M j, Y g:i:s A', $value), $this->formatter->asDatetime($value));
261
        $this->assertSame(date('Y/m/d h:i:s A', $value), $this->formatter->asDatetime($value, 'php:Y/m/d h:i:s A'));
262

263 264 265 266
        // empty input
        $this->assertSame('Jan 1, 1970 12:00:00 AM', $this->formatter->asDatetime(''));
        $this->assertSame('Jan 1, 1970 12:00:00 AM', $this->formatter->asDatetime(0));
        $this->assertSame('Jan 1, 1970 12:00:00 AM', $this->formatter->asDatetime(false));
267
        // null display
David Renty committed
268 269 270
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asDatetime(null));
    }

271 272 273 274 275
    public function testIntlAsTimestamp()
    {
        $this->testAsTimestamp();
    }

276
    public function testAsTimestamp()
David Renty committed
277
    {
278 279 280
        $value = time();
        $this->assertSame("$value", $this->formatter->asTimestamp($value));
        $this->assertSame("$value", $this->formatter->asTimestamp((string) $value));
281

282
        $this->assertSame("$value", $this->formatter->asTimestamp(date('Y-m-d H:i:s', $value)));
283

284 285 286 287 288
        // empty input
        $this->assertSame("0", $this->formatter->asTimestamp(0));
        $this->assertSame("0", $this->formatter->asTimestamp(false));
        $this->assertSame("0", $this->formatter->asTimestamp(""));

289
        // null display
290
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asTimestamp(null));
291 292
    }

293 294
    public function testIntlDateRangeLow()
    {
295 296 297
        if (PHP_INT_SIZE == 4) { // 32bit systems
            $this->markTestSkipped('intl does not support high date ranges on 32bit systems.');
        }
298 299 300
        $this->testDateRangeLow();
    }

Carsten Brandt committed
301 302 303 304 305 306 307
    /**
     * Test for dates before 1970
     * https://github.com/yiisoft/yii2/issues/3126
     */
    public function testDateRangeLow()
    {
        $this->assertSame('12-08-1922', $this->formatter->asDate('1922-08-12', 'dd-MM-yyyy'));
308
        $this->assertSame('14-01-1732', $this->formatter->asDate('1732-01-14', 'dd-MM-yyyy'));
Carsten Brandt committed
309 310
    }

311 312
    public function testIntlDateRangeHigh()
    {
313 314 315
        if (PHP_INT_SIZE == 4) { // 32bit systems
            $this->markTestSkipped('intl does not support high date ranges on 32bit systems.');
        }
316 317 318 319
        $this->testDateRangeHigh();
    }

        /**
Carsten Brandt committed
320 321 322 323 324 325
     * Test for dates after 2038
     * https://github.com/yiisoft/yii2/issues/3126
     */
    public function testDateRangeHigh()
    {
        $this->assertSame('17-12-2048', $this->formatter->asDate('2048-12-17', 'dd-MM-yyyy'));
326
        $this->assertSame('17-12-3048', $this->formatter->asDate('3048-12-17', 'dd-MM-yyyy'));
Carsten Brandt committed
327 328
    }

David Renty committed
329 330 331 332 333 334 335 336 337
    private function buildDateSubIntervals($referenceDate, $intervals)
    {
        $date = new DateTime($referenceDate);
        foreach ($intervals as $interval) {
            $date->sub($interval);
        }
        return $date;
    }

338 339 340 341 342
    public function testIntlAsRelativeTime()
    {
        $this->testAsRelativeTime();
    }

David Renty committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    public function testAsRelativeTime()
    {
        $interval_1_second    = new DateInterval("PT1S");
        $interval_244_seconds = new DateInterval("PT244S");
        $interval_1_minute    = new DateInterval("PT1M");
        $interval_33_minutes  = new DateInterval("PT33M");
        $interval_1_hour      = new DateInterval("PT1H");
        $interval_6_hours     = new DateInterval("PT6H");
        $interval_1_day       = new DateInterval("P1D");
        $interval_89_days     = new DateInterval("P89D");
        $interval_1_month     = new DateInterval("P1M");
        $interval_5_months    = new DateInterval("P5M");
        $interval_1_year      = new DateInterval("P1Y");
        $interval_12_years    = new DateInterval("P12Y");

        // Pass a DateInterval
        $this->assertSame('a second ago', $this->formatter->asRelativeTime($interval_1_second));
        $this->assertSame('244 seconds ago', $this->formatter->asRelativeTime($interval_244_seconds));
        $this->assertSame('a minute ago', $this->formatter->asRelativeTime($interval_1_minute));
        $this->assertSame('33 minutes ago', $this->formatter->asRelativeTime($interval_33_minutes));
        $this->assertSame('an hour ago', $this->formatter->asRelativeTime($interval_1_hour));
        $this->assertSame('6 hours ago', $this->formatter->asRelativeTime($interval_6_hours));
        $this->assertSame('a day ago', $this->formatter->asRelativeTime($interval_1_day));
        $this->assertSame('89 days ago', $this->formatter->asRelativeTime($interval_89_days));
        $this->assertSame('a month ago', $this->formatter->asRelativeTime($interval_1_month));
        $this->assertSame('5 months ago', $this->formatter->asRelativeTime($interval_5_months));
        $this->assertSame('a year ago', $this->formatter->asRelativeTime($interval_1_year));
        $this->assertSame('12 years ago', $this->formatter->asRelativeTime($interval_12_years));

372 373 374 375 376 377
        // Pass a DateInterval string -> isn't possible
        //    $this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/2008-05-11T15:30:00Z'));
        //    $this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/P1Y2M10DT2H30M'));
        //    $this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M/2008-05-11T15:30:00Z'));
        //    $this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M'));
        //    $this->assertSame('94 months ago', $this->formatter->asRelativeTime('P94M'));
David Renty committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

        // Force the reference time and pass a past DateTime
        $dateNow = new DateTime('2014-03-13');
        $this->assertSame('a second ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_second]), $dateNow));
        $this->assertSame('4 minutes ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_244_seconds]), $dateNow));
        $this->assertSame('a minute ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_minute]), $dateNow));
        $this->assertSame('33 minutes ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_33_minutes]), $dateNow));
        $this->assertSame('an hour ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_hour]), $dateNow));
        $this->assertSame('6 hours ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_6_hours]), $dateNow));
        $this->assertSame('a day ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_day]), $dateNow));
        $this->assertSame('2 months ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_89_days]), $dateNow));
        $this->assertSame('a month ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_month]), $dateNow));
        $this->assertSame('5 months ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_5_months]), $dateNow));
        $this->assertSame('a year ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_year]), $dateNow));
        $this->assertSame('12 years ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_12_years]), $dateNow));

        // Tricky 31-days month stuff
        // See: http://www.gnu.org/software/tar/manual/html_section/Relative-items-in-date-strings.html
        $dateNow = new DateTime('2014-03-31');
        $dateThen = new DateTime('2014-03-03');
        $this->assertSame('28 days ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-31', [$interval_1_month]), $dateNow));
        $this->assertSame('28 days ago', $this->formatter->asRelativeTime($dateThen, $dateNow));
        $dateThen = new DateTime('2014-02-28');
        $this->assertSame('a month ago', $this->formatter->asRelativeTime($dateThen, $dateNow));

        // Invert all the DateIntervals
        $interval_1_second->invert = true;
        $interval_244_seconds->invert = true;
        $interval_1_minute->invert = true;
        $interval_33_minutes->invert = true;
        $interval_1_hour->invert = true;
        $interval_6_hours->invert = true;
        $interval_1_day->invert = true;
        $interval_89_days->invert = true;
        $interval_1_month->invert = true;
        $interval_5_months->invert = true;
        $interval_1_year->invert = true;
        $interval_12_years->invert = true;

        // Pass a inverted DateInterval
        $this->assertSame('in a second', $this->formatter->asRelativeTime($interval_1_second));
        $this->assertSame('in 244 seconds', $this->formatter->asRelativeTime($interval_244_seconds));
        $this->assertSame('in a minute', $this->formatter->asRelativeTime($interval_1_minute));
        $this->assertSame('in 33 minutes', $this->formatter->asRelativeTime($interval_33_minutes));
        $this->assertSame('in an hour', $this->formatter->asRelativeTime($interval_1_hour));
        $this->assertSame('in 6 hours', $this->formatter->asRelativeTime($interval_6_hours));
        $this->assertSame('in a day', $this->formatter->asRelativeTime($interval_1_day));
        $this->assertSame('in 89 days', $this->formatter->asRelativeTime($interval_89_days));
        $this->assertSame('in a month', $this->formatter->asRelativeTime($interval_1_month));
        $this->assertSame('in 5 months', $this->formatter->asRelativeTime($interval_5_months));
        $this->assertSame('in a year', $this->formatter->asRelativeTime($interval_1_year));
        $this->assertSame('in 12 years', $this->formatter->asRelativeTime($interval_12_years));

        // Pass a inverted DateInterval string
432
        // $this->assertSame('in a year', $this->formatter->asRelativeTime('2008-05-11T15:30:00Z/2007-03-01T13:00:00Z'));
David Renty committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

        // Force the reference time and pass a future DateTime
        $dateNow = new DateTime('2014-03-13');
        $this->assertSame('in a second', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_second]), $dateNow));
        $this->assertSame('in 4 minutes', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_244_seconds]), $dateNow));
        $this->assertSame('in a minute', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_minute]), $dateNow));
        $this->assertSame('in 33 minutes', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_33_minutes]), $dateNow));
        $this->assertSame('in an hour', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_hour]), $dateNow));
        $this->assertSame('in 6 hours', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_6_hours]), $dateNow));
        $this->assertSame('in a day', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_day]), $dateNow));
        $this->assertSame('in 2 months', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_89_days]), $dateNow));
        $this->assertSame('in a month', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_month]), $dateNow));
        $this->assertSame('in 5 months', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_5_months]), $dateNow));
        $this->assertSame('in a year', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_year]), $dateNow));
        $this->assertSame('in 12 years', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_12_years]), $dateNow));

        // Tricky 31-days month stuff
        // See: http://www.gnu.org/software/tar/manual/html_section/Relative-items-in-date-strings.html
        $dateNow = new DateTime('2014-03-03');
        $dateThen = new DateTime('2014-03-31');
        $this->assertSame('in a month', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-03', [$interval_1_month]), $dateNow));
        $this->assertSame('in 28 days', $this->formatter->asRelativeTime($dateThen, $dateNow));
455

456 457 458 459 460 461 462 463
        // just now
        $this->assertSame("just now", $this->formatter->asRelativeTime($t = time(), $t));
        $this->assertSame("just now", $this->formatter->asRelativeTime(0, 0));

        // empty input
        $this->assertSame("just now", $this->formatter->asRelativeTime(false, 0));
        $this->assertSame("just now", $this->formatter->asRelativeTime("", 0));

464 465 466
        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asRelativeTime(null));
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asRelativeTime(null, time()));
David Renty committed
467
    }
468

469 470 471
    public function dateInputs()
    {
        return [
472 473
//            ['2015-01-01 00:00:00', '2014-13-01'], // TODO evals to current time on that date
            ['2015-01-01 00:00:00', '2014-13-01 00:00:00'],
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
            [false, 'asdfg', 'yii\base\InvalidParamException'],
//            [(string)strtotime('now'), 'now'], // fails randomly
        ];
    }

    /**
     * @dataProvider dateInputs
     */
    public function testIntlDateInput($expected, $value, $expectedException = null)
    {
        $this->testDateInput($expected, $value, $expectedException);
    }

    /**
     * @dataProvider dateInputs
     */
    public function testDateInput($expected, $value, $expectedException = null)
    {
        if ($expectedException !== null) {
            $this->setExpectedException($expectedException);
        }
495 496 497
        $this->assertSame($expected, $this->formatter->asDate($value, 'yyyy-MM-dd HH:mm:ss'));
        $this->assertSame($expected, $this->formatter->asTime($value, 'yyyy-MM-dd HH:mm:ss'));
        $this->assertSame($expected, $this->formatter->asDatetime($value, 'yyyy-MM-dd HH:mm:ss'));
498 499
    }

500

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
    public function provideTimezones()
    {
        return [
            ['UTC'],
            ['Europe/Berlin'],
            ['America/Jamaica'],
        ];
    }

    /**
     * provide default timezones times input date value
     */
    public function provideTimesAndTz()
    {
        $result = [];
        foreach($this->provideTimezones() as $tz) {
            $result[] = [$tz[0], 1407674460,                          1388580060];
            $result[] = [$tz[0], '2014-08-10 12:41:00',               '2014-01-01 12:41:00'];
            $result[] = [$tz[0], '2014-08-10 12:41:00 UTC',           '2014-01-01 12:41:00 UTC'];
            $result[] = [$tz[0], '2014-08-10 14:41:00 Europe/Berlin', '2014-01-01 13:41:00 Europe/Berlin'];
            $result[] = [$tz[0], '2014-08-10 14:41:00 CEST',          '2014-01-01 13:41:00 CET'];
            $result[] = [$tz[0], '2014-08-10 14:41:00+0200',          '2014-01-01 13:41:00+0100'];
            $result[] = [$tz[0], '2014-08-10 14:41:00+02:00',         '2014-01-01 13:41:00+01:00'];
            $result[] = [$tz[0], '2014-08-10 14:41:00 +0200',         '2014-01-01 13:41:00 +0100'];
            $result[] = [$tz[0], '2014-08-10 14:41:00 +02:00',        '2014-01-01 13:41:00 +01:00'];
            $result[] = [$tz[0], '2014-08-10T14:41:00+02:00',         '2014-01-01T13:41:00+01:00']; // ISO 8601
        }
        return $result;
    }

    /**
     * Test timezones with input date and time in other timezones
     * @dataProvider provideTimesAndTz
     */
    public function testIntlTimezoneInput($defaultTz, $inputTimeDst, $inputTimeNonDst)
    {
        $this->testTimezoneInput($defaultTz, $inputTimeDst, $inputTimeNonDst);
    }

    /**
     * Test timezones with input date and time in other timezones
     * @dataProvider provideTimesAndTz
     */
    public function testTimezoneInput($defaultTz, $inputTimeDst, $inputTimeNonDst)
    {
        date_default_timezone_set($defaultTz); // formatting has to be independent of the default timezone set by PHP
        $this->formatter->datetimeFormat = 'yyyy-MM-dd HH:mm:ss';
        $this->formatter->dateFormat = 'yyyy-MM-dd';
        $this->formatter->timeFormat = 'HH:mm:ss';

        // daylight saving time
        $this->formatter->timeZone = 'UTC';
        $this->assertSame('2014-08-10 12:41:00', $this->formatter->asDatetime($inputTimeDst));
        $this->assertSame('2014-08-10', $this->formatter->asDate($inputTimeDst));
        $this->assertSame('12:41:00', $this->formatter->asTime($inputTimeDst));
        $this->assertSame('1407674460', $this->formatter->asTimestamp($inputTimeDst));
        $this->formatter->timeZone = 'Europe/Berlin';
        $this->assertSame('2014-08-10 14:41:00', $this->formatter->asDatetime($inputTimeDst));
        $this->assertSame('2014-08-10', $this->formatter->asDate($inputTimeDst));
        $this->assertSame('14:41:00', $this->formatter->asTime($inputTimeDst));
        $this->assertSame('1407674460', $this->formatter->asTimestamp($inputTimeDst));

        // non daylight saving time
        $this->formatter->timeZone = 'UTC';
        $this->assertSame('2014-01-01 12:41:00', $this->formatter->asDatetime($inputTimeNonDst));
        $this->assertSame('2014-01-01', $this->formatter->asDate($inputTimeNonDst));
        $this->assertSame('12:41:00', $this->formatter->asTime($inputTimeNonDst));
        $this->assertSame('1388580060', $this->formatter->asTimestamp($inputTimeNonDst));
        $this->formatter->timeZone = 'Europe/Berlin';
        $this->assertSame('2014-01-01 13:41:00', $this->formatter->asDatetime($inputTimeNonDst));
        $this->assertSame('2014-01-01', $this->formatter->asDate($inputTimeNonDst));
        $this->assertSame('13:41:00', $this->formatter->asTime($inputTimeNonDst));
        $this->assertSame('1388580060', $this->formatter->asTimestamp($inputTimeNonDst));

        // tests for relative time
        if ($inputTimeDst !== 1407674460) {
            $this->assertSame('3 hours ago', $this->formatter->asRelativeTime($inputTimeDst, $relativeTime = str_replace(['14:41', '12:41'], ['17:41', '15:41'], $inputTimeDst)));
            $this->assertSame('in 3 hours', $this->formatter->asRelativeTime($relativeTime, $inputTimeDst));
            $this->assertSame('3 hours ago', $this->formatter->asRelativeTime($inputTimeNonDst, $relativeTime = str_replace(['13:41', '12:41'], ['16:41', '15:41'], $inputTimeNonDst)));
            $this->assertSame('in 3 hours', $this->formatter->asRelativeTime($relativeTime, $inputTimeNonDst));
        }
    }


585 586
    // number format

587 588 589 590 591 592 593 594 595
    /**
     * Provides some configuration that should not affect Integer formatter
     */
    public function differentConfigProvider()
    {
        // make this test not break when intl is not installed
        if (!extension_loaded('intl')) {
            return [];
        }
596

597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
        return [
            [[
                'numberFormatterOptions' => [
                    NumberFormatter::MIN_FRACTION_DIGITS => 2,
                ],
            ]],
            [[
                'numberFormatterOptions' => [
                    NumberFormatter::MAX_FRACTION_DIGITS => 2,
                ],
            ]],
            [[
                'numberFormatterOptions' => [
                    NumberFormatter::FRACTION_DIGITS => 2,
                ],
            ]],
            [[
                'numberFormatterOptions' => [
                    NumberFormatter::MIN_FRACTION_DIGITS => 2,
                    NumberFormatter::MAX_FRACTION_DIGITS => 4,
                ],
            ]],
        ];
    }


    /**
     * @dataProvider differentConfigProvider
     */
    public function testIntlAsInteger($config)
627
    {
628 629
        // configure formatter with different configs that should not affect integer format
        Yii::configure($this->formatter, $config);
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
        $this->testAsInteger();
    }

    public function testAsInteger()
    {
        $this->assertSame("123", $this->formatter->asInteger(123));
        $this->assertSame("123", $this->formatter->asInteger(123.23));
        $this->assertSame("123", $this->formatter->asInteger(123.53));
        $this->assertSame("0", $this->formatter->asInteger(0));
        $this->assertSame("-123", $this->formatter->asInteger(-123.23));
        $this->assertSame("-123", $this->formatter->asInteger(-123.53));

        $this->assertSame("123,456", $this->formatter->asInteger(123456));
        $this->assertSame("123,456", $this->formatter->asInteger(123456.789));

645 646 647 648
        // empty input
        $this->assertSame("0", $this->formatter->asInteger(false));
        $this->assertSame("0", $this->formatter->asInteger(""));

649 650 651 652
        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asInteger(null));
    }

653 654 655 656 657
    /**
     * @expectedException \yii\base\InvalidParamException
     */
    public function testAsIntegerException()
    {
658
        $this->formatter->asInteger('a');
659 660 661 662 663 664 665
    }

    /**
     * @expectedException \yii\base\InvalidParamException
     */
    public function testAsIntegerException2()
    {
666 667 668
        $this->formatter->asInteger('-123abc');
    }

669 670
    public function testIntlAsDecimal()
    {
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
        $value = 123.12;
        $this->assertSame("123.12", $this->formatter->asDecimal($value, 2));
        $this->assertSame("123.1", $this->formatter->asDecimal($value, 1));
        $this->assertSame("123", $this->formatter->asDecimal($value, 0));

        $value = 123;
        $this->assertSame("123", $this->formatter->asDecimal($value));
        $this->assertSame("123.00", $this->formatter->asDecimal($value, 2));
        $this->formatter->decimalSeparator = ',';
        $this->formatter->thousandSeparator = '.';
        $value = 123.12;
        $this->assertSame("123,12", $this->formatter->asDecimal($value));
        $this->assertSame("123,1", $this->formatter->asDecimal($value, 1));
        $this->assertSame("123", $this->formatter->asDecimal($value, 0));
        $value = 123123.123;
        $this->assertSame("123.123", $this->formatter->asDecimal($value, 0));
        $this->assertSame("123.123,12", $this->formatter->asDecimal($value, 2));
        $this->formatter->thousandSeparator = '';
        $this->assertSame("123123,1", $this->formatter->asDecimal($value, 1));
        $this->formatter->thousandSeparator = ' ';
        $this->assertSame("12 31 23,1", $this->formatter->asDecimal($value, 1, [\NumberFormatter::GROUPING_SIZE => 2]));

        $value = 123123.123;
        $this->formatter->decimalSeparator = ',';
        $this->formatter->thousandSeparator = ' ';
        $this->assertSame("123 123", $this->formatter->asDecimal($value, 0));
        $this->assertSame("123 123,12", $this->formatter->asDecimal($value, 2));

        $this->formatter->decimalSeparator = null;
        $this->formatter->thousandSeparator = null;
        $value = '-123456.123';
        $this->assertSame("-123,456.123", $this->formatter->asDecimal($value));

704 705 706 707
        // empty input
        $this->assertSame("0", $this->formatter->asInteger(false));
        $this->assertSame("0", $this->formatter->asInteger(""));

708 709
        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asDecimal(null));
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
    }

    public function testAsDecimal()
    {
        $value = 123.12;
        $this->assertSame("123.12", $this->formatter->asDecimal($value));
        $this->assertSame("123.1", $this->formatter->asDecimal($value, 1));
        $this->assertSame("123", $this->formatter->asDecimal($value, 0));
        $value = 123;
        $this->assertSame("123.00", $this->formatter->asDecimal($value));
        $this->formatter->decimalSeparator = ',';
        $this->formatter->thousandSeparator = '.';
        $value = 123.12;
        $this->assertSame("123,12", $this->formatter->asDecimal($value));
        $this->assertSame("123,1", $this->formatter->asDecimal($value, 1));
        $this->assertSame("123", $this->formatter->asDecimal($value, 0));
        $value = 123123.123;
        $this->assertSame("123.123,12", $this->formatter->asDecimal($value));

        $value = 123123.123;
730 731
        $this->assertSame("123.123,12", $this->formatter->asDecimal($value));
        $this->assertSame("123.123,12", $this->formatter->asDecimal($value, 2));
732 733
        $this->formatter->decimalSeparator = ',';
        $this->formatter->thousandSeparator = ' ';
734
        $this->assertSame("123 123,12", $this->formatter->asDecimal($value));
735 736
        $this->assertSame("123 123,12", $this->formatter->asDecimal($value, 2));
        $this->formatter->thousandSeparator = '';
737
        $this->assertSame("123123,12", $this->formatter->asDecimal($value));
738 739
        $this->assertSame("123123,12", $this->formatter->asDecimal($value, 2));

740 741
        $this->formatter->decimalSeparator = null;
        $this->formatter->thousandSeparator = null;
742
        $value = '-123456.123';
743
        $this->assertSame("-123,456.123", $this->formatter->asDecimal($value, 3));
744

745 746 747 748
        // empty input
        $this->assertSame("0", $this->formatter->asInteger(false));
        $this->assertSame("0", $this->formatter->asInteger(""));

749 750 751 752 753 754 755 756 757 758 759
        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asDecimal(null));
    }

    public function testIntlAsPercent()
    {
        $this->testAsPercent();
    }

    public function testAsPercent()
    {
760 761 762 763 764 765
        $this->assertSame('12,300%', $this->formatter->asPercent(123));
        $this->assertSame('12,300%', $this->formatter->asPercent('123'));
        $this->assertSame("12%", $this->formatter->asPercent(0.1234));
        $this->assertSame("12%", $this->formatter->asPercent('0.1234'));
        $this->assertSame("-1%", $this->formatter->asPercent(-0.009343));
        $this->assertSame("-1%", $this->formatter->asPercent('-0.009343'));
766

767 768 769 770
        // empty input
        $this->assertSame("0", $this->formatter->asInteger(false));
        $this->assertSame("0", $this->formatter->asInteger(""));

771 772 773 774 775 776
        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asPercent(null));
    }

    public function testIntlAsCurrency()
    {
Carsten Brandt committed
777 778 779 780 781 782
        $this->formatter->locale = 'en-US';
        $this->assertSame('$123.00', $this->formatter->asCurrency('123'));
        $this->assertSame('$123,456.00', $this->formatter->asCurrency('123456'));
        $this->assertSame('$0.00', $this->formatter->asCurrency('0'));

        $this->formatter->locale = 'en-US';
783 784 785 786
        $this->formatter->currencyCode = 'USD';
        $this->assertSame('$123.00', $this->formatter->asCurrency('123'));
        $this->assertSame('$123,456.00', $this->formatter->asCurrency('123456'));
        $this->assertSame('$0.00', $this->formatter->asCurrency('0'));
787 788 789 790 791
        // Starting from ICU 52.1, negative currency value will be formatted as -$123,456.12
        // see: http://source.icu-project.org/repos/icu/icu/tags/release-52-1/source/data/locales/en.txt
//		$value = '-123456.123';
//		$this->assertSame("($123,456.12)", $this->formatter->asCurrency($value));

Carsten Brandt committed
792 793 794
        $this->formatter->locale = 'de-DE';
        $this->formatter->currencyCode = null;
        $this->assertSame('123,00 €', $this->formatter->asCurrency('123'));
795 796 797 798 799
        $this->formatter->currencyCode = 'USD';
        $this->assertSame('123,00 $', $this->formatter->asCurrency('123'));
        $this->formatter->currencyCode = 'EUR';
        $this->assertSame('123,00 €', $this->formatter->asCurrency('123'));

800 801 802 803
        // empty input
        $this->assertSame("0", $this->formatter->asInteger(false));
        $this->assertSame("0", $this->formatter->asInteger(""));

804 805 806 807
        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
    }

808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    /**
     * https://github.com/yiisoft/yii2/pull/5261
     */
    public function testIntlIssue5261()
    {
        $this->formatter->locale = 'en-US';
        $this->formatter->numberFormatterOptions = [
            \NumberFormatter::FRACTION_DIGITS => 0
        ];
        $this->formatter->numberFormatterTextOptions = [
            \NumberFormatter::CURRENCY_CODE => 'EUR'
        ];
        $this->assertSame('€100', $this->formatter->asCurrency(100, 'EUR'));
    }

823 824 825 826 827
    public function testAsCurrency()
    {
        $this->formatter->currencyCode = 'USD';
        $this->assertSame('USD 123.00', $this->formatter->asCurrency('123'));
        $this->assertSame('USD 0.00', $this->formatter->asCurrency('0'));
828
        $this->assertSame('USD -123.45', $this->formatter->asCurrency('-123.45'));
829 830 831 832 833
        $this->assertSame('USD -123.45', $this->formatter->asCurrency(-123.45));

        $this->formatter->currencyCode = 'EUR';
        $this->assertSame('EUR 123.00', $this->formatter->asCurrency('123'));
        $this->assertSame('EUR 0.00', $this->formatter->asCurrency('0'));
834
        $this->assertSame('EUR -123.45', $this->formatter->asCurrency('-123.45'));
835 836
        $this->assertSame('EUR -123.45', $this->formatter->asCurrency(-123.45));

837 838 839 840
        // empty input
        $this->assertSame("0", $this->formatter->asInteger(false));
        $this->assertSame("0", $this->formatter->asInteger(""));

841 842 843 844 845 846
        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
    }

    public function testIntlAsScientific()
    {
847 848 849 850 851 852 853
        $value = '123';
        $this->assertSame('1.23E2', $this->formatter->asScientific($value));
        $value = '123456';
        $this->assertSame("1.23456E5", $this->formatter->asScientific($value));
        $value = '-123456.123';
        $this->assertSame("-1.23456123E5", $this->formatter->asScientific($value));

854 855 856 857
        // empty input
        $this->assertSame("0", $this->formatter->asInteger(false));
        $this->assertSame("0", $this->formatter->asInteger(""));

858 859
        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
860 861 862 863 864
    }

    public function testAsScientific()
    {
        $value = '123';
865
        $this->assertSame('1.23E+2', $this->formatter->asScientific($value, 2));
866
        $value = '123456';
867
        $this->assertSame("1.234560E+5", $this->formatter->asScientific($value));
868
        $value = '-123456.123';
869
        $this->assertSame("-1.234561E+5", $this->formatter->asScientific($value));
870

871 872 873 874
        // empty input
        $this->assertSame("0", $this->formatter->asInteger(false));
        $this->assertSame("0", $this->formatter->asInteger(""));

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
    }

    public function testIntlAsSpellout()
    {
        $this->assertSame('one hundred twenty-three', $this->formatter->asSpellout(123));

        $this->formatter->locale = 'de_DE';
        $this->assertSame('ein­hundert­drei­und­zwanzig', $this->formatter->asSpellout(123));

        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asSpellout(null));
    }

    public function testIntlAsOrdinal()
    {
        $this->assertSame('0th', $this->formatter->asOrdinal(0));
        $this->assertSame('1st', $this->formatter->asOrdinal(1));
        $this->assertSame('2nd', $this->formatter->asOrdinal(2));
        $this->assertSame('3rd', $this->formatter->asOrdinal(3));
        $this->assertSame('5th', $this->formatter->asOrdinal(5));

        $this->formatter->locale = 'de_DE';
        $this->assertSame('0.', $this->formatter->asOrdinal(0));
        $this->assertSame('1.', $this->formatter->asOrdinal(1));
        $this->assertSame('2.', $this->formatter->asOrdinal(2));
        $this->assertSame('3.', $this->formatter->asOrdinal(3));
        $this->assertSame('5.', $this->formatter->asOrdinal(5));

        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asOrdinal(null));
    }

Carsten Brandt committed
909 910 911 912 913 914 915 916 917 918
    public function testIntlAsShortSize()
    {
        $this->formatter->numberFormatterOptions = [
            \NumberFormatter::MIN_FRACTION_DIGITS => 0,
            \NumberFormatter::MAX_FRACTION_DIGITS => 2,
        ];

        // tests for base 1000
        $this->formatter->sizeFormatBase = 1000;
        $this->assertSame("999 B", $this->formatter->asShortSize(999));
919
        $this->assertSame("999 B", $this->formatter->asShortSize('999'));
Carsten Brandt committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
        $this->assertSame("1.05 MB", $this->formatter->asShortSize(1024 * 1024));
        $this->assertSame("1 KB", $this->formatter->asShortSize(1000));
        $this->assertSame("1.02 KB", $this->formatter->asShortSize(1023));
        $this->assertNotEquals("3 PB", $this->formatter->asShortSize(3 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000)); // this is 3 EB not 3 PB
        // tests for base 1024
        $this->formatter->sizeFormatBase = 1024;
        $this->assertSame("1 KiB", $this->formatter->asShortSize(1024));
        $this->assertSame("1 MiB", $this->formatter->asShortSize(1024 * 1024));
        // https://github.com/yiisoft/yii2/issues/4960
        $this->assertSame("1023 B", $this->formatter->asShortSize(1023));
        $this->assertSame("5 GiB", $this->formatter->asShortSize(5 * 1024 * 1024 * 1024));
        $this->assertNotEquals("5 PiB", $this->formatter->asShortSize(5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)); // this is 5 EiB not 5 PiB
        //$this->assertSame("1 YiB", $this->formatter->asShortSize(pow(2, 80)));
        $this->assertSame("2 GiB", $this->formatter->asShortSize(2147483647)); // round 1.999 up to 2
        $this->formatter->decimalSeparator = ',';
        $this->formatter->numberFormatterOptions = [];
        $this->assertSame("1,001 KiB", $this->formatter->asShortSize(1025, 3));

938 939 940
        // empty values
        $this->assertSame('0 B', $this->formatter->asShortSize(0));

Carsten Brandt committed
941
        // null display
942
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asShortSize(null));
Carsten Brandt committed
943 944 945 946 947 948 949
    }

    public function testAsShortSize()
    {
        // tests for base 1000
        $this->formatter->sizeFormatBase = 1000;
        $this->assertSame("999 B", $this->formatter->asShortSize(999));
950
        $this->assertSame("999 B", $this->formatter->asShortSize('999'));
Carsten Brandt committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
        $this->assertSame("1.05 MB", $this->formatter->asShortSize(1024 * 1024));
        $this->assertSame("1.0486 MB", $this->formatter->asShortSize(1024 * 1024, 4));
        $this->assertSame("1.00 KB", $this->formatter->asShortSize(1000));
        $this->assertSame("1.02 KB", $this->formatter->asShortSize(1023));
        $this->assertNotEquals("3 PB", $this->formatter->asShortSize(3 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000)); // this is 3 EB not 3 PB
        // tests for base 1024
        $this->formatter->sizeFormatBase = 1024;
        $this->assertSame("1.00 KiB", $this->formatter->asShortSize(1024));
        $this->assertSame("1.00 MiB", $this->formatter->asShortSize(1024 * 1024));
        // https://github.com/yiisoft/yii2/issues/4960
        $this->assertSame("1023 B", $this->formatter->asShortSize(1023));
        $this->assertSame("5.00 GiB", $this->formatter->asShortSize(5 * 1024 * 1024 * 1024));
        $this->assertNotEquals("5.00 PiB", $this->formatter->asShortSize(5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)); // this is 5 EiB not 5 PiB
        //$this->assertSame("1 YiB", $this->formatter->asShortSize(pow(2, 80)));
        $this->assertSame("2.00 GiB", $this->formatter->asShortSize(2147483647)); // round 1.999 up to 2
        $this->formatter->decimalSeparator = ',';
        $this->assertSame("1,001 KiB", $this->formatter->asShortSize(1025, 3));

969
        // empty values
970
        $this->assertSame('0 B', $this->formatter->asShortSize(0));
971

Carsten Brandt committed
972
        // null display
973
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asShortSize(null));
Carsten Brandt committed
974 975
    }

976 977
    public function testIntlAsSize()
    {
Carsten Brandt committed
978 979 980 981 982 983 984 985
        $this->formatter->numberFormatterOptions = [
            \NumberFormatter::MIN_FRACTION_DIGITS => 0,
            \NumberFormatter::MAX_FRACTION_DIGITS => 2,
        ];

        // tests for base 1000
        $this->formatter->sizeFormatBase = 1000;
        $this->assertSame("999 bytes", $this->formatter->asSize(999));
986
        $this->assertSame("999 bytes", $this->formatter->asSize('999'));
Carsten Brandt committed
987 988 989 990 991 992 993 994 995
        $this->assertSame("1.05 megabytes", $this->formatter->asSize(1024 * 1024));
        $this->assertSame("1 kilobyte", $this->formatter->asSize(1000));
        $this->assertSame("1.02 kilobytes", $this->formatter->asSize(1023));
        $this->assertSame("3 gigabytes", $this->formatter->asSize(3 * 1000 * 1000 * 1000));
        $this->assertNotEquals("3 PB", $this->formatter->asSize(3 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000)); // this is 3 EB not 3 PB
        // tests for base 1024
        $this->formatter->sizeFormatBase = 1024;
        $this->assertSame("1 kibibyte", $this->formatter->asSize(1024));
        $this->assertSame("1 mebibyte", $this->formatter->asSize(1024 * 1024));
996
        $this->assertSame("1023 bytes", $this->formatter->asSize(1023));
Carsten Brandt committed
997 998 999 1000 1001
        $this->assertSame("5 gibibytes", $this->formatter->asSize(5 * 1024 * 1024 * 1024));
        $this->assertNotEquals("5 pibibytes", $this->formatter->asSize(5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)); // this is 5 EiB not 5 PiB
        $this->assertSame("2 gibibytes", $this->formatter->asSize(2147483647)); // round 1.999 up to 2
        $this->formatter->decimalSeparator = ',';
        $this->formatter->numberFormatterOptions = [];
1002
        $this->assertSame("1,001 kibibytes", $this->formatter->asSize(1025, 3));
Carsten Brandt committed
1003 1004 1005

        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asSize(null));
1006 1007 1008 1009
    }

    public function testAsSize()
    {
1010
        // tests for base 1000
Carsten Brandt committed
1011 1012
        $this->formatter->sizeFormatBase = 1000;
        $this->assertSame("999 bytes", $this->formatter->asSize(999));
1013
        $this->assertSame("999 bytes", $this->formatter->asSize('999'));
Carsten Brandt committed
1014
        $this->assertSame("1.05 megabytes", $this->formatter->asSize(1024 * 1024));
1015 1016
        $this->assertSame("1.0486 megabytes", $this->formatter->asSize(1024 * 1024, 4));
        $this->assertSame("1.00 kilobyte", $this->formatter->asSize(1000));
Carsten Brandt committed
1017
        $this->assertSame("1.02 kilobytes", $this->formatter->asSize(1023));
1018
        $this->assertSame("3.00 gigabytes", $this->formatter->asSize(3 * 1000 * 1000 * 1000));
1019 1020
        $this->assertNotEquals("3 PB", $this->formatter->asSize(3 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000)); // this is 3 EB not 3 PB
        // tests for base 1024
Carsten Brandt committed
1021
        $this->formatter->sizeFormatBase = 1024;
1022 1023 1024 1025 1026 1027
        $this->assertSame("1.00 kibibyte", $this->formatter->asSize(1024));
        $this->assertSame("1.00 mebibyte", $this->formatter->asSize(1024 * 1024));
        $this->assertSame("1023 bytes", $this->formatter->asSize(1023));
        $this->assertSame("5.00 gibibytes", $this->formatter->asSize(5 * 1024 * 1024 * 1024));
        $this->assertNotEquals("5.00 pibibytes", $this->formatter->asSize(5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)); // this is 5 EiB not 5 PiB
        $this->assertSame("2.00 gibibytes", $this->formatter->asSize(2147483647)); // round 1.999 up to 2
Carsten Brandt committed
1028 1029
        $this->formatter->decimalSeparator = ',';
        $this->formatter->numberFormatterOptions = [];
1030
        $this->assertSame("1,001 kibibytes", $this->formatter->asSize(1025, 3));
Carsten Brandt committed
1031 1032 1033

        // null display
        $this->assertSame($this->formatter->nullDisplay, $this->formatter->asSize(null));
1034
    }
1035

1036 1037 1038
    public function testIntlAsSizeConfiguration()
    {
        $this->assertSame("1023 bytes", $this->formatter->asSize(1023));
1039
        $this->assertSame("1023 B", $this->formatter->asShortSize(1023));
1040 1041
        $this->formatter->thousandSeparator = '.';
        $this->assertSame("1023 bytes", $this->formatter->asSize(1023));
1042
        $this->assertSame("1023 B", $this->formatter->asShortSize(1023));
1043 1044
    }

1045 1046 1047 1048 1049
    /**
     * https://github.com/yiisoft/yii2/issues/4960
     */
    public function testAsSizeConfiguration()
    {
1050
        $this->assertSame("1023 bytes", $this->formatter->asSize(1023));
1051
        $this->assertSame("1023 B", $this->formatter->asShortSize(1023));
1052
        $this->formatter->thousandSeparator = '.';
1053
        $this->assertSame("1023 bytes", $this->formatter->asSize(1023));
1054
        $this->assertSame("1023 B", $this->formatter->asShortSize(1023));
1055
    }
Qiang Xue committed
1056
}