diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml index 3b751da3..a169f2bb 100644 --- a/.github/workflows/laravel.yml +++ b/.github/workflows/laravel.yml @@ -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 diff --git a/.gitignore b/.gitignore index 48b61660..c2d4570f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.lock .phpunit.result.cache phpunit.xml +.idea diff --git a/composer.json b/composer.json index d60aea86..0460718b 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/CacheKey.php b/src/CacheKey.php index 37ec4a48..08685500 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -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; @@ -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"] + ); }) ?: ""; } @@ -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); @@ -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; @@ -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()); + } } diff --git a/tests/CreatesApplication.php b/tests/CreatesApplication.php index 43e674ec..b402ab3a 100644 --- a/tests/CreatesApplication.php +++ b/tests/CreatesApplication.php @@ -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', '<'); + } } diff --git a/tests/Feature/PaginationTest.php b/tests/Feature/PaginationTest.php index b122b23f..465ec823 100644 --- a/tests/Feature/PaginationTest.php +++ b/tests/Feature/PaginationTest.php @@ -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 = '1'; $page2ActiveLink = '2'; } - // 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 = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; } // Checking the version 5.4 and 5.5 - if (preg_match("/^5\.[4-5]/", app()->version())) { + if ($this->appVersionOld()) { $page1ActiveLink = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; } @@ -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 = '1'; $page2ActiveLink = '2'; } - if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) { + if ($this->appVersionFiveBetweenSeven()) { $page1ActiveLink = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; } - if (preg_match("/^5\.[4-5]/", app()->version())) { + if ($this->appVersionOld()) { $page1ActiveLink = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; } @@ -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 = '1'; $page2ActiveLink = '2'; } - if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) { + if ($this->appVersionFiveBetweenSeven()) { $page1ActiveLink = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; } - if (preg_match("/^5\.[4-5]/", app()->version())) { + if ($this->appVersionOld()) { $page1ActiveLink = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; } diff --git a/tests/Fixtures/Book.php b/tests/Fixtures/Book.php index 8609380e..59abb6e8 100644 --- a/tests/Fixtures/Book.php +++ b/tests/Fixtures/Book.php @@ -14,10 +14,9 @@ class Book extends Model protected $casts = [ 'price' => 'float', + 'published_at' => 'datetime', ]; - protected $dates = [ - 'published_at', - ]; + protected $fillable = [ "author_id", 'description', diff --git a/tests/Fixtures/BookWithUncachedStore.php b/tests/Fixtures/BookWithUncachedStore.php index a9bd30f0..83a4a2a6 100644 --- a/tests/Fixtures/BookWithUncachedStore.php +++ b/tests/Fixtures/BookWithUncachedStore.php @@ -14,10 +14,9 @@ class BookWithUncachedStore extends Model protected $casts = [ 'price' => 'float', + 'published_at' => 'datetime', ]; - protected $dates = [ - 'published_at', - ]; + protected $fillable = [ "author_id", 'description', diff --git a/tests/Fixtures/UncachedBook.php b/tests/Fixtures/UncachedBook.php index c73583aa..bbdb359c 100644 --- a/tests/Fixtures/UncachedBook.php +++ b/tests/Fixtures/UncachedBook.php @@ -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 diff --git a/tests/Fixtures/UncachedBookWithStores.php b/tests/Fixtures/UncachedBookWithStores.php index 173c939a..f1972a02 100644 --- a/tests/Fixtures/UncachedBookWithStores.php +++ b/tests/Fixtures/UncachedBookWithStores.php @@ -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 diff --git a/tests/Integration/CachedBuilder/PaginateTest.php b/tests/Integration/CachedBuilder/PaginateTest.php index 8f24ebb4..0ac4ead0 100644 --- a/tests/Integration/CachedBuilder/PaginateTest.php +++ b/tests/Integration/CachedBuilder/PaginateTest.php @@ -36,19 +36,19 @@ public function testPaginationIsCached() public function testPaginationReturnsCorrectLinks() { - if (preg_match("/^([8|9]\.)/", app()->version())) { + if ($this->appVersionEightAndUp()) { $page1ActiveLink = '1'; $page2ActiveLink = '2'; $page24ActiveLink = '24'; } - if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) { + if ($this->appVersionFiveBetweenSeven()) { $page1ActiveLink = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; $page24ActiveLink = '
  • 24
  • '; } - if (preg_match("/^5\.[4-5]/", app()->version())) { + if ($this->appVersionOld()) { $page1ActiveLink = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; $page24ActiveLink = '
  • 24
  • '; @@ -71,19 +71,19 @@ public function testPaginationReturnsCorrectLinks() public function testPaginationWithOptionsReturnsCorrectLinks() { - if (preg_match("/^([8|9]\.)/", app()->version())) { + if ($this->appVersionEightAndUp()) { $page1ActiveLink = '1'; $page2ActiveLink = '2'; $page24ActiveLink = '24'; } - if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) { + if ($this->appVersionFiveBetweenSeven()) { $page1ActiveLink = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; $page24ActiveLink = '
  • 24
  • '; } - if (preg_match("/^5\.[4-5]/", app()->version())) { + if ($this->appVersionOld()) { $page1ActiveLink = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; $page24ActiveLink = '
  • 24
  • '; @@ -106,19 +106,19 @@ public function testPaginationWithOptionsReturnsCorrectLinks() public function testPaginationWithCustomOptionsReturnsCorrectLinks() { - if (preg_match("/^([8|9]\.)/", app()->version())) { + if ($this->appVersionEightAndUp()) { $page1ActiveLink = '1'; $page2ActiveLink = '2'; $page24ActiveLink = '24'; } - if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) { + if ($this->appVersionFiveBetweenSeven()) { $page1ActiveLink = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; $page24ActiveLink = '
  • 24
  • '; } - if (preg_match("/^5\.[4-5]/", app()->version())) { + if ($this->appVersionOld()) { $page1ActiveLink = '
  • 1
  • '; $page2ActiveLink = '
  • 2
  • '; $page24ActiveLink = '
  • 24
  • ';