Skip to content

Commit d34a54c

Browse files
committed
Covering the SetAttributeTabBlockObserver for Bundles by Unit Test
1 parent 9400c1e commit d34a54c

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Bundle\Test\Unit\Observer;
9+
10+
use Magento\Bundle\Block\Adminhtml\Catalog\Product\Edit\Tab\Attributes;
11+
use Magento\Bundle\Observer\SetAttributeTabBlockObserver;
12+
use Magento\Catalog\Helper\Catalog;
13+
use Magento\Catalog\Model\Product;
14+
use Magento\Catalog\Model\Product\Type;
15+
use Magento\Framework\Event;
16+
use Magento\Framework\Event\Observer;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Class SetAttributeTabBlockObserverTest
22+
*
23+
* Test setting attribute tab block for bundle products
24+
*/
25+
class SetAttributeTabBlockObserverTest extends TestCase
26+
{
27+
/**
28+
* @var SetAttributeTabBlockObserver
29+
*/
30+
private $observer;
31+
32+
/**
33+
* @var Catalog|MockObject
34+
*/
35+
private $helperCatalogMock;
36+
37+
/**
38+
* @var Observer|MockObject
39+
*/
40+
private $observerMock;
41+
42+
/**
43+
* @var Event|MockObject
44+
*/
45+
private $eventMock;
46+
47+
/**
48+
* @var Product|MockObject
49+
*/
50+
private $productMock;
51+
52+
/**
53+
* Set Up
54+
*/
55+
public function setUp()
56+
{
57+
$this->helperCatalogMock = $this->createMock(Catalog::class);
58+
$this->observerMock = $this->createMock(Observer::class);
59+
$this->eventMock = $this->getMockBuilder(Event::class)
60+
->disableOriginalConstructor()
61+
->setMethods(['getProduct'])
62+
->getMock();
63+
$this->productMock = $this->getMockBuilder(Product::class)
64+
->disableOriginalConstructor()
65+
->getMock();
66+
67+
$this->observer = new SetAttributeTabBlockObserver(
68+
$this->helperCatalogMock
69+
);
70+
}
71+
72+
/**
73+
* Test setting attribute tab block for bundle product
74+
*/
75+
public function testAddingAttributeTabForBundleProduct()
76+
{
77+
$this->productMock->expects($this->any())
78+
->method('getTypeId')
79+
->willReturn(Type::TYPE_BUNDLE);
80+
$this->eventMock->expects($this->any())
81+
->method('getProduct')
82+
->willReturn($this->productMock);
83+
$this->observerMock->expects($this->any())
84+
->method('getEvent')
85+
->willReturn($this->eventMock);
86+
$this->helperCatalogMock->expects($this->once())
87+
->method('setAttributeTabBlock')
88+
->with(Attributes::class);
89+
90+
$this->observer->execute($this->observerMock);
91+
}
92+
93+
/**
94+
* Test setting attribute tab block for a non bundle product
95+
*/
96+
public function testAddingAttributeTabForNonBundleProduct()
97+
{
98+
$this->productMock->expects($this->any())
99+
->method('getTypeId')
100+
->willReturn(Type::TYPE_VIRTUAL);
101+
$this->eventMock->expects($this->any())
102+
->method('getProduct')
103+
->willReturn($this->productMock);
104+
$this->observerMock->expects($this->any())
105+
->method('getEvent')
106+
->willReturn($this->eventMock);
107+
$this->helperCatalogMock->expects($this->never())
108+
->method('setAttributeTabBlock');
109+
110+
$this->observer->execute($this->observerMock);
111+
}
112+
}

0 commit comments

Comments
 (0)