1818use function array_values ;
1919use function count ;
2020use function explode ;
21- use function get_class ;
2221use function is_array ;
2322use function is_file ;
2423use function sort ;
25- use PHPUnit \Framework \TestCase ;
26- use PHPUnit \Runner \PhptTestCase ;
2724use ReflectionClass ;
2825use SebastianBergmann \CodeCoverage \Data \ProcessedCodeCoverageData ;
2926use SebastianBergmann \CodeCoverage \Data \RawCodeCoverageData ;
3330use SebastianBergmann \CodeCoverage \StaticAnalysis \CachingFileAnalyser ;
3431use SebastianBergmann \CodeCoverage \StaticAnalysis \FileAnalyser ;
3532use SebastianBergmann \CodeCoverage \StaticAnalysis \ParsingFileAnalyser ;
33+ use SebastianBergmann \CodeCoverage \Test \TestSize \TestSize ;
34+ use SebastianBergmann \CodeCoverage \Test \TestStatus \TestStatus ;
3635use SebastianBergmann \CodeUnitReverseLookup \Wizard ;
3736
3837/**
@@ -54,7 +53,9 @@ final class CodeCoverage
5453
5554 private bool $ ignoreDeprecatedCode = false ;
5655
57- private PhptTestCase |string |TestCase |null $ currentId ;
56+ private ?string $ currentId = null ;
57+
58+ private ?TestSize $ currentSize = null ;
5859
5960 private ProcessedCodeCoverageData $ data ;
6061
@@ -92,9 +93,10 @@ public function getReport(): Directory
9293 */
9394 public function clear (): void
9495 {
95- $ this ->currentId = null ;
96- $ this ->data = new ProcessedCodeCoverageData ;
97- $ this ->tests = [];
96+ $ this ->currentId = null ;
97+ $ this ->currentSize = null ;
98+ $ this ->data = new ProcessedCodeCoverageData ;
99+ $ this ->tests = [];
98100 }
99101
100102 /**
@@ -143,23 +145,26 @@ public function setTests(array $tests): void
143145 $ this ->tests = $ tests ;
144146 }
145147
146- public function start (PhptTestCase | string | TestCase $ id , bool $ clear = false ): void
148+ public function start (string $ id, TestSize $ size = null , bool $ clear = false ): void
147149 {
148150 if ($ clear ) {
149151 $ this ->clear ();
150152 }
151153
152- $ this ->currentId = $ id ;
154+ $ this ->currentId = $ id ;
155+ $ this ->currentSize = $ size ;
153156
154157 $ this ->driver ->start ();
155158 }
156159
157- public function stop (bool $ append = true , array |false $ linesToBeCovered = [], array $ linesToBeUsed = []): RawCodeCoverageData
160+ public function stop (bool $ append = true , TestStatus $ status = null , array |false $ linesToBeCovered = [], array $ linesToBeUsed = []): RawCodeCoverageData
158161 {
159162 $ data = $ this ->driver ->stop ();
160- $ this ->append ($ data , null , $ append , $ linesToBeCovered , $ linesToBeUsed );
161163
162- $ this ->currentId = null ;
164+ $ this ->append ($ data , null , $ append , $ status , $ linesToBeCovered , $ linesToBeUsed );
165+
166+ $ this ->currentId = null ;
167+ $ this ->currentSize = null ;
163168
164169 return $ data ;
165170 }
@@ -169,7 +174,7 @@ public function stop(bool $append = true, array|false $linesToBeCovered = [], ar
169174 * @throws TestIdMissingException
170175 * @throws UnintentionallyCoveredCodeException
171176 */
172- public function append (RawCodeCoverageData $ rawData , PhptTestCase | string | TestCase | null $ id = null , bool $ append = true , array |false $ linesToBeCovered = [], array $ linesToBeUsed = []): void
177+ public function append (RawCodeCoverageData $ rawData , string $ id = null , bool $ append = true , TestStatus $ status = null , array |false $ linesToBeCovered = [], array $ linesToBeUsed = []): void
173178 {
174179 if ($ id === null ) {
175180 $ id = $ this ->currentId ;
@@ -179,6 +184,16 @@ public function append(RawCodeCoverageData $rawData, PhptTestCase|string|TestCas
179184 throw new TestIdMissingException ;
180185 }
181186
187+ if ($ status === null ) {
188+ $ status = TestStatus::unknown ();
189+ }
190+
191+ $ size = $ this ->currentSize ;
192+
193+ if ($ size === null ) {
194+ $ size = TestSize::unknown ();
195+ }
196+
182197 $ this ->applyFilter ($ rawData );
183198
184199 $ this ->applyExecutableLinesFilter ($ rawData );
@@ -197,30 +212,18 @@ public function append(RawCodeCoverageData $rawData, PhptTestCase|string|TestCas
197212 $ this ->applyCoversAnnotationFilter (
198213 $ rawData ,
199214 $ linesToBeCovered ,
200- $ linesToBeUsed
215+ $ linesToBeUsed ,
216+ $ size
201217 );
202218
203219 if (empty ($ rawData ->lineCoverage ())) {
204220 return ;
205221 }
206222
207- $ size = 'unknown ' ;
208- $ status = 'unknown ' ;
209- $ fromTestcase = false ;
210-
211- if ($ id instanceof TestCase) {
212- $ fromTestcase = true ;
213-
214- $ size = $ id ->size ()->asString ();
215- $ status = $ id ->status ()->asString ();
216- $ id = get_class ($ id ) . ':: ' . $ id ->nameWithDataSet ();
217- } elseif ($ id instanceof PhptTestCase) {
218- $ fromTestcase = true ;
219- $ size = 'large ' ;
220- $ id = $ id ->getName ();
221- }
222-
223- $ this ->tests [$ id ] = ['size ' => $ size , 'status ' => $ status , 'fromTestcase ' => $ fromTestcase ];
223+ $ this ->tests [$ id ] = [
224+ 'size ' => $ size ->asString (),
225+ 'status ' => $ status ->asString (),
226+ ];
224227
225228 $ this ->data ->markCodeAsExecutedByTestCase ($ id , $ rawData );
226229 }
@@ -344,7 +347,7 @@ public function detectsDeadCode(): bool
344347 * @throws ReflectionException
345348 * @throws UnintentionallyCoveredCodeException
346349 */
347- private function applyCoversAnnotationFilter (RawCodeCoverageData $ rawData , array |false $ linesToBeCovered , array $ linesToBeUsed ): void
350+ private function applyCoversAnnotationFilter (RawCodeCoverageData $ rawData , array |false $ linesToBeCovered , array $ linesToBeUsed, TestSize $ size ): void
348351 {
349352 if ($ linesToBeCovered === false ) {
350353 $ rawData ->clear ();
@@ -356,9 +359,7 @@ private function applyCoversAnnotationFilter(RawCodeCoverageData $rawData, array
356359 return ;
357360 }
358361
359- if ($ this ->checkForUnintentionallyCoveredCode &&
360- (!$ this ->currentId instanceof TestCase ||
361- (!$ this ->currentId ->size ()->isMedium () && !$ this ->currentId ->size ()->isLarge ()))) {
362+ if ($ this ->checkForUnintentionallyCoveredCode && !$ size ->isMedium () && !$ size ->isLarge ()) {
362363 $ this ->performUnintentionallyCoveredCodeCheck ($ rawData , $ linesToBeCovered , $ linesToBeUsed );
363364 }
364365
0 commit comments