Skip to content

Commit d79dfea

Browse files
author
Joan He
committed
MAGETWO-62253: Refactor code to remove unserialize
- fixed unit test failures
1 parent 24b608a commit d79dfea

File tree

4 files changed

+126
-166
lines changed

4 files changed

+126
-166
lines changed

app/code/Magento/Backend/Test/Unit/Model/Menu/BuilderTest.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,36 @@
55
*/
66
namespace Magento\Backend\Test\Unit\Model\Menu;
77

8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
810
class BuilderTest extends \PHPUnit_Framework_TestCase
911
{
1012
/**
1113
* @var \Magento\Backend\Model\Menu\Builder
1214
*/
13-
protected $_model;
15+
private $model;
1416

1517
/**
16-
* @var \PHPUnit_Framework_MockObject_MockObject
18+
* @var \Magento\Backend\Model\Menu|\PHPUnit_Framework_MockObject_MockObject
1719
*/
18-
protected $_menuMock;
20+
private $menuMock;
1921

2022
/**
21-
* @var \PHPUnit_Framework_MockObject_MockObject
23+
* @var \Magento\Backend\Model\Menu\Item\Factory|\PHPUnit_Framework_MockObject_MockObject
2224
*/
23-
protected $_factoryMock;
25+
private $factoryMock;
2426

2527
protected function setUp()
2628
{
27-
$this->_factoryMock = $this->getMock(\Magento\Backend\Model\Menu\Item\Factory::class, [], [], '', false);
28-
$this->_menuMock = $this->getMock(
29-
\Magento\Backend\Model\Menu::class,
30-
[],
31-
[$this->getMock(\Psr\Log\LoggerInterface::class)]
29+
$this->factoryMock = $this->getMock(\Magento\Backend\Model\Menu\Item\Factory::class, [], [], '', false);
30+
$this->menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false);
31+
32+
$this->model = (new ObjectManager($this))->getObject(
33+
\Magento\Backend\Model\Menu\Builder::class,
34+
[
35+
'menuItemFactory' => $this->factoryMock
36+
]
3237
);
33-
34-
$this->_model = new \Magento\Backend\Model\Menu\Builder($this->_factoryMock, $this->_menuMock);
3538
}
3639

3740
public function testProcessCommand()
@@ -41,20 +44,20 @@ public function testProcessCommand()
4144
$command2 = $this->getMock(\Magento\Backend\Model\Menu\Builder\Command\Update::class, [], [], '', false);
4245
$command2->expects($this->any())->method('getId')->will($this->returnValue(1));
4346
$command->expects($this->once())->method('chain')->with($this->equalTo($command2));
44-
$this->_model->processCommand($command);
45-
$this->_model->processCommand($command2);
47+
$this->model->processCommand($command);
48+
$this->model->processCommand($command2);
4649
}
4750

4851
public function testGetResultBuildsTreeStructure()
4952
{
5053
$item1 = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false);
51-
$item1->expects($this->once())->method('getChildren')->will($this->returnValue($this->_menuMock));
52-
$this->_factoryMock->expects($this->any())->method('create')->will($this->returnValue($item1));
54+
$item1->expects($this->once())->method('getChildren')->will($this->returnValue($this->menuMock));
55+
$this->factoryMock->expects($this->any())->method('create')->will($this->returnValue($item1));
5356

5457
$item2 = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false);
55-
$this->_factoryMock->expects($this->at(1))->method('create')->will($this->returnValue($item2));
58+
$this->factoryMock->expects($this->at(1))->method('create')->will($this->returnValue($item2));
5659

