Skip to content

Commit

Permalink
Handle null locales (#63)
Browse files Browse the repository at this point in the history
* add more php versions to unit tests

* handle nullable locale
  • Loading branch information
guillaume-sainthillier committed Jul 25, 2023
1 parent 94afdf4 commit b8e612c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ on:
jobs:
symfony-tests:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.0', '8.1', '8.2' ]
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: ${{ matrix.php-versions }}
- uses: actions/checkout@v2
- name: Copy .env.test.local
run: php -r "file_exists('.env.test.local') || copy('.env.test', '.env.test.local');"
Expand All @@ -21,7 +24,7 @@ jobs:
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install Dependencies
Expand Down
11 changes: 7 additions & 4 deletions src/Model/TranslatableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,11 @@ public function getTranslations(): Collection
*/
public function hasTranslation(TranslationInterface $translation): bool
{
return isset($this->translationsCache[$translation->getLocale()])
|| (null !== $translation->getLocale() && $this->translations->containsKey($translation->getLocale()));
return null !== $translation->getLocale()
&& (
isset($this->translationsCache[$translation->getLocale()])
|| $this->translations->containsKey($translation->getLocale())
);
}

/**
Expand All @@ -155,7 +158,7 @@ public function hasTranslation(TranslationInterface $translation): bool
*/
public function addTranslation(TranslationInterface $translation): void
{
if (!$this->hasTranslation($translation)) {
if (!$this->hasTranslation($translation) && null !== $translation->getLocale()) {
$this->translationsCache[$translation->getLocale()] = $translation;

$this->translations->set($translation->getLocale(), $translation);
Expand All @@ -170,7 +173,7 @@ public function addTranslation(TranslationInterface $translation): void
*/
public function removeTranslation(TranslationInterface $translation): void
{
if ($this->translations->removeElement($translation)) {
if ($this->translations->removeElement($translation) && null !== $translation->getLocale()) {
unset($this->translationsCache[$translation->getLocale()]);

$translation->setTranslatable(null);
Expand Down
13 changes: 12 additions & 1 deletion tests/Model/TranslatableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
*/
class TranslatableTraitTest extends TestCase
{
/**
* @test getTranslationLocales
*/
public function testSetTranslatable(): void
{
$dummyTranslatable = $this->setTranslatable('es', 'en');
$this->setTranslation(null, 'no locale', $dummyTranslatable);

$this->assertEquals([], $dummyTranslatable->getTranslationLocales());
}

/**
* @test getTranslationLocales
*/
Expand Down Expand Up @@ -79,7 +90,7 @@ public function testGetTranslationWithoutLocales(): void
$dummyTranslatable->getTranslation();
}

private function setTranslation(string $locale, string $translation, TranslatableInterface $translatable): DummyTranslation
private function setTranslation(?string $locale, string $translation, TranslatableInterface $translatable): DummyTranslation
{
$dummyTranslation = new DummyTranslation();
$dummyTranslation->setLocale($locale);
Expand Down

0 comments on commit b8e612c

Please sign in to comment.