From 1f076e675d24e163734d5081e76a8bfd2ec61d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Popowicz?= Date: Wed, 22 Feb 2023 14:21:33 +0100 Subject: [PATCH] Initial commit Initial commit Initial commit Initial commit Initial commit Initial commit test: remove phpstan fix: fix test feat: initial commit --- .editorconfig | 15 ++++ .gitattributes | 19 ++++++ .github/ISSUE_TEMPLATE/bug.yml | 66 ++++++++++++++++++ .github/ISSUE_TEMPLATE/config.yml | 11 +++ .github/dependabot.yml | 12 ++++ .github/workflows/dependabot-auto-merge.yml | 32 +++++++++ .../workflows/fix-php-code-style-issues.yml | 24 +++++++ .github/workflows/run-tests.yml | 54 +++++++++++++++ .github/workflows/update-changelog.yml | 28 ++++++++ .gitignore | 11 +++ CHANGELOG.md | 3 + LICENSE.md | 21 ++++++ README.md | 53 +++++++++++++++ composer.json | 68 +++++++++++++++++++ phpstan-baseline.neon | 0 phpstan.neon.dist | 12 ++++ phpunit.xml.dist | 39 +++++++++++ src/LaravelOrderByFieldServiceProvider.php | 35 ++++++++++ tests/Pest.php | 44 ++++++++++++ tests/QueryBuilderTest.php | 8 +++ tests/TestCase.php | 22 ++++++ 21 files changed, 577 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .github/ISSUE_TEMPLATE/bug.yml create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/dependabot-auto-merge.yml create mode 100644 .github/workflows/fix-php-code-style-issues.yml create mode 100644 .github/workflows/run-tests.yml create mode 100644 .github/workflows/update-changelog.yml create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpstan-baseline.neon create mode 100644 phpstan.neon.dist create mode 100644 phpunit.xml.dist create mode 100644 src/LaravelOrderByFieldServiceProvider.php create mode 100644 tests/Pest.php create mode 100644 tests/QueryBuilderTest.php create mode 100644 tests/TestCase.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..dd9a2b5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9e9519b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,19 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/phpunit.xml.dist export-ignore +/art export-ignore +/docs export-ignore +/tests export-ignore +/.editorconfig export-ignore +/.php_cs.dist.php export-ignore +/psalm.xml export-ignore +/psalm.xml.dist export-ignore +/testbench.yaml export-ignore +/UPGRADING.md export-ignore +/phpstan.neon.dist export-ignore +/phpstan-baseline.neon export-ignore diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml new file mode 100644 index 0000000..fe4cfe6 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug.yml @@ -0,0 +1,66 @@ +name: Bug Report +description: Report an Issue or Bug with the Package +title: "[Bug]: " +labels: ["bug"] +body: + - type: markdown + attributes: + value: | + We're sorry to hear you have a problem. Can you help us solve it by providing the following details. + - type: textarea + id: what-happened + attributes: + label: What happened? + description: What did you expect to happen? + placeholder: I cannot currently do X thing because when I do, it breaks X thing. + validations: + required: true + - type: textarea + id: how-to-reproduce + attributes: + label: How to reproduce the bug + description: How did this occur, please add any config values used and provide a set of reliable steps if possible. + placeholder: When I do X I see Y. + validations: + required: true + - type: input + id: package-version + attributes: + label: Package Version + description: What version of our Package are you running? Please be as specific as possible + placeholder: 2.0.0 + validations: + required: true + - type: input + id: php-version + attributes: + label: PHP Version + description: What version of PHP are you running? Please be as specific as possible + placeholder: 8.2.0 + validations: + required: true + - type: input + id: laravel-version + attributes: + label: Laravel Version + description: What version of Laravel are you running? Please be as specific as possible + placeholder: 9.0.0 + validations: + required: true + - type: dropdown + id: operating-systems + attributes: + label: Which operating systems does with happen with? + description: You may select more than one. + multiple: true + options: + - macOS + - Windows + - Linux + - type: textarea + id: notes + attributes: + label: Notes + description: Use this field to provide any other notes that you feel might be relevant to the issue. + validations: + required: false diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..36f6968 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,11 @@ +blank_issues_enabled: false +contact_links: + - name: Ask a question + url: https://github.com/oneduo/laravel-order-by-field/discussions/new?category=q-a + about: Ask the community for help + - name: Request a feature + url: https://github.com/oneduo/laravel-order-by-field/discussions/new?category=ideas + about: Share ideas for new features + - name: Report a security issue + url: https://github.com/oneduo/laravel-order-by-field/security/policy + about: Learn how to notify us for sensitive bugs diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..30c8a49 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,12 @@ +# Please see the documentation for all configuration options: +# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" \ No newline at end of file diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml new file mode 100644 index 0000000..32f7754 --- /dev/null +++ b/.github/workflows/dependabot-auto-merge.yml @@ -0,0 +1,32 @@ +name: dependabot-auto-merge +on: pull_request_target + +permissions: + pull-requests: write + contents: write + +jobs: + dependabot: + runs-on: ubuntu-latest + if: ${{ github.actor == 'dependabot[bot]' }} + steps: + + - name: Dependabot metadata + id: metadata + uses: dependabot/fetch-metadata@v1.3.6 + with: + github-token: "${{ secrets.GITHUB_TOKEN }}" + + - name: Auto-merge Dependabot PRs for semver-minor updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + + - name: Auto-merge Dependabot PRs for semver-patch updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml new file mode 100644 index 0000000..39a77e7 --- /dev/null +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -0,0 +1,24 @@ +name: Fix PHP code style issues + +on: + push: + paths: + - '**.php' + +jobs: + php-code-styling: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} + + - name: Fix PHP code style issues + uses: aglipanci/laravel-pint-action@2.1.0 + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Fix styling diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..e7f4d11 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,54 @@ +name: run-tests + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ubuntu-latest, windows-latest] + php: [8.2, 8.1] + laravel: [10.*, 9.*] + stability: [prefer-lowest, prefer-stable] + include: + - laravel: 10.* + testbench: 8.* + carbon: ^2.63 + - laravel: 9.* + testbench: ^7.0 + carbon: ^2.63 + + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + coverage: none + + - name: Setup problem matchers + run: | + echo "::add-matcher::${{ runner.tool_cache }}/php.json" + echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Install dependencies + run: | + composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.carbon }}" --no-interaction --no-update + composer update --${{ matrix.stability }} --prefer-dist --no-interaction + + - name: List Installed Dependencies + run: composer show -D + + - name: Execute tests + run: vendor/bin/pest diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml new file mode 100644 index 0000000..b20f3b6 --- /dev/null +++ b/.github/workflows/update-changelog.yml @@ -0,0 +1,28 @@ +name: "Update Changelog" + +on: + release: + types: [released] + +jobs: + update: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + ref: main + + - name: Update Changelog + uses: stefanzweifel/changelog-updater-action@v1 + with: + latest-version: ${{ github.event.release.name }} + release-notes: ${{ github.event.release.body }} + + - name: Commit updated CHANGELOG + uses: stefanzweifel/git-auto-commit-action@v4 + with: + branch: main + commit_message: Update CHANGELOG + file_pattern: CHANGELOG.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..83c9b9f --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.idea +.phpunit.result.cache +build +composer.lock +coverage +docs +phpunit.xml +phpstan.neon +testbench.yaml +vendor +node_modules diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..091ffa2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# Changelog + +All notable changes to `laravel-order-by-field` will be documented in this file. diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..9a4ab6c --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) oneduo + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fdebb4f --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +# Laravel query builder - order by field + +[![Latest Version on Packagist](https://img.shields.io/packagist/v/oneduo/laravel-order-by-field.svg?style=flat-square)](https://packagist.org/packages/oneduo/laravel-order-by-field) +[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/oneduo/laravel-order-by-field/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/oneduo/laravel-order-by-field/actions?query=workflow%3Arun-tests+branch%3Amain) +[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/oneduo/laravel-order-by-field/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/oneduo/laravel-order-by-field/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) +[![Total Downloads](https://img.shields.io/packagist/dt/oneduo/laravel-order-by-field.svg?style=flat-square)](https://packagist.org/packages/oneduo/laravel-order-by-field) + +This package adds a `orderByField` macro on the query Builder. + +## Installation + +You can install the package via composer: + +```bash +composer require oneduo/laravel-order-by-field +``` + +## Usage + +```php +$keys = [42, 1, 100] +$users = User::query() + ->whereKey($keys) + ->orderByField('id', $keys) + ->get(); +``` + +## Testing + +```bash +composer test +``` + +## Changelog + +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. + +## Security Vulnerabilities + +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. + +## Credits + +- [Mikaël Popowicz](https://github.com/mikaelpopowicz) +- [All Contributors](../../contributors) + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6b839c8 --- /dev/null +++ b/composer.json @@ -0,0 +1,68 @@ +{ + "name": "oneduo/laravel-order-by-field", + "description": "Laravel query builder order by field method", + "keywords": [ + "oneduo", + "laravel", + "laravel-order-by-field" + ], + "homepage": "https://github.com/oneduo/laravel-order-by-field", + "license": "MIT", + "authors": [ + { + "name": "Mikaël Popowicz", + "email": "mikael@popowicz.fr" + } + ], + "require": { + "php": "^8.0", + "spatie/laravel-package-tools": "^1.14.0", + "illuminate/contracts": "^9.0 || ^10.0" + }, + "require-dev": { + "laravel/pint": "^1.0", + "nunomaduro/collision": "^6.1", + "nunomaduro/larastan": "^2.0.1", + "orchestra/testbench": "^8.0", + "pestphp/pest": "^1.21", + "pestphp/pest-plugin-laravel": "^1.1", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-phpunit": "^1.0", + "phpunit/phpunit": "^9.6", + "spatie/laravel-ray": "^1.26" + }, + "autoload": { + "psr-4": { + "Oneduo\\LaravelOrderByField\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Oneduo\\LaravelOrderByField\\Tests\\": "tests" + } + }, + "scripts": { + "post-autoload-dump": "@php ./vendor/bin/testbench package:discover --ansi", + "analyse": "vendor/bin/phpstan analyse", + "test": "vendor/bin/pest", + "test-coverage": "vendor/bin/pest --coverage", + "format": "vendor/bin/pint" + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "phpstan/extension-installer": true + } + }, + "extra": { + "laravel": { + "providers": [ + "Oneduo\\LaravelOrderByField\\LaravelOrderByFieldServiceProvider" + ] + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..e69de29 diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..9914a5e --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,12 @@ +includes: + - phpstan-baseline.neon + +parameters: + level: 4 + paths: + - src + tmpDir: build/phpstan + checkOctaneCompatibility: true + checkModelProperties: true + checkMissingIterableValueType: false + diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..dad3c77 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,39 @@ + + + + + tests + + + + + ./src + + + + + + + + + + + diff --git a/src/LaravelOrderByFieldServiceProvider.php b/src/LaravelOrderByFieldServiceProvider.php new file mode 100644 index 0000000..f28fea5 --- /dev/null +++ b/src/LaravelOrderByFieldServiceProvider.php @@ -0,0 +1,35 @@ +name('laravel-order-by-field'); + + $this->registerMacros(); + } + + protected function registerMacros(): void + { + Grammar::macro('compileOrderField', function (string $column, array $values): string { + throw new RuntimeException('This database engine does not support ordering by field.'); + }); + + MySqlGrammar::macro('compileOrderField', function (string $column, array $values): string { + return 'field('.$this->wrap($column).', '.implode(', ', $values).')'; + }); + + Builder::macro('orderByField', function (string $column, array $values): static { + return $this->orderByRaw($this->grammar->compileOrderField($column, $values)); + }); + } +} diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..7e3d919 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,44 @@ +in(__DIR__); + +function getConnection($connection = null, $table = null) +{ + $connection = m::mock(ConnectionInterface::class); + $connection->shouldReceive('getDatabaseName')->andReturn('database'); + + return $connection; +} + +function getPostgresBuilder(): Builder +{ + $grammar = new PostgresGrammar; + $processor = m::mock(Processor::class); + + return new Builder(getConnection(), $grammar, $processor); +} + +function getMySqlBuilder(): Builder +{ + $grammar = new MySqlGrammar; + $processor = m::mock(Processor::class); + + return new Builder(m::mock(ConnectionInterface::class), $grammar, $processor); +} + +function getSqlServerBuilder(): Builder +{ + $grammar = new SqlServerGrammar; + $processor = m::mock(Processor::class); + + return new Builder(getConnection(), $grammar, $processor); +} diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php new file mode 100644 index 0000000..9105f53 --- /dev/null +++ b/tests/QueryBuilderTest.php @@ -0,0 +1,8 @@ +select('*')->from('users')->orderByField('id', [42, 1, 1000]); + + expect($builder->toSql())->toBe('select * from `users` order by field(`id`, 42, 1, 1000)'); +}); diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..7ca7dfd --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,22 @@ +