Skip to content

Add support for Laravel 10 #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [8.0, 8.1]
php: [8.2, 8.1]

name: PHP ${{ matrix.php }}

steps:
- uses: actions/checkout@v2
- name: Checkout code
uses: actions/checkout@v3

- name: Cache dependencies
uses: actions/cache@v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
composer.lock
.phpunit.result.cache
phpunit.xml
.idea
22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
}
],
"require": {
"php": "^8.0",
"genealabs/laravel-pivot-events": "^9.0",
"illuminate/cache": "^9.0",
"illuminate/config": "^9.0",
"illuminate/console": "^9.0",
"illuminate/container": "^9.0",
"illuminate/database": "^9.0",
"illuminate/http": "^9.0",
"illuminate/support": "^9.0"
"php": "^8.1",
"genealabs/laravel-pivot-events": "^10.0",
"illuminate/cache": "^10.0",
"illuminate/config": "^10.0",
"illuminate/console": "^10.0",
"illuminate/container": "^10.0",
"illuminate/database": "^10.0",
"illuminate/http": "^10.0",
"illuminate/support": "^10.0"
},
"require-dev": {
"doctrine/dbal": "^3.3",
"fakerphp/faker": "^1.11",
"laravel/nova": "^3.9",
"orchestra/testbench-browser-kit": "^7.0",
"orchestra/testbench": "^7.0",
"orchestra/testbench-browser-kit": "^8.0",
"orchestra/testbench": "^8.0",
"php-coveralls/php-coveralls" : "^2.2",
"phpmd/phpmd": "^2.11",
"phpunit/phpunit": "^9.5",
Expand Down
29 changes: 25 additions & 4 deletions src/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Exception;
use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -170,14 +171,19 @@ protected function getOrderByClauses() : string
}

$orders = collect($this->query->orders);

return $orders
->reduce(function ($carry, $order) {
if (($order["type"] ?? "") === "Raw") {
return $carry . "_orderByRaw_" . (new Str)->slug($order["sql"]);
}

return $carry . "_orderBy_" . $order["column"] . "_" . $order["direction"];
return sprintf(
'%s_orderBy_%s_%s',
$carry,
$this->expressionToString($order["column"]),
$order["direction"]
);
})
?: "";
}
Expand Down Expand Up @@ -211,7 +217,11 @@ protected function getQueryColumns(array $columns) : string
if (property_exists($this->query, "columns")
&& $this->query->columns
) {
return "_" . implode("_", $this->query->columns);
$columns = array_map(function ($column) {
return $this->expressionToString($column);
}, $this->query->columns);

return "_" . implode("_", $columns);
}

return "_" . implode("_", $columns);
Expand Down Expand Up @@ -393,12 +403,14 @@ protected function recursiveImplode(array $items, string $glue = ",") : string
return $result;
}

private function processEnum(\BackedEnum|\UnitEnum|string $value): string
private function processEnum(\BackedEnum|\UnitEnum|Expression|string $value): string
{
if ($value instanceof \BackedEnum) {
return $value->value;
} elseif ($value instanceof \UnitEnum) {
return $value->name;
} elseif ($value instanceof Expression) {
return $this->expressionToString($value);
}

return $value;
Expand All @@ -408,4 +420,13 @@ private function processEnums(array $values): array
{
return array_map(fn($value) => $this->processEnum($value), $values);
}

private function expressionToString(Expression|string $value): string
{
if (is_string($value)) {
return $value;
}

return $value->getValue($this->query->getConnection()->getQueryGrammar());
}
}
15 changes: 15 additions & 0 deletions tests/CreatesApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,19 @@ protected function getEnvironmentSetUp($app)
'pagination' => 'simple',
]);
}

public function appVersionEightAndUp(): bool
{
return version_compare(app()->version(), '8.0.0', '>=');
}

public function appVersionFiveBetweenSeven(): bool
{
return version_compare(app()->version(), '5.6.0', '>=') && version_compare(app()->version(), '8.0.0', '<');
}

