Skip to content

Commit

Permalink
Refactor not needed before and around plugins
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
ihor-sviziev committed Aug 14, 2020
1 parent 895c284 commit b213b79
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Magento\Catalog\Model\Indexer\Category\Flat\State;
use Magento\Framework\Indexer\IndexerInterface;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\Group as GroupModel;
use Magento\Store\Model\ResourceModel\Group;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -22,32 +21,32 @@ class StoreGroupTest extends TestCase
/**
* @var MockObject|IndexerInterface
*/
protected $indexerMock;
private $indexerMock;

/**
* @var MockObject|State
*/
protected $stateMock;
private $stateMock;

/**
* @var StoreGroup
*/
protected $model;
private $model;

/**
* @var MockObject|Group
*/
protected $subjectMock;
private $subjectMock;

/**
* @var IndexerRegistry|MockObject
*/
protected $indexerRegistryMock;
private $indexerRegistryMock;

/**
* @var MockObject|GroupModel
*/
protected $groupMock;
private $groupMock;

protected function setUp(): void
{
Expand All @@ -70,14 +69,10 @@ protected function setUp(): void

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

$this->model = (new ObjectManager($this))
->getObject(
StoreGroup::class,
['indexerRegistry' => $this->indexerRegistryMock, 'state' => $this->stateMock]
);
$this->model = new StoreGroup($this->indexerRegistryMock, $this->stateMock);
}

public function testBeforeAndAfterSave()
public function testAfterSave(): void
{
$this->stateMock->expects($this->once())->method('isFlatEnabled')->willReturn(true);
$this->indexerMock->expects($this->once())->method('invalidate');
Expand All @@ -90,22 +85,22 @@ public function testBeforeAndAfterSave()
->with('root_category_id')
->willReturn(true);
$this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(false);
$this->model->beforeSave($this->subjectMock, $this->groupMock);

$this->assertSame(
$this->subjectMock,
$this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock)
);
}

public function testBeforeAndAfterSaveNotNew()
public function testAfterSaveNotNew(): void
{
$this->stateMock->expects($this->never())->method('isFlatEnabled');
$this->groupMock->expects($this->once())
->method('dataHasChangedFor')
->with('root_category_id')
->willReturn(true);
$this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(true);
$this->model->beforeSave($this->subjectMock, $this->groupMock);

$this->assertSame(
$this->subjectMock,
$this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\Indexer\IndexerInterface;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Store\Model\ResourceModel\Store;
use Magento\Store\Model\Store as StoreModel;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand All @@ -20,27 +21,27 @@ class StoreViewTest extends TestCase
/**
* @var MockObject|IndexerInterface
*/
protected $indexerMock;
private $indexerMock;

/**
* @var MockObject|State
*/
protected $stateMock;
private $stateMock;

/**
* @var StoreView
*/
protected $model;
private $model;

/**
* @var IndexerRegistry|MockObject
*/
protected $indexerRegistryMock;
private $indexerRegistryMock;

/**
* @var MockObject
* @var Store|MockObject
*/
protected $subjectMock;
private $subjectMock;

protected function setUp(): void
{
Expand All @@ -65,50 +66,51 @@ protected function setUp(): void
$this->model = new StoreView($this->indexerRegistryMock, $this->stateMock);
}

public function testBeforeAndAfterSaveNewObject()
public function testAfterSaveNewObject(): void
{
$this->mockConfigFlatEnabled();
$this->mockIndexerMethods();
$storeMock = $this->createPartialMock(
\Magento\Store\Model\Store::class,
StoreModel::class,
['isObjectNew', 'dataHasChangedFor']
);
$storeMock->expects($this->once())->method('isObjectNew')->willReturn(true);
$this->model->beforeSave($this->subjectMock, $storeMock);

$this->assertSame(
$this->subjectMock,
$this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock)
);
}

public function testBeforeAndAfterSaveHasChanged()
public function testAfterSaveHasChanged(): void
{
$storeMock = $this->createPartialMock(
\Magento\Store\Model\Store::class,
StoreModel::class,
['isObjectNew', 'dataHasChangedFor']
);
$this->model->beforeSave($this->subjectMock, $storeMock);

$this->assertSame(
$this->subjectMock,
$this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock)
);
}

public function testBeforeAndAfterSaveNoNeed()
public function testAfterSaveNoNeed(): void
{
$this->mockConfigFlatEnabledNever();

$storeMock = $this->createPartialMock(
\Magento\Store\Model\Store::class,
StoreModel::class,
['isObjectNew', 'dataHasChangedFor']
);
$this->model->beforeSave($this->subjectMock, $storeMock);

$this->assertSame(
$this->subjectMock,
$this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock)
);
}

