66
77use PHPStan \Analyser \Error ;
88use PHPStan \Command \AnalysisResult ;
9+ use PHPStan \Command \ProgressBar ;
910use PHPStan \Command \ErrorFormatter \ErrorFormatter ;
1011use PHPStan \Command \Output ;
12+ use PHPStan \Command \OutputStyle ;
1113use PHPStan \File \NullRelativePathHelper ;
12- use PHPUnit \Framework \MockObject \Stub ;
1314use PHPUnit \Framework \TestCase ;
1415
1516/**
@@ -274,20 +275,117 @@ public function testFormatErrorsNoErrorsWritesNoErrorsAndReturnsZero() : void
274275 [],
275276 );
276277
277- $ writes = [];
278- $ output = $ this ->createMock (Output::class);
279- $ output ->method ('isDecorated ' )->willReturn (true );
280- $ output ->method ('isVerbose ' )->willReturn (false );
281- $ output ->method ('isVeryVerbose ' )->willReturn (false );
282- $ output ->method ('isDebug ' )->willReturn (false );
283- $ output ->method ('getStyle ' )->willReturnCallback (function () { return Stub::returnValueMap ([]); });
284- $ output ->method ('writeLineFormatted ' )->willReturnCallback (function (string $ line ) use (&$ writes ) : void { $ writes [] = $ line ; });
285- $ output ->method ('writeRaw ' )->willReturnCallback (function (string $ line ) use (&$ writes ) : void { $ writes [] = $ line ; });
278+ $ writesWrapper = ['writes ' => []];
279+ $ output = new class ($ writesWrapper ) implements Output {
280+ private array $ writesWrapper ;
281+
282+ public function __construct (array $ writesWrapper )
283+ {
284+ $ this ->writesWrapper = $ writesWrapper ;
285+ }
286+
287+ public function writeFormatted (string $ message ) : void
288+ {
289+ }
290+
291+ public function writeLineFormatted (string $ message ) : void
292+ {
293+ $ this ->writesWrapper ['writes ' ][] = $ message ;
294+ }
295+
296+ public function writeRaw (string $ message ) : void
297+ {
298+ $ this ->writesWrapper ['writes ' ][] = $ message ;
299+ }
300+
301+ public function getStyle () : OutputStyle
302+ {
303+ return new class () implements OutputStyle {
304+ public function title (string $ message ) : void
305+ {
306+ }
307+
308+ public function section (string $ message ) : void
309+ {
310+ }
311+
312+ public function listing (array $ elements ) : void
313+ {
314+ }
315+
316+ public function success (string $ message ) : void
317+ {
318+ }
319+
320+ public function error (string $ message ) : void
321+ {
322+ }
323+
324+ public function warning (string $ message ) : void
325+ {
326+ }
327+
328+ public function note (string $ message ) : void
329+ {
330+ }
331+
332+ public function caution (string $ message ) : void
333+ {
334+ }
335+
336+ public function table (array $ headers , array $ rows ) : void
337+ {
338+ }
339+
340+ public function createProgressBar (int $ max = 0 ) : ProgressBar
341+ {
342+ return new class () implements ProgressBar {
343+ public function start (int $ max = 0 ) : void
344+ {
345+ }
346+
347+ public function advance (int $ step = 1 ) : void
348+ {
349+ }
350+
351+ public function finish () : void
352+ {
353+ }
354+ };
355+ }
356+ };
357+ }
358+
359+ public function isVerbose () : bool
360+ {
361+ return false ;
362+ }
363+
364+ public function isVeryVerbose () : bool
365+ {
366+ return false ;
367+ }
368+
369+ public function isDebug () : bool
370+ {
371+ return false ;
372+ }
373+
374+ public function isDecorated () : bool
375+ {
376+ return true ;
377+ }
378+
379+ public function getWrites () : array
380+ {
381+ return $ this ->writesWrapper ['writes ' ];
382+ }
383+ };
286384
287385 $ result = $ this ->formatter ->formatErrors ($ analysisResult , $ output );
288386
289387 self ::assertSame (0 , $ result );
290- self ::assertSame (['<fg=green;options=bold>No errors</> ' , '' ], $ writes );
388+ self ::assertSame (['<fg=green;options=bold>No errors</> ' , '' ], $ output -> getWrites () );
291389 }
292390
293391 public function testFormatErrorsWithErrorsPrintsMessagesLinksSummaryAndReturnsOne () : void
@@ -320,15 +418,112 @@ public function testFormatErrorsWithErrorsPrintsMessagesLinksSummaryAndReturnsOn
320418 [],
321419 );
322420
323- $ writes = [];
324- $ output = $ this ->createMock (Output::class);
325- $ output ->method ('isDecorated ' )->willReturn (true );
326- $ output ->method ('isVerbose ' )->willReturn (false );
327- $ output ->method ('isVeryVerbose ' )->willReturn (false );
328- $ output ->method ('isDebug ' )->willReturn (false );
329- $ output ->method ('getStyle ' )->willReturnCallback (function () { return Stub::returnValueMap ([]); });
330- $ output ->method ('writeLineFormatted ' )->willReturnCallback (function (string $ line ) use (&$ writes ) : void { $ writes [] = $ line ; });
331- $ output ->method ('writeRaw ' )->willReturnCallback (function (string $ line ) use (&$ writes ) : void { $ writes [] = $ line ; });
421+ $ writesWrapper = ['writes ' => []];
422+ $ output = new class ($ writesWrapper ) implements Output {
423+ private array $ writesWrapper ;
424+
425+ public function __construct (array $ writesWrapper )
426+ {
427+ $ this ->writesWrapper = $ writesWrapper ;
428+ }
429+
430+ public function writeFormatted (string $ message ) : void
431+ {
432+ }
433+
434+ public function writeLineFormatted (string $ message ) : void
435+ {
436+ $ this ->writesWrapper ['writes ' ][] = $ message ;
437+ }
438+
439+ public function writeRaw (string $ message ) : void
440+ {
441+ $ this ->writesWrapper ['writes ' ][] = $ message ;
442+ }
443+
444+ public function getStyle () : OutputStyle
445+ {
446+ return new class () implements OutputStyle {
447+ public function title (string $ message ) : void
448+ {
449+ }
450+
451+ public function section (string $ message ) : void
452+ {
453+ }
454+
455+ public function listing (array $ elements ) : void
456+ {
457+ }
458+
459+ public function success (string $ message ) : void
460+ {
461+ }
462+
463+ public function error (string $ message ) : void
464+ {
465+ }
466+
467+ public function warning (string $ message ) : void
468+ {
469+ }
470+
471+ public function note (string $ message ) : void
472+ {
473+ }
474+
475+ public function caution (string $ message ) : void
476+ {
477+ }
478+
479+ public function table (array $ headers , array $ rows ) : void
480+ {
481+ }
482+
483+ public function createProgressBar (int $ max = 0 ) : ProgressBar
484+ {
485+ return new class () implements ProgressBar {
486+ public function start (int $ max = 0 ) : void
487+ {
488+ }
489+
490+ public function advance (int $ step = 1 ) : void
491+ {
492+ }
493+
494+ public function finish () : void
495+ {
496+ }
497+ };
498+ }
499+ };
500+ }
501+
502+ public function isVerbose () : bool
503+ {
504+ return false ;
505+ }
506+
507+ public function isVeryVerbose () : bool
508+ {
509+ return false ;
510+ }
511+
512+ public function isDebug () : bool
513+ {
514+ return false ;
515+ }
516+
517+ public function isDecorated () : bool
518+ {
519+ return true ;
520+ }
521+
522+ public function getWrites () : array
523+ {
524+ return $ this ->writesWrapper ['writes ' ];
525+ }
526+ };
332527
333528 $ result = $ this ->formatter ->formatErrors ($ analysisResult , $ output );
334529
@@ -337,6 +532,7 @@ public function testFormatErrorsWithErrorsPrintsMessagesLinksSummaryAndReturnsOn
337532 $ expectedLink = "↳ <href=phpstorm://open?file=/www/project/src/Foo/Bar.php&line=12>/www/project/.../Foo/Bar.php:12</> \n" ;
338533 $ expectedSummary = '<bg=red;options=bold>Found 1 error</> ' ;
339534
535+ $ writes = $ output ->getWrites ();
340536 $ linkFound = false ;
341537 foreach ($ writes as $ w ) {
342538 if (strpos ($ w , $ expectedLink ) !== false ) {
0 commit comments