Skip to content
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

Throw exception if using unsupported callback #195

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/Exceptions/SingleCallbackNotAllowed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Kirschbaum\PowerJoins\Exceptions;

use Exception;

class SingleCallbackNotAllowed extends Exception
{
/** The error message */
protected $message = 'Single callback is not allowed here.';
}
3 changes: 3 additions & 0 deletions src/Mixins/RelationshipsExtraMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Database\MySqlConnection;
use Illuminate\Support\Str;
use Kirschbaum\PowerJoins\Exceptions\SingleCallbackNotAllowed;
use Kirschbaum\PowerJoins\PowerJoinClause;
use Kirschbaum\PowerJoins\StaticCache;

Expand Down Expand Up @@ -129,6 +130,8 @@ protected function performJoinForEloquentPowerJoinsForBelongsToMany()

if (is_array($callback) && isset($callback[$this->getTable()])) {
$callback[$this->getTable()]($join);
} elseif ($callback && is_callable($callback)) {
throw new SingleCallbackNotAllowed();
}
});

Expand Down
9 changes: 9 additions & 0 deletions tests/JoinRelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirschbaum\PowerJoins\Tests;

use Exception;
use Kirschbaum\PowerJoins\Exceptions\SingleCallbackNotAllowed;
use Kirschbaum\PowerJoins\PowerJoinClause;
use Kirschbaum\PowerJoins\Tests\Models\Category;
use Kirschbaum\PowerJoins\Tests\Models\Comment;
Expand Down Expand Up @@ -271,6 +272,14 @@ public function test_join_belongs_to_many_with_callback()
);
}

/** @test */
public function test_single_callback_in_belongs_to_many_should_throw_exception()
{
$this->expectException(SingleCallbackNotAllowed::class);

User::query()->joinRelationship('groups', fn ($join) => $join->where('groups.name', 'Test'));
}

/** @test */
public function test_it_doesnt_join_the_same_relationship_twice()
{
Expand Down
Loading