| 1 | <?php |
| 2 | /** |
| 3 | * Copyright (C) 2017 Kunal Mehta <[email protected]> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | use PHPUnit\Framework\CodeCoverageException; |
| 22 | use PHPUnit\Util\Test; |
| 23 | |
| 24 | /** |
| 25 | * Check that `@covers` tags are valid. PHPUnit only does this when generating |
| 26 | * code coverage reports, which is slow and we generally don't do that when |
| 27 | * running tests during development and pre-merge in CI. |
| 28 | * |
| 29 | * @since 1.31 |
| 30 | */ |
| 31 | trait MediaWikiCoversValidator { |
| 32 | |
| 33 | /** |
| 34 | * Assert that all "test*" methods in the host class have valid `@covers` tags. |
| 35 | * |
| 36 | * Don't use a data provider here because this assertion will be run many |
| 37 | * thousands of times in CI, and the implicit overhead from PHPUnit with |
| 38 | * generating and executing a test case around each becomes rather significant |
| 39 | * at that scale. Also, when using a data provider, the setUp() and tearDown() |
| 40 | * of the host class would be re-run for every check, which becomes very |
| 41 | * expensive for integration tests that involve databases. |
| 42 | * @coversNothing |
| 43 | */ |
| 44 | public function testValidCovers() { |
| 45 | $class = static::class; |
| 46 | foreach ( get_class_methods( $this ) as $method ) { |
| 47 | if ( str_starts_with( $method, 'test' ) ) { |
| 48 | try { |
| 49 | Test::getLinesToBeCovered( $class, $method ); |
| 50 | } catch ( CodeCoverageException $ex ) { |
| 51 | $this->fail( "$class::$method: " . $ex->getMessage() ); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | $this->addToAssertionCount( 1 ); |
| 57 | } |
| 58 | } |