Skip to content

Commit

Permalink
Test translation after serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Apr 30, 2024
1 parent 40a728d commit 0aa5f1a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/Carbon/CarbonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,9 @@ public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null
* ` it can be the unit name (singular or plural) or its shortcut
* ` (y, m, w, d, h, min, s, ms, µs).
* - 'aUnit' entry, prefer "an hour" over "1 hour" if true
* - 'altNumbers' entry, use alternative numbers if available
* (from the current language if true is passed, from the given language(s)
* if array or string is passed)
* - 'join' entry determines how to join multiple parts of the string
* ` - if $join is a string, it's used as a joiner glue
* ` - if $join is a callable/closure, it get the list of string and should return a string
Expand Down
3 changes: 3 additions & 0 deletions src/Carbon/CarbonInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,9 @@ public function getValuesSequence(): array
* ` it can be the unit name (singular or plural) or its shortcut
* ` (y, m, w, d, h, min, s, ms, µs).
* - 'aUnit' entry, prefer "an hour" over "1 hour" if true
* - 'altNumbers' entry, use alternative numbers if available
* (from the current language if true is passed, from the given language(s)
* if array or string is passed)
* - 'join' entry determines how to join multiple parts of the string
* ` - if $join is a string, it's used as a joiner glue
* ` - if $join is a callable/closure, it get the list of string and should return a string
Expand Down
3 changes: 3 additions & 0 deletions src/Carbon/Traits/Difference.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ public function secondsUntilEndOfDay(): float
* ` it can be the unit name (singular or plural) or its shortcut
* ` (y, m, w, d, h, min, s, ms, µs).
* - 'aUnit' entry, prefer "an hour" over "1 hour" if true
* - 'altNumbers' entry, use alternative numbers if available
* (from the current language if true is passed, from the given language(s)
* if array or string is passed)
* - 'join' entry determines how to join multiple parts of the string
* ` - if $join is a string, it's used as a joiner glue
* ` - if $join is a callable/closure, it get the list of string and should return a string
Expand Down
24 changes: 20 additions & 4 deletions tests/CarbonInterval/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,18 +435,34 @@ public function testFromSerialization()
$past = new Carbon('-3 Days');
$today = new Carbon('today');
$interval = $today->diffAsCarbonInterval($past);
/** @var CarbonInterval $copy */
$copy = unserialize(serialize($interval));

$this->assertInstanceOf(CarbonInterval::class, $copy);

$this->assertSame('2 days', $interval->forHumans(parts: 1));
$this->assertSame('2 days', $copy->forHumans(parts: 1));

$this->assertSame(['en'], array_keys($interval->getLocalTranslator()->getMessages()) ?: ['en']);

Check failure on line 446 in tests/CarbonInterval/ConstructTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Symfony\Contracts\Translation\TranslatorInterface::getMessages().

Check failure on line 446 in tests/CarbonInterval/ConstructTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Symfony\Contracts\Translation\TranslatorInterface::getMessages().
$this->assertSame(['en'], array_keys($copy->getLocalTranslator()->getMessages()) ?: ['en']);

Check failure on line 447 in tests/CarbonInterval/ConstructTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Symfony\Contracts\Translation\TranslatorInterface::getMessages().

Check failure on line 447 in tests/CarbonInterval/ConstructTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Symfony\Contracts\Translation\TranslatorInterface::getMessages().
$this->assertSame($interval->locale, $copy->locale);

// Ignore translator for the English comparison
$copy->setLocalTranslator($interval->getLocalTranslator());

$this->assertEquals($interval->locale('en'), $copy);
$this->assertEquals($interval, $copy);

$interval = $today->locale('zh')->diffAsCarbonInterval($past);
$interval = $today->locale('ja')->diffAsCarbonInterval($past);
/** @var CarbonInterval $copy */
$copy = unserialize(serialize($interval));

$this->assertSame(['zh'], array_keys($interval->getLocalTranslator()->getMessages()) ?: ['en']);
$this->assertSame(['zh'], array_keys($copy->getLocalTranslator()->getMessages()) ?: ['en']);
$this->assertInstanceOf(CarbonInterval::class, $copy);

$this->assertSame('二日', $interval->forHumans(['altNumbers' => true, 'parts' => 1]));
$this->assertSame('二日', $copy->forHumans(['altNumbers' => true, 'parts' => 1]));

$this->assertSame(['ja'], array_keys($interval->getLocalTranslator()->getMessages()));

Check failure on line 464 in tests/CarbonInterval/ConstructTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Symfony\Contracts\Translation\TranslatorInterface::getMessages().

Check failure on line 464 in tests/CarbonInterval/ConstructTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Symfony\Contracts\Translation\TranslatorInterface::getMessages().
$this->assertSame(['ja'], array_keys($copy->getLocalTranslator()->getMessages()));

Check failure on line 465 in tests/CarbonInterval/ConstructTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Symfony\Contracts\Translation\TranslatorInterface::getMessages().

Check failure on line 465 in tests/CarbonInterval/ConstructTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Symfony\Contracts\Translation\TranslatorInterface::getMessages().

$this->assertEquals($interval, $copy);
}
Expand Down

0 comments on commit 0aa5f1a

Please sign in to comment.