Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SearchWeightTest.php
Go to the documentation of this file.
1 <?php
10 
12 
13 class SearchWeightTest extends \PHPUnit\Framework\TestCase
14 {
18  private $closure;
19 
23  private $attribute;
24 
28  private $config;
29 
33  private $attributeResourceModel;
34 
38  private $searchWeightPlugin;
39 
43  protected function setUp()
44  {
45  $this->config = $this->getMockBuilder(\Magento\Framework\Search\Request\Config::class)
46  ->setMethods(['reset'])
47  ->disableOriginalConstructor()
48  ->getMock();
49  $this->attribute = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
50  ->setMethods(['isObjectNew', 'dataHasChangedFor'])
51  ->disableOriginalConstructor()
52  ->getMockForAbstractClass();
53  $this->attributeResourceModel = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Attribute::class)
54  ->setMethods([])
55  ->disableOriginalConstructor()
56  ->getMock();
57  $this->closure = function (\Magento\Framework\Model\AbstractModel $model) {
58  return $model;
59  };
60 
61  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
62  $this->searchWeightPlugin = $objectManager->getObject(
63  SearchWeight::class,
64  [
65  'config' => $this->config,
66  ]
67  );
68  }
69 
70  public function testSaveNewAttribute()
71  {
72  $this->attribute->expects(self::once())->method('isObjectNew')->willReturn(true);
73  $this->attribute->expects(self::once())->method('dataHasChangedFor')->with('search_weight')->willReturn(false);
74  $this->config->expects(self::once())->method('reset');
75  $this->searchWeightPlugin->aroundSave($this->attributeResourceModel, $this->closure, $this->attribute);
76  }
77 
79  {
80  $this->attribute->expects(self::once())->method('isObjectNew')->willReturn(true);
81  $this->attribute->expects(self::once())->method('dataHasChangedFor')->with('search_weight')->willReturn(true);
82  $this->config->expects(self::once())->method('reset');
83  $this->searchWeightPlugin->aroundSave($this->attributeResourceModel, $this->closure, $this->attribute);
84  }
85 
87  {
88  $this->attribute->expects(self::once())->method('isObjectNew')->willReturn(false);
89  $this->attribute->expects(self::once())->method('dataHasChangedFor')->with('search_weight')->willReturn(true);
90  $this->config->expects(self::once())->method('reset');
91  $this->searchWeightPlugin->aroundSave($this->attributeResourceModel, $this->closure, $this->attribute);
92  }
93 
95  {
96  $this->attribute->expects(self::once())->method('isObjectNew')->willReturn(false);
97  $this->attribute->expects(self::once())->method('dataHasChangedFor')->with('search_weight')->willReturn(false);
98  $this->config->expects(self::never())->method('reset');
99  $this->searchWeightPlugin->aroundSave($this->attributeResourceModel, $this->closure, $this->attribute);
100  }
101 }
$objectManager
Definition: bootstrap.php:17