Skip to content

Commit b213b79

Browse files
committed
Refactor not needed before and around plugins
Update tests
1 parent 895c284 commit b213b79

File tree

11 files changed

+249
-212
lines changed

11 files changed

+249
-212
lines changed

app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Catalog\Model\Indexer\Category\Flat\State;
1212
use Magento\Framework\Indexer\IndexerInterface;
1313
use Magento\Framework\Indexer\IndexerRegistry;
14-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1514
use Magento\Store\Model\Group as GroupModel;
1615
use Magento\Store\Model\ResourceModel\Group;
1716
use PHPUnit\Framework\MockObject\MockObject;
@@ -22,32 +21,32 @@ class StoreGroupTest extends TestCase
2221
/**
2322
* @var MockObject|IndexerInterface
2423
*/
25-
protected $indexerMock;
24+
private $indexerMock;
2625

2726
/**
2827
* @var MockObject|State
2928
*/
30-
protected $stateMock;
29+
private $stateMock;
3130

3231
/**
3332
* @var StoreGroup
3433
*/
35-
protected $model;
34+
private $model;
3635

3736
/**
3837
* @var MockObject|Group
3938
*/
40-
protected $subjectMock;
39+
private $subjectMock;
4140

4241
/**
4342
* @var IndexerRegistry|MockObject
4443
*/
45-
protected $indexerRegistryMock;
44+
private $indexerRegistryMock;
4645

4746
/**
4847
* @var MockObject|GroupModel
4948
*/
50-
protected $groupMock;
49+
private $groupMock;
5150

5251
protected function setUp(): void
5352
{
@@ -70,14 +69,10 @@ protected function setUp(): void
7069

7170
$this->indexerRegistryMock = $this->createPartialMock(IndexerRegistry::class, ['get']);
7271

73-
$this->model = (new ObjectManager($this))
74-
->getObject(
75-
StoreGroup::class,
76-
['indexerRegistry' => $this->indexerRegistryMock, 'state' => $this->stateMock]
77-
);
72+
$this->model = new StoreGroup($this->indexerRegistryMock, $this->stateMock);
7873
}
7974

80-
public function testBeforeAndAfterSave()
75+
public function testAfterSave(): void
8176
{
8277
$this->stateMock->expects($this->once())->method('isFlatEnabled')->willReturn(true);
8378
$this->indexerMock->expects($this->once())->method('invalidate');
@@ -90,22 +85,22 @@ public function testBeforeAndAfterSave()
9085
->with('root_category_id')
9186
->willReturn(true);
9287
$this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(false);
93-
$this->model->beforeSave($this->subjectMock, $this->groupMock);
88+
9489
$this->assertSame(
9590
$this->subjectMock,
9691
$this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock)
9792
);
9893
}
9994

100-
public function testBeforeAndAfterSaveNotNew()
95+
public function testAfterSaveNotNew(): void
10196
{
10297
$this->stateMock->expects($this->never())->method('isFlatEnabled');
10398
$this->groupMock->expects($this->once())
10499
->method('dataHasChangedFor')
105100
->with('root_category_id')
106101
->willReturn(true);
107102
$this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(true);
108-
$this->model->beforeSave($this->subjectMock, $this->groupMock);
103+
109104
$this->assertSame(
110105
$this->subjectMock,
111106
$this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock)

app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Indexer\IndexerInterface;
1313
use Magento\Framework\Indexer\IndexerRegistry;
1414
use Magento\Store\Model\ResourceModel\Store;
15+
use Magento\Store\Model\Store as StoreModel;
1516
use PHPUnit\Framework\MockObject\MockObject;
1617
use PHPUnit\Framework\TestCase;
1718

@@ -20,27 +21,27 @@ class StoreViewTest extends TestCase
2021
/**
2122
* @var MockObject|IndexerInterface
2223
*/
23-
protected $indexerMock;
24+
private $indexerMock;
2425

2526
/**
2627
* @var MockObject|State
2728
*/
28-
protected $stateMock;
29+
private $stateMock;
2930

3031
/**
3132
* @var StoreView
3233
*/
33-
protected $model;
34+
private $model;
3435

3536
/**
3637
* @var IndexerRegistry|MockObject
3738
*/
38-
protected $indexerRegistryMock;
39+
private $indexerRegistryMock;
3940

4041
/**
41-
* @var MockObject
42+
* @var Store|MockObject
4243
*/
43-
protected $subjectMock;
44+
private $subjectMock;
4445

4546
protected function setUp(): void
4647
{
@@ -65,50 +66,51 @@ protected function setUp(): void
6566
$this->model = new StoreView($this->indexerRegistryMock, $this->stateMock);
6667
}
6768

68-
public function testBeforeAndAfterSaveNewObject()
69+
public function testAfterSaveNewObject(): void
6970
{
7071
$this->mockConfigFlatEnabled();
7172
$this->mockIndexerMethods();
7273
$storeMock = $this->createPartialMock(
73-
\Magento\Store\Model\Store::class,
74+
StoreModel::class,
7475
['isObjectNew', 'dataHasChangedFor']
7576
);
7677
$storeMock->expects($this->once())->method('isObjectNew')->willReturn(true);
77-
$this->model->beforeSave($this->subjectMock, $storeMock);
78+
7879
$this->assertSame(
7980
$this->subjectMock,
8081
$this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock)
8182
);
8283
}
8384

