Skip to content

Commit

Permalink
Merge pull request Kyslik#208 from aglipanci/master
Browse files Browse the repository at this point in the history
Laravel 10 support, fixing tests, adding types
  • Loading branch information
aglipanci authored Feb 17, 2023
2 parents 91792a1 + d898138 commit 80a7f05
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 48 deletions.
30 changes: 27 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.2, 7.3, 7.4, 8.0]
laravel: [6.*, 7.*, 8.*, 9.*]
php: [7.2, 7.3, 7.4, 8.0, 8.1]
laravel: [6.*, 7.*, 8.*, 9.*, 10.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 9.*
testbench: 7.*
- laravel: 8.*
Expand All @@ -29,6 +31,28 @@ jobs:
exclude:
- php: 7.2
laravel: 8.*
- php: 7.2
laravel: 9.*
- php: 7.2
laravel: 10.*
- php: 7.3
laravel: 9.*
- php: 7.3
laravel: 10.*
- php: 7.4
laravel: 9.*
- php: 7.4
laravel: 10.*
- php: 8.0
laravel: 10.*
- php: 8.0
laravel: 8.*
- php: 8.1
laravel: 6.*
- php: 8.1
laravel: 7.*
- php: 8.1
laravel: 8.*
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
steps:
- name: Checkout code
Expand All @@ -39,7 +63,7 @@ jobs:
path: ~/.composer/cache/files
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
- name: Setup PHP
uses: shivammathur/setup-php@v1
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [Column sorting for Laravel 5.5-8](#column-sorting-for-laravel-55-8)
- [Column sorting for Laravel 5.5-10](#column-sorting-for-laravel-55-8)
- [Setup](#setup)
- [Composer](#composer)
- [Laravel's >=5.5 auto discovery](#laravels-55-auto-discovery)
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
],
"require": {
"php": ">=7.2",
"illuminate/support": "5.8.*|^6.0|^7.0|^8.0|^9.0",
"illuminate/database": "5.8.*|^6.0|^7.0|^8.0|^9.0"
"illuminate/support": "5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0",
"illuminate/database": "5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.5.10",
"orchestra/testbench": "^5.0|^7.0"
"orchestra/testbench": "^5.0|^7.0|^8.0"
},
"autoload": {
"psr-4": {
Expand Down
32 changes: 12 additions & 20 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/ColumnSortable/</directory>
</whitelist>
</filter>
</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src/ColumnSortable/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
33 changes: 17 additions & 16 deletions src/ColumnSortable/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BadMethodCallException;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Schema;
Expand All @@ -17,13 +18,13 @@ trait Sortable
{

/**
* @param \Illuminate\Database\Query\Builder $query
* @param array|null $defaultParameters
* @param Builder $query
* @param array|string|null $defaultParameters
*
* @return \Illuminate\Database\Query\Builder
* @throws \Kyslik\ColumnSortable\Exceptions\ColumnSortableException
* @return Builder
* @throws ColumnSortableException
*/
public function scopeSortable($query, $defaultParameters = null)
public function scopeSortable(Builder $query, $defaultParameters = null): Builder
{
if (request()->allFilled(['sort', 'direction'])) { // allFilled() is macro
return $this->queryOrderBuilder($query, request()->only(['sort', 'direction']));
Expand Down Expand Up @@ -51,7 +52,7 @@ public function scopeSortable($query, $defaultParameters = null)
*
* @return array|null
*/
private function getDefaultSortable()
private function getDefaultSortable(): ?array
{
if (config('columnsortable.default_first_column', false)) {
$sortBy = Arr::first($this->sortable);
Expand All @@ -65,14 +66,14 @@ private function getDefaultSortable()


/**
* @param \Illuminate\Database\Query\Builder $query
* @param Builder $query
* @param array $sortParameters
*
* @return \Illuminate\Database\Query\Builder
* @return Builder
*
* @throws ColumnSortableException
*/
private function queryOrderBuilder($query, array $sortParameters)
private function queryOrderBuilder(Builder $query, array $sortParameters): Builder
{
$model = $this;

Expand Down Expand Up @@ -119,7 +120,7 @@ private function queryOrderBuilder($query, array $sortParameters)
*
* @return array
*/
private function parseParameters(array $parameters)
private function parseParameters(array $parameters): array
{
$column = Arr::get($parameters, 'sort');
if (empty($column)) {
Expand All @@ -136,14 +137,14 @@ private function parseParameters(array $parameters)


/**
* @param \Illuminate\Database\Query\Builder $query
* @param \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\HasOne $relation
* @param Builder $query
* @param BelongsTo|\Illuminate\Database\Eloquent\Relations\HasOne $relation
*
* @return \Illuminate\Database\Query\Builder
* @return Builder
*
* @throws \Exception
*/
private function queryJoinBuilder($query, $relation)
private function queryJoinBuilder(Builder $query, $relation): Builder
{
$relatedTable = $relation->getRelated()->getTable();
$parentTable = $relation->getParent()->getTable();
Expand Down Expand Up @@ -174,7 +175,7 @@ private function queryJoinBuilder($query, $relation)
*
* @return bool
*/
private function columnExists($model, $column)
private function columnExists($model, $column): bool
{
return (isset($model->sortable)) ? in_array($column, $model->sortable) :
Schema::connection($model->getConnectionName())->hasColumn($model->getTable(), $column);
Expand All @@ -186,7 +187,7 @@ private function columnExists($model, $column)
*
* @return array
*/
private function formatToParameters($array)
private function formatToParameters($array): array
{
if (empty($array)) {
return [];
Expand Down
12 changes: 7 additions & 5 deletions tests/SortableLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,17 @@ public function testCustomTitleWithHTML()
$this->assertSame('<a href="http://localhost?sort=column&direction=asc"><em>columnTitle</em></a><i class=""></i>', $link);
}


public function testCustomHrefAttribute()
{
Config::set('columnsortable.sortable_icon', 'fa fa-sort');

$link = SortableLink::render(['column', 'ColumnTitle', ['a' => 'b'], ['c' => 'd', 'href' => 'http://localhost/custom-path']]);
$this->assertSame('<a href="http://localhost/custom-path?a=b&sort=column&direction=asc" c="d">ColumnTitle</a> <i class="fa fa-sort"></i>', $link);

$this->assertSame('<a href="http://localhost/custom-path?a=b&sort=column&direction=asc" c="d">ColumnTitle</a><i class="fa fa-sort"></i>', $link);
}


public function testParseParameters()
{
$parameters = ['column'];
Expand Down

0 comments on commit 80a7f05

Please sign in to comment.