[
  {
    "path": ".gitattributes",
    "content": "/tests export-ignore\n.gitattributes export-ignore\n.gitignore export-ignore\n.travis.yml export-ignore\nphpunit.xml.dist export-ignore\n"
  },
  {
    "path": ".gitignore",
    "content": ".idea/\n/vendor\ncomposer.phar\ncomposer.lock\n/.phpunit.result.cache\n"
  },
  {
    "path": ".scrutinizer.yml",
    "content": "checks:\n    php:\n        code_rating: true\n\nfilter:\n    paths:\n        - src/ClamavValidator/*\n\ntools:\n    external_code_coverage: true\n"
  },
  {
    "path": ".travis.yml",
    "content": "language: php\n\nphp:\n  - 8.0\n  - 8.1\n  - 8.2\n  - 8.3\n  - 8.4\n\nbefore_install:\n  - sudo apt-get update -qq\n  - sudo apt-get install clamav-daemon -qq\n  - sudo freshclam\n  - sudo service clamav-daemon start\n\nbefore_script:\n  - composer self-update\n  - composer install --prefer-source --no-interaction --dev\n\nscript:\n  - php vendor/bin/phpunit --colors --coverage-clover build/logs/clover.xml\n\nafter_script: if [ $(phpenv version-name) = \"8.4\" ]; then php vendor/bin/ocular code-coverage:upload --format=php-clover build/logs/clover.xml; fi\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2014 Krishnaprasad MG\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n"
  },
  {
    "path": "README.md",
    "content": "# ClamAV Virus Validator For Laravel\n\n[![Code Coverage](https://scrutinizer-ci.com/g/sunspikes/clamav-validator/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/sunspikes/clamav-validator/?branch=master)\n[![Code Quality](https://scrutinizer-ci.com/g/sunspikes/clamav-validator/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/sunspikes/clamav-validator)\n[![Latest Stable Version](https://poser.pugx.org/sunspikes/clamav-validator/v/stable)](https://packagist.org/packages/sunspikes/clamav-validator)\n[![License](https://poser.pugx.org/sunspikes/clamav-validator/license)](https://packagist.org/packages/sunspikes/clamav-validator)\n\nA custom Laravel virus validator based on ClamAV anti-virus scanner for file uploads.\n\n* [Requirements](#requirements)\n* [Installation](#installation)\n* [Configuration](#configuration)\n* [Usage](#usage)\n* [Author](#author)\n\n<a name=\"requirements\"></a>\n## Requirements\n\n- PHP >= 8.0\n- Laravel 9.x, 10.x, 11.x, 12.x, or 13.x\n- ClamAV anti-virus scanner running on the server\n\nYou can see the ClamAV installation instructions on the official [ClamAV documentation](http://www.clamav.net/documents/installing-clamav).\n\nFor example on an Ubuntu machine, you can do:\n\n```sh\n# Install clamav virus scanner\nsudo apt-get update && sudo apt-get install -y clamav-daemon\n\n# Update virus definitions\nsudo freshclam\n\n# Start the scanner service\nsudo systemctl enable --now clamav-daemon clamav-freshclam\n```\n\nThis package is not tested on Windows, but if you have ClamAV running (usually on port 3310) it should work.\nYou will also need to have `sockets` extension installed and enabled (all executions without this module will fail with this error - `\"Use of undefined constant 'AF_INET'\"`).\n\n<a name=\"installation\"></a>\n## Installation\n\n#### 1. Install the package through [Composer](http://getcomposer.org).\n\n   ```bash\n   composer require sunspikes/clamav-validator\n   ```\n\n#### 2. Publish assets from the vendor package\n\n##### Config file\n\nThe default configuration file does use `ENV` to override the defaults. If you want to change the configuration file\nanyway you run the following command to publish the package config file:\n\n    php artisan vendor:publish --provider=\"Sunspikes\\ClamavValidator\\ClamavValidatorServiceProvider\" --tag=config\n\nOnce the command is finished you should have a `config/clamav.php` file that will be used as well.\n\n##### Language files\n\nIf you want to customize the translation or add your own language you can run the following command to\npublish the language files to a folder you maintain:\n\n    php artisan vendor:publish --provider=\"Sunspikes\\ClamavValidator\\ClamavValidatorServiceProvider\" --tag=lang\n\nThis will copy the language files to `lang/vendor/clamav-validator`.\n\n<a name=\"configuration\"></a>\n## Configuration\n\nThe package can be configured using environment variables:\n\n| Environment Variable | Default | Description |\n|---|---|---|\n| `CLAMAV_PREFERRED_SOCKET` | `unix_socket` | Socket type: `unix_socket` or `tcp_socket` |\n| `CLAMAV_UNIX_SOCKET` | `/var/run/clamav/clamd.ctl` | Path to the ClamAV unix socket |\n| `CLAMAV_TCP_SOCKET` | `tcp://127.0.0.1:3310` | TCP socket connection string |\n| `CLAMAV_SOCKET_CONNECT_TIMEOUT` | `null` | Connection timeout in seconds (`null` = no limit) |\n| `CLAMAV_SOCKET_READ_TIMEOUT` | `30` | Read timeout in seconds |\n| `CLAMAV_CLIENT_EXCEPTIONS` | `false` | Throw exceptions on scan failures instead of returning validation failure |\n| `CLAMAV_SKIP_VALIDATION` | `false` | Skip virus scanning entirely (useful for local development) |\n\n<a name=\"usage\"></a>\n## Usage\n\nUse it like any `Validator` rule:\n\n```php\n$rules = [\n    'file' => 'required|file|clamav',\n];\n```\n\nOr in a Form Request:\n\n```php\nclass UploadRequest extends FormRequest\n{\n    public function rules(): array\n    {\n        return [\n            'file' => 'required|file|clamav',\n        ];\n    }\n}\n```\n\n`ClamavValidator` will automatically run multiple files one-by-one through ClamAV in case `file` represents multiple uploaded files.\n\n<a name=\"author\"></a>\n## Author\n\nKrishnaprasad MG [@sunspikes] and other [awesome contributors](https://github.com/sunspikes/clamav-validator/graphs/contributors)\n"
  },
  {
    "path": "composer.json",
    "content": "{\n    \"name\": \"sunspikes/clamav-validator\",\n    \"description\": \"Custom Laravel anti-virus validator for file uploads using ClamAV.\",\n    \"keywords\": [\n        \"laravel\",\n        \"validator\",\n        \"clamav\",\n        \"virus\",\n        \"antivirus\"\n    ],\n    \"homepage\": \"https://github.com/sunspikes/clamav-validator\",\n    \"license\": \"MIT\",\n    \"authors\": [\n        {\n            \"name\": \"Krishnaprasad MG\",\n            \"email\": \"sunspikes@gmail.com\"\n        }\n    ],\n    \"require\": {\n        \"php\": \"^8.0\",\n        \"ext-sockets\": \"*\",\n        \"xenolope/quahog\": \"^3.0\",\n        \"illuminate/support\": \"^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0\",\n        \"illuminate/validation\": \"^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0\"\n    },\n    \"require-dev\": {\n        \"roave/security-advisories\": \"dev-master\",\n        \"phpunit/phpunit\": \"^10.5 || ^11.0\",\n        \"mockery/mockery\": \"^1.6\"\n    },\n    \"autoload\": {\n        \"psr-4\": {\n            \"Sunspikes\\\\\": \"src/\"\n        }\n    },\n    \"extra\": {\n        \"laravel\": {\n            \"providers\": [\n                \"Sunspikes\\\\ClamavValidator\\\\ClamavValidatorServiceProvider\"\n            ]\n        }\n    },\n    \"autoload-dev\": {\n        \"psr-4\": {\n            \"Sunspikes\\\\Tests\\\\ClamavValidator\\\\\": \"tests/\"\n        }\n    },\n    \"minimum-stability\": \"dev\",\n    \"prefer-stable\": true\n}\n"
  },
  {
    "path": "config/clamav.php",
    "content": "<?php\n\nreturn [\n    /*\n    |--------------------------------------------------------------------------\n    | Preferred socket\n    |--------------------------------------------------------------------------\n    |\n    | This option controls the socket which is used, which is unix_socket or tcp_socket.\n    |\n    | Please note if the unix_socket is used and the socket-file is not found the tcp socket will be\n    | used as fallback.\n    */\n    'preferred_socket' => env('CLAMAV_PREFERRED_SOCKET', 'unix_socket'),\n\n    /*\n    |--------------------------------------------------------------------------\n    | Unix Socket\n    |--------------------------------------------------------------------------\n    | This option defines the location to the unix socket-file. For example\n    | /var/run/clamav/clamd.ctl\n    */\n    'unix_socket' => env('CLAMAV_UNIX_SOCKET', '/var/run/clamav/clamd.ctl'),\n\n    /*\n    |--------------------------------------------------------------------------\n    | TCP Socket\n    |--------------------------------------------------------------------------\n    | This option defines the TCP socket to the ClamAV instance.\n    */\n    'tcp_socket' => env('CLAMAV_TCP_SOCKET', 'tcp://127.0.0.1:3310'),\n\n    /*\n    |--------------------------------------------------------------------------\n    | Socket connect timeout\n    |--------------------------------------------------------------------------\n    | This option defines the maximum time to wait in seconds for socket connection attempts before failure or timeout, default null = no limit.\n    */\n    'socket_connect_timeout' => env('CLAMAV_SOCKET_CONNECT_TIMEOUT', null),\n\n    /*\n    |--------------------------------------------------------------------------\n    | Socket read timeout\n    |--------------------------------------------------------------------------\n    | This option defines the maximum time to wait in seconds for a read.\n    */\n    'socket_read_timeout' => env('CLAMAV_SOCKET_READ_TIMEOUT', 30),\n\n    /*\n    |--------------------------------------------------------------------------\n    | Throw exceptions instead of returning failures when scan fails.\n    |--------------------------------------------------------------------------\n    | This makes it easier for a developer to find the source of a clamav\n    | failure, but an end user may only see a 500 error for the user\n    | if exceptions are not displayed.\n    */\n    'client_exceptions' => env('CLAMAV_CLIENT_EXCEPTIONS', false),\n\n    /*\n    |--------------------------------------------------------------------------\n    | Skip validation\n    |--------------------------------------------------------------------------\n    | This skips the virus validation for current environment.\n    |\n    | Please note when true it won't connect to ClamAV and will skip the virus validation.\n    */\n    'skip_validation' => env('CLAMAV_SKIP_VALIDATION', false),\n];\n"
  },
  {
    "path": "phpunit.xml",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<phpunit backupGlobals=\"false\"\n         bootstrap=\"vendor/autoload.php\"\n         colors=\"true\"\n         processIsolation=\"false\"\n         stopOnFailure=\"false\"\n>\n    <source>\n        <include>\n            <directory suffix=\".php\">src</directory>\n        </include>\n    </source>\n    <testsuites>\n        <testsuite name=\"ClamAV Validator Test Suite\">\n            <directory suffix=\".php\">./tests/</directory>\n        </testsuite>\n    </testsuites>\n</phpunit>\n"
  },
  {
    "path": "provides.json",
    "content": "{\n    \"providers\": [\n        \"Sunspikes\\\\ClamavValidator\\\\ClamavValidatorServiceProvider\"\n    ]\n}"
  },
  {
    "path": "src/ClamavValidator/ClamavValidatorException.php",
    "content": "<?php\n\nnamespace Sunspikes\\ClamavValidator;\n\nuse Exception;\nuse Throwable;\nuse Xenolope\\Quahog\\Result;\n\nclass ClamavValidatorException extends Exception\n{\n    public static function forNonReadableFile(string $file): static\n    {\n        return new static(\n            sprintf('The file \"%s\" is not readable', $file)\n        );\n    }\n\n    public static function forScanResult(Result $result): static\n    {\n        return new static(\n            sprintf(\n                'ClamAV scanner failed to scan file \"%s\" with error \"%s\"',\n                $result->getFilename(),\n                $result->getReason()\n            )\n        );\n    }\n\n    public static function forClientException(Throwable $exception): static\n    {\n        return new static(\n            sprintf('ClamAV scanner client failed with error \"%s\"', $exception->getMessage()),\n            0,\n            $exception\n        );\n    }\n}\n"
  },
  {
    "path": "src/ClamavValidator/ClamavValidatorServiceProvider.php",
    "content": "<?php\n\nnamespace Sunspikes\\ClamavValidator;\n\nuse Illuminate\\Support\\ServiceProvider;\nuse Sunspikes\\ClamavValidator\\Rules\\ClamAv;\n\nclass ClamavValidatorServiceProvider extends ServiceProvider\n{\n    protected array $rules = [\n        'clamav' => ClamAv::class,\n    ];\n\n    public function boot(): void\n    {\n        $this->loadTranslationsFrom(__DIR__ . '/../lang', 'clamav-validator');\n\n        $this->publishes([\n            __DIR__ . '/../../config/clamav.php' => $this->app->configPath('clamav.php'),\n        ], 'config');\n\n        $this->publishes([\n            __DIR__ . '/../lang' => lang_path('vendor/clamav-validator'),\n        ], 'lang');\n\n        $this->addNewRules();\n    }\n\n    public function getRules(): array\n    {\n        return $this->rules;\n    }\n\n    protected function addNewRules(): void\n    {\n        foreach ($this->getRules() as $token => $rule) {\n            $this->extendValidator($token, $rule);\n        }\n    }\n\n    protected function extendValidator(string $token, string $rule): void\n    {\n        $translation = $this->app['translator']->get('clamav-validator::validation');\n\n        $this->app['validator']->extend(\n            $token,\n            $rule . '@validate',\n            $translation[$token] ?? []\n        );\n    }\n\n    public function register(): void\n    {\n        $this->mergeConfigFrom(__DIR__ . '/../../config/clamav.php', 'clamav');\n    }\n}\n"
  },
  {
    "path": "src/ClamavValidator/Rules/ClamAv.php",
    "content": "<?php\n\nnamespace Sunspikes\\ClamavValidator\\Rules;\n\nuse Exception;\nuse Illuminate\\Support\\Arr;\nuse Illuminate\\Support\\Facades\\Config;\nuse Socket\\Raw\\Factory as SocketFactory;\nuse Sunspikes\\ClamavValidator\\ClamavValidatorException;\nuse Symfony\\Component\\HttpFoundation\\File\\UploadedFile;\nuse Xenolope\\Quahog\\Client as QuahogClient;\n\nclass ClamAv\n{\n    public function validate(string $attribute, mixed $value, array $parameters): bool\n    {\n        if (filter_var(Config::get('clamav.skip_validation'), FILTER_VALIDATE_BOOLEAN)) {\n            return true;\n        }\n\n        if (is_array($value)) {\n            $result = true;\n            foreach ($value as $file) {\n                $result &= $this->validateFileWithClamAv($file);\n            }\n\n            return (bool) $result;\n        }\n\n        return $this->validateFileWithClamAv($value);\n    }\n\n    protected function validateFileWithClamAv(mixed $value): bool\n    {\n        $file = $this->getFilePath($value);\n        if (!is_readable($file)) {\n            throw ClamavValidatorException::forNonReadableFile($file);\n        }\n\n        try {\n            $socket = $this->getClamavSocket();\n            $scanner = $this->createQuahogScannerClient($socket);\n            $result = $scanner->scanResourceStream(fopen($file, 'rb'));\n        } catch (Exception $exception) {\n            if (Config::get('clamav.client_exceptions')) {\n                throw ClamavValidatorException::forClientException($exception);\n            }\n            return false;\n        }\n\n        if ($result->isError()) {\n            if (Config::get('clamav.client_exceptions')) {\n                throw ClamavValidatorException::forScanResult($result);\n            }\n            return false;\n        }\n\n        return $result->isOk();\n    }\n\n    protected function getClamavSocket(): string\n    {\n        $preferredSocket = Config::get('clamav.preferred_socket');\n\n        if ($preferredSocket === 'unix_socket') {\n            $unixSocket = Config::get('clamav.unix_socket');\n            if (file_exists($unixSocket)) {\n                return 'unix://' . $unixSocket;\n            }\n        }\n\n        return Config::get('clamav.tcp_socket');\n    }\n\n    protected function getFilePath(UploadedFile|array|string $file): string\n    {\n        if ($file instanceof UploadedFile) {\n            return $file->getRealPath();\n        }\n\n        if (is_array($file) && Arr::get($file, 'tmp_name') !== null) {\n            return $file['tmp_name'];\n        }\n\n        return $file;\n    }\n\n    protected function createQuahogScannerClient(string $socket): QuahogClient\n    {\n        $client = (new SocketFactory())->createClient($socket, Config::get('clamav.socket_connect_timeout'));\n\n        return new QuahogClient($client, Config::get('clamav.socket_read_timeout'), PHP_NORMAL_READ);\n    }\n}\n"
  },
  {
    "path": "src/lang/en/validation.php",
    "content": "<?php\n\nreturn [\n    'clamav' => ':attribute contains virus.',\n];\n"
  },
  {
    "path": "tests/.gitkeep",
    "content": ""
  },
  {
    "path": "tests/ClamavValidatorServiceProviderTest.php",
    "content": "<?php\n\nnamespace Sunspikes\\Tests\\ClamavValidator;\n\nuse Illuminate\\Container\\Container;\nuse Illuminate\\Contracts\\Foundation\\Application;\nuse Illuminate\\Contracts\\Translation\\Translator;\nuse Illuminate\\Support\\Facades\\Facade;\nuse Illuminate\\Validation\\PresenceVerifierInterface;\nuse Mockery;\nuse Illuminate\\Validation\\Factory;\nuse Illuminate\\Support\\Str;\nuse PHPUnit\\Framework\\TestCase;\nuse Sunspikes\\ClamavValidator\\ClamavValidatorServiceProvider;\n\nclass ClamavValidatorServiceProviderTest extends TestCase\n{\n    public function testBoot(): void\n    {\n        $translator = Mockery::mock(Translator::class);\n        $translator->shouldReceive('get')->with('clamav-validator::validation')->andReturn('error');\n        $translator->shouldReceive('addNamespace');\n\n        $presence = Mockery::mock(PresenceVerifierInterface::class);\n\n        $factory = new Factory($translator);\n        $factory->setPresenceVerifier($presence);\n\n        /** @var Mockery\\Mock|Application $container */\n        $container = Mockery::mock(Container::class)->makePartial();\n        $container->shouldReceive('offsetGet')->with('translator')->andReturn($translator);\n        $container->shouldReceive('offsetGet')->with('validator')->andReturn($factory);\n        $container->shouldReceive('configPath');\n\n        Facade::setFacadeApplication($container);\n\n        $serviceProvider = new ClamavValidatorServiceProvider($container);\n        $serviceProvider->boot();\n\n        $validator = $factory->make([], []);\n\n        foreach ($validator->extensions as $rule => $class_and_method) {\n            $this->assertArrayHasKey($rule, $serviceProvider->getRules());\n\n            [$class, $method] = Str::parseCallback($class_and_method);\n            $this->assertTrue(method_exists($class, $method));\n        }\n    }\n\n    protected function tearDown(): void\n    {\n        Mockery::close();\n    }\n}\n"
  },
  {
    "path": "tests/ClamavValidatorTest.php",
    "content": "<?php\n\nnamespace Sunspikes\\Tests\\ClamavValidator;\n\nuse Illuminate\\Container\\Container;\nuse Illuminate\\Support\\Facades\\Config;\nuse Mockery;\nuse Sunspikes\\ClamavValidator\\ClamavValidatorException;\nuse PHPUnit\\Framework\\TestCase;\nuse Sunspikes\\Tests\\ClamavValidator\\Helpers\\ValidatorHelper;\n\nclass ClamavValidatorTest extends TestCase\n{\n    use ValidatorHelper;\n\n    protected string $cleanFile;\n    protected string $virusFile;\n    protected string $errorFile;\n    protected array $clean_data;\n    protected array $virus_data;\n    protected array $error_data;\n    protected array $multiple_files_all_clean;\n    protected array $multiple_files_some_with_virus;\n\n    protected function setUp(): void\n    {\n        $this->cleanFile = $this->getTempPath(__DIR__ . '/files/test1.txt');\n        $this->virusFile = $this->getTempPath(__DIR__ . '/files/test2.txt');\n        $this->errorFile = $this->getTempPath(__DIR__ . '/files/test3.txt');\n\n        $this->clean_data = ['file' => $this->cleanFile];\n        $this->virus_data = ['file' => $this->virusFile];\n        $this->error_data = ['file' => $this->errorFile];\n        $this->multiple_files_all_clean = [\n            'files' => [\n                $this->cleanFile,\n                $this->getTempPath(__DIR__ . '/files/test4.txt'),\n            ]\n        ];\n        $this->multiple_files_some_with_virus = [\n            'files' => [\n                $this->cleanFile,\n                $this->virusFile,\n                $this->getTempPath(__DIR__ . '/files/test4.txt'),\n            ]\n        ];\n    }\n\n    private function setConfig(array $opts = []): void\n    {\n        $opts = array_merge(['error' => false, 'skip' => false, 'exception' => false], $opts);\n\n        $config = Mockery::mock();\n        $config->shouldReceive('get')->with('clamav.preferred_socket')->andReturn('unix_socket');\n        $config->shouldReceive('get')->with('clamav.client_exceptions')->andReturn($opts['exception']);\n        $config->shouldReceive('get')->with('clamav.unix_socket')->andReturn(!$opts['error'] ? '/var/run/clamav/clamd.ctl' : '/dev/null');\n        $config->shouldReceive('get')->with('clamav.tcp_socket')->andReturn(!$opts['error'] ? 'tcp://127.0.0.1:3310' : 'tcp://127.0.0.1:0');\n        $config->shouldReceive('get')->with('clamav.socket_read_timeout')->andReturn(30);\n        $config->shouldReceive('get')->with('clamav.socket_connect_timeout')->andReturn(5);\n        $config->shouldReceive('get')->with('clamav.skip_validation')->andReturn($opts['skip']);\n\n        Config::swap($config);\n    }\n\n    protected function tearDown(): void\n    {\n        chmod($this->errorFile, 0644);\n\n        Container::getInstance()->flush();\n\n        Mockery::close();\n    }\n\n    public function testValidatesSkipped(): void\n    {\n        $this->setConfig(['skip' => true]);\n\n        $validator = $this->makeValidator(\n            $this->clean_data,\n            ['file' => 'clamav'],\n        );\n\n        $this->assertTrue($validator->passes());\n    }\n\n    public function testValidatesSkippedForBoolValidatedConfigValues(): void\n    {\n        $this->setConfig(['skip' => '1']);\n\n        $validator = $this->makeValidator(\n            $this->clean_data,\n            ['file' => 'clamav'],\n        );\n\n        $this->assertTrue($validator->passes());\n    }\n\n    public function testValidatesClean(): void\n    {\n        $this->setConfig();\n\n        $validator = $this->makeValidator(\n            $this->clean_data,\n            ['file' => 'clamav'],\n        );\n\n        $this->assertTrue($validator->passes());\n    }\n\n    public function testValidatesCleanMultiFile(): void\n    {\n        $this->setConfig();\n\n        $validator = $this->makeValidator(\n            $this->multiple_files_all_clean,\n            ['files' => 'clamav'],\n        );\n\n        $this->assertTrue($validator->passes());\n    }\n\n    public function testValidatesVirus(): void\n    {\n        $this->setConfig();\n\n        $validator = $this->makeValidator(\n            $this->virus_data,\n            ['file' => 'clamav'],\n        );\n\n        $this->assertTrue($validator->fails());\n    }\n\n    public function testValidatesVirusMultiFile(): void\n    {\n        $this->setConfig();\n\n        $validator = $this->makeValidator(\n            $this->multiple_files_some_with_virus,\n            ['files' => 'clamav'],\n        );\n\n        $this->assertTrue($validator->fails());\n    }\n\n    public function testCannotValidateNonReadable(): void\n    {\n        $this->setConfig();\n\n        $this->expectException(ClamavValidatorException::class);\n\n        $validator = $this->makeValidator(\n            $this->error_data,\n            ['file' => 'clamav'],\n        );\n\n        chmod($this->errorFile, 0000);\n\n        $validator->passes();\n    }\n\n    public function testFailsValidationOnError(): void\n    {\n        $this->setConfig(['error' => true]);\n\n        $validator = $this->makeValidator(\n            $this->clean_data,\n            ['file' => 'clamav'],\n        );\n\n        $this->assertTrue($validator->fails());\n    }\n\n    public function testThrowsExceptionOnValidationError(): void\n    {\n        $this->setConfig(['error' => true, 'exception' => true]);\n\n        $this->expectException(ClamavValidatorException::class);\n\n        $validator = $this->makeValidator(\n            $this->clean_data,\n            ['file' => 'clamav'],\n        );\n\n        $this->assertTrue($validator->fails());\n    }\n}\n"
  },
  {
    "path": "tests/Helpers/ValidatorHelper.php",
    "content": "<?php\n\nnamespace Sunspikes\\Tests\\ClamavValidator\\Helpers;\n\nuse Illuminate\\Container\\Container;\nuse Illuminate\\Contracts\\Translation\\Translator;\nuse Illuminate\\Validation\\Factory;\nuse Illuminate\\Validation\\Validator;\nuse Mockery;\nuse Sunspikes\\ClamavValidator\\Rules\\ClamAv;\n\ntrait ValidatorHelper\n{\n    public function makeValidator(array $data, array $rules, ?Translator $translator = null, array $messages = []): Validator\n    {\n        $translator = $translator ?? $this->makeMockedTranslator();\n        $messages = !empty($messages) ? $messages : $this->defaultErrorMessages();\n\n        $factory = new Factory($translator, Container::getInstance());\n\n        foreach ($this->rules() as $token => $rule) {\n            $factory->extend(\n                $token,\n                $rule . '@validate',\n                $messages\n            );\n        }\n\n        return $factory->make($data, $rules);\n    }\n\n    protected function rules(): array\n    {\n        return [\n            'clamav' => ClamAv::class,\n        ];\n    }\n\n    protected function makeMockedTranslator(): Translator\n    {\n        $translator = Mockery::mock(Translator::class);\n\n        $translator\n            ->shouldReceive('get')\n            ->with('validation.custom.file.clamav')\n            ->andReturn('error');\n\n        $translator\n            ->shouldReceive('get')\n            ->withAnyArgs()\n            ->andReturn(null);\n\n        $translator\n            ->shouldReceive('get')\n            ->with('validation.attributes')\n            ->andReturn([]);\n\n        return $translator;\n    }\n\n    protected function defaultErrorMessages(): array\n    {\n        return [\n            'clamav' => ':attribute contains virus.'\n        ];\n    }\n\n    protected function getTempPath(string $file): string\n    {\n        $tempPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . basename($file);\n        copy($file, $tempPath);\n        chmod($tempPath, 0644);\n\n        return $tempPath;\n    }\n}\n"
  },
  {
    "path": "tests/files/test1.txt",
    "content": "dfdsfdsfdsf\nds\nfds\nfdsfds\nfdsfdsfds"
  },
  {
    "path": "tests/files/test2.txt",
    "content": "X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*   \n"
  },
  {
    "path": "tests/files/test3.txt",
    "content": "dfdsfdsfdsf\nds\nfds\nfdsfds\nfdsfdsfds"
  },
  {
    "path": "tests/files/test4.txt",
    "content": "dfdsfdsfdsf\nds\nfds\nfdsfds\nfdsfdsfds"
  }
]