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

Commit 37d032b

Browse files
authored
Merge pull request #27 from grayloon/category-link-fix
Skip link to non-existent category
2 parents 80cea84 + ca2c041 commit 37d032b

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

src/Jobs/SyncMagentoProductCategory.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Grayloon\MagentoStorage\Jobs;
44

5-
use Grayloon\MagentoStorage\Models\MagentoProduct;
6-
use Grayloon\MagentoStorage\Models\MagentoProductCategory;
75
use Illuminate\Bus\Queueable;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Queue\InteractsWithQueue;
88
use Illuminate\Contracts\Queue\ShouldQueue;
99
use Illuminate\Foundation\Bus\Dispatchable;
10-
use Illuminate\Queue\InteractsWithQueue;
11-
use Illuminate\Queue\SerializesModels;
10+
use Grayloon\MagentoStorage\Models\MagentoProduct;
11+
use Grayloon\MagentoStorage\Models\MagentoCategory;
12+
use Grayloon\MagentoStorage\Models\MagentoProductCategory;
1213

1314
class SyncMagentoProductCategory implements ShouldQueue
1415
{
@@ -63,9 +64,14 @@ public function handle()
6364
{
6465
$this->product = MagentoProduct::where('sku', $this->sku)->firstOrFail();
6566

66-
MagentoProductCategory::updateOrCreate([
67-
'magento_product_id' => $this->product->id,
68-
'magento_category_id' => $this->categoryId,
69-
], ['position' => $this->position]);
67+
// verify that the category exists.
68+
$category = MagentoCategory::find($this->categoryId);
69+
70+
if ($category) {
71+
MagentoProductCategory::updateOrCreate([
72+
'magento_product_id' => $this->product->id,
73+
'magento_category_id' => $this->categoryId,
74+
], ['position' => $this->position]);
75+
}
7076
}
7177
}

src/Support/HasExtensionAttributes.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ protected function syncExtensionAttributes($attributes, $model)
4343
protected function resolveCategoryLinks($links, $product)
4444
{
4545
foreach ($links as $link) {
46-
MagentoProductCategory::updateOrCreate([
47-
'magento_product_id' => $product->id,
48-
'magento_category_id' => MagentoCategory::find($link['category_id'])->id,
49-
], ['position' => $link['position']]);
46+
$category = MagentoCategory::find($link['category_id']);
47+
48+
if ($category) {
49+
MagentoProductCategory::updateOrCreate([
50+
'magento_product_id' => $product->id,
51+
'magento_category_id' => $category->id,
52+
], ['position' => $link['position']]);
53+
}
5054
}
5155
}
5256
}

tests/Jobs/SyncMagentoProductCategoryTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,14 @@ public function test_product_categories_sync_missing_product_throws_exception()
4949

5050
SyncMagentoProductCategory::dispatchNow('foo', $category->id, 1);
5151
}
52+
53+
/** @test */
54+
public function it_doesnt_sync_product_with_missing_category()
55+
{
56+
$product = MagentoProductFactory::new()->create();
57+
58+
SyncMagentoProductCategory::dispatchNow($product->sku, 123, 1);
59+
60+
$this->assertEquals(0, MagentoProductCategory::count());
61+
}
5262
}

tests/Support/HasExtensionAttributesTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ public function test_resolves_extension_attributes_category_types()
5959
$this->assertEquals($category->id, MagentoProductCategory::first()->magento_category_id);
6060
$this->assertEquals($product->id, MagentoProductCategory::first()->magento_product_id);
6161
}
62+
63+
/** @test */
64+
public function it_skips_categories_that_dont_exist_in_database()
65+
{
66+
$product = MagentoProductFactory::new()->create();
67+
68+
(new FakeSupportingExtensionClass)->exposedSyncExtensionAttributes(['category_links' => [
69+
[
70+
'category_id' => "123",
71+
'position' => 1,
72+
],
73+
]], $product);
74+
75+
$this->assertEquals(0, MagentoProductCategory::count());
76+
}
6277
}
6378

6479
class FakeSupportingExtensionClass

0 commit comments

Comments
 (0)