-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Auto-review tests to enforce @group
annotations
#6772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
paulbalandan
merged 5 commits into
codeigniter4:develop
from
paulbalandan:auto-review-group
Nov 12, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
55ff428
Move 'tests/AutoReview' to 'tests/system/AutoReview'
paulbalandan 6b241e4
Add auto review test for group annotations
paulbalandan d82e702
Update auto-review group name
paulbalandan 74b64bd
Update phpunit.xml.dist
paulbalandan 15225f6
Remove unnecessary method-level group annotations
paulbalandan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\AutoReview; | ||
|
||
use FilesystemIterator; | ||
use PHPUnit\Framework\TestCase; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use ReflectionClass; | ||
use SplFileInfo; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @group AutoReview | ||
*/ | ||
final class FrameworkCodeTest extends TestCase | ||
{ | ||
/** | ||
* Cache of discovered test class names. | ||
*/ | ||
private static array $testClasses = []; | ||
|
||
private static array $recognizedGroupAnnotations = [ | ||
'AutoReview', | ||
'CacheLive', | ||
'DatabaseLive', | ||
'Others', | ||
'SeparateProcess', | ||
]; | ||
|
||
/** | ||
* @dataProvider provideTestClassCases | ||
* | ||
* @phpstan-param class-string $class | ||
*/ | ||
public function testEachTestClassHasCorrectGroupAnnotation(string $class): void | ||
{ | ||
$reflection = new ReflectionClass($class); | ||
|
||
if ($reflection->isAbstract()) { | ||
$this->addToAssertionCount(1); | ||
|
||
return; | ||
} | ||
|
||
$docComment = (string) $reflection->getDocComment(); | ||
$this->assertNotEmpty($docComment, sprintf('[%s] Test class is missing a class-level PHPDoc.', $class)); | ||
|
||
preg_match_all('/@group (\S+)/', $docComment, $matches); | ||
array_shift($matches); | ||
$this->assertNotEmpty($matches[0], sprintf('[%s] Test class is missing a @group annotation.', $class)); | ||
|
||
$unrecognizedGroups = array_diff($matches[0], self::$recognizedGroupAnnotations); | ||
$this->assertEmpty($unrecognizedGroups, sprintf( | ||
"[%s] Unexpected @group annotation%s:\n%s\nExpected annotations to be in \"%s\".", | ||
$class, | ||
count($unrecognizedGroups) > 1 ? 's' : '', | ||
implode("\n", array_map( | ||
static fn (string $group): string => sprintf(' * @group %s', $group), | ||
$unrecognizedGroups | ||
)), | ||
implode(', ', self::$recognizedGroupAnnotations) | ||
)); | ||
} | ||
|
||
public function provideTestClassCases(): iterable | ||
{ | ||
foreach ($this->getTestClasses() as $class) { | ||
yield $class => [$class]; | ||
} | ||
} | ||
|
||
private function getTestClasses(): array | ||
{ | ||
if (self::$testClasses !== []) { | ||
return self::$testClasses; | ||
} | ||
|
||
helper('filesystem'); | ||
|
||
$directory = set_realpath(dirname(__DIR__), true); | ||
|
||
$iterator = new RecursiveIteratorIterator( | ||
new RecursiveDirectoryIterator( | ||
$directory, | ||
FilesystemIterator::SKIP_DOTS | ||
), | ||
RecursiveIteratorIterator::CHILD_FIRST | ||
); | ||
|
||
$testClasses = array_map( | ||
static function (SplFileInfo $file) use ($directory): string { | ||
$relativePath = substr_replace( | ||
$file->getPathname(), | ||
'', | ||
0, | ||
strlen($directory) | ||
); | ||
$relativePath = substr_replace( | ||
$relativePath, | ||
'', | ||
strlen($relativePath) - strlen(DIRECTORY_SEPARATOR . $file->getBasename()) | ||
); | ||
|
||
return sprintf( | ||
'CodeIgniter\\%s%s%s', | ||
strtr($relativePath, DIRECTORY_SEPARATOR, '\\'), | ||
$relativePath === '' ? '' : '\\', | ||
$file->getBasename('.' . $file->getExtension()) | ||
); | ||
MGatner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
array_filter( | ||
iterator_to_array($iterator, false), | ||
static fn (SplFileInfo $file): bool => $file->isFile() | ||
&& strpos($file->getPathname(), DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR) === false | ||
&& strpos($file->getPathname(), DIRECTORY_SEPARATOR . 'Views' . DIRECTORY_SEPARATOR) === false | ||
) | ||
); | ||
|
||
$testClasses = array_filter( | ||
$testClasses, | ||
static fn (string $class) => is_subclass_of($class, TestCase::class) | ||
); | ||
|
||
sort($testClasses); | ||
|
||
self::$testClasses = $testClasses; | ||
|
||
return $testClasses; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.