-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed def Cron execution time; Added ProductType attr handler. xml …
…generation improvements.
- Loading branch information
Denis Slepnev
committed
Sep 20, 2022
1 parent
d002661
commit b063d85
Showing
9 changed files
with
178 additions
and
21 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
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,68 @@ | ||
<?php | ||
|
||
namespace RunAsRoot\GoogleShoppingFeed\DataProvider\AttributeHandlers; | ||
|
||
use Magento\Catalog\Model\Product; | ||
use Magento\Catalog\Api\CategoryRepositoryInterface; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
|
||
class ProductTypeProvider implements AttributeHandlerInterface | ||
{ | ||
/** | ||
* @var CategoryRepositoryInterface | ||
*/ | ||
private CategoryRepositoryInterface $categoryRepository; | ||
|
||
private array $cache = []; | ||
|
||
/** | ||
* @param CategoryRepositoryInterface $categoryRepository | ||
*/ | ||
public function __construct(CategoryRepositoryInterface $categoryRepository) | ||
{ | ||
$this->categoryRepository = $categoryRepository; | ||
} | ||
|
||
public function get(Product $product) | ||
{ | ||
return $this->getProductType($product); | ||
} | ||
|
||
/** | ||
* @param Product $product | ||
* | ||
* @return string | ||
*/ | ||
private function getProductType(Product $product): string | ||
{ | ||
$categoryIds = $product->getCategoryIds(); | ||
if (empty($categoryIds)) { | ||
return ''; | ||
} | ||
|
||
$categoryId = (int)$categoryIds[0]; | ||
if (array_key_exists($categoryId, $this->cache)) { | ||
return $this->cache[$categoryId]; | ||
} | ||
|
||
try { | ||
$fullCategoryPath = ''; | ||
$category = $this->categoryRepository->get($categoryId, $product->getStoreId()); | ||
$pathInStore = $category->getPathInStore(); | ||
$pathIds = array_reverse(explode(',', $pathInStore)); | ||
|
||
$categories = $category->getParentCategories(); | ||
|
||
foreach ($pathIds as $categoryId) { | ||
if (isset($categories[$categoryId]) && $categories[$categoryId]->getName()) { | ||
$fullCategoryPath .= $categories[$categoryId]->getName() . ' > '; | ||
} | ||
} | ||
$this->cache[$categoryId] = rtrim($fullCategoryPath, '> '); | ||
return $this->cache[$categoryId]; | ||
|
||
} catch (NoSuchEntityException $noSuchEntityException) { | ||
return ''; | ||
} | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
Test/Unit/DataProvider/AttributeHandlers/ProductTypeProviderTest.php
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,67 @@ | ||
<?php | ||
|
||
namespace RunAsRoot\GoogleShoppingFeed\Test\Unit\DataProvider\AttributeHandlers; | ||
|
||
use Magento\Catalog\Api\CategoryRepositoryInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Catalog\Model\Category; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use RunAsRoot\GoogleShoppingFeed\DataProvider\AttributeHandlers\ProductTypeProvider; | ||
|
||
final class ProductTypeProviderTest extends TestCase | ||
{ | ||
/** | ||
* @var CategoryRepositoryInterface|MockObject | ||
*/ | ||
private CategoryRepositoryInterface $categoryRepositoryMock; | ||
|
||
/** | ||
* @var Product|MockObject | ||
*/ | ||
private Product $productMock; | ||
|
||
private ProductTypeProvider $sut; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->categoryRepositoryMock = $this->createMock(CategoryRepositoryInterface::class); | ||
$this->productMock = $this->createMock(Product::class); | ||
|
||
$this->sut = new ProductTypeProvider($this->categoryRepositoryMock); | ||
} | ||
|
||
public function testProductTypeInCaseOfEmptyCategoryIds(): void | ||
{ | ||
$this->productMock->method('getCategoryIds')->willReturn([]); | ||
$this->assertEquals('', $this->sut->get($this->productMock)); | ||
} | ||
|
||
public function testProductTypeInCaseOfNonExistingCategory(): void | ||
{ | ||
$this->productMock->method('getCategoryIds')->willReturn([1000]); | ||
$this->categoryRepositoryMock->method('get')->willThrowException(new NoSuchEntityException()); | ||
$this->assertEquals('', $this->sut->get($this->productMock)); | ||
} | ||
|
||
public function testProductTypeInCaseOfExistingCategory(): void | ||
{ | ||
$this->productMock->method('getCategoryIds')->willReturn([10]); | ||
$category = $this->createMock(Category::class); | ||
|
||
$this->productMock->method('getStoreId')->willReturn(1); | ||
|
||
$this->categoryRepositoryMock->method('get')->with(10)->willReturn($category); | ||
|
||
$category->method('getPathInStore')->willReturn("10,2"); | ||
|
||
$parentCategory = $this->createMock(Category::class); | ||
$category->method('getParentCategories')->willReturn([10 => $category, 2 => $parentCategory]); | ||
|
||
$category->method('getName')->willReturn('Child Category Name'); | ||
$parentCategory->method('getName')->willReturn('Parent Category Name'); | ||
|
||
$this->assertEquals('Parent Category Name > Child Category Name', $this->sut->get($this->productMock)); | ||
} | ||
} |
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