1313use App \Value \Violation ;
1414use PHPUnit \Framework \MockObject \MockObject ;
1515use PHPUnit \Framework \TestCase ;
16+ use Symfony \Component \Filesystem \Filesystem ;
1617
1718/** @covers \App\Handler\GenerateHandler */
1819class GenerateHandlerTest extends TestCase
@@ -35,38 +36,58 @@ class GenerateHandlerTest extends TestCase
3536 private $ sniffFinder ;
3637
3738 /** @test */
38- public function handle ()
39+ public function handle_WithoutArguments_CreatesFile ()
3940 {
4041 $ this ->codeRepository ->method ('downloadCode ' )->willReturn (new Folder ('var/tests/ ' ));
41- $ this ->sniffFinder ->method ('getSniffs ' )->willReturn ($ this ->createSniffs ());
42+ $ sniffs = $ this ->createSniffs (['First ' , 'Second ' ]);
43+ $ this ->sniffFinder ->method ('getSniffs ' )->willReturn ($ sniffs );
44+ $ this ->generator ->method ('createSniffDoc ' )->withConsecutive ([$ sniffs [0 ]], [$ sniffs [1 ]]);
4245
43- $ this ->handler ->handle ();
46+ /** @var \Generator $messages */
47+ $ messages = $ this ->handler ->handle ();
4448
45- self ::assertFileExists ('var/markdown/Standard/Category/My.md ' );
49+ self ::assertEquals (
50+ [
51+ 'Found 2 sniff(s) ' ,
52+ 'Created file: var/markdown/Standard/Category/First.md ' ,
53+ 'Created file: var/markdown/Standard/Category/Second.md '
54+ ],
55+ iterator_to_array ($ messages )
56+ );
4657 }
4758
48- private function createSniffs ()
59+ /** @test */
60+ public function handle_WithSniffPath_CreatesSingleFile ()
4961 {
50- yield new Sniff (
51- ' Standard.Category.My ' ,
52- '' ,
53- [],
54- new Urls ([]),
55- ' Description ' ,
56- [],
62+ $ this -> codeRepository -> method ( ' downloadCode ' )-> willReturn ( new Folder ( ' var/tests/ ' ));
63+ $ this -> sniffFinder -> method ( ' getSniff ' )-> willReturn ( $ this -> createSniff ( ' First ' ));
64+
65+ /** @var \Generator $messages */
66+ $ messages = $ this -> handler -> handle ( ' var/tests/Standard/Category/Sniffs/FirstSniff.php ' );
67+
68+ self :: assertEquals (
5769 [
58- new Violation (
59- 'Standard.Category.My.ErrorCode ' ,
60- 'Description ' ,
61- [],
62- new Urls ([])
63- )
64- ]
70+ 'Found 1 sniff(s) ' ,
71+ 'Created file: var/markdown/Standard/Category/First.md ' ,
72+ ],
73+ iterator_to_array ($ messages )
6574 );
6675 }
6776
77+ /**
78+ * @param string[] $names
79+ */
80+ private function createSniffs (array $ names ): iterable
81+ {
82+ return array_map (function (string $ name ) {
83+ return $ this ->createSniff ($ name );
84+ }, $ names );
85+ }
86+
6887 protected function setUp (): void
6988 {
89+ (new Filesystem ())->remove ('var/markdown/Standard ' );
90+
7091 $ this ->codeRepository = $ this ->createMock (CodeRepository::class);
7192 $ this ->generator = $ this ->createMock (Generator::class);
7293 $ this ->sniffFinder = $ this ->createMock (SniffFinder::class);
@@ -77,4 +98,24 @@ protected function setUp(): void
7798 $ this ->sniffFinder
7899 );
79100 }
101+
102+ private function createSniff (string $ name ): Sniff
103+ {
104+ return new Sniff (
105+ 'Standard.Category. ' . $ name ,
106+ '' ,
107+ [],
108+ new Urls ([]),
109+ 'Description ' ,
110+ [],
111+ [
112+ new Violation (
113+ 'Standard.Category. ' . $ name . '.ErrorCode ' ,
114+ 'Description ' ,
115+ [],
116+ new Urls ([])
117+ )
118+ ]
119+ );
120+ }
80121}
0 commit comments