This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #24 from grayloon/stable-version
Inline Commands for API sync operations, cleanup command
- Loading branch information
Showing
9 changed files
with
245 additions
and
55 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,38 @@ | ||
<?php | ||
|
||
namespace Grayloon\MagentoStorage\Console; | ||
|
||
use Grayloon\MagentoStorage\Models\MagentoCustomAttribute; | ||
use Illuminate\Console\Command; | ||
|
||
class MagentoCleanAttributesCommand extends Command | ||
{ | ||
protected $signature = 'magento:clean'; | ||
|
||
protected $description = 'Cleans up the database by removing missing relationships.'; | ||
|
||
protected $deleted = 0; | ||
protected $attributes; | ||
protected $count; | ||
|
||
public function handle() | ||
{ | ||
$count = MagentoCustomAttribute::count(); | ||
|
||
$this->info('Checking ' . $count . ' custom attributes...'); | ||
|
||
$this->attributes = MagentoCustomAttribute::with('attributable') | ||
->cursor(); | ||
|
||
$this->withProgressBar($this->attributes, function ($attribute) { | ||
if (! $attribute->attributable) { | ||
$attribute->delete(); | ||
|
||
$this->deleted++; | ||
} | ||
}); | ||
|
||
$this->newLine(); | ||
$this->info($this->deleted . ' attributes with missing attributable relationships removed.'); | ||
} | ||
} |
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
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,37 @@ | ||
<?php | ||
|
||
namespace Grayloon\MagentoStorage\Tests\Console; | ||
|
||
use Grayloon\MagentoStorage\Database\Factories\MagentoCustomAttributeFactory; | ||
use Grayloon\MagentoStorage\Database\Factories\MagentoProductFactory; | ||
use Grayloon\MagentoStorage\Models\MagentoCustomAttribute; | ||
use Grayloon\MagentoStorage\Models\MagentoProduct; | ||
use Grayloon\MagentoStorage\Tests\TestCase; | ||
|
||
class MagentoCleanAttributesCommandTest extends TestCase | ||
{ | ||
public function test_it_removes_missing_relational_custom_attributes() | ||
{ | ||
$attribute = MagentoCustomAttributeFactory::new()->create([ | ||
'attributable_type' => MagentoProduct::class, | ||
'attributable_id' => 123, | ||
]); | ||
|
||
$this->artisan('magento:clean'); | ||
|
||
$this->assertDeleted($attribute); | ||
} | ||
|
||
public function test_it_keeps_relational_match_custom_attribute() | ||
{ | ||
$product = MagentoProductFactory::new()->create(); | ||
$attribute = MagentoCustomAttributeFactory::new()->create([ | ||
'attributable_type' => MagentoProduct::class, | ||
'attributable_id' => $product->id, | ||
]); | ||
|
||
$this->artisan('magento:clean'); | ||
|
||
$this->assertEquals(1, MagentoCustomAttribute::count()); | ||
} | ||
} |
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