Skip to content
This repository was archived by the owner on Oct 27, 2022. It is now read-only.

Commit 11765a5

Browse files
authored
Merge pull request #25 from grayloon/remove-out-if-sync-magento-categories
Remove old product links on sync
2 parents 07f0feb + 3a8b73a commit 11765a5

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Jobs/SyncMagentoProductCategories.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Grayloon\MagentoStorage\Jobs;
44

55
use Grayloon\Magento\Magento;
6+
use Grayloon\MagentoStorage\Models\MagentoProductCategory;
67
use Illuminate\Bus\Queueable;
78
use Illuminate\Contracts\Queue\ShouldQueue;
89
use Illuminate\Foundation\Bus\Dispatchable;
@@ -36,6 +37,8 @@ public function handle()
3637
$this->links = (new Magento())->api('categories')
3738
->products($this->categoryId)
3839
->json();
40+
41+
MagentoProductCategory::where('magento_category_id', $this->categoryId)->delete();
3942

4043
foreach ($this->links as $link) {
4144
SyncMagentoProductCategory::dispatch($link['sku'], $this->categoryId, $link['position']);

tests/Jobs/SyncMagentoProductCategoriesTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Grayloon\MagentoStorage\Tests\Jobs;
44

55
use Grayloon\MagentoStorage\Database\Factories\MagentoCategoryFactory;
6+
use Grayloon\MagentoStorage\Database\Factories\MagentoProductCategoryFactory;
67
use Grayloon\MagentoStorage\Database\Factories\MagentoProductFactory;
78
use Grayloon\MagentoStorage\Jobs\SyncMagentoProductCategories;
89
use Grayloon\MagentoStorage\Models\MagentoProductCategory;
@@ -45,4 +46,30 @@ public function test_can_sync_magento_product_categories_handles_empty_response(
4546

4647
$this->assertEquals(0, MagentoProductCategory::count());
4748
}
49+
50+
/** @test */
51+
public function it_removes_out_of_sync_categories()
52+
{
53+
config(['magento.store_code' => 'foo']);
54+
$category = MagentoCategoryFactory::new()->create();
55+
$oldProductLink = MagentoProductCategoryFactory::new()->create([
56+
'magento_category_id' => $category->id,
57+
]);
58+
$otherCategoryLink = MagentoProductCategoryFactory::new()->create();
59+
$product = MagentoProductFactory::new()->create();
60+
Http::fake([
61+
'*rest/foo/V1/categories/'.$category->id.'/products' => Http::response([
62+
[
63+
'sku' => $product->sku,
64+
'category_id' => $category->id,
65+
'position' => 1,
66+
],
67+
], 200),
68+
]);
69+
70+
(new SyncMagentoProductCategories($category->id))->handle();
71+
72+
$this->assertEquals(2, MagentoProductCategory::count());
73+
$this->assertDeleted($oldProductLink);
74+
}
4875
}

0 commit comments

Comments
 (0)