Skip to content

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
merged 5 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-autoreview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ jobs:
COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}

- name: Run AutoReview Tests
run: vendor/bin/phpunit --color=always --group=auto-review --no-coverage
run: vendor/bin/phpunit --color=always --group=AutoReview --no-coverage
2 changes: 1 addition & 1 deletion .github/workflows/test-phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ jobs:
COVERAGE_NAME: php-v${{ env.COVERAGE_PHP_VERSION }}-${{ matrix.db-platforms }}

- name: Test with PHPUnit
run: script -e -c "vendor/bin/phpunit --color=always --exclude-group=auto-review ${{ steps.phpunit-coverage-option.outputs.result }}"
run: script -e -c "vendor/bin/phpunit --color=always --exclude-group=AutoReview ${{ steps.phpunit-coverage-option.outputs.result }}"
env:
DB: ${{ matrix.db-platforms }}
TERM: xterm-256color
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"autoload-dev": {
"psr-4": {
"CodeIgniter\\": "tests/system/",
"CodeIgniter\\AutoReview\\": "tests/AutoReview/",
"Utils\\": "utils/"
}
},
Expand Down
38 changes: 17 additions & 21 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,30 @@
beStrictAboutTodoAnnotatedTests="true"
cacheResultFile="build/.phpunit.cache/test-results"
colors="true"
columns="max"
failOnRisky="true"
failOnWarning="true"
verbose="true">

<coverage processUncoveredFiles="true" ignoreDeprecatedCodeUnits="true">
<coverage cacheDirectory="build/.phpunit.cache/code-coverage"
processUncoveredFiles="true"
ignoreDeprecatedCodeUnits="true">
<include>
<directory suffix=".php">./system</directory>
<directory suffix=".php">system</directory>
</include>

<exclude>
<directory>./system/Commands/Generators/Views</directory>
<directory>./system/Debug/Toolbar/Views</directory>
<directory>./system/Pager/Views</directory>
<directory>./system/ThirdParty</directory>
<directory>./system/Validation/Views</directory>
<file>./system/bootstrap.php</file>
<file>./system/ComposerScripts.php</file>
<file>./system/Config/Routes.php</file>
<file>./system/Test/bootstrap.php</file>
<file>./system/Test/ControllerTester.php</file>
<file>./system/Test/FeatureTestCase.php</file>
<directory>system/Commands/Generators/Views</directory>
<directory>system/Debug/Toolbar/Views</directory>
<directory>system/Pager/Views</directory>
<directory>system/ThirdParty</directory>
<directory>system/Validation/Views</directory>
<file>system/bootstrap.php</file>
<file>system/ComposerScripts.php</file>
<file>system/Config/Routes.php</file>
<file>system/Test/bootstrap.php</file>
<file>system/Test/ControllerTester.php</file>
<file>system/Test/FeatureTestCase.php</file>
</exclude>

<report>
Expand All @@ -36,15 +39,8 @@
</coverage>

<testsuites>
<testsuite name="AutoReview">
<directory>./tests/AutoReview</directory>
</testsuite>
<testsuite name="System">
<directory>./tests/system</directory>
<exclude>./tests/system/Database</exclude>
</testsuite>
<testsuite name="Database">
<directory>./tests/system/Database</directory>
<directory>tests/system</directory>
</testsuite>
</testsuites>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @coversNothing
*
* @group auto-review
* @group AutoReview
*/
final class ComposerJsonTest extends TestCase
{
Expand All @@ -32,8 +32,8 @@ protected function setUp(): void
{
parent::setUp();

$this->devComposer = $this->getComposerJson(dirname(__DIR__, 2) . '/composer.json');
$this->frameworkComposer = $this->getComposerJson(dirname(__DIR__, 2) . '/admin/framework/composer.json');
$this->devComposer = $this->getComposerJson(dirname(__DIR__, 3) . '/composer.json');
$this->frameworkComposer = $this->getComposerJson(dirname(__DIR__, 3) . '/admin/framework/composer.json');
}

public function testFrameworkRequireIsTheSameWithDevRequire(): void
Expand Down
141 changes: 141 additions & 0 deletions tests/system/AutoReview/FrameworkCodeTest.php
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())
);
},
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;
}
}
3 changes: 0 additions & 3 deletions tests/system/Database/BaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ public function testCanConnectAndStoreConnection()
$this->assertSame(123, $db->getConnection());
}

