7
7
8
8
namespace Magento \Catalog \Controller \Product ;
9
9
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
10
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
12
use Magento \Catalog \Model \Product ;
12
13
use Magento \Eav \Model \AttributeSetSearchResults ;
13
14
use Magento \Eav \Model \Entity \Attribute \Set ;
14
15
use Magento \Framework \Api \SearchCriteriaBuilder ;
15
16
use Magento \Framework \Api \SortOrderBuilder ;
16
- use Magento \Framework \App \Filesystem \DirectoryList ;
17
17
use Magento \Framework \Data \Collection ;
18
18
use Magento \Catalog \Api \AttributeSetRepositoryInterface ;
19
19
use Magento \Eav \Model \Entity \Type ;
20
- use Magento \Framework \Filesystem ;
21
- use Magento \Framework \Filesystem \Directory \WriteInterface ;
22
- use Magento \Framework \Filesystem \File \WriteInterface as FileWriteInterface ;
23
- use Magento \Framework \Filesystem \Driver \File ;
20
+ use PHPUnit \Framework \MockObject \MockObject ;
21
+ use Psr \Log \LoggerInterface ;
22
+ use Magento \Catalog \Api \Data \ProductAttributeInterface ;
23
+ use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
24
+ use Magento \Framework \Logger \Monolog as MagentoMonologLogger ;
24
25
25
26
/**
26
27
* Integration test for product view front action.
@@ -45,6 +46,11 @@ class ViewTest extends \Magento\TestFramework\TestCase\AbstractController
45
46
*/
46
47
private $ attributeSetRepository ;
47
48
49
+ /**
50
+ * @var ProductAttributeRepositoryInterface $attributeSetRepository
51
+ */
52
+ private $ attributeRepository ;
53
+
48
54
/**
49
55
* @var Type $productEntityType
50
56
*/
@@ -59,6 +65,7 @@ protected function setUp()
59
65
60
66
$ this ->productRepository = $ this ->_objectManager ->create (ProductRepositoryInterface::class);
61
67
$ this ->attributeSetRepository = $ this ->_objectManager ->create (AttributeSetRepositoryInterface::class);
68
+ $ this ->attributeRepository = $ this ->_objectManager ->create (ProductAttributeRepositoryInterface::class);
62
69
$ this ->productEntityType = $ this ->_objectManager ->create (Type::class)
63
70
->loadByCode (Product::ENTITY );
64
71
}
@@ -94,32 +101,42 @@ public function testViewActionWithCanonicalTag(): void
94
101
*/
95
102
public function testViewActionCustomAttributeSetWithoutCountryOfManufacture (): void
96
103
{
104
+ /** @var MockObject|LoggerInterface $logger */
105
+ $ logger = $ this ->setupLoggerMock ();
106
+
97
107
$ product = $ this ->getProductBySku ('simple_with_com ' );
98
108
$ attributeSetCustom = $ this ->getProductAttributeSetByName ('custom_attribute_set_wout_com ' );
99
-
100
109
$ product ->setAttributeSetId ($ attributeSetCustom ->getAttributeSetId ());
101
110
$ this ->productRepository ->save ($ product );
102
111
112
+ /** @var ProductAttributeInterface $attributeCountryOfManufacture */
113
+ $ attributeCountryOfManufacture = $ this ->attributeRepository ->get ('country_of_manufacture ' );
114
+ $ logger ->expects ($ this ->never ())
115
+ ->method ('warning ' )
116
+ ->with (
117
+ "Attempt to load value of nonexistent EAV attribute " ,
118
+ [
119
+ 'attribute_id ' => $ attributeCountryOfManufacture ->getAttributeId (),
120
+ 'entity_type ' => ProductInterface::class,
121
+ ]
122
+ );
123
+
103
124
$ this ->dispatch (sprintf ('catalog/product/view/id/%s/ ' , $ product ->getId ()));
104
- $ message = 'Attempt to load value of nonexistent EAV attribute ' ;
105
- $ this ->assertFalse (
106
- $ this ->checkSystemLogForMessage ($ message ),
107
- sprintf ("Warning message found in %s: %s " , $ this ->systemLogFileName , $ message )
108
- );
109
125
}
110
126
111
127
/**
112
- * Check system log file for error message .
128
+ * Setup logger mock to check there are no warning messages logged .
113
129
*
114
- * @param string $message
115
- * @return bool
130
+ * @return MockObject
116
131
*/
117
- private function checkSystemLogForMessage ( string $ message ): bool
132
+ private function setupLoggerMock () : MockObject
118
133
{
119
- $ content = $ this ->getSystemLogContent ();
120
- $ pos = strpos ($ content , $ message );
134
+ $ logger = $ this ->getMockBuilder (LoggerInterface::class)
135
+ ->disableOriginalConstructor ()
136
+ ->getMock ();
137
+ $ this ->_objectManager ->addSharedInstance ($ logger , MagentoMonologLogger::class);
121
138
122
- return $ pos !== false ;
139
+ return $ logger ;
123
140
}
124
141
125
142
/**
@@ -165,86 +182,4 @@ private function getProductAttributeSetByName(string $attributeSetName): ?Set
165
182
166
183
return null ;
167
184
}
168
-
169
- /**
170
- * Get system log content.
171
- *
172
- * @return string
173
- */
174
- private function getSystemLogContent (): string
175
- {
176
- $ logDir = $ this ->getLogDirectoryWrite ();
177
- $ logFile = $ logDir ->openFile ($ this ->systemLogFileName , 'rb ' );
178
- $ content = $ this ->tail ($ logFile , 10 );
179
-
180
- return $ content ;
181
- }
182
-
183
- /**
184
- * Get file tail.
185
- *
186
- * @param FileWriteInterface $file
187
- * @param int $lines
188
- * @param int $buffer
189
- * @return false|string
190
- */
191
- private function tail (FileWriteInterface $ file , int $ lines = 10 , int $ buffer = 4096 )
192
- {
193
- // Jump to last character
194
- $ file ->seek (-1 , SEEK_END );
195
-
196
- // Read it and adjust line number if necessary
197
- // (Otherwise the result would be wrong if file doesn't end with a blank line)
198
- if ($ file ->read (1 ) != "\n" ) {
199
- $ lines --;
200
- }
201
-
202
- // Start reading
203
- $ output = '' ;
204
- $ chunk = '' ;
205
-
206
- // While we would like more
207
- while ($ file ->tell () > 0 && $ lines >= 0 ) {
208
- // Figure out how far back we should jump
209
- $ seek = min ($ file ->tell (), $ buffer );
210
-
211
- // Do the jump (backwards, relative to where we are)
212
- $ file ->seek (-$ seek , SEEK_CUR );
213
-
214
- // Read a chunk and prepend it to our output
215
- $ output = ($ chunk = $ file ->read ($ seek )) . $ output ;
216
-
217
- // Jump back to where we started reading
218
- $ file ->seek (-mb_strlen ($ chunk , '8bit ' ), SEEK_CUR );
219
-
220
- // Decrease our line counter
221
- $ lines -= substr_count ($ chunk , "\n" );
222
- }
223
-
224
- // While we have too many lines
225
- // (Because of buffer size we might have read too many)
226
- while ($ lines ++ < 0 ) {
227
- // Find first newline and remove all text before that
228
- $ output = substr ($ output , strpos ($ output , "\n" ) + 1 );
229
- }
230
-
231
- // Close file and return
232
- $ file ->close ();
233
-
234
- return $ output ;
235
- }
236
-
237
- /**
238
- * Get current LOG directory write.
239
- *
240
- * @return WriteInterface
241
- */
242
- private function getLogDirectoryWrite ()
243
- {
244
- /** @var Filesystem $filesystem */
245
- $ filesystem = $ this ->_objectManager ->create (Filesystem::class);
246
- $ logDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::LOG );
247
-
248
- return $ logDirectory ;
249
- }
250
185
}
0 commit comments