13
13
use App \Value \Violation ;
14
14
use PHPUnit \Framework \MockObject \MockObject ;
15
15
use PHPUnit \Framework \TestCase ;
16
+ use Symfony \Component \Filesystem \Filesystem ;
16
17
17
18
/** @covers \App\Handler\GenerateHandler */
18
19
class GenerateHandlerTest extends TestCase
@@ -35,38 +36,58 @@ class GenerateHandlerTest extends TestCase
35
36
private $ sniffFinder ;
36
37
37
38
/** @test */
38
- public function handle ()
39
+ public function handle_WithoutArguments_CreatesFile ()
39
40
{
40
41
$ 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 ]]);
42
45
43
- $ this ->handler ->handle ();
46
+ /** @var \Generator $messages */
47
+ $ messages = $ this ->handler ->handle ();
44
48
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
+ );
46
57
}
47
58
48
- private function createSniffs ()
59
+ /** @test */
60
+ public function handle_WithSniffPath_CreatesSingleFile ()
49
61
{
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 (
57
69
[
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 )
65
74
);
66
75
}
67
76
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
+
68
87
protected function setUp (): void
69
88
{
89
+ (new Filesystem ())->remove ('var/markdown/Standard ' );
90
+
70
91
$ this ->codeRepository = $ this ->createMock (CodeRepository::class);
71
92
$ this ->generator = $ this ->createMock (Generator::class);
72
93
$ this ->sniffFinder = $ this ->createMock (SniffFinder::class);
@@ -77,4 +98,24 @@ protected function setUp(): void
77
98
$ this ->sniffFinder
78
99
);
79
100
}
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
+ }
80
121
}
0 commit comments