Skip to content

Commit debe1e6

Browse files
committed
add and manage unit test covergae
1 parent 3bf5674 commit debe1e6

File tree

1 file changed

+109
-20
lines changed
  • app/code/Magento/Catalog/Test/Unit/Block/Adminhtml/Category/Checkboxes

1 file changed

+109
-20
lines changed

app/code/Magento/Catalog/Test/Unit/Block/Adminhtml/Category/Checkboxes/TreeTest.php

Lines changed: 109 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,44 @@ protected function setUp(): void
4242
private function createCategoryCollectionStub(array $paths)
4343
{
4444
// Simple iterable stub with chainable methods
45-
return new class($paths) extends \ArrayObject {
45+
return new class($paths) implements \IteratorAggregate, \Countable {
46+
/** @var array */
47+
private $items;
4648
public function __construct(array $paths)
4749
{
4850
$items = [];
4951
foreach ($paths as $path) {
5052
$items[] = new class($path) {
53+
/** @var string */
5154
private $path;
52-
public function __construct($p) { $this->path = $p; }
53-
public function getPath() { return $this->path; }
55+
public function __construct($p)
56+
{
57+
$this->path = $p;
58+
}
59+
public function getPath()
60+
{
61+
return $this->path;
62+
}
5463
};
5564
}
56-
parent::__construct($items);
65+
$this->items = $items;
66+
}
67+
public function addAttributeToSelect($arg)
68+
{
69+
return $this;
70+
}
71+
public function addAttributeToFilter($field, $cond)
72+
{
73+
return $this;
74+
}
75+
public function getIterator(): \Traversable
76+
{
77+
return new \ArrayIterator($this->items);
78+
}
79+
public function count(): int
80+
{
81+
return count($this->items);
5782
}
58-
public function addAttributeToSelect($arg) { return $this; }
59-
public function addAttributeToFilter($field, $cond) { return $this; }
6083
};
6184
}
6285

@@ -149,11 +172,32 @@ public function testSetCategoryIdsWithScalarCastsToIntArrayAndFiltersCollection(
149172
$block = $this->buildBlockMock();
150173

151174
// Custom stub that captures the last filter applied
152-
$collectionStub = new class([]) extends \ArrayObject {
175+
$collectionStub = new class([]) implements \IteratorAggregate, \Countable {
176+
/** @var array */
177+
private $items = [];
178+
/** @var array|null */
153179
public $lastFilter;
154-
public function __construct($paths) { parent::__construct([]); }
155-
public function addAttributeToSelect($arg) { return $this; }
156-
public function addAttributeToFilter($field, $cond) { $this->lastFilter = [$field, $cond]; return $this; }
180+
public function __construct($paths)
181+
{
182+
$this->items = [];
183+
}
184+
public function addAttributeToSelect($arg)
185+
{
186+
return $this;
187+
}
188+
public function addAttributeToFilter($field, $cond)
189+
{
190+
$this->lastFilter = [$field, $cond];
191+
return $this;
192+
}
193+
public function getIterator(): \Traversable
194+
{
195+
return new \ArrayIterator($this->items);
196+
}
197+
public function count(): int
198+
{
199+
return count($this->items);
200+
}
157201
};
158202
$categoryModelMock = $this->getMockBuilder(\stdClass::class)
159203
->addMethods(['getCollection'])
@@ -176,7 +220,14 @@ public function testGetTreeUsesParentNodeWhenProvided()
176220
$block = $this->buildBlockMock();
177221
$parent = new Node(['id' => 99, 'children' => []], 'id', new \Magento\Framework\Data\Tree());
178222
$block->expects($this->once())->method('getRoot')->with($parent)->willReturn($parent);
179-
$block->expects($this->once())->method('_getNodeJson')->with($parent)->willReturn(['children' => [['id' => 1]]]);
223+
$block->expects($this->once())
224+
->method('_getNodeJson')
225+
->with($parent)
226+
->willReturn([
227+
'children' => [
228+
['id' => 1],
229+
],
230+
]);
180231
$tree = $block->getTree($parent);
181232
$this->assertSame([['id' => 1]], $tree);
182233
}
@@ -240,7 +291,14 @@ public function testGetTreeJsonUsesGetRootWhenNoSelectedIds()
240291
$block = $this->buildBlockMock();
241292
$root = new Node(['id' => 1, 'children' => []], 'id', new \Magento\Framework\Data\Tree());
242293
$block->expects($this->once())->method('getRoot')->with(null)->willReturn($root);
243-
$block->expects($this->once())->method('_getNodeJson')->with($root)->willReturn(['children' => [['id' => 3]]]);
294+
$block->expects($this->once())
295+
->method('_getNodeJson')
296+
->with($root)
297+
->willReturn([
298+
'children' => [
299+
['id' => 3],
300+
],
301+
]);
244302
$this->jsonEncoderMock->method('encode')->with([['id' => 3]])->willReturn('[{"id":3}]');
245303
$this->assertSame('[{"id":3}]', $block->getTreeJson());
246304
}
@@ -250,8 +308,16 @@ public function testGetTreeJsonUsesParentNodeWhenProvided()
250308
$block = $this->buildBlockMock();
251309
$parent = new Node(['id' => 99, 'children' => []], 'id', new \Magento\Framework\Data\Tree());
252310
$block->expects($this->once())->method('getRoot')->with($parent)->willReturn($parent);
253-
$block->expects($this->once())->method('_getNodeJson')->with($parent)->willReturn(['children' => [['id' => 11]]]);
254-
$this->jsonEncoderMock->method('encode')->with([[ 'id' => 11 ]])->willReturn('[{"id":11}]');
311+
$block->expects($this->once())->method('_getNodeJson')->with($parent)->willReturn(
312+
['children' => [['id' => 11]]]
313+
);
314+
$this->jsonEncoderMock->method('encode')
315+
->with([
316+
[
317+
'id' => 11,
318+
],
319+
])
320+
->willReturn('[{"id":11}]');
255321

256322
$json = $block->getTreeJson($parent);
257323
$this->assertSame('[{"id":11}]', $json);
@@ -267,7 +333,9 @@ public function testGetRootByIdsBuildsTreeAndReturnsRoot()
267333

268334
// Inject _categoryTree and other props
269335
$resourceTree = $this->createMock(CategoryTreeResource::class);
270-
$resourceTree->method('getExistingCategoryIdsBySpecifiedIds')->willReturnCallback(function ($ids) { return $ids; });
336+
$resourceTree->method('getExistingCategoryIdsBySpecifiedIds')->willReturnCallback(function ($ids) {
337+
return $ids;
338+
});
271339
$resourceTree->method('loadByIds')->willReturn($resourceTree);
272340
$rootNode = new Node(['id' => 1, 'children' => []], 'id', new \Magento\Framework\Data\Tree());
273341
$resourceTree->method('getNodeById')->willReturn($rootNode);
@@ -290,7 +358,20 @@ public function testGetRootByIdsBuildsTreeAndReturnsRoot()
290358
$setProp($block, '_jsonEncoder', $this->jsonEncoderMock);
291359

292360
// Stub category collection
293-
$block->method('getCategoryCollection')->willReturn(new \ArrayObject());
361+
$block->method('getCategoryCollection')->willReturn(
362+
new class() implements \IteratorAggregate, \Countable {
363+
/** @var array */
364+
private $items = [];
365+
public function getIterator(): \Traversable
366+
{
367+
return new \ArrayIterator($this->items);
368+
}
369+
public function count(): int
370+
{
371+
return count($this->items);
372+
}
373+
}
374+
);
294375

295376
$result = $block->getRootByIds([10]);
296377
$this->assertInstanceOf(Node::class, $result);
@@ -305,7 +386,9 @@ public function testGetNodeJsonBuildsExpectedArray()
305386
->onlyMethods(['escapeHtml'])
306387
->getMock();
307388

308-
$block->method('escapeHtml')->willReturnCallback(function ($v) { return $v; });
389+
$block->method('escapeHtml')->willReturnCallback(function ($v) {
390+
return $v;
391+
});
309392

310393
// Inject required props
311394
$setProp = function ($object, $prop, $value) {
@@ -366,7 +449,9 @@ public function testGetNodeJsonMarksExpandedWhenLevelLessThanTwo()
366449
->disableOriginalConstructor()
367450
->onlyMethods(['escapeHtml'])
368451
->getMock();
369-
$block->method('escapeHtml')->willReturnCallback(function ($v) { return $v; });
452+
$block->method('escapeHtml')->willReturnCallback(function ($v) {
453+
return $v;
454+
});
370455

371456
$node = new Node([
372457
'id' => 2,
@@ -391,7 +476,9 @@ public function testGetNodeJsonCreatesChildrenArrayWhenNodeHasChildren()
391476
->disableOriginalConstructor()
392477
->onlyMethods(['escapeHtml'])
393478
->getMock();
394-
$block->method('escapeHtml')->willReturnCallback(function ($v) { return $v; });
479+
$block->method('escapeHtml')->willReturnCallback(function ($v) {
480+
return $v;
481+
});
395482

396483
$child = new Node([
397484
'id' => 6,
@@ -429,7 +516,9 @@ public function testGetNodeJsonSetsEmptyChildrenWhenCountPositiveButNoChildren()
429516
->disableOriginalConstructor()
430517
->onlyMethods(['escapeHtml'])
431518
->getMock();
432-
$block->method('escapeHtml')->willReturnCallback(function ($v) { return $v; });
519+
$block->method('escapeHtml')->willReturnCallback(function ($v) {
520+
return $v;
521+
});
433522

434523
$node = new Node([
435524
'id' => 8,

0 commit comments

Comments
 (0)