From bd05256996d578d1161be6247735de369bec7f3e Mon Sep 17 00:00:00 2001 From: brahimRizq Date: Tue, 23 Jul 2024 10:52:41 +0100 Subject: [PATCH 1/2] add github action integration --- .github/workflows/ci.yml | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100755 index 0000000..dd79d29 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,55 @@ +name: CI + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + services: + mongodb: + image: mongo:7.0 + options: >- + --health-cmd "echo 'db.runCommand({ ping: 1 })' | mongosh --quiet" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 27017:27017 + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + extensions: mongodb, mbstring + ini-values: | + memory_limit = 2G + coverage: xdebug + tools: composer + + - name: debug + run: docker ps + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Run PHPCS + run: composer check-style + + - name: Run tests + env: + DB_CONNECTION: mongodb + DB_HOST: mongodb + DB_PORT: 27017 + DB_DATABASE: laravel_permission_mongodb_test + run: | + composer test From 4c5b6335f4eabd9da4451318705352713b12ecac Mon Sep 17 00:00:00 2001 From: brahimRizq Date: Tue, 23 Jul 2024 18:43:41 +0100 Subject: [PATCH 2/2] fix ci --- .env.example | 7 +++++++ .github/workflows/ci.yml | 21 ++++++++++++--------- .scrutinizer.yml | 39 --------------------------------------- src/Models/Role.php | 6 +++++- tests/TestCase.php | 9 +++++++-- 5 files changed, 31 insertions(+), 51 deletions(-) create mode 100644 .env.example delete mode 100644 .scrutinizer.yml diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..54d722e --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +DB_CONNECTION=mongodb +DB_HOST=localhost +DB_PORT=27017 +DB_DATABASE=laravel_permission_mongodb_test +DB_USERNAME=db_user +DB_PASSWORD=db_password +DB_AUTHENTICATION_DATABASE=admin diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dd79d29..1a7c8c3 100755 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,10 +3,10 @@ name: CI on: push: branches: - - main + - master pull_request: branches: - - main + - master jobs: test: @@ -46,10 +46,13 @@ jobs: run: composer check-style - name: Run tests - env: - DB_CONNECTION: mongodb - DB_HOST: mongodb - DB_PORT: 27017 - DB_DATABASE: laravel_permission_mongodb_test - run: | - composer test + run: composer test + + - name: Run tests with coverage + run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml --coverage-html=coverage-html + +# - name: Upload coverage to GitHub Pages +# uses: peaceiris/actions-gh-pages@v3 +# with: +# github_token: ${{ secrets.GITHUB_TOKEN }} +# publish_dir: ./build/coverage/coverage-html diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index c51da33..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,39 +0,0 @@ -build: - image: default-bionic - environment: - mongodb: true - selenium: true - php: - version: 8.1 - tests: - override: - - - command: 'vendor/bin/phpunit --coverage-clover=coverage' - coverage: - file: 'coverage' - format: 'clover' - nodes: - analysis: - tests: - override: - - php-scrutinizer-run - -filter: - excluded_paths: - - "tests/" - -checks: - php: - remove_extra_empty_lines: true - remove_php_closing_tag: true - remove_trailing_whitespace: true - fix_use_statements: - remove_unused: true - preserve_multiple: false - preserve_blanklines: true - order_alphabetically: true - fix_php_opening_tag: true - fix_linefeed: true - fix_line_ending: true - fix_identation_4spaces: true - fix_doc_comments: true diff --git a/src/Models/Role.php b/src/Models/Role.php index b2a7964..43f42d0 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -59,8 +59,12 @@ public static function create(array $attributes = []): \Illuminate\Database\Eloq { $attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class); $helpers = new Helpers(); + $role = static::query() + ->where('name', $attributes['name']) + ->where('guard_name', $attributes['guard_name']) + ->first(); - if (static::query()->where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) { + if ($role) { $name = (string)$attributes['name']; $guardName = (string)$attributes['guard_name']; throw new RoleAlreadyExists($helpers->getRoleAlreadyExistsMessage($name, $guardName)); diff --git a/tests/TestCase.php b/tests/TestCase.php index 6acd7dd..98c6664 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -86,10 +86,15 @@ protected function getEnvironmentSetUp($app) { $app['config']->set('database.default', 'mongodb'); $app['config']->set('database.connections.mongodb', [ - 'host' => 'localhost', + 'host' => env('DB_HOST', 'localhost'), 'port' => '27017', 'driver' => 'mongodb', - 'database' => 'laravel_permission_mongodb_test', + 'database' => env('DB_DATABASE', 'laravel_permission_mongodb_test'), + 'username' => env('DB_USERNAME', ''), + 'password' => env('DB_PASSWORD', ''), + 'options' => [ + 'database' => env('DB_AUTHENTICATION_DATABASE', ''), // required with Mongo 3+ + ], 'prefix' => '', ]);