From dd417f526e0648347db752564f3057b4a5e930d9 Mon Sep 17 00:00:00 2001 From: Colin Viebrock Date: Wed, 6 Dec 2023 23:20:31 -0600 Subject: [PATCH 1/3] add test for eager relations --- composer.json | 2 +- phpunit.xml | 10 ++++--- tests/Models/PostWithEagerRelation.php | 14 ++++++++++ tests/RelationTests.php | 38 ++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 tests/Models/PostWithEagerRelation.php create mode 100644 tests/RelationTests.php diff --git a/composer.json b/composer.json index 970f496..89f81bb 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "require-dev": { "mockery/mockery": "^1.4.4", "orchestra/testbench": "^8.0", - "pestphp/pest": "2.x-dev" + "pestphp/pest": "^2.28" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml index 9639a60..023929d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -8,12 +8,9 @@ processIsolation="false" stopOnFailure="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" > - - src/ - @@ -26,4 +23,9 @@ + + + src/ + + diff --git a/tests/Models/PostWithEagerRelation.php b/tests/Models/PostWithEagerRelation.php new file mode 100644 index 0000000..959d3dc --- /dev/null +++ b/tests/Models/PostWithEagerRelation.php @@ -0,0 +1,14 @@ + 'Arthur Conan Doyle' + ]); + $post = new PostWithEagerRelation([ + 'title' => 'My First Post' + ]); + $post->author()->associate($author); + $post->save(); + + self::assertEquals('arthur-conan-doyle-my-first-post', $post->slug); + + $post2 = new PostWithEagerRelation([ + 'title' => 'My second post', + ]); + $post2->author()->associate($author); + $post2->save(); + self::assertEquals('arthur-conan-doyle-my-second-post', $post2->slug); + } + +} From 6c5024cf1d405510dc4dc945f0aeb8c5a609abfb Mon Sep 17 00:00:00 2001 From: Colin Viebrock Date: Wed, 6 Dec 2023 23:26:17 -0600 Subject: [PATCH 2/3] turn on strict mode --- tests/RelationTests.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/RelationTests.php b/tests/RelationTests.php index 8f6e6fe..8735301 100644 --- a/tests/RelationTests.php +++ b/tests/RelationTests.php @@ -2,6 +2,7 @@ use Cviebrock\EloquentSluggable\Tests\Models\Author; use Cviebrock\EloquentSluggable\Tests\Models\PostWithEagerRelation; +use Illuminate\Database\Eloquent\Model; /** * Class RelationTests @@ -16,6 +17,8 @@ class RelationTests extends TestCase */ public function testEagerLoading(): void { + Model::shouldBeStrict(true); + $author = Author::create([ 'name' => 'Arthur Conan Doyle' ]); From 6a8f17310ebcde8c015d44fb606c1c28c86ecf8b Mon Sep 17 00:00:00 2001 From: Colin Viebrock Date: Sat, 6 Jan 2024 15:31:04 -0600 Subject: [PATCH 3/3] fix for test runners --- .github/workflows/tests.yml | 6 +++--- src/Services/SlugService.php | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 06f297c..d561134 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -16,7 +16,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 with: @@ -27,9 +27,9 @@ jobs: run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - name: Get composer cache directory id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" + run: echo "dir=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT" - name: Cache dependencies - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ${{ steps.composer-cache.outputs.dir }} key: dependencies-${{ matrix.php }}-${{ matrix.stability }}-composer-${{ hashFiles('**/composer.lock') }} diff --git a/src/Services/SlugService.php b/src/Services/SlugService.php index 7e87228..093dae6 100644 --- a/src/Services/SlugService.php +++ b/src/Services/SlugService.php @@ -375,7 +375,9 @@ protected function getExistingSlugs(string $slug, string $attribute, array $conf } // get the list of all matching slugs - $results = $query->select([$attribute, $this->model->getQualifiedKeyName()]) + $results = $query + ->withoutEagerLoads() + ->select([$attribute, $this->model->getQualifiedKeyName()]) ->get() ->toBase();