Skip to content

Commit

Permalink
Merge pull request #105 from Astrotomic/ft-extend-cascade-unittests
Browse files Browse the repository at this point in the history
extend the cascade delete unittests
  • Loading branch information
Gummibeer authored Nov 6, 2019
2 parents 0ae89b4 + 44e9d76 commit f7b294c
Show file tree
Hide file tree
Showing 21 changed files with 82 additions and 47 deletions.
4 changes: 0 additions & 4 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
preset: laravel

risky: true

disabled:
- no_useless_return
- simplified_null_return
23 changes: 15 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ dist: bionic

services:
- mysql
- postgresql

addons:
postgresql: "9.4"

cache:
directories:
Expand All @@ -14,23 +18,26 @@ php:
- 7.3

env:
- LARAVEL='5.8.*' COMPOSER_FLAGS='--prefer-lowest'
- LARAVEL='5.8.*' COMPOSER_FLAGS='--prefer-stable'
- LARAVEL='^6.0' COMPOSER_FLAGS='--prefer-lowest'
- LARAVEL='^6.0' COMPOSER_FLAGS='--prefer-stable'
- LARAVEL='5.8.*' COMPOSER_FLAGS='--prefer-lowest' DB_CONNECTION='mysql' DB_DATABASE='translatable_test' DB_USERNAME='travis'
- LARAVEL='5.8.*' COMPOSER_FLAGS='--prefer-stable' DB_CONNECTION='mysql' DB_DATABASE='translatable_test' DB_USERNAME='travis'
- LARAVEL='^6.0' COMPOSER_FLAGS='--prefer-lowest' DB_CONNECTION='mysql' DB_DATABASE='translatable_test' DB_USERNAME='travis'
- LARAVEL='^6.0' COMPOSER_FLAGS='--prefer-stable' DB_CONNECTION='mysql' DB_DATABASE='translatable_test' DB_USERNAME='travis'

matrix:
include:
- php: 7.4snapshot
env: LARAVEL='^6.0' COMPOSER_FLAGS='--prefer-lowest'
env: LARAVEL='^6.0' COMPOSER_FLAGS='--prefer-lowest' DB_CONNECTION='mysql' DB_DATABASE='translatable_test' DB_USERNAME='travis'
- php: 7.4snapshot
env: LARAVEL='^6.0' COMPOSER_FLAGS='--prefer-stable'
env: LARAVEL='^6.0' COMPOSER_FLAGS='--prefer-stable' DB_CONNECTION='mysql' DB_DATABASE='translatable_test' DB_USERNAME='travis'
- php: 7.3
env: LARAVEL='^6.0' COMPOSER_FLAGS='--prefer-stable' DB_CONNECTION='pgsql' DB_DATABASE='translatable_test' DB_USERNAME='postgres'
fast_finish: true
allow_failures:
- php: 7.4snapshot

before_install:
- mysql -e 'CREATE DATABASE translatable_test;'
- sh -c "if [ '$DB_CONNECTION' = 'mysql' ]; then mysql -e 'CREATE DATABASE translatable_test;'; fi"
- sh -c "if [ '$DB_CONNECTION' = 'pgsql' ]; then psql -c 'create database translatable_test;' -U postgres; fi"
- composer config discard-changes true
- travis_retry composer self-update
- travis_retry composer require --dev "laravel/framework:${LARAVEL}" --no-interaction --no-update
Expand All @@ -39,7 +46,7 @@ install:
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-dist --no-suggest --no-interaction

script:
- DB_CONNECTION=mysql DB_DATABASE=translatable_test DB_USERNAME=travis vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## v11

### v11.6.1