protected function mockIndexerMethods()
private function mockIndexerMethods(): void
{
$this->indexerMock->expects($this->once())->method('invalidate');
$this->indexerRegistryMock->expects($this->once())
Expand All @@ -117,12 +119,12 @@ protected function mockIndexerMethods()
->willReturn($this->indexerMock);
}

protected function mockConfigFlatEnabled()
private function mockConfigFlatEnabled(): void
{
$this->stateMock->expects($this->once())->method('isFlatEnabled')->willReturn(true);
}

protected function mockConfigFlatEnabledNever()
private function mockConfigFlatEnabledNever(): void
{
$this->stateMock->expects($this->never())->method('isFlatEnabled');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

use Magento\Catalog\Model\Indexer\Category\Product;
use Magento\Catalog\Model\Indexer\Category\Product\Plugin\StoreGroup;
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
use Magento\Framework\Indexer\IndexerInterface;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\Group as GroupModel;
use Magento\Store\Model\ResourceModel\Group;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -22,7 +22,7 @@ class StoreGroupTest extends TestCase
/**
* @var GroupModel|MockObject
*/
private $groupMock;
private $groupModelMock;

/**
* @var MockObject|IndexerInterface
Expand All @@ -32,21 +32,26 @@ class StoreGroupTest extends TestCase
/**
* @var MockObject|Group
*/
private $subject;
private $subjectMock;

/**
* @var IndexerRegistry|MockObject
*/
private $indexerRegistryMock;

/**
* @var TableMaintainer|MockObject
*/
private $tableMaintainerMock;

/**
* @var StoreGroup
*/
private $model;

protected function setUp(): void
{
$this->groupMock = $this->createPartialMock(
$this->groupModelMock = $this->createPartialMock(
GroupModel::class,
['dataHasChangedFor', 'isObjectNew']
);
Expand All @@ -59,44 +64,48 @@ protected function setUp(): void
true,
['getId', 'getState']
);
$this->subject = $this->createMock(Group::class);
$this->subjectMock = $this->createMock(Group::class);
$this->indexerRegistryMock = $this->createPartialMock(IndexerRegistry::class, ['get']);
$this->tableMaintainerMock = $this->createMock(TableMaintainer::class);

$this->model = (new ObjectManager($this))
->getObject(StoreGroup::class, ['indexerRegistry' => $this->indexerRegistryMock]);
$this->model = new StoreGroup($this->indexerRegistryMock, $this->tableMaintainerMock);
}

/**
* @param array $valueMap
* @dataProvider changedDataProvider
*/
public function testBeforeAndAfterSave($valueMap)
public function testAfterSave(array $valueMap): void
{
$this->mockIndexerMethods();
$this->groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap);
$this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(false);
$this->groupModelMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap);
$this->groupModelMock->expects($this->once())->method('isObjectNew')->willReturn(false);

$this->model->beforeSave($this->subject, $this->groupMock);
$this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock));
$this->assertSame(
$this->subjectMock,
$this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupModelMock)
);
}

/**
* @param array $valueMap
* @dataProvider changedDataProvider
*/
public function testBeforeAndAfterSaveNotNew($valueMap)
public function testAfterSaveNotNew(array $valueMap): void
{
$this->groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap);
$this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(true);
$this->groupModelMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap);
$this->groupModelMock->expects($this->once())->method('isObjectNew')->willReturn(true);

$this->model->beforeSave($this->subject, $this->groupMock);
$this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock));
$this->assertSame(
$this->subjectMock,
$this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupModelMock)
);
}

/**
* @return array
*/
public function changedDataProvider()
public function changedDataProvider(): array
{
return [
[
Expand All @@ -106,18 +115,20 @@ public function changedDataProvider()
];
}

public function testBeforeAndAfterSaveWithoutChanges()
public function testAfterSaveWithoutChanges(): void
{
$this->groupMock->expects($this->exactly(2))
$this->groupModelMock->expects($this->exactly(2))
->method('dataHasChangedFor')
->willReturnMap([['root_category_id', false], ['website_id', false]]);
$this->groupMock->expects($this->never())->method('isObjectNew');
$this->groupModelMock->expects($this->never())->method('isObjectNew');

$this->model->beforeSave($this->subject, $this->groupMock);
$this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock));
$this->assertSame(
$this->subjectMock,
$this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupModelMock)
);
}

private function mockIndexerMethods()
private function mockIndexerMethods(): void
{
$this->indexerMock->expects($this->once())->method('invalidate');
$this->indexerRegistryMock->expects($this->once())
Expand Down
Loading

0 comments on commit b213b79

Please sign in to comment.