57-
$this->_menuMock->expects(
60+
$this->menuMock->expects(
5861
$this->at(0)
5962
)->method(
6063
'add'
@@ -64,7 +67,7 @@ public function testGetResultBuildsTreeStructure()
6467
$this->equalTo(2)
6568
);
6669

67-
$this->_menuMock->expects(
70+
$this->menuMock->expects(
6871
$this->at(1)
6972
)->method(
7073
'add'
@@ -74,7 +77,7 @@ public function testGetResultBuildsTreeStructure()
7477
$this->equalTo(4)
7578
);
7679

77-
$this->_model->processCommand(
80+
$this->model->processCommand(
7881
new \Magento\Backend\Model\Menu\Builder\Command\Add(
7982
[
8083
'id' => 'item1',
@@ -85,7 +88,7 @@ public function testGetResultBuildsTreeStructure()
8588
]
8689
)
8790
);
88-
$this->_model->processCommand(
91+
$this->model->processCommand(
8992
new \Magento\Backend\Model\Menu\Builder\Command\Add(
9093
[
9194
'id' => 'item2',
@@ -98,12 +101,12 @@ public function testGetResultBuildsTreeStructure()
98101
)
99102
);
100103

101-
$this->_model->getResult($this->_menuMock);
104+
$this->model->getResult($this->menuMock);
102105
}
103106

104107
public function testGetResultSkipsRemovedItems()
105108
{
106-
$this->_model->processCommand(
109+
$this->model->processCommand(
107110
new \Magento\Backend\Model\Menu\Builder\Command\Add(
108111
[
109112
'id' => 1,
@@ -113,11 +116,11 @@ public function testGetResultSkipsRemovedItems()
113116
]
114117
)
115118
);
116-
$this->_model->processCommand(new \Magento\Backend\Model\Menu\Builder\Command\Remove(['id' => 1]));
119+
$this->model->processCommand(new \Magento\Backend\Model\Menu\Builder\Command\Remove(['id' => 1]));
117120

118-
$this->_menuMock->expects($this->never())->method('addChild');
121+
$this->menuMock->expects($this->never())->method('addChild');
119122

120-
$this->_model->getResult($this->_menuMock);
123+
$this->model->getResult($this->menuMock);
121124
}
122125

123126
/**
@@ -126,9 +129,9 @@ public function testGetResultSkipsRemovedItems()
126129
public function testGetResultSkipItemsWithInvalidParent()
127130
{
128131
$item1 = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false);
129-
$this->_factoryMock->expects($this->any())->method('create')->will($this->returnValue($item1));
132+
$this->factoryMock->expects($this->any())->method('create')->will($this->returnValue($item1));
130133

131-
$this->_model->processCommand(
134+
$this->model->processCommand(
132135
new \Magento\Backend\Model\Menu\Builder\Command\Add(
133136
[
134137
'id' => 'item1',
@@ -140,6 +143,6 @@ public function testGetResultSkipItemsWithInvalidParent()
140143
)
141144
);
142145

143-
$this->_model->getResult($this->_menuMock);
146+
$this->model->getResult($this->menuMock);
144147
}
145148
}

app/code/Magento/Backend/Test/Unit/Model/Menu/ConfigTest.php

Lines changed: 39 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,114 +5,75 @@
55
*/
66
namespace Magento\Backend\Test\Unit\Model\Menu;
77

8-
/**
9-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
10-
*/
8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
1110
class ConfigTest extends \PHPUnit_Framework_TestCase
1211
{
1312
/**
1413
* @var \PHPUnit_Framework_MockObject_MockObject
1514
*/
16-
protected $_cacheInstanceMock;
17-
18-
/**
19-
* @var \PHPUnit_Framework_MockObject_MockObject
20-
*/
21-
protected $_directorMock;
22-
23-
/**
24-
* @var \PHPUnit_Framework_MockObject_MockObject
25-
*/
26-
protected $_configReaderMock;
27-
28-
/**
29-
* @var \PHPUnit_Framework_MockObject_MockObject
30-
*/
31-
protected $_menuFactoryMock;
15+
private $cacheInstanceMock;
3216

3317
/**
3418
* @var \PHPUnit_Framework_MockObject_MockObject
3519
*/
36-
protected $_eventManagerMock;
20+
private $configReaderMock;
3721

3822
/**
3923
* @var \PHPUnit_Framework_MockObject_MockObject
4024
*/
41-
protected $_menuMock;
25+
private $menuMock;
4226

4327
/**
4428
* @var \PHPUnit_Framework_MockObject_MockObject
4529
*/
46-
protected $_menuBuilderMock;
30+
private $menuBuilderMock;
4731

4832
/**
4933
* @var \PHPUnit_Framework_MockObject_MockObject
5034
*/
51-
protected $_logger;
35+
private $logger;
5236

5337
/**
5438
* @var \Magento\Backend\Model\Menu\Config
5539
*/
56-
protected $_model;
40+
private $model;
5741

5842
protected function setUp()
5943
{
60-
$this->_cacheInstanceMock = $this->getMock(
44+
$this->cacheInstanceMock = $this->getMock(
6145
\Magento\Framework\App\Cache\Type\Config::class,
6246
[],
6347
[],
6448
'',
6549
false
6650
);
6751

68-
$this->_directorMock = $this->getMock(
69-
\Magento\Backend\Model\Menu\AbstractDirector::class,
70-
[],
71-
[],
72-
'',
73-
false
74-
);
75-
76-
$this->_menuFactoryMock = $this->getMock(
52+
$menuFactoryMock = $this->getMock(
7753
\Magento\Backend\Model\MenuFactory::class,
7854
['create'],
7955
[],
8056
'',
8157
false
8258
);
8359

84-
$this->_configReaderMock = $this->getMock(
60+
$this->configReaderMock = $this->getMock(
8561
\Magento\Backend\Model\Menu\Config\Reader::class,
8662
[],
8763
[],
8864
'',
8965
false
9066
);
9167

92-
$this->_eventManagerMock = $this->getMock(
93-
\Magento\Framework\Event\ManagerInterface::class,
94-
[],
95-
[],
96-
'',
97-
false,
98-
false
99-
);
100-
101-
$this->_logger = $this->getMock(\Psr\Log\LoggerInterface::class);
102-
103-
$this->_menuMock = $this->getMock(
104-
\Magento\Backend\Model\Menu::class,
105-
[],
106-
[$this->getMock(\Psr\Log\LoggerInterface::class)]
107-
);
68+
$this->logger = $this->getMock(\Psr\Log\LoggerInterface::class);
10869

109-
$this->_menuBuilderMock = $this->getMock(\Magento\Backend\Model\Menu\Builder::class, [], [], '', false);
70+
$this->menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false);
11071

111-
$this->_menuFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_menuMock));
72+
$this->menuBuilderMock = $this->getMock(\Magento\Backend\Model\Menu\Builder::class, [], [], '', false);
11273

113-
$scopeConfig = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
74+
$menuFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->menuMock));;
11475

115-
$this->_configReaderMock->expects($this->any())->method('read')->will($this->returnValue([]));
76+
$this->configReaderMock->expects($this->any())->method('read')->will($this->returnValue([]));
11677

11778
$appState = $this->getMock(\Magento\Framework\App\State::class, ['getAreaCode'], [], '', false);
11879
$appState->expects(
@@ -123,22 +84,22 @@ protected function setUp()
12384
$this->returnValue(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE)
12485
);
12586

126-
$this->_model = new \Magento\Backend\Model\Menu\Config(
127-
$this->_menuBuilderMock,
128-
$this->_directorMock,
129-
$this->_menuFactoryMock,
130-
$this->_configReaderMock,
131-
$this->_cacheInstanceMock,
132-
$this->_eventManagerMock,
133-
$this->_logger,
134-
$scopeConfig,
135-
$appState
87+
$this->model = (new ObjectManager($this))->getObject(
88+
\Magento\Backend\Model\Menu\Config::class,
89+
[
90+
'menuBuilder' => $this->menuBuilderMock,
91+
'menuFactory' => $menuFactoryMock,
92+
'configReader' => $this->configReaderMock,
93+
'configCacheType' => $this->cacheInstanceMock,
94+
'logger' => $this->logger,
95+
'appState' => $appState,
96+
]
13697
);
13798
}
13899

139100
public function testGetMenuWithCachedObjectReturnsUnserializedObject()
140101
{
141-
$this->_cacheInstanceMock->expects(
102+
$this->cacheInstanceMock->expects(
142103
$this->once()
143104
)->method(
144105
'load'
@@ -148,14 +109,14 @@ public function testGetMenuWithCachedObjectReturnsUnserializedObject()
148109
$this->returnValue('menu_cache')
149110
);
150111

151-
$this->_menuMock->expects($this->once())->method('unserialize')->with('menu_cache');
112+
$this->menuMock->expects($this->once())->method('unserialize')->with('menu_cache');
152113

153-
$this->assertEquals($this->_menuMock, $this->_model->getMenu());
114+
$this->assertEquals($this->menuMock, $this->model->getMenu());
154115
}
155116

156117
public function testGetMenuWithNotCachedObjectBuidlsObject()
157118
{
158-
$this->_cacheInstanceMock->expects(
119+
$this->cacheInstanceMock->expects(
159120
$this->at(0)
160121
)->method(
161122
'load'
@@ -165,17 +126,17 @@ public function testGetMenuWithNotCachedObjectBuidlsObject()
165126
$this->returnValue(false)
166127
);
167128

168-
$this->_configReaderMock->expects($this->once())->method('read')->will($this->returnValue([]));
129+
$this->configReaderMock->expects($this->once())->method('read')->will($this->returnValue([]));
169130

170-
$this->_menuBuilderMock->expects(
131+
$this->menuBuilderMock->expects(
171132
$this->exactly(1)
172133
)->method(
173134
'getResult'
174135
)->will(
175-
$this->returnValue($this->_menuMock)
136+
$this->returnValue($this->menuMock)
176137
);
177138

178-
$this->assertEquals($this->_menuMock, $this->_model->getMenu());
139+
$this->assertEquals($this->menuMock, $this->model->getMenu());
179140
}
180141

181142
/**
@@ -186,15 +147,15 @@ public function testGetMenuWithNotCachedObjectBuidlsObject()
186147
public function testGetMenuExceptionLogged($expectedException)
187148
{
188149
$this->setExpectedException($expectedException);
189-
$this->_menuBuilderMock->expects(
150+
$this->menuBuilderMock->expects(
190151
$this->exactly(1)
191152
)->method(
192153
'getResult'
193154
)->will(
194155
$this->throwException(new $expectedException())
195156
);
196157

197-
$this->_model->getMenu();
158+
$this->model->getMenu();
198159
}
199160

200161
public function getMenuExceptionLoggedDataProvider()
@@ -208,17 +169,17 @@ public function getMenuExceptionLoggedDataProvider()
208169

209170
public function testGetMenuGenericExceptionIsNotLogged()
210171
{
211-
$this->_logger->expects($this->never())->method('critical');
172+
$this->logger->expects($this->never())->method('critical');
212173

213-
$this->_menuBuilderMock->expects(
174+
$this->menuBuilderMock->expects(
214175
$this->exactly(1)
215176
)->method(
216177
'getResult'
217178
)->will(
218179
$this->throwException(new \Exception())
219180
);
220181
try {
221-
$this->_model->getMenu();
182+
$this->model->getMenu();
222183
} catch (\Exception $e) {
223184
return;
224185
}

0 commit comments

Comments
 (0)