* Fix PHP translation cascade deletion - [\#105](https://github.com/Astrotomic/laravel-translatable/pull/105)

### v11.6.0

* Drop PHP 7.1 support
Expand Down
2 changes: 1 addition & 1 deletion src/Translatable/Contracts/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Astrotomic\Translatable\Contracts;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

interface Translatable
{
Expand Down
4 changes: 2 additions & 2 deletions src/Translatable/Locales.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Astrotomic\Translatable;

use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Astrotomic\Translatable\Exception\LocalesNotDefinedException;
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Translation\Translator as TranslatorContract;

class Locales implements Arrayable, ArrayAccess
Expand Down
2 changes: 1 addition & 1 deletion src/Translatable/Traits/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Astrotomic\Translatable\Traits;

use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
* @property-read string $translationModel
Expand Down
2 changes: 1 addition & 1 deletion src/Translatable/Traits/Scopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Astrotomic\Translatable\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\JoinClause;

trait Scopes
{
Expand Down
13 changes: 6 additions & 7 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Astrotomic\Translatable;

use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Astrotomic\Translatable\Traits\Relationship;
use Astrotomic\Translatable\Traits\Scopes;
use Illuminate\Database\Eloquent\Collection;
use Astrotomic\Translatable\Traits\Relationship;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

/**
* @property-read null|Model $translation
Expand Down Expand Up @@ -35,7 +35,8 @@ public static function bootTranslatable(): void
return $model->saveTranslations();
});

static::deleted(function (Model $model) {
static::deleting(function (Model $model) {
/* @var Translatable $model */
if (self::$deleteTranslationsCascade === true) {
return $model->deleteTranslations();
}
Expand Down Expand Up @@ -103,9 +104,7 @@ public function deleteTranslations($locales = null): void
$translations = $this->translations()->whereIn($this->getLocaleKey(), $locales)->get();
}

foreach ($translations as $translation) {
$translation->delete();
}
$translations->each->delete();

// we need to manually "reload" the collection built from the relationship
// otherwise $this->translations()->get() would NOT be the same as $this->translations
Expand Down
2 changes: 1 addition & 1 deletion src/Translatable/Validation/RuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Astrotomic\Translatable\Validation;

use InvalidArgumentException;
use Astrotomic\Translatable\Locales;
use Illuminate\Contracts\Config\Repository;
use InvalidArgumentException;

class RuleFactory
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Eloquent/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Astrotomic\Translatable\Tests\Eloquent;

use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;

class Country extends Eloquent implements TranslatableContract
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Eloquent/CountryStrict.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Astrotomic\Translatable\Tests\Eloquent;

use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;

class CountryStrict extends Eloquent implements TranslatableContract
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Eloquent/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Astrotomic\Translatable\Tests\Eloquent;

use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;

class Person extends Eloquent implements TranslatableContract
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Eloquent/Vegetable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Astrotomic\Translatable\Tests\Eloquent;

use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;

class Vegetable extends Eloquent implements TranslatableContract
{
Expand Down
2 changes: 1 addition & 1 deletion tests/LocalesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Astrotomic\Translatable\Tests;

use Astrotomic\Translatable\Locales;
use Astrotomic\Translatable\Exception\LocalesNotDefinedException;
use Astrotomic\Translatable\Locales;

final class LocalesTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/ScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Astrotomic\Translatable\Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Astrotomic\Translatable\Tests\Eloquent\Country;
use Astrotomic\Translatable\Tests\Eloquent\Vegetable;
use Illuminate\Foundation\Testing\RefreshDatabase;

final class ScopesTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Astrotomic\Translatable\Tests;

use Orchestra\Testbench\TestCase as OrchestraTestCase;
use Astrotomic\Translatable\TranslatableServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

abstract class TestCase extends OrchestraTestCase
{
Expand Down
41 changes: 35 additions & 6 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Astrotomic\Translatable\Tests;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\App;
use Astrotomic\Translatable\Locales;
use Astrotomic\Translatable\Tests\Eloquent\Person;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Astrotomic\Translatable\Tests\Eloquent\Country;
use Astrotomic\Translatable\Tests\Eloquent\Vegetable;
use Astrotomic\Translatable\Tests\Eloquent\CountryStrict;
use Illuminate\Database\Eloquent\MassAssignmentException;
use Astrotomic\Translatable\Tests\Eloquent\CountryTranslation;
use Astrotomic\Translatable\Tests\Eloquent\Person;
use Astrotomic\Translatable\Tests\Eloquent\Vegetable;
use Astrotomic\Translatable\Tests\Eloquent\VegetableTranslation;
use Illuminate\Database\Eloquent\MassAssignmentException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;

final class TranslatableTest extends TestCase
{
Expand Down Expand Up @@ -1005,8 +1005,12 @@ public function it_does_not_delete_translations_on_cascade_by_default()
{
$vegetable = factory(Vegetable::class)->create(['name:en' => 'Peas']);

$this->assertDatabaseHas('vegetables', ['identity' => $vegetable->identity]);
$this->assertDatabaseHas('vegetable_translations', ['vegetable_identity' => $vegetable->identity]);

$vegetable->delete();

$this->assertDatabaseMissing('vegetables', ['identity' => $vegetable->identity]);
$this->assertDatabaseHas('vegetable_translations', ['vegetable_identity' => $vegetable->identity]);
}

Expand All @@ -1016,8 +1020,12 @@ public function it_deletes_translations_on_cascade()
Vegetable::enableDeleteTranslationsCascade();
$vegetable = factory(Vegetable::class)->create(['name:en' => 'Peas']);

$this->assertDatabaseHas('vegetables', ['identity' => $vegetable->identity]);
$this->assertDatabaseHas('vegetable_translations', ['vegetable_identity' => $vegetable->identity]);

$vegetable->delete();

$this->assertDatabaseMissing('vegetables', ['identity' => $vegetable->identity]);
$this->assertDatabaseMissing('vegetable_translations', ['vegetable_identity' => $vegetable->identity]);
}

Expand All @@ -1028,8 +1036,29 @@ public function it_does_not_delete_on_cascade_after_retrieving_a_model()
$vegetable = factory(Vegetable::class)->create(['name:en' => 'Peas']);
Vegetable::disableDeleteTranslationsCascade();

$this->assertDatabaseHas('vegetables', ['identity' => $vegetable->identity]);
$this->assertDatabaseHas('vegetable_translations', ['vegetable_identity' => $vegetable->identity]);

$vegetable->delete();

$this->assertDatabaseMissing('vegetables', ['identity' => $vegetable->identity]);
$this->assertDatabaseHas('vegetable_translations', ['vegetable_identity' => $vegetable->identity]);
}

/** @test */
public function it_can_restore_translations_in_a_transaction()
{
Vegetable::enableDeleteTranslationsCascade();
$vegetable = factory(Vegetable::class)->create(['name:en' => 'Peas']);

$this->assertDatabaseHas('vegetables', ['identity' => $vegetable->identity]);
$this->assertDatabaseHas('vegetable_translations', ['vegetable_identity' => $vegetable->identity]);

DB::connection()->beginTransaction();
$vegetable->delete();
DB::connection()->rollBack();

$this->assertDatabaseHas('vegetables', ['identity' => $vegetable->identity]);
$this->assertDatabaseHas('vegetable_translations', ['vegetable_identity' => $vegetable->identity]);
}
}
6 changes: 3 additions & 3 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Astrotomic\Translatable\Tests;

use InvalidArgumentException;
use Illuminate\Validation\Rule;
use Astrotomic\Translatable\Locales;
use Illuminate\Validation\Rules\RequiredIf;
use Astrotomic\Translatable\Validation\RuleFactory;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\RequiredIf;
use InvalidArgumentException;

final class ValidationTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/factories/CountryFactory.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Faker\Generator as Faker;
use Astrotomic\Translatable\Tests\Eloquent\Country;
use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factory as ModelFactory;

/* @var ModelFactory $factory */
Expand Down
2 changes: 1 addition & 1 deletion tests/factories/VegetableFactory.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Faker\Generator as Faker;
use Astrotomic\Translatable\Tests\Eloquent\Vegetable;
use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factory as ModelFactory;

/* @var ModelFactory $factory */
Expand Down
8 changes: 4 additions & 4 deletions tests/migrations/2013_11_28_152610_create_tables.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Builder;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
use Illuminate\Support\Facades\Schema;

class CreateTables extends Migration
{
Expand Down

0 comments on commit f7b294c

Please sign in to comment.