|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\CatalogSearch\Test\Unit\Model\Indexer\Fulltext\Plugin; |
| 8 | + |
| 9 | +use Magento\Catalog\Model\Category as CategoryModel; |
| 10 | +use Magento\Catalog\Model\ResourceModel\Category as CategoryResourceModel; |
| 11 | +use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Category as CategoryPlugin; |
| 12 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 13 | +use Magento\Framework\Indexer\IndexerInterface; |
| 14 | +use Magento\Framework\Indexer\IndexerRegistry; |
| 15 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 16 | + |
| 17 | +class CategoryTest extends \PHPUnit_Framework_TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var \PHPUnit_Framework_MockObject_MockObject|IndexerInterface |
| 21 | + */ |
| 22 | + protected $indexerMock; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \PHPUnit_Framework_MockObject_MockObject|CategoryResourceModel |
| 26 | + */ |
| 27 | + protected $categoryResourceMock; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var \PHPUnit_Framework_MockObject_MockObject|CategoryModel |
| 31 | + */ |
| 32 | + protected $categoryMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var \Closure |
| 36 | + */ |
| 37 | + protected $proceed; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + protected $indexerRegistryMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var CategoryPlugin |
| 46 | + */ |
| 47 | + protected $model; |
| 48 | + |
| 49 | + protected function setUp() |
| 50 | + { |
| 51 | + $this->categoryMock = $this->getMockBuilder(CategoryModel::class) |
| 52 | + ->disableOriginalConstructor() |
| 53 | + ->getMock(); |
| 54 | + $this->categoryResourceMock = $this->getMockBuilder(CategoryResourceModel::class) |
| 55 | + ->disableOriginalConstructor() |
| 56 | + ->getMock(); |
| 57 | + $connection = $this->getMockBuilder(AdapterInterface::class) |
| 58 | + ->disableOriginalConstructor() |
| 59 | + ->getMockForAbstractClass(); |
| 60 | + $this->categoryResourceMock->method('getConnection')->willReturn($connection); |
| 61 | + |
| 62 | + $this->indexerMock = $this->getMockBuilder(IndexerInterface::class) |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->setMethods(['getId', 'getState', '__wakeup']) |
| 65 | + ->getMockForAbstractClass(); |
| 66 | + $this->indexerRegistryMock = $this->getMockBuilder(IndexerRegistry::class) |
| 67 | + ->disableOriginalConstructor() |
| 68 | + ->setMethods(['get']) |
| 69 | + ->getMock(); |
| 70 | + |
| 71 | + $this->proceed = function () { |
| 72 | + return $this->categoryResourceMock; |
| 73 | + }; |
| 74 | + |
| 75 | + $this->model = (new ObjectManager($this))->getObject( |
| 76 | + CategoryPlugin::class, |
| 77 | + ['indexerRegistry' => $this->indexerRegistryMock] |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public function testAfterSaveNonScheduled() |
| 82 | + { |
| 83 | + $this->categoryResourceMock->expects($this->once())->method('addCommitCallback'); |
| 84 | + $this->assertEquals( |
| 85 | + $this->categoryResourceMock, |
| 86 | + $this->model->aroundSave($this->categoryResourceMock, $this->proceed, $this->categoryMock) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + public function testAfterSaveScheduled() |
| 91 | + { |
| 92 | + $this->categoryResourceMock->expects($this->once())->method('addCommitCallback'); |
| 93 | + $this->assertEquals( |
| 94 | + $this->categoryResourceMock, |
| 95 | + $this->model->aroundSave($this->categoryResourceMock, $this->proceed, $this->categoryMock) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + protected function prepareIndexer() |
| 100 | + { |
| 101 | + $this->indexerRegistryMock->expects($this->once()) |
| 102 | + ->method('get') |
| 103 | + ->with(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID) |
| 104 | + ->will($this->returnValue($this->indexerMock)); |
| 105 | + } |
| 106 | +} |
0 commit comments