From 18e2390c025e98d17e3054eb167c71ed1c050a56 Mon Sep 17 00:00:00 2001 From: Rutam21 Date: Mon, 17 Oct 2022 14:04:18 +0530 Subject: [PATCH 1/2] APPW-4366: Add Code Linter To utopia-php/audit --- .github/workflows/linter.yml | 20 +++++++ composer.json | 7 ++- composer.lock | 70 ++++++++++++++++++++++- src/Audit/Audit.php | 108 +++++++++++++++++------------------ tests/Audit/AuditTest.php | 20 ++++--- 5 files changed, 159 insertions(+), 66 deletions(-) create mode 100644 .github/workflows/linter.yml diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml new file mode 100644 index 0000000..ffb8a09 --- /dev/null +++ b/.github/workflows/linter.yml @@ -0,0 +1,20 @@ +name: "Linter" + +on: [pull_request] +jobs: + lint: + name: Linter + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 2 + + - run: git checkout HEAD^2 + + - name: Run Linter + run: | + docker run --rm -v $PWD:/app composer sh -c \ + "composer install --profile --ignore-platform-reqs && composer lint" diff --git a/composer.json b/composer.json index 1b88729..72e0bcc 100755 --- a/composer.json +++ b/composer.json @@ -8,6 +8,10 @@ "autoload": { "psr-4": {"Utopia\\Audit\\": "src/Audit"} }, + "scripts": { + "lint": "./vendor/bin/pint --test", + "format": "./vendor/bin/pint" + }, "require": { "php": ">=8.0", "ext-pdo": "*", @@ -15,6 +19,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.3", - "vimeo/psalm": "4.0.1" + "vimeo/psalm": "4.0.1", + "laravel/pint": "^1.2" } } diff --git a/composer.lock b/composer.lock index 6b78952..7dc2d9e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3d4afa5d69674eef8b0b8a71961114dc", + "content-hash": "1750d109137d8af7786251d7004a3c97", "packages": [ { "name": "composer/package-versions-deprecated", @@ -972,6 +972,72 @@ }, "time": "2022-03-02T22:36:06+00:00" }, + { + "name": "laravel/pint", + "version": "v1.2.0", + "source": { + "type": "git", + "url": "https://github.com/laravel/pint.git", + "reference": "1d276e4c803397a26cc337df908f55c2a4e90d86" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/pint/zipball/1d276e4c803397a26cc337df908f55c2a4e90d86", + "reference": "1d276e4c803397a26cc337df908f55c2a4e90d86", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-mbstring": "*", + "ext-tokenizer": "*", + "ext-xml": "*", + "php": "^8.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.11.0", + "illuminate/view": "^9.27", + "laravel-zero/framework": "^9.1.3", + "mockery/mockery": "^1.5.0", + "nunomaduro/larastan": "^2.2", + "nunomaduro/termwind": "^1.14.0", + "pestphp/pest": "^1.22.1" + }, + "bin": [ + "builds/pint" + ], + "type": "project", + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Seeders\\": "database/seeders/", + "Database\\Factories\\": "database/factories/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nuno Maduro", + "email": "enunomaduro@gmail.com" + } + ], + "description": "An opinionated code formatter for PHP.", + "homepage": "https://laravel.com", + "keywords": [ + "format", + "formatter", + "lint", + "linter", + "php" + ], + "support": { + "issues": "https://github.com/laravel/pint/issues", + "source": "https://github.com/laravel/pint" + }, + "time": "2022-09-13T15:07:15+00:00" + }, { "name": "myclabs/deep-copy", "version": "1.11.0", @@ -4013,5 +4079,5 @@ "ext-pdo": "*" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.3.0" } diff --git a/src/Audit/Audit.php b/src/Audit/Audit.php index 81f3d4a..31551e1 100644 --- a/src/Audit/Audit.php +++ b/src/Audit/Audit.php @@ -11,7 +11,7 @@ class Audit { - const COLLECTION = "audit"; + const COLLECTION = 'audit'; /** * @var Database @@ -19,7 +19,7 @@ class Audit private Database $db; /** - * @param Database $db + * @param Database $db */ public function __construct(Database $db) { @@ -28,8 +28,8 @@ public function __construct(Database $db) public function setup(): void { - if (!$this->db->exists($this->db->getDefaultDatabase())) { - throw new Exception("You need to create the database before running Audit setup"); + if (! $this->db->exists($this->db->getDefaultDatabase())) { + throw new Exception('You need to create the database before running Audit setup'); } $attributes = [ @@ -147,14 +147,13 @@ public function setup(): void * * Add specific event log * - * @param string $userId - * @param string $event - * @param string $resource - * @param string $userAgent - * @param string $ip - * @param string $location - * @param array $data - * + * @param string $userId + * @param string $event + * @param string $resource + * @param string $userAgent + * @param string $ip + * @param string $location + * @param array $data * @return bool */ public function log(string $userId, string $event, string $resource, string $userAgent, string $ip, string $location, array $data = []): bool @@ -172,17 +171,17 @@ public function log(string $userId, string $event, string $resource, string $use 'time' => DateTime::now(), ])); }); + return true; } /** * Get All Logs By User ID. * - * @param string $userId - * @param int $limit - * @param int $offset - * @param Document|null $orderAfter - * + * @param string $userId + * @param int $limit + * @param int $offset + * @param Document|null $orderAfter * @return array */ public function getLogsByUser(string $userId, int $limit = 25, int $offset = 0, Document $orderAfter = null): array @@ -202,14 +201,14 @@ public function getLogsByUser(string $userId, int $limit = 25, int $offset = 0, queries: $queries, ); }); + return $result; } /** * Get Logs Count By User ID. * - * @param string $userId - * + * @param string $userId * @return int */ public function countLogsByUser(string $userId): int @@ -220,18 +219,17 @@ public function countLogsByUser(string $userId): int queries: $this->buildQuery(['userId' => $userId], Query::TYPE_EQUAL) ); }); + return $result; } - /** * Get All Logs By Resource. * - * @param string $resource - * @param int $limit - * @param int $offset - * @param Document|null $orderAfter - * + * @param string $resource + * @param int $limit + * @param int $offset + * @param Document|null $orderAfter * @return array */ public function getLogsByResource(string $resource, int $limit = 25, int $offset = 0, Document $orderAfter = null): array @@ -251,14 +249,14 @@ public function getLogsByResource(string $resource, int $limit = 25, int $offset queries: $queries, ); }); + return $results; } /** * Get Logs Count By Resource. * - * @param string $resource - * + * @param string $resource * @return int */ public function countLogsByResource(string $resource): int @@ -269,18 +267,18 @@ public function countLogsByResource(string $resource): int queries: $this->buildQuery(['resource' => $resource], Query::TYPE_EQUAL) ); }); + return $results; } /** * Get All Logs By User and Events. * - * @param string $userId - * @param array $events - * @param int $limit - * @param int $offset - * @param Document|null $orderAfter - * + * @param string $userId + * @param array $events + * @param int $limit + * @param int $offset + * @param Document|null $orderAfter * @return array */ public function getLogsByUserAndEvents(string $userId, array $events, int $limit = 25, int $offset = 0, Document $orderAfter = null): array @@ -303,15 +301,15 @@ public function getLogsByUserAndEvents(string $userId, array $events, int $limit queries: $queries, ); }); + return $results; } /** * Get Logs Count By User and Events. * - * @param string $userId - * @param array $events - * + * @param string $userId + * @param array $events * @return int */ public function countLogsByUserAndEvents(string $userId, array $events): int @@ -325,18 +323,18 @@ public function countLogsByUserAndEvents(string $userId, array $events): int ], Query::TYPE_EQUAL) ); }); + return $results; } /** * Get All Logs By Resource and Events. * - * @param string $resource - * @param array $events - * @param int $limit - * @param int $offset - * @param Document|null $orderAfter - * + * @param string $resource + * @param array $events + * @param int $limit + * @param int $offset + * @param Document|null $orderAfter * @return array */ public function getLogsByResourceAndEvents(string $resource, array $events, int $limit = 25, int $offset = 0, Document $orderAfter = null): array @@ -359,15 +357,15 @@ public function getLogsByResourceAndEvents(string $resource, array $events, int queries: $queries, ); }); + return $results; } /** * Get All Logs By Resource and Events. * - * @param string $resource - * @param array $events - * + * @param string $resource + * @param array $events * @return int */ public function countLogsByResourceAndEvents(string $resource, array $events): int @@ -381,14 +379,14 @@ public function countLogsByResourceAndEvents(string $resource, array $events): i ], Query::TYPE_EQUAL) ); }); + return $results; } /** * Delete all logs older than $timestamp seconds * - * @param string $datetime - * + * @param string $datetime * @return bool */ public function cleanup(string $datetime): bool @@ -398,15 +396,16 @@ public function cleanup(string $datetime): bool $documents = $this->db->find( collection: Audit::COLLECTION, queries: $this->buildQuery([ - 'time' => $datetime + 'time' => $datetime, ], Query::TYPE_LESSER) ); foreach ($documents as $document) { $this->db->deleteDocument(Audit::COLLECTION, $document['$id']); } - } while (!empty($documents)); + } while (! empty($documents)); }); + return true; } @@ -416,25 +415,26 @@ public function cleanup(string $datetime): bool * * The $method is applied to each k/v pair * - * @param array $values - * @param string $method - * + * @param array $values + * @param string $method * @return Query[] + * * @throws Exception */ private function buildQuery(array $values, string $method): array { - if (!Query::isMethod($method)) { + if (! Query::isMethod($method)) { throw new Exception('Method not supported'); } $query = []; foreach ($values as $key => $value) { - if (!\is_array($value)) { + if (! \is_array($value)) { $value = [$value]; } $query[] = new Query($method, $key, $value); } + return $query; } } diff --git a/tests/Audit/AuditTest.php b/tests/Audit/AuditTest.php index 7d011bc..a57201d 100755 --- a/tests/Audit/AuditTest.php +++ b/tests/Audit/AuditTest.php @@ -3,10 +3,10 @@ namespace Utopia\Tests; use PDO; -use Utopia\Audit\Audit; use PHPUnit\Framework\TestCase; -use Utopia\Cache\Cache; +use Utopia\Audit\Audit; use Utopia\Cache\Adapter\None as NoCache; +use Utopia\Cache\Cache; use Utopia\Database\Adapter\MariaDB; use Utopia\Database\Database; use Utopia\Database\DateTime; @@ -17,6 +17,7 @@ class AuditTest extends TestCase * @var Audit */ protected $audit = null; + protected $initialized = false; public function setUp(): void @@ -28,12 +29,12 @@ public function setUp(): void $pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, MariaDB::getPdoAttributes()); $cache = new Cache(new NoCache()); - $database = new Database(new MariaDB($pdo),$cache); + $database = new Database(new MariaDB($pdo), $cache); $database->setDefaultDatabase('utopiaTests'); $database->setNamespace('namespace'); $this->audit = new Audit($database); - if(!$database->exists('utopiaTests')) { + if (! $database->exists('utopiaTests')) { $database->create('utopiaTests'); $this->audit->setup(); } @@ -52,7 +53,7 @@ public function createLogs() $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'; $ip = '127.0.0.1'; $location = 'US'; - $data = ['key1' => 'value1','key2' => 'value2']; + $data = ['key1' => 'value1', 'key2' => 'value2']; $this->assertTrue($this->audit->log($userId, 'update', 'database/document/1', $userAgent, $ip, $location, $data)); $this->assertTrue($this->audit->log($userId, 'update', 'database/document/2', $userAgent, $ip, $location, $data)); $this->assertTrue($this->audit->log($userId, 'delete', 'database/document/2', $userAgent, $ip, $location, $data)); @@ -148,22 +149,23 @@ public function testGetLogsByResource() $this->assertEquals($logs4[0]->getId(), $logs2[1]->getId()); } - public function testCleanup() { + public function testCleanup() + { sleep(3); // First delete all the logs $status = $this->audit->cleanup(DateTime::now()); $this->assertEquals($status, true); - // Check that all logs have been deleted + // Check that all logs have been deleted $logs = $this->audit->getLogsByUser('userId'); $this->assertEquals(0, \count($logs)); - // Add three sample logs + // Add three sample logs $userId = 'userId'; $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'; $ip = '127.0.0.1'; $location = 'US'; - $data = ['key1' => 'value1','key2' => 'value2']; + $data = ['key1' => 'value1', 'key2' => 'value2']; $this->assertEquals($this->audit->log($userId, 'update', 'database/document/1', $userAgent, $ip, $location, $data), true); sleep(5); From e3b582c651731e339285a83caed393e64b813b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Oct 2022 07:33:20 +0000 Subject: [PATCH 2/2] Update Pint version --- composer.json | 2 +- composer.lock | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 72e0bcc..8c131e9 100755 --- a/composer.json +++ b/composer.json @@ -20,6 +20,6 @@ "require-dev": { "phpunit/phpunit": "^9.3", "vimeo/psalm": "4.0.1", - "laravel/pint": "^1.2" + "laravel/pint": "1.2.*" } } diff --git a/composer.lock b/composer.lock index 7dc2d9e..8ba66b8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1750d109137d8af7786251d7004a3c97", + "content-hash": "f5007a703bcd4ed2cdce3e624031a4fa", "packages": [ { "name": "composer/package-versions-deprecated", @@ -1480,25 +1480,30 @@ }, { "name": "phpdocumentor/type-resolver", - "version": "1.6.1", + "version": "1.6.2", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "77a32518733312af16a44300404e945338981de3" + "reference": "48f445a408c131e38cab1c235aa6d2bb7a0bb20d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/77a32518733312af16a44300404e945338981de3", - "reference": "77a32518733312af16a44300404e945338981de3", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/48f445a408c131e38cab1c235aa6d2bb7a0bb20d", + "reference": "48f445a408c131e38cab1c235aa6d2bb7a0bb20d", "shasum": "" }, "require": { - "php": "^7.2 || ^8.0", + "php": "^7.4 || ^8.0", "phpdocumentor/reflection-common": "^2.0" }, "require-dev": { "ext-tokenizer": "*", - "psalm/phar": "^4.8" + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan": "^1.8", + "phpstan/phpstan-phpunit": "^1.1", + "phpunit/phpunit": "^9.5", + "rector/rector": "^0.13.9", + "vimeo/psalm": "^4.25" }, "type": "library", "extra": { @@ -1524,9 +1529,9 @@ "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", "support": { "issues": "https://github.com/phpDocumentor/TypeResolver/issues", - "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.6.1" + "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.6.2" }, - "time": "2022-03-15T21:29:03+00:00" + "time": "2022-10-14T12:47:21+00:00" }, { "name": "phpunit/php-code-coverage", @@ -4079,5 +4084,5 @@ "ext-pdo": "*" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" }