9
9
10
10
use ArrayIterator ;
11
11
use Magento \Catalog \Model \ResourceModel \Attribute as AttributeResourceModel ;
12
+ use Magento \Catalog \Model \ResourceModel \Eav \Attribute as AttributeModel ;
12
13
use Magento \CatalogSearch \Model \Indexer \Fulltext \Processor ;
13
14
use Magento \CatalogSearch \Model \Indexer \IndexerHandlerFactory ;
14
15
use Magento \Elasticsearch \Model \Config ;
15
- use Magento \Elasticsearch \Model \Indexer \Fulltext \Plugin \Category \Product \Attribute ;
16
+ use Magento \Elasticsearch \Model \Indexer \Fulltext \Plugin \Category \Product \Attribute as AttributePlugin ;
16
17
use Magento \Elasticsearch \Model \Indexer \IndexerHandler ;
17
18
use Magento \Framework \Indexer \DimensionProviderInterface ;
18
19
use Magento \Framework \Indexer \IndexerInterface ;
19
20
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
20
- use PHPUnit \Framework \MockObject \Rule \InvokedCount as InvokedCountMatcher ;
21
21
use PHPUnit \Framework \MockObject \MockObject ;
22
+ use PHPUnit \Framework \MockObject \Rule \InvokedCount as InvokedCountMatcher ;
22
23
use PHPUnit \Framework \TestCase ;
23
24
24
25
/**
@@ -47,7 +48,7 @@ class AttributeTest extends TestCase
47
48
private $ indexerHandlerFactoryMock ;
48
49
49
50
/**
50
- * @var Attribute
51
+ * @var AttributePlugin
51
52
*/
52
53
private $ attributePlugin ;
53
54
@@ -65,7 +66,7 @@ protected function setUp(): void
65
66
$ this ->indexerHandlerFactoryMock = $ this ->createMock (IndexerHandlerFactory::class);
66
67
67
68
$ this ->attributePlugin = (new ObjectManager ($ this ))->getObject (
68
- Attribute ::class,
69
+ AttributePlugin ::class,
69
70
[
70
71
'config ' => $ this ->configMock ,
71
72
'indexerProcessor ' => $ this ->indexerProcessorMock ,
@@ -78,55 +79,56 @@ protected function setUp(): void
78
79
/**
79
80
* Test update catalog search indexer process.
80
81
*
81
- * @param bool $isInvalid
82
+ * @param bool $isNewObject
82
83
* @param bool $isElasticsearchEnabled
83
84
* @param array $dimensions
84
85
* @return void
85
86
* @dataProvider afterSaveDataProvider
86
87
*
87
88
*/
88
- public function testAfterSave (bool $ isInvalid , bool $ isElasticsearchEnabled , array $ dimensions ): void
89
+ public function testAfterSave (bool $ isNewObject , bool $ isElasticsearchEnabled , array $ dimensions ): void
89
90
{
90
- $ indexerData = ['indexer_example_data ' ];
91
-
91
+ /** @var AttributeModel $attribute */
92
+ $ attribute = (new ObjectManager ($ this ))->getObject (AttributeModel::class);
93
+ $ attribute ->isObjectNew ($ isNewObject );
92
94
/** @var AttributeResourceModel|MockObject $subjectMock */
93
95
$ subjectMock = $ this ->createMock (AttributeResourceModel::class);
96
+ $ this ->attributePlugin ->beforeSave ($ subjectMock , $ attribute );
97
+
98
+ $ indexerData = ['indexer_example_data ' ];
99
+
94
100
/** @var IndexerInterface|MockObject $indexerMock */
95
101
$ indexerMock = $ this ->getMockBuilder (IndexerInterface::class)
96
- ->setMethods (['isInvalid ' , ' getData ' ])
102
+ ->setMethods (['getData ' ])
97
103
->disableOriginalConstructor ()
98
104
->getMockForAbstractClass ();
99
105
100
- $ indexerMock ->expects ($ this ->once ())
101
- ->method ('isInvalid ' )
102
- ->willReturn ($ isInvalid );
103
-
104
- $ indexerMock ->expects ($ this ->getExpectsCount ($ isInvalid , $ isElasticsearchEnabled ))
106
+ $ indexerMock ->expects ($ this ->getExpectsCount ($ isNewObject , $ isElasticsearchEnabled ))
105
107
->method ('getData ' )
106
108
->willReturn ($ indexerData );
107
109
108
110
$ this ->indexerProcessorMock ->expects ($ this ->once ())
109
111
->method ('getIndexer ' )
110
112
->willReturn ($ indexerMock );
111
113
112
- $ this ->configMock ->expects ($ isInvalid ? $ this ->once () : $ this ->never ())
114
+ $ this ->configMock ->expects ($ isNewObject ? $ this ->once () : $ this ->never ())
113
115
->method ('isElasticsearchEnabled ' )
114
116
->willReturn ($ isElasticsearchEnabled );
115
117
116
118
/** @var IndexerHandler|MockObject $indexerHandlerMock */
117
119
$ indexerHandlerMock = $ this ->createMock (IndexerHandler::class);
118
120
119
121
$ indexerHandlerMock
120
- ->expects (($ isInvalid && $ isElasticsearchEnabled ) ? $ this ->exactly (count ($ dimensions )) : $ this ->never ())
122
+ ->expects (($ isNewObject && $ isElasticsearchEnabled ) ? $ this ->exactly (count ($ dimensions )) : $ this ->never ())
121
123
->method ('updateIndex ' )
122
124
->willReturnSelf ();
123
125
124
- $ this ->indexerHandlerFactoryMock ->expects ($ this ->getExpectsCount ($ isInvalid , $ isElasticsearchEnabled ))
126
+ $ this ->indexerHandlerFactoryMock ->expects ($ this ->getExpectsCount ($ isNewObject , $ isElasticsearchEnabled ))
125
127
->method ('create ' )
126
128
->with (['data ' => $ indexerData ])
127
129
->willReturn ($ indexerHandlerMock );
128
130
129
- $ this ->dimensionProviderMock ->expects ($ this ->getExpectsCount ($ isInvalid , $ isElasticsearchEnabled ))
131
+ $ this ->dimensionProviderMock ->expects ($ this ->getExpectsCount ($ isNewObject , $ isElasticsearchEnabled ))
130
132
->method ('getIterator ' )
131
133
->willReturn (new ArrayIterator ($ dimensions ));
132
134
@@ -143,8 +145,8 @@ public function afterSaveDataProvider(): array
143
145
$ dimensions = [['scope ' => 1 ], ['scope ' => 2 ]];
144
146
145
147
return [
146
- 'save_without_invalidation ' => [false , false , [] ],
147
- 'save_with_mysql_search ' => [true , false , $ dimensions ],
148
+ 'save_existing_object ' => [false , false , $ dimensions ],
149
+ 'save_with_another_search_engine ' => [true , false , $ dimensions ],
148
150
'save_with_elasticsearch ' => [true , true , []],
149
151
'save_with_elasticsearch_and_dimensions ' => [true , true , $ dimensions ],
150
152
];
@@ -153,12 +155,12 @@ public function afterSaveDataProvider(): array
153
155
/**
154
156
* Retrieves how many times method is executed.
155
157
*
156
- * @param bool $isInvalid
158
+ * @param bool $isNewObject
157
159
* @param bool $isElasticsearchEnabled
158
160
* @return InvokedCountMatcher
159
161
*/
160
- private function getExpectsCount (bool $ isInvalid , bool $ isElasticsearchEnabled ): InvokedCountMatcher
162
+ private function getExpectsCount (bool $ isNewObject , bool $ isElasticsearchEnabled ): InvokedCountMatcher
161
163
{
162
- return ($ isInvalid && $ isElasticsearchEnabled ) ? $ this ->once () : $ this ->never ();
164
+ return ($ isNewObject && $ isElasticsearchEnabled ) ? $ this ->once () : $ this ->never ();
163
165
}
164
166
}
0 commit comments