diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index 61f9c70..6563da6 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -27,9 +27,12 @@ jobs: # 4️⃣ PSR-12 Code Style Check - name: Check PSR-12 Coding Standards run: | - composer global require "squizlabs/php_codesniffer=*" - phpcs --standard=PSR12 --extensions=php -n . bin/audit-class-generator + composer global require squizlabs/php_codesniffer + phpcs --standard=PSR12 --extensions=php -n \ + bin/audit-class-generator \ + src \ + tests # 5️⃣ Run PHPUnit Tests - name: Run PHPUnit Tests - run: vendor/bin/phpunit --colors=always Tests/ + run: vendor/bin/phpunit --colors=always tests/ diff --git a/bin/audit-class-generator b/bin/audit-class-generator index a80859e..3038b58 100755 --- a/bin/audit-class-generator +++ b/bin/audit-class-generator @@ -1,8 +1,13 @@ #!/usr/bin/env php -getExtension()); +$fileIO = new FileIO(); -foreach ($files as $index => $file) { - $contents = file_get_contents($file); +foreach ($scanner->scan($config->getDirectory()) as $file) { + if (!$filter->matches($file)) { + continue; + } - file_put_contents($file, $applier->applyAllAuditTags($contents)); + $content = $fileIO->read($file); + $processed = $applier->applyAllAuditTags($content); + $fileIO->write($file, $processed); } echo "Done!"; echo PHP_EOL; - -//TODO: Allow blade as well. Maybe solved with configuration -function filterAllFilesForTwigExtension(Config $config): array -{ - $directoryToParse = getcwd() . DIRECTORY_SEPARATOR . $config->getDirectory(); - - echo PHP_EOL . 'We will be looking recursively in ' . $directoryToParse . ' for Twig files .. ' . PHP_EOL; - - $files = getDirContents($directoryToParse); - $output = []; - - foreach ($files as $file) { - if (str_ends_with($file, $config->getExtension())) { - $output[] = $file; - } - } - - return $output; -} - -function getDirContents($dir): array -{ - $results = []; - $files = scandir($dir); - - foreach ($files as $key => $value) { - if (!is_dir($dir . DIRECTORY_SEPARATOR . $value)) { - $results[] = $dir . DIRECTORY_SEPARATOR . $value; - } elseif ( - is_dir($dir . DIRECTORY_SEPARATOR . $value) - && !str_contains($value, '..') - && $value !== '.' - ) { - $results[] = $value; - $results = array_merge($results, getDirContents($dir . DIRECTORY_SEPARATOR . $value)); - } - } - - return $results; -} diff --git a/composer.json b/composer.json index 7554e5e..89d5963 100644 --- a/composer.json +++ b/composer.json @@ -20,12 +20,17 @@ "twig/twig": "*" }, "require-dev": { - "symfony/phpunit-bridge": "^4.4.9|^5.0.9|^6.0" + "phpunit/phpunit": "^9.6", + "mikey179/vfsstream": "^1.6" }, "autoload": { - "psr-4" : { "PatrickMaynard\\AuditClassGenerator\\" : "" }, - "exclude-from-classmap": [ - "/Tests/" - ] + "psr-4" : { + "PatrickMaynard\\AuditClassGenerator\\" : "src/" + } + }, + "autoload-dev": { + "psr-4" : { + "PatrickMaynard\\AuditClassGenerator\\Tests\\" : "tests/" + } } } diff --git a/Applier.php b/src/Application/BusinessLogic/Applier.php similarity index 93% rename from Applier.php rename to src/Application/BusinessLogic/Applier.php index 5699645..7b416ea 100644 --- a/Applier.php +++ b/src/Application/BusinessLogic/Applier.php @@ -1,6 +1,6 @@ isFile()) { + yield $file->getPathname(); + } + } + } +} diff --git a/src/Infrastructure/FileSystem/ExtensionFilter.php b/src/Infrastructure/FileSystem/ExtensionFilter.php new file mode 100644 index 0000000..f7f41ac --- /dev/null +++ b/src/Infrastructure/FileSystem/ExtensionFilter.php @@ -0,0 +1,17 @@ +extension; + } +} diff --git a/src/Infrastructure/FileSystem/FileIO.php b/src/Infrastructure/FileSystem/FileIO.php new file mode 100644 index 0000000..d502b6e --- /dev/null +++ b/src/Infrastructure/FileSystem/FileIO.php @@ -0,0 +1,42 @@ +expectException(FileDoesNotExistException::class); + $this->expectExceptionMessage('File does not exist: ' . $path); + + $fileIO->read($path); + } + + public function testReadFileIsEmpty(): void + { + $path = vfsStream::url('root/emptyfile.txt'); + + vfsStream::newFile('emptyfile.txt')->at($this->root); // Leere Datei erstellen + + $fileIO = new FileIO(); + + $this->expectException(EmptyFileException::class); + $this->expectExceptionMessage('File is empty: ' . $path); + + $fileIO->read($path); + } + + public function testReadFileWithBomIsEmpty(): void + { + vfsStream::newFile('emptyfile.txt') + ->withContent("\xEF\xBB\xBF") + ->at($this->root); // Leere Datei mit BOM erstellen + + $fileIO = new FileIO(); + $this->expectException(\RuntimeException::class); + $fileIO->read(vfsStream::url('root/emptyfile.txt')); + } + + public function testReadFileContainsTwigContent(): void + { + $content = 'Hello, {{ name }}!'; + vfsStream::newFile('twigfile.txt')->withContent($content)->at($this->root); + + $fileIO = new FileIO(); + $result = $fileIO->read(vfsStream::url('root/twigfile.txt')); + + $this->assertEquals($content, $result); + } + + public function testWriteFileDoesNotExist(): void + { + $path = vfsStream::url('root/nonexistingdir/newfile.txt'); + + $fileIO = new FileIO(); + + $this->expectException(FileDoesNotExistException::class); + $this->expectExceptionMessage('File does not exist: ' . $path); + + $fileIO->write($path, 'Test content'); + } + + public function testWriteFileExistsAndIsOverwritten(): void + { + $path = vfsStream::url('root/existingfile.txt'); + + vfsStream::newFile('existingfile.txt')->withContent('Old content')->at($this->root); + + $fileIO = new FileIO(); + $fileIO->write($path, 'New content'); + + $content = file_get_contents($path); + $this->assertEquals('New content', $content); + } + + public function testWriteFileWithoutWritePermissions(): void + { + $path = vfsStream::url('root/readonlyfile.txt'); + + vfsStream::newFile('readonlyfile.txt') + ->chmod(0444) + ->at($this->root); + + $fileIO = new FileIO(); + + $this->expectException(FileNotWritableException::class); + $this->expectExceptionMessage('File not writable: ' . $path); + + $fileIO->write($path, 'Test content'); + } + + protected function setUp(): void + { + // Erstelle ein virtuelles Dateisystem (vfsStream) + $this->root = vfsStream::setup('root'); // Wurzelverzeichnis erstellen + } +}