From c17bb575cf5de386cfbcab67a8a5d2bf5cd74168 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sat, 7 Nov 2026 00:34:11 +0800 Subject: [PATCH] chore: re-enable mutation testing --- .gitattributes | 2 +- .github/workflows/mutation.yml | 74 +++++++++++++++++++ bin/mutation-filter | 24 ++++++ composer.json | 13 +++- infection.json.dist | 19 ----- infection.json5 | 28 +++++++ .../Authentication/Filters/AuthRatesTest.php | 69 +++++++++++++++++ .../GenerateDummyPasswordHashTest.php | 17 +++++ tests/Commands/Setup/ContentReplacerTest.php | 9 +++ tests/Commands/SetupTest.php | 32 +++++++- tests/Commands/UserModelGeneratorTest.php | 67 +++++++++-------- 11 files changed, 301 insertions(+), 53 deletions(-) create mode 100644 .github/workflows/mutation.yml create mode 100755 bin/mutation-filter delete mode 100644 infection.json.dist create mode 100644 infection.json5 create mode 100644 tests/Authentication/Filters/AuthRatesTest.php diff --git a/.gitattributes b/.gitattributes index fb192d9bc..8fb160e28 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8,7 +8,7 @@ /.php-cs-fixer.dist.php export-ignore /composer-unused.php export-ignore /structarmed.php export-ignore -/infection.json.dist export-ignore +/infection.json5 export-ignore /mkdocs.yml export-ignore /phpstan-baseline.php export-ignore /phpstan.neon.dist export-ignore diff --git a/.github/workflows/mutation.yml b/.github/workflows/mutation.yml new file mode 100644 index 000000000..0425207c2 --- /dev/null +++ b/.github/workflows/mutation.yml @@ -0,0 +1,74 @@ +name: Mutation Testing + +on: + pull_request: + branches: + - develop + paths: + - 'src/**.php' + - 'tests/**.php' + - 'composer.json' + - 'infection.json5' + - 'phpunit.dist.xml' + - 'phpstan.neon.dist' + - '.github/workflows/mutation.yml' + push: + branches: + - develop + paths: + - 'src/**.php' + - 'tests/**.php' + - 'composer.json' + - 'infection.json5' + - 'phpunit.dist.xml' + - 'phpstan.neon.dist' + - '.github/workflows/mutation.yml' + +permissions: + contents: read + +jobs: + mutation: + name: Infection + runs-on: ubuntu-latest + if: "!contains(github.event.head_commit.message, '[ci skip]')" + + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Set up PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # v2.40.0 + with: + php-version: '8.5' + extensions: sqlite3 + coverage: xdebug + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Get composer cache directory + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Cache composer dependencies + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: composer-8.5-${{ hashFiles('**/composer.json') }} + restore-keys: | + composer-8.5- + composer- + + - name: Install dependencies + run: composer update --ansi + + - name: Run mutation tests on changed lines + if: github.event_name == 'pull_request' + run: composer mutation:filter -- --ansi + + - name: Run full mutation tests + if: github.event_name == 'push' + run: composer mutation:check -- --ansi diff --git a/bin/mutation-filter b/bin/mutation-filter new file mode 100755 index 000000000..20340e2d2 --- /dev/null +++ b/bin/mutation-filter @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +cd "$(dirname "$0")/.." || exit 1 + +untracked=$(git ls-files --others --exclude-standard src tests) + +restore() { + if [ -n "$untracked" ]; then + git reset --quiet -- $untracked + fi +} + +trap restore EXIT + +if [ -n "$untracked" ]; then + git add --intent-to-add -- $untracked +fi + +vendor/bin/infection \ + --static-analysis-tool=phpstan \ + --with-uncovered \ + --git-diff-lines \ + --git-diff-base=origin/develop \ + "$@" diff --git a/composer.json b/composer.json index ff2ae2bff..9a34e76c8 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "codeigniter4/devkit": "^1.3", "codeigniter4/framework": ">=4.3.5 <4.5.0 || ^4.5.1", "firebase/php-jwt": "^7.0.3", + "infection/infection": "^0.32 || ^0.35", "mikey179/vfsstream": "^1.6.7", "mockery/mockery": "^1.0", "phpstan/phpstan-strict-rules": "^2.0" @@ -64,6 +65,7 @@ }, "config": { "allow-plugins": { + "infection/extension-installer": true, "phpstan/extension-installer": true }, "sort-packages": true @@ -89,7 +91,16 @@ "cs-fix": "php-cs-fixer fix --ansi --verbose --diff", "deduplicate": "phpcpd app/ src/ --exclude src/Database/Migrations/2020-12-28-223112_create_auth_tables.php --exclude src/Authentication/Authenticators/HmacSha256.php", "inspect": "vendor/bin/structarmed analyze", - "mutate": "infection --threads=2 --skip-initial-tests --coverage=build/phpunit", + "mutation:check": [ + "Composer\\Config::disableProcessTimeout", + "@putenv PYRAMETER_DISABLED=1", + "infection --static-analysis-tool=phpstan --with-uncovered" + ], + "mutation:filter": [ + "Composer\\Config::disableProcessTimeout", + "@putenv PYRAMETER_DISABLED=1", + "bin/mutation-filter" + ], "sa": "@analyze", "style": "@cs-fix", "test": "phpunit" diff --git a/infection.json.dist b/infection.json.dist deleted file mode 100644 index 7badcc674..000000000 --- a/infection.json.dist +++ /dev/null @@ -1,19 +0,0 @@ -{ - "source": { - "directories": [ - "src/" - ], - "excludes": [ - "Config", - "Database/Migrations", - "Views" - ] - }, - "logs": { - "text": "build/infection.log" - }, - "mutators": { - "@default": true - }, - "bootstrap": "vendor/codeigniter4/framework/system/Test/bootstrap.php" -} diff --git a/infection.json5 b/infection.json5 new file mode 100644 index 000000000..ba1571093 --- /dev/null +++ b/infection.json5 @@ -0,0 +1,28 @@ +{ + "$schema": "./vendor/infection/infection/resources/schema.json", + "source": { + "directories": [ + "src/" + ], + "excludes": [ + "Config", + "Database/Migrations", + "Views" + ] + }, + "timeout": 30, + "threads": "max", + "dotsPerRow": "max", + "logs": { + "text": "build/infection.log" + }, + "tmpDir": "build", + "minMsi": 72, + "minCoveredMsi": 79, + "ignoreMsiWithNoMutations": true, + "mutators": { + "@default": true + }, + "bootstrap": "vendor/codeigniter4/framework/system/Test/bootstrap.php", + "testFramework": "phpunit" +} diff --git a/tests/Authentication/Filters/AuthRatesTest.php b/tests/Authentication/Filters/AuthRatesTest.php new file mode 100644 index 000000000..e81c43247 --- /dev/null +++ b/tests/Authentication/Filters/AuthRatesTest.php @@ -0,0 +1,69 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace Tests\Authentication\Filters; + +use CodeIgniter\HTTP\CLIRequest; +use CodeIgniter\HTTP\IncomingRequest; +use CodeIgniter\HTTP\ResponseInterface; +use CodeIgniter\Shield\Filters\AuthRates; +use CodeIgniter\Throttle\Throttler; +use Config\Services; +use Tests\Support\TestCase; + +/** + * @internal + */ +final class AuthRatesTest extends TestCase +{ + public function testAllowsRequestWithinLimit(): void + { + $request = $this->createMock(IncomingRequest::class); + $request->method('getIPAddress')->willReturn('203.0.113.7'); + + $throttler = $this->createMock(Throttler::class); + $throttler->expects($this->once()) + ->method('check') + ->with(md5('203.0.113.7'), 10, MINUTE, 1) + ->willReturn(true); + Services::injectMock('throttler', $throttler); + + $this->assertNull((new AuthRates())->before($request)); + } + + public function testRejectsRequestOverLimit(): void + { + $request = $this->createMock(IncomingRequest::class); + $request->method('getIPAddress')->willReturn('203.0.113.7'); + + $throttler = $this->createMock(Throttler::class); + $throttler->method('check')->willReturn(false); + $throttler->method('getTokenTime')->willReturn(42); + Services::injectMock('throttler', $throttler); + + $response = (new AuthRates())->before($request); + + $this->assertInstanceOf(ResponseInterface::class, $response); + $this->assertSame(429, $response->getStatusCode()); + $this->assertSame(lang('Auth.throttled', [42]), $response->getReasonPhrase()); + } + + public function testIgnoresRequestThatIsNotIncomingRequest(): void + { + $throttler = $this->createMock(Throttler::class); + $throttler->expects($this->never())->method('check'); + Services::injectMock('throttler', $throttler); + + $this->assertNull((new AuthRates())->before($this->createStub(CLIRequest::class))); + } +} diff --git a/tests/Commands/GenerateDummyPasswordHashTest.php b/tests/Commands/GenerateDummyPasswordHashTest.php index ef22c0d73..6527443df 100644 --- a/tests/Commands/GenerateDummyPasswordHashTest.php +++ b/tests/Commands/GenerateDummyPasswordHashTest.php @@ -13,8 +13,10 @@ namespace Tests\Commands; +use CodeIgniter\Shield\Authentication\Passwords; use CodeIgniter\Shield\Commands\GenerateDummyPasswordHash; use CodeIgniter\Shield\Test\MockInputOutput; +use Config\Services; use Tests\Support\TestCase; /** @@ -62,6 +64,21 @@ public function testGeneratesHashUsingCurrentArgon2idSettings(): void $this->assertFalse(service('passwords')->needsRehash($hash)); } + public function testHashesThirtyTwoRandomBytesAsHex(): void + { + $passwords = $this->createMock(Passwords::class); + $passwords->expects($this->once()) + ->method('hash') + ->with($this->matchesRegularExpression('/\A[0-9a-f]{64}\z/')) + ->willReturn('$2y$04$hash'); + Services::injectMock('passwords', $passwords); + + $io = new MockInputOutput(); + GenerateDummyPasswordHash::setInputOutput($io); + + $this->assertNotFalse(command('shield:generate-dummy-hash')); + } + private function generateHash(): string { $io = new MockInputOutput(); diff --git a/tests/Commands/Setup/ContentReplacerTest.php b/tests/Commands/Setup/ContentReplacerTest.php index 2a6c029e3..e24e693d6 100644 --- a/tests/Commands/Setup/ContentReplacerTest.php +++ b/tests/Commands/Setup/ContentReplacerTest.php @@ -118,6 +118,15 @@ public function testAddAfter(): void $this->assertSame($expected, $output); } + public function testAddReturnsTrueWhenTextWithRegexCharactersIsAlreadyPresent(): void + { + $replacer = new ContentReplacer(); + $text = 'service(\'auth\')->routes($routes);'; + $content = "assertTrue($replacer->add($content, $text, '/(.*)/su', '$1')); + } + public function testAddBefore(): void { $replacer = new ContentReplacer(); diff --git a/tests/Commands/SetupTest.php b/tests/Commands/SetupTest.php index 104980a91..377bfde42 100644 --- a/tests/Commands/SetupTest.php +++ b/tests/Commands/SetupTest.php @@ -17,7 +17,10 @@ use CodeIgniter\Shield\Test\MockInputOutput; use Config\Email as EmailConfig; use Config\Services; +use FilesystemIterator; use org\bovigo\vfs\vfsStream; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; use Tests\Support\TestCase; /** @@ -25,15 +28,36 @@ */ final class SetupTest extends TestCase { - private ?MockInputOutput $io = null; + private ?MockInputOutput $io = null; + private string $originalWorkingDirectory = ''; + private string $workingDirectory = ''; protected function tearDown(): void { + if ($this->workingDirectory !== '') { + chdir($this->originalWorkingDirectory); + $this->removeDirectory($this->workingDirectory); + } + parent::tearDown(); Setup::resetInputOutput(); } + private function removeDirectory(string $directory): void + { + $entries = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS), + RecursiveIteratorIterator::CHILD_FIRST, + ); + + foreach ($entries as $entry) { + $entry->isDir() && ! $entry->isLink() ? rmdir($entry->getPathname()) : unlink($entry->getPathname()); + } + + rmdir($directory); + } + /** * Set MockInputOutput and user inputs. * @@ -175,6 +199,12 @@ class Autoload extends AutoloadConfig */ private function createFilesystem(): string { + $this->originalWorkingDirectory = (string) getcwd(); + $this->workingDirectory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'shield_setup_' . bin2hex(random_bytes(8)); + + mkdir($this->workingDirectory); + chdir($this->workingDirectory); + $root = vfsStream::setup('root'); vfsStream::copyFromFileSystem( APPPATH, diff --git a/tests/Commands/UserModelGeneratorTest.php b/tests/Commands/UserModelGeneratorTest.php index 0e9124448..0e64f4080 100644 --- a/tests/Commands/UserModelGeneratorTest.php +++ b/tests/Commands/UserModelGeneratorTest.php @@ -15,12 +15,26 @@ use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Filters\CITestStreamFilter; +use org\bovigo\vfs\vfsStream; +use org\bovigo\vfs\vfsStreamDirectory; /** * @internal */ final class UserModelGeneratorTest extends CIUnitTestCase { + private const NAMESPACES = [ + 'App' => 'app', + 'CodeIgniter\Shield' => 'src', + ]; + + private vfsStreamDirectory $root; + + /** + * @var array> + */ + private array $originalNamespaces = []; + protected function setUp(): void { parent::setUp(); @@ -29,11 +43,18 @@ protected function setUp(): void CITestStreamFilter::addOutputFilter(); CITestStreamFilter::addErrorFilter(); - if (is_file(HOMEPATH . 'src/Models/UserModel.php')) { - copy(HOMEPATH . 'src/Models/UserModel.php', HOMEPATH . 'src/Models/UserModel.php.bak'); - } + $this->root = vfsStream::setup('root'); - $this->deleteTestFiles(); + $autoloader = service('autoloader'); + + foreach (self::NAMESPACES as $namespace => $directory) { + $this->originalNamespaces[$namespace] = $autoloader->getNamespace($namespace); + + $autoloader->removeNamespace($namespace); + $autoloader->addNamespace([ + $namespace => [$this->root->url() . '/' . $directory, ...$this->originalNamespaces[$namespace]], + ]); + } } protected function tearDown(): void @@ -43,27 +64,11 @@ protected function tearDown(): void CITestStreamFilter::removeOutputFilter(); CITestStreamFilter::removeErrorFilter(); - $this->deleteTestFiles(); - - if (is_file(HOMEPATH . 'src/Models/UserModel.php.bak')) { - copy(HOMEPATH . 'src/Models/UserModel.php.bak', HOMEPATH . 'src/Models/UserModel.php'); - unlink(HOMEPATH . 'src/Models/UserModel.php.bak'); - } - } - - private function deleteTestFiles(): void - { - $possibleFiles = [ - APPPATH . 'Models/UserModel.php', - HOMEPATH . 'src/Models/UserModel.php', - ]; - - foreach ($possibleFiles as $file) { - clearstatcache(true, $file); + $autoloader = service('autoloader'); - if (is_file($file)) { - unlink($file); - } + foreach ($this->originalNamespaces as $namespace => $paths) { + $autoloader->removeNamespace($namespace); + $autoloader->addNamespace([$namespace => $paths]); } } @@ -76,7 +81,7 @@ public function testGenerateUserModel(): void { command('shield:model UserModel'); - $filepath = APPPATH . 'Models/UserModel.php'; + $filepath = $this->root->url() . '/app/Models/UserModel.php'; $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); $this->assertFileExists($filepath); @@ -92,7 +97,7 @@ public function testGenerateUserModelCustomNamespace(): void { command('shield:model UserModel --namespace CodeIgniter\\\\Shield'); - $filepath = HOMEPATH . 'src/Models/UserModel.php'; + $filepath = $this->root->url() . '/src/Models/UserModel.php'; $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); $this->assertFileExists($filepath); @@ -109,7 +114,7 @@ public function testGenerateUserModelWithForce(): void command('shield:model UserModel --force'); $this->assertStringContainsString('File overwritten: ', CITestStreamFilter::$buffer); - $this->assertFileExists(APPPATH . 'Models/UserModel.php'); + $this->assertFileExists($this->root->url() . '/app/Models/UserModel.php'); } public function testGenerateUserModelWithSuffix(): void @@ -118,7 +123,7 @@ public function testGenerateUserModelWithSuffix(): void $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); - $filepath = APPPATH . 'Models/UserModel.php'; + $filepath = $this->root->url() . '/app/Models/UserModel.php'; $this->assertFileExists($filepath); $this->assertStringContainsString('class UserModel extends ShieldUserModel', $this->getFileContents($filepath)); } @@ -129,7 +134,7 @@ public function testGenerateUserModelWithoutClassNameInput(): void $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); - $filepath = APPPATH . 'Models/UserModel.php'; + $filepath = $this->root->url() . '/app/Models/UserModel.php'; $this->assertFileExists($filepath); $this->assertStringContainsString('class UserModel extends ShieldUserModel', $this->getFileContents($filepath)); } @@ -139,13 +144,13 @@ public function testGenerateUserCannotAcceptShieldUserModelAsInput(): void command('shield:model ShieldUserModel'); $this->assertStringContainsString('Cannot use `ShieldUserModel` as class name as this conflicts with the parent class.', CITestStreamFilter::$buffer); - $this->assertFileDoesNotExist(APPPATH . 'Models/UserModel.php'); + $this->assertFileDoesNotExist($this->root->url() . '/app/Models/UserModel.php'); CITestStreamFilter::$buffer = ''; command('shield:model ShieldUser --suffix'); $this->assertStringContainsString('Cannot use `ShieldUserModel` as class name as this conflicts with the parent class.', CITestStreamFilter::$buffer); - $this->assertFileDoesNotExist(APPPATH . 'Models/UserModel.php'); + $this->assertFileDoesNotExist($this->root->url() . '/app/Models/UserModel.php'); } }