Skip to content

Commit

Permalink
Merge pull request #404 from Astrotomic/pr-395
Browse files Browse the repository at this point in the history
Pr 395
  • Loading branch information
Gummibeer committed Apr 17, 2024
2 parents 00433e7 + e2e364a commit 990e2c1
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 177 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.gitbook.yaml export-ignore
/.php_cs export-ignore
/.styleci.yml export-ignore
/.phpunit.xml.dist export-ignore
/pint.json export-ignore
28 changes: 24 additions & 4 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,32 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.1', '8.2', '8.3']
laravel: ['10.*', '11.*']
php: [8.0, 8.1, 8.2, 8.3]
laravel: [8.*, 9.*, 10.*, 11.*]
dependency-version: [prefer-stable]
os: [ubuntu-latest]
include:
- laravel: '8.*'
phpunit: '9.*'
- laravel: '9.*'
phpunit: '9.*'
- laravel: '10.*'
phpunit: '10.*'
- laravel: '11.*'
phpunit: '10.*'
exclude:
- laravel: 11.*
php: 8.1
- php: '8.0'
laravel: '10.*'
- php: '8.0'
laravel: '11.*'
- php: '8.2'
laravel: '8.*'
- php: '8.3'
laravel: '8.*'
- php: '8.3'
laravel: '9.*'
- php: '8.1'
laravel: '11.*'

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

Expand All @@ -38,6 +57,7 @@ jobs:
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
composer require "phpunit/phpunit:${{ matrix.phpunit }}" --dev --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
- name: Execute tests
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/vendor/
/composer.lock
/coverage/
/.phpunit.result.cache
/.phpunit.cache
/build/
/coverage.clover
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ If you discover any security related issues, please check [SECURITY](https://git

| Package | Laravel | PHP |
| :------------------ | :---------------------------- | :-------- |
| **v11.13 - v11.13** | `10.* / 11.*` | `^8.1` |
| **v11.13 - v11.13** | `8.* / 9.* / 10.*` / `11.*` | `^8.0` |
| **v11.12 - v11.12** | `8.* / 9.* / 10.*` | `^8.0` |
| **v11.10 - v11.11** | `8.* / 9.*` | `^8.0` |
| **v11.6 - v11.9** | `5.8.* / 6.* / 7.* / 8.*` | `>=7.2` |
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
"docs": "https://docs.astrotomic.info/laravel-translatable"
},
"require": {
"php": "^8.1",
"illuminate/contracts": "^10.0 || ^11.0",
"illuminate/database": "^10.0 || ^11.0",
"illuminate/support": "^10.0 || ^11.0"
"php": "^8.0",
"illuminate/contracts": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"illuminate/database": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0"
},
"require-dev": {
"laravel/legacy-factories": "^1.0.4",
"laravel/pint": "^1.15",
"laravel/pint": "^1.0",
"mockery/mockery": "^1.3.3",
"orchestra/testbench": "^8.0 || ^9.0",
"phpunit/phpunit": "^10.5"
"orchestra/testbench": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"phpunit/phpunit": "^10.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Latest Version](http://img.shields.io/packagist/v/astrotomic/laravel-translatable.svg?label=Release&style=for-the-badge)](https://packagist.org/packages/astrotomic/laravel-translatable) [![MIT License](https://img.shields.io/github/license/Astrotomic/laravel-translatable.svg?label=License&color=blue&style=for-the-badge)](https://github.com/Astrotomic/laravel-translatable/blob/master/LICENSE) [![Offset Earth](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-green?style=for-the-badge)](https://plant.treeware.earth/Astrotomic/laravel-translatable)

[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Astrotomic/laravel-translatable/run-tests?style=flat-square&logoColor=white&logo=github&label=Tests)](https://github.com/Astrotomic/laravel-translatable/actions?query=workflow%3Arun-tests) [![StyleCI](https://styleci.io/repos/192333549/shield)](https://styleci.io/repos/192333549) [![Codecov Coverage](https://img.shields.io/codecov/c/github/Astrotomic/laravel-translatable?logo=codecov&logoColor=white&label=Codecov&style=flat-square)](https://codecov.io/gh/Astrotomic/laravel-translatable) [![Total Downloads](https://img.shields.io/packagist/dt/astrotomic/laravel-translatable.svg?label=Downloads&style=flat-square)](https://packagist.org/packages/astrotomic/laravel-translatable)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Astrotomic/laravel-translatable/phpunit?style=flat-square&logoColor=white&logo=github&label=PHPunit)](https://github.com/Astrotomic/laravel-translatable/actions?query=workflow%3Aphpunit) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Astrotomic/laravel-translatable/pint?style=flat-square&logoColor=white&logo=github&label=Pint)](https://github.com/Astrotomic/laravel-translatable/actions?query=workflow%3Apint) [![Codecov Coverage](https://img.shields.io/codecov/c/github/Astrotomic/laravel-translatable?logo=codecov&logoColor=white&label=Codecov&style=flat-square)](https://codecov.io/gh/Astrotomic/laravel-translatable) [![Total Downloads](https://img.shields.io/packagist/dt/astrotomic/laravel-translatable.svg?label=Downloads&style=flat-square)](https://packagist.org/packages/astrotomic/laravel-translatable)

![Laravel Translatable](.gitbook/assets/socialcard.png)

Expand Down Expand Up @@ -75,6 +75,7 @@ echo $post->translate('fr')->title; // Mon premier post

| Package | Laravel | PHP |
| :------------------ | :---------------------------- | :-------- |
| **v11.13 - v11.13** | `8.* / 9.* / 10.*` / `11.*` | `^8.0` |
| **v11.12 - v11.12** | `8.* / 9.* / 10.*` | `^8.0` |
| **v11.10 - v11.11** | `8.* / 9.*` | `^8.0` |
| **v11.6 - v11.9** | `5.8.* / 6.* / 7.* / 8.*` | `>=7.2` |
Expand Down
29 changes: 29 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"rules": {
"ordered_class_elements": {
"order": [
"use_trait",
"constant_public",
"constant_protected",
"constant_private",
"property_public",
"property_protected",
"property_private",
"construct",
"method_public_static",
"method_public",
"method_protected_static",
"method_protected",
"method_private_static",
"method_private",
"destruct",
"magic"
]
},
"yoda_style": {
"equal": false,
"identical": false,
"less_and_greater": null
}
}
}
10 changes: 5 additions & 5 deletions src/Translatable/Traits/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ public function translation(): HasOne
});
}