84-
public function testBeforeAndAfterSaveHasChanged()
85+
public function testAfterSaveHasChanged(): void
8586
{
8687
$storeMock = $this->createPartialMock(
87-
\Magento\Store\Model\Store::class,
88+
StoreModel::class,
8889
['isObjectNew', 'dataHasChangedFor']
8990
);
90-
$this->model->beforeSave($this->subjectMock, $storeMock);
91+
9192
$this->assertSame(
9293
$this->subjectMock,
9394
$this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock)
9495
);
9596
}
9697

97-
public function testBeforeAndAfterSaveNoNeed()
98+
public function testAfterSaveNoNeed(): void
9899
{
99100
$this->mockConfigFlatEnabledNever();
101+
100102
$storeMock = $this->createPartialMock(
101-
\Magento\Store\Model\Store::class,
103+
StoreModel::class,
102104
['isObjectNew', 'dataHasChangedFor']
103105
);
104-
$this->model->beforeSave($this->subjectMock, $storeMock);
106+
105107
$this->assertSame(
106108
$this->subjectMock,
107109
$this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock)
108110
);
109111
}
110112

111-
protected function mockIndexerMethods()
113+
private function mockIndexerMethods(): void
112114
{
113115
$this->indexerMock->expects($this->once())->method('invalidate');
114116
$this->indexerRegistryMock->expects($this->once())
@@ -117,12 +119,12 @@ protected function mockIndexerMethods()
117119
->willReturn($this->indexerMock);
118120
}
119121

120-
protected function mockConfigFlatEnabled()
122+
private function mockConfigFlatEnabled(): void
121123
{
122124
$this->stateMock->expects($this->once())->method('isFlatEnabled')->willReturn(true);
123125
}
124126

125-
protected function mockConfigFlatEnabledNever()
127+
private function mockConfigFlatEnabledNever(): void
126128
{
127129
$this->stateMock->expects($this->never())->method('isFlatEnabled');
128130
}

app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
use Magento\Catalog\Model\Indexer\Category\Product;
1111
use Magento\Catalog\Model\Indexer\Category\Product\Plugin\StoreGroup;
12+
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
1213
use Magento\Framework\Indexer\IndexerInterface;
1314
use Magento\Framework\Indexer\IndexerRegistry;
14-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1515
use Magento\Store\Model\Group as GroupModel;
1616
use Magento\Store\Model\ResourceModel\Group;
1717
use PHPUnit\Framework\MockObject\MockObject;
@@ -22,7 +22,7 @@ class StoreGroupTest extends TestCase
2222
/**
2323
* @var GroupModel|MockObject
2424
*/
25-
private $groupMock;
25+
private $groupModelMock;
2626

2727
/**
2828
* @var MockObject|IndexerInterface
@@ -32,21 +32,26 @@ class StoreGroupTest extends TestCase
3232
/**
3333
* @var MockObject|Group
3434
*/
35-
private $subject;
35+
private $subjectMock;
3636

