Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryProcessorTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class CategoryProcessorTest extends \PHPUnit\Framework\TestCase
12 {
13  const PARENT_CATEGORY_ID = 1;
14 
15  const CHILD_CATEGORY_ID = 2;
16 
17  const CHILD_CATEGORY_NAME = 'Child';
18 
22  protected $objectManager;
23 
26 
30  protected $categoryProcessor;
31 
35  protected $product;
36 
40  private $childCategory;
41 
45  private $parentCategory;
46 
47  protected function setUp()
48  {
49  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
50  $this->objectManagerHelper = new ObjectManagerHelper($this);
51 
52  $this->childCategory = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
53  ->disableOriginalConstructor()
54  ->getMock();
55  $this->childCategory->method('getId')->will($this->returnValue(self::CHILD_CATEGORY_ID));
56  $this->childCategory->method('getName')->will($this->returnValue(self::CHILD_CATEGORY_NAME));
57  $this->childCategory->method('getPath')->will($this->returnValue(
58  self::PARENT_CATEGORY_ID . CategoryProcessor::DELIMITER_CATEGORY
59  . self::CHILD_CATEGORY_ID
60  ));
61 
62  $this->parentCategory = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
63  ->disableOriginalConstructor()
64  ->getMock();
65  $this->parentCategory->method('getId')->will($this->returnValue(self::PARENT_CATEGORY_ID));
66  $this->parentCategory->method('getName')->will($this->returnValue('Parent'));
67  $this->parentCategory->method('getPath')->will($this->returnValue(self::PARENT_CATEGORY_ID));
68 
70  $this->objectManagerHelper->getCollectionMock(
71  \Magento\Catalog\Model\ResourceModel\Category\Collection::class,
72  [
73  self::PARENT_CATEGORY_ID => $this->parentCategory,
74  self::CHILD_CATEGORY_ID => $this->childCategory,
75  ]
76  );
77  $map = [
78  [self::PARENT_CATEGORY_ID, $this->parentCategory],
79  [self::CHILD_CATEGORY_ID, $this->childCategory],
80  ];
81  $categoryCollection->expects($this->any())
82  ->method('getItemById')
83  ->will($this->returnValueMap($map));
84  $categoryCollection->expects($this->exactly(3))
85  ->method('addAttributeToSelect')
86  ->withConsecutive(
87  ['name'],
88  ['url_key'],
89  ['url_path']
90  )
91  ->will($this->returnSelf());
92 
93  $categoryColFactory = $this->createPartialMock(
94  \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory::class,
95  ['create']
96  );
97 
98  $categoryColFactory->method('create')->will($this->returnValue($categoryCollection));
99 
100  $categoryFactory = $this->createPartialMock(\Magento\Catalog\Model\CategoryFactory::class, ['create']);
101 
102  $categoryFactory->method('create')->will($this->returnValue($this->childCategory));
103 
104  $this->categoryProcessor =
105  new \Magento\CatalogImportExport\Model\Import\Product\CategoryProcessor(
106  $categoryColFactory,
107  $categoryFactory
108  );
109  }
110 
111  public function testUpsertCategories()
112  {
113  $categoriesSeparator = ',';
114  $categoryIds = $this->categoryProcessor->upsertCategories(self::CHILD_CATEGORY_NAME, $categoriesSeparator);
115  $this->assertArrayHasKey(self::CHILD_CATEGORY_ID, array_flip($categoryIds));
116  }
117 
122  {
123  $exception = new \Magento\Framework\Exception\AlreadyExistsException();
124  $categoriesSeparator = '/';
125  $categoryName = 'Exception Category';
126  $this->childCategory->method('save')->willThrowException($exception);
127  $this->categoryProcessor->upsertCategories($categoryName, $categoriesSeparator);
128  $this->assertNotEmpty($this->categoryProcessor->getFailedCategories());
129  }
130 
131  public function testClearFailedCategories()
132  {
133  $dummyFailedCategory = [
134  [
135  'category' => 'dummy category',
136  'exception' => 'dummy exception',
137  ]
138  ];
139 
140  $this->setPropertyValue($this->categoryProcessor, 'failedCategories', $dummyFailedCategory);
141  $this->assertCount(count($dummyFailedCategory), $this->categoryProcessor->getFailedCategories());
142 
143  $this->categoryProcessor->clearFailedCategories();
144  $this->assertEmpty($this->categoryProcessor->getFailedCategories());
145  }
146 
152  public function testGetCategoryById($categoriesCache, $expectedResult)
153  {
154  $categoryId = 'category_id';
155  $this->setPropertyValue($this->categoryProcessor, 'categoriesCache', $categoriesCache);
156 
157  $actualResult = $this->categoryProcessor->getCategoryById($categoryId);
158  $this->assertEquals($expectedResult, $actualResult);
159  }
160 
164  public function getCategoryByIdDataProvider()
165  {
166  return [
167  [
168  '$categoriesCache' => [
169  'category_id' => 'category_id value',
170  ],
171  '$expectedResult' => 'category_id value',
172  ],
173  [
174  '$categoriesCache' => [],
175  '$expectedResult' => null,
176  ],
177  ];
178  }
179 
187  protected function setPropertyValue(&$object, $property, $value)
188  {
189  $reflection = new \ReflectionClass(get_class($object));
190  $reflectionProperty = $reflection->getProperty($property);
191  $reflectionProperty->setAccessible(true);
192  $reflectionProperty->setValue($object, $value);
193  return $object;
194  }
195 }
$value
Definition: gender.phtml:16