-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from cesargb/residual
add clean residuals
- Loading branch information
Showing
14 changed files
with
498 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use PhpCsFixer\Finder; | ||
use PhpCsFixer\Config; | ||
|
||
$finder = Finder::create() | ||
->exclude('build') | ||
->exclude('vendor') | ||
->in(__DIR__) | ||
->ignoreDotFiles(true) | ||
->ignoreVCS(true); | ||
|
||
return Config::create() | ||
->setRules([ | ||
'@PSR2' => true, | ||
'array_syntax' => ['syntax' => 'short'], | ||
'ordered_imports' => ['sort_algorithm' => 'length'], | ||
'no_unused_imports' => true, | ||
'no_useless_else' => true, | ||
'no_useless_return' => true, | ||
'no_superfluous_elseif' => true, | ||
'no_unneeded_curly_braces' => true, | ||
'phpdoc_order' => true, | ||
'phpdoc_types_order' => true, | ||
'align_multiline_comment' => true, | ||
]) | ||
->setUsingCache(false) | ||
->setFinder($finder); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,17 +7,20 @@ | |
"cascade", | ||
"delete", | ||
"polymorphic", | ||
"relation" | ||
"relation", | ||
"residual" | ||
], | ||
"type": "library", | ||
"require": { | ||
"php": "^7.0", | ||
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0", | ||
"illuminate/events": "~5.5.0|~5.6.0|~5.7.0" | ||
"illuminate/database": "~5.5.34|~5.6.0|~5.7.0", | ||
"illuminate/console": "~5.5.34|~5.6.0|~5.7.0", | ||
"illuminate/support": "~5.5.34|~5.6.0|~5.7.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^6.0|^7.0", | ||
"orchestra/testbench": "^3.5" | ||
"orchestra/testbench": "^3.5.5", | ||
"friendsofphp/php-cs-fixer": "^2.14" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
|
@@ -29,12 +32,23 @@ | |
"Tests\\": "tests/" | ||
} | ||
}, | ||
"scripts": { | ||
"test": "vendor/bin/phpunit --colors=always", | ||
"fix": "vendor/bin/php-cs-fixer fix" | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Cesar Garcia", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "stable" | ||
"minimum-stability": "stable", | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Cesargb\\Database\\Support\\CascadeDeleteServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Cesargb\Database\Support; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class CascadeDeleteServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
if ($this->app->runningInConsole()) { | ||
$this->commands([ | ||
MorphCleanCommand::class, | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* Register the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Cesargb\Database\Support; | ||
|
||
use LogicException; | ||
use Illuminate\Console\Command; | ||
|
||
class MorphCleanCommand extends Command | ||
{ | ||
protected $signature = 'morph:clean'; | ||
|
||
protected $description = 'Clean break relations morph'; | ||
|
||
public function handle() | ||
{ | ||
foreach (get_declared_classes() as $class) { | ||
if (array_key_exists(CascadeDelete::class, class_uses($class))) { | ||
try { | ||
(new $class)->deleteMorphResidual(); | ||
|
||
$this->info(sprintf( | ||
'Clean class %s', | ||
$class | ||
)); | ||
} catch (LogicException $e) { | ||
$this->error(sprintf( | ||
'Error to delete residual of class %s: %s', | ||
$class, | ||
$e->getMessage() | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Tests\Models\Photo; | ||
use Tests\Models\Video; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class CascadeDeleteElocuentTest extends TestCase | ||
{ | ||
public function test_it_can_delete_relations_with_morph_many() | ||
{ | ||
$photo1 = Photo::with('options')->first(); | ||
$photo2 = Photo::with('options')->skip(1)->first(); | ||
|
||
$this->assertGreaterThan(0, count($photo1->options)); | ||
$this->assertGreaterThan(0, count($photo2->options)); | ||
|
||
Photo::first()->delete(); | ||
|
||
$this->assertEquals( | ||
0, | ||
DB::table('options')->where([ | ||
'optionable_type' => Photo::class, | ||
'optionable_id' => $photo1->id, | ||
])->count() | ||
); | ||
|
||
$this->assertEquals( | ||
count($photo2->options), | ||
DB::table('options')->where([ | ||
'optionable_type' => Photo::class, | ||
'optionable_id' => $photo2->id, | ||
])->count() | ||
); | ||
} | ||
|
||
public function test_it_can_delete_relations_with_morph_to_many() | ||
{ | ||
$video1 = Video::with('tags')->first(); | ||
$video2 = Video::with('tags')->skip(1)->first(); | ||
|
||
$this->assertGreaterThan(0, count($video1->tags)); | ||
$this->assertGreaterThan(0, count($video2->tags)); | ||
|
||
Video::first()->delete(); | ||
|
||
$this->assertEquals( | ||
0, | ||
DB::table('taggables')->where([ | ||
'taggable_type' => Video::class, | ||
'taggable_id' => $video1->id, | ||
])->count() | ||
); | ||
|
||
$this->assertEquals( | ||
count($video2->tags), | ||
DB::table('taggables')->where([ | ||
'taggable_type' => Video::class, | ||
'taggable_id' => $video2->id, | ||
])->count() | ||
); | ||
} | ||
} |
Oops, something went wrong.