@@ -93,25 +93,135 @@ public function dataProviderFiles(): array
9393 * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
9494 * @dataProvider dataProviderFiles
9595 */
96- public function testNoErrors (string $ filepath , int $ line , string $ functionName , int $ expectedComplexity ): void
96+ public function testNoErrorsOrWarnings (string $ filepath , int $ line , string $ functionName , int $ expectedComplexity ): void
9797 {
9898 $ report = self ::checkFile ($ filepath , [
99- 'maxComplexity ' => $ expectedComplexity ,
99+ 'errorThreshold ' => $ expectedComplexity + 1 ,
100+ 'warningThreshold ' => $ expectedComplexity + 1 ,
100101 ]);
101102 self ::assertNoSniffErrorInFile ($ report );
103+ self ::assertNoSniffWarningInFile ($ report );
104+ }
105+
106+ /**
107+ * @dataProvider dataProviderFiles
108+ */
109+ public function testWarnings (string $ filepath , int $ line , string $ functionName , int $ expectedComplexity ): void
110+ {
111+ $ report = self ::checkFile ($ filepath , [
112+ 'errorThreshold ' => $ expectedComplexity + 1 ,
113+ 'warningThreshold ' => $ expectedComplexity ,
114+ ]);
115+
116+ self ::assertSame (1 , $ report ->getWarningCount ());
117+ self ::assertNoSniffErrorInFile ($ report );
118+ self ::assertSniffWarning (
119+ $ report ,
120+ $ line ,
121+ CognitiveSniff::CODE_COMPLEXITY ,
122+ sprintf (
123+ 'Cognitive complexity for "%s" is %s but has to be less than or equal to %s. ' ,
124+ $ functionName ,
125+ $ expectedComplexity ,
126+ $ expectedComplexity - 1
127+ )
128+ );
102129 }
103130
104131 /**
105132 * @dataProvider dataProviderFiles
106133 */
107134 public function testErrors (string $ filepath , int $ line , string $ functionName , int $ expectedComplexity ): void
108135 {
109- $ maxComplexity = $ expectedComplexity - 1 ;
110136 $ report = self ::checkFile ($ filepath , [
111- 'maxComplexity ' => $ maxComplexity ,
137+ 'errorThreshold ' => $ expectedComplexity ,
138+ 'warningThreshold ' => $ expectedComplexity - 1 ,
139+ ]);
140+
141+ self ::assertSame (1 , $ report ->getErrorCount ());
142+ self ::assertNoSniffWarningInFile ($ report );
143+ self ::assertSniffError (
144+ $ report ,
145+ $ line ,
146+ CognitiveSniff::CODE_COMPLEXITY ,
147+ sprintf (
148+ 'Cognitive complexity for "%s" is %s but has to be less than or equal to %s. ' ,
149+ $ functionName ,
150+ $ expectedComplexity ,
151+ $ expectedComplexity - 2
152+ )
153+ );
154+ }
155+
156+ public function testErrorAndWarning (): void
157+ {
158+ $ filepath = __DIR__ . '/data/cognitive/warnAndError.php ' ;
159+ $ warnInfo = [
160+ 'complexity ' => 6 ,
161+ 'func ' => 'warning ' ,
162+ 'line ' => 3 ,
163+ ];
164+ $ errorInfo = [
165+ 'complexity ' => 9 ,
166+ 'func ' => 'error ' ,
167+ 'line ' => 15 ,
168+ ];
169+
170+ $ report = self ::checkFile ($ filepath , [
171+ 'errorThreshold ' => 9 ,
172+ 'warningThreshold ' => 6 ,
173+ ]);
174+
175+ self ::assertSame (1 , $ report ->getWarningCount ());
176+ self ::assertSame (1 , $ report ->getErrorCount ());
177+ self ::assertSniffWarning (
178+ $ report ,
179+ $ warnInfo ['line ' ],
180+ CognitiveSniff::CODE_COMPLEXITY ,
181+ sprintf (
182+ 'Cognitive complexity for "%s" is %s but has to be less than or equal to %s. ' ,
183+ $ warnInfo ['func ' ],
184+ $ warnInfo ['complexity ' ],
185+ 5
186+ )
187+ );
188+ self ::assertSniffError (
189+ $ report ,
190+ $ errorInfo ['line ' ],
191+ CognitiveSniff::CODE_COMPLEXITY ,
192+ sprintf (
193+ 'Cognitive complexity for "%s" is %s but has to be less than or equal to %s. ' ,
194+ $ errorInfo ['func ' ],
195+ $ errorInfo ['complexity ' ],
196+ 5
197+ )
198+ );
199+ }
200+
201+ /**
202+ * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
203+ * @dataProvider dataProviderFiles
204+ */
205+ public function testDeprecatedNoErrorsOrWarnings (string $ filepath , int $ line , string $ functionName , int $ expectedComplexity ): void
206+ {
207+ $ report = self ::checkFile ($ filepath , [
208+ 'maxComplexity ' => $ expectedComplexity ,
209+ ]);
210+ self ::assertNoSniffErrorInFile ($ report );
211+ self ::assertNoSniffWarningInFile ($ report );
212+ }
213+
214+ /**
215+ * @dataProvider dataProviderFiles
216+ */
217+ public function testDeprecatedErrors (string $ filepath , int $ line , string $ functionName , int $ expectedComplexity ): void
218+ {
219+ $ report = self ::checkFile ($ filepath , [
220+ 'maxComplexity ' => $ expectedComplexity - 1 ,
112221 ]);
113222
114223 self ::assertSame (1 , $ report ->getErrorCount ());
224+ self ::assertNoSniffWarningInFile ($ report );
115225 self ::assertSniffError (
116226 $ report ,
117227 $ line ,
@@ -120,7 +230,7 @@ public function testErrors(string $filepath, int $line, string $functionName, in
120230 'Cognitive complexity for "%s" is %s but has to be less than or equal to %s. ' ,
121231 $ functionName ,
122232 $ expectedComplexity ,
123- $ maxComplexity
233+ $ expectedComplexity - 1
124234 )
125235 );
126236 }
0 commit comments