3737
/**
3838
* @var IndexerRegistry|MockObject
3939
*/
4040
private $indexerRegistryMock;
4141

42+
/**
43+
* @var TableMaintainer|MockObject
44+
*/
45+
private $tableMaintainerMock;
46+
4247
/**
4348
* @var StoreGroup
4449
*/
4550
private $model;
4651

4752
protected function setUp(): void
4853
{
49-
$this->groupMock = $this->createPartialMock(
54+
$this->groupModelMock = $this->createPartialMock(
5055
GroupModel::class,
5156
['dataHasChangedFor', 'isObjectNew']
5257
);
@@ -59,44 +64,48 @@ protected function setUp(): void
5964
true,
6065
['getId', 'getState']
6166
);
62-
$this->subject = $this->createMock(Group::class);
67+
$this->subjectMock = $this->createMock(Group::class);
6368
$this->indexerRegistryMock = $this->createPartialMock(IndexerRegistry::class, ['get']);
69+
$this->tableMaintainerMock = $this->createMock(TableMaintainer::class);
6470

65-
$this->model = (new ObjectManager($this))
66-
->getObject(StoreGroup::class, ['indexerRegistry' => $this->indexerRegistryMock]);
71+
$this->model = new StoreGroup($this->indexerRegistryMock, $this->tableMaintainerMock);
6772
}
6873

6974
/**
7075
* @param array $valueMap
7176
* @dataProvider changedDataProvider
7277
*/
73-
public function testBeforeAndAfterSave($valueMap)
78+
public function testAfterSave(array $valueMap): void
7479
{
7580
$this->mockIndexerMethods();
76-
$this->groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap);
77-
$this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(false);
81+
$this->groupModelMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap);
82+
$this->groupModelMock->expects($this->once())->method('isObjectNew')->willReturn(false);
7883

79-
$this->model->beforeSave($this->subject, $this->groupMock);
80-
$this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock));
84+
$this->assertSame(
85+
$this->subjectMock,
86+
$this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupModelMock)
87+
);
8188
}
8289

8390
/**
8491
* @param array $valueMap
8592
* @dataProvider changedDataProvider
8693
*/
87-
public function testBeforeAndAfterSaveNotNew($valueMap)
94+
public function testAfterSaveNotNew(array $valueMap): void
8895
{
89-
$this->groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap);
90-
$this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(true);
96+
$this->groupModelMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap);
97+
$this->groupModelMock->expects($this->once())->method('isObjectNew')->willReturn(true);
9198

92-
$this->model->beforeSave($this->subject, $this->groupMock);
93-
$this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock));
99+
$this->assertSame(
100+
$this->subjectMock,
101+
$this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupModelMock)
102+
);
94103
}
95104

96105
/**
97106
* @return array
98107
*/
99-
public function changedDataProvider()
108+
public function changedDataProvider(): array
100109
{
101110
return [
102111
[
@@ -106,18 +115,20 @@ public function changedDataProvider()
106115
];
107116
}
108117

109-
public function testBeforeAndAfterSaveWithoutChanges()
118+
public function testAfterSaveWithoutChanges(): void
110119
{
111-
$this->groupMock->expects($this->exactly(2))
120+
$this->groupModelMock->expects($this->exactly(2))
112121
->method('dataHasChangedFor')
113122
->willReturnMap([['root_category_id', false], ['website_id', false]]);
114-
$this->groupMock->expects($this->never())->method('isObjectNew');
123+
$this->groupModelMock->expects($this->never())->method('isObjectNew');
115124

116-
$this->model->beforeSave($this->subject, $this->groupMock);
117-
$this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock));
125+
$this->assertSame(
126+
$this->subjectMock,
127+
$this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupModelMock)
128+
);
118129
}
119130

120-
private function mockIndexerMethods()
131+
private function mockIndexerMethods(): void
121132
{
122133
$this->indexerMock->expects($this->once())->method('invalidate');
123134
$this->indexerRegistryMock->expects($this->once())

0 commit comments

Comments
 (0)