public function appVersionOld(): bool
{
return version_compare(app()->version(), '5.4.0', '>=') && version_compare(app()->version(), '5.6.0', '<');
}
}
20 changes: 10 additions & 10 deletions tests/Feature/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ class PaginationTest extends FeatureTestCase
public function testPaginationProvidesDifferentLinksOnDifferentPages()
{
// Checking the version start with 8.0.
if (preg_match("/^([8|9]\.)/", app()->version())) {
if ($this->appVersionEightAndUp()) {
$page1ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">1</span>';
$page2ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">2</span>';
}

// Checking the version start with 5.6, 5.7, 5.8 or 6.
if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) {
// Checking the version start with 5.6, 5.7, 5.8, 6 or 7.
if ($this->appVersionFiveBetweenSeven()) {
$page1ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>';
$page2ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">2</span></li>';
}

// Checking the version 5.4 and 5.5
if (preg_match("/^5\.[4-5]/", app()->version())) {
if ($this->appVersionOld()) {
$page1ActiveLink = '<li class="active"><span>1</span></li>';
$page2ActiveLink = '<li class="active"><span>2</span></li>';
}
Expand All @@ -40,17 +40,17 @@ public function testPaginationProvidesDifferentLinksOnDifferentPages()
public function testAdvancedPagination()
{
// Checking the version start with 8.0.
if (preg_match("/^([8|9]\.)/", app()->version())) {
if ($this->appVersionEightAndUp()) {
$page1ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">1</span>';
$page2ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">2</span>';
}

if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) {
if ($this->appVersionFiveBetweenSeven()) {
$page1ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>';
$page2ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">2</span></li>';
}

if (preg_match("/^5\.[4-5]/", app()->version())) {
if ($this->appVersionOld()) {
$page1ActiveLink = '<li class="active"><span>1</span></li>';
$page2ActiveLink = '<li class="active"><span>2</span></li>';
}
Expand All @@ -63,17 +63,17 @@ public function testAdvancedPagination()
public function testCustomPagination()
{
// Checking the version start with 8.0.
if (preg_match("/^([8|9]\.)/", app()->version())) {
if ($this->appVersionEightAndUp()) {
$page1ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">1</span>';
$page2ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">2</span>';
}

if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) {
if ($this->appVersionFiveBetweenSeven()) {
$page1ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>';
$page2ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">2</span></li>';
}

if (preg_match("/^5\.[4-5]/", app()->version())) {
if ($this->appVersionOld()) {
$page1ActiveLink = '<li class="active"><span>1</span></li>';
$page2ActiveLink = '<li class="active"><span>2</span></li>';
}
Expand Down
5 changes: 2 additions & 3 deletions tests/Fixtures/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ class Book extends Model

protected $casts = [
'price' => 'float',
'published_at' => 'datetime',
];
protected $dates = [
'published_at',
];

protected $fillable = [
"author_id",
'description',
Expand Down
5 changes: 2 additions & 3 deletions tests/Fixtures/BookWithUncachedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ class BookWithUncachedStore extends Model

protected $casts = [
'price' => 'float',
'published_at' => 'datetime',
];
protected $dates = [
'published_at',
];

protected $fillable = [
"author_id",
'description',
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/UncachedBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class UncachedBook extends Model
{
protected $casts = [
'price' => 'float',
'published_at' => 'datetime',
];
protected $dates = [
'published_at',
];

protected $fillable = [
'description',
'published_at',
'title',
];

protected $table = 'books';

public function author() : BelongsTo
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/UncachedBookWithStores.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class UncachedBookWithStores extends Model
{
protected $casts = [
'price' => 'float',
'published_at' => 'datetime',
];
protected $dates = [
'published_at',
];

protected $fillable = [
'description',
'published_at',
'title',
];

protected $table = 'books';

public function author() : BelongsTo
Expand Down
18 changes: 9 additions & 9 deletions tests/Integration/CachedBuilder/PaginateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ public function testPaginationIsCached()

public function testPaginationReturnsCorrectLinks()
{
if (preg_match("/^([8|9]\.)/", app()->version())) {
if ($this->appVersionEightAndUp()) {
$page1ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">1</span>';
$page2ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">2</span>';
$page24ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">24</span>';
}

if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) {
if ($this->appVersionFiveBetweenSeven()) {
$page1ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>';
$page2ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">2</span></li>';
$page24ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">24</span></li>';
}

if (preg_match("/^5\.[4-5]/", app()->version())) {
if ($this->appVersionOld()) {
$page1ActiveLink = '<li class="active"><span>1</span></li>';
$page2ActiveLink = '<li class="active"><span>2</span></li>';
$page24ActiveLink = '<li class="active"><span>24</span></li>';
Expand All @@ -71,19 +71,19 @@ public function testPaginationReturnsCorrectLinks()

public function testPaginationWithOptionsReturnsCorrectLinks()
{
if (preg_match("/^([8|9]\.)/", app()->version())) {
if ($this->appVersionEightAndUp()) {
$page1ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">1</span>';
$page2ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">2</span>';
$page24ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">24</span>';
}

if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) {
if ($this->appVersionFiveBetweenSeven()) {
$page1ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>';
$page2ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">2</span></li>';
$page24ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">24</span></li>';
}

if (preg_match("/^5\.[4-5]/", app()->version())) {
if ($this->appVersionOld()) {
$page1ActiveLink = '<li class="active"><span>1</span></li>';
$page2ActiveLink = '<li class="active"><span>2</span></li>';
$page24ActiveLink = '<li class="active"><span>24</span></li>';
Expand All @@ -106,19 +106,19 @@ public function testPaginationWithOptionsReturnsCorrectLinks()

public function testPaginationWithCustomOptionsReturnsCorrectLinks()
{
if (preg_match("/^([8|9]\.)/", app()->version())) {
if ($this->appVersionEightAndUp()) {
$page1ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">1</span>';
$page2ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">2</span>';
$page24ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">24</span>';
}

if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) {
if ($this->appVersionFiveBetweenSeven()) {
$page1ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>';
$page2ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">2</span></li>';
$page24ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">24</span></li>';
}

if (preg_match("/^5\.[4-5]/", app()->version())) {
if ($this->appVersionOld()) {
$page1ActiveLink = '<li class="active"><span>1</span></li>';
$page2ActiveLink = '<li class="active"><span>2</span></li>';
$page24ActiveLink = '<li class="active"><span>24</span></li>';
Expand Down