public function translations(): HasMany
{
return $this->hasMany($this->getTranslationModelName(), $this->getTranslationRelationKey());
}

protected function localeOrFallback()
{
return $this->useFallback() && ! $this->translations()->where($this->getLocaleKey(), $this->locale())->exists()
? $this->getFallbackLocale()
: $this->locale();
}

public function translations(): HasMany
{
return $this->hasMany($this->getTranslationModelName(), $this->getTranslationRelationKey());
}
}
4 changes: 2 additions & 2 deletions tests/Eloquent/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

class Person extends Eloquent implements TranslatableContract
{
protected $table = 'people';

use Translatable;

/**
Expand Down Expand Up @@ -42,6 +40,8 @@ class Person extends Eloquent implements TranslatableContract
*/
public $localeKey;

protected $table = 'people';

/**
* Mutate name attribute into upper-case.
*
Expand Down
8 changes: 4 additions & 4 deletions tests/Eloquent/Vegetable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class Vegetable extends Eloquent implements TranslatableContract
{
use Translatable;

protected $primaryKey = 'identity';

public $translationForeignKey = 'vegetable_identity';

public $translatedAttributes = ['name'];

protected $fillable = ['quantity'];

public $localeKey;

public $translationModel;

protected $primaryKey = 'identity';

protected $fillable = ['quantity'];
}
13 changes: 7 additions & 6 deletions tests/EloquentOverrideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Tests;

use PHPUnit\Framework\Attributes\Test;
use Tests\Eloquent\Vegetable;

final class EloquentOverrideTest extends TestCase
{
/** @test */
#[Test]
public function to_array_returns_translated_attributes(): void
{
$vegetable = factory(Vegetable::class)->make(['name:en' => 'Peas']);
Expand All @@ -15,15 +16,15 @@ public function to_array_returns_translated_attributes(): void
self::assertEquals('Peas', $vegetable->toArray()['name']);
}

/** @test */
#[Test]
public function to_array_wont_break_if_no_translations_exist(): void
{
$vegetable = factory(Vegetable::class)->make();

self::assertIsArray($vegetable->toArray());
}

/** @test */
#[Test]
public function translated_attributes_can_be_accessed_as_properties(): void
{
$vegetable = factory(Vegetable::class)->make(['name:en' => 'Peas']);
Expand All @@ -32,7 +33,7 @@ public function translated_attributes_can_be_accessed_as_properties(): void
self::assertEquals('Peas', $vegetable->name);
}

/** @test */
#[Test]
public function it_can_hide_translated_attributes(): void
{
$vegetable = factory(Vegetable::class)->make(['name:en' => 'Peas']);
Expand All @@ -44,15 +45,15 @@ public function it_can_hide_translated_attributes(): void
self::assertFalse(isset($vegetable->toArray()['name']));
}

/** @test */
#[Test]
public function it_finds_custom_primary_keys(): void
{
$vegetable = new Vegetable();

self::assertEquals('vegetable_identity', $vegetable->getTranslationRelationKey());
}

/** @test */
#[Test]
public function setAttribute_returns_parent_setAttribute(): void
{
$vegetable = new Vegetable();
Expand Down
Loading

0 comments on commit 990e2c1

Please sign in to comment.