/**
* @group single
*/
public function testCanConnectToFailoverWhenNoConnectionAvailable()
{
$options = $this->options;
Expand Down
2 changes: 0 additions & 2 deletions tests/system/Database/BaseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ public function testSimpleBindsWithNamedBindPlaceholderElsewhere()
}

/**
* @group single
*
* @see https://github.com/codeigniter4/CodeIgniter4/issues/201
*/
public function testSimilarNamedBinds()
Expand Down
3 changes: 0 additions & 3 deletions tests/system/Database/Builder/LikeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@ public function testOrNotLike()
$this->assertSame($expectedBinds, $builder->getBinds());
}

/**
* @group single
*/
public function testCaseInsensitiveLike()
{
$builder = new BaseBuilder('job', $this->db);
Expand Down
5 changes: 0 additions & 5 deletions tests/system/Database/Live/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ public function testDeleteWithInternalWhere()
$this->dontSeeInDatabase('job', ['name' => 'Developer']);
}

/**
* @group single
*
* @throws DatabaseException
*/
public function testDeleteWithLimit()
{
$this->seeNumRecords(2, 'user', ['country' => 'US']);
Expand Down
4 changes: 1 addition & 3 deletions tests/system/Database/Live/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ public function testUpdateWithWhereSameColumn3()
}

/**
* @group single
*
* @see https://github.com/codeigniter4/CodeIgniter4/issues/324
* @see https://github.com/codeigniter4/CodeIgniter4/issues/324
*/
public function testUpdatePeriods()
{
Expand Down
3 changes: 0 additions & 3 deletions tests/system/Database/Live/WhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ public function testWhereIn()
$this->assertSame('Accountant', $jobs[1]->name);
}

/**
* @group single
*/
public function testWhereNotIn()
{
$jobs = $this->db->table('job')
Expand Down
6 changes: 0 additions & 6 deletions tests/system/HTTP/Files/FileCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ public function testExtensionGuessing()
$this->assertSame('zip', $file->guessExtension());
}

/**
* @group single
*/
public function testAllReturnsValidSingleFileNestedName()
{
$_FILES = [
Expand Down Expand Up @@ -309,9 +306,6 @@ public function testHasFileWithMultipleFilesWithDifferentNames()
$this->assertTrue($collection->hasFile('userfile2'));
}

/**
* @group single
*/
public function testHasFileWithSingleFileNestedName()
{
$_FILES = [
Expand Down
3 changes: 0 additions & 3 deletions tests/system/HTTP/NegotiateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ public function testNegotiateMediaSupportsStrictMatching()
$this->assertSame('', $this->negotiate->media(['text/plain'], true));
}

/**
* @group single
*/
public function testAcceptCharsetMatchesBasics()
{
$this->request->setHeader('Accept-Charset', 'iso-8859-5, unicode-1-1;q=0.8');
Expand Down
4 changes: 0 additions & 4 deletions tests/system/HTTP/URITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,6 @@ public function testResolveRelativeURI($rel, $expected)
/**
* @dataProvider defaultResolutions
*
* @group single
*
* @param mixed $rel
* @param mixed $expected
*/
Expand Down Expand Up @@ -792,8 +790,6 @@ public function testGetQueryWithStrings()

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/331
*
* @group single
*/
public function testNoExtraSlashes()
{
Expand Down
3 changes: 0 additions & 3 deletions tests/system/Helpers/NumberHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ public function testQuadrillions()
$this->assertSame('123.5 quadrillion', number_to_amount('123,456,700,000,000,000', 1, 'en_US'));
}

/**
* @group single
*/
public function testCurrencyCurrentLocale()
{
$this->assertSame('$1,235', number_to_currency(1234.56, 'USD', 'en_US'));
Expand Down
3 changes: 0 additions & 3 deletions tests/system/Session/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,6 @@ public function testSetTempDataArraySingleTTL()
$this->assertLessThanOrEqual($_SESSION['__ci_vars']['baz'], $time + 200);
}

/**
* @group single
*/
public function testGetTestDataReturnsAll()
{
$session = $this->getInstance();
Expand Down
3 changes: 0 additions & 3 deletions tests/system/Throttle/ThrottleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ public function testRemove()
$this->assertTrue($throttler->check('127.0.0.1', 1, MINUTE));
}

/**
* @group single
*/
public function testDecrementsValues()
{
$throttler = new Throttler($this->cache);
Expand Down
Loading