Skip to content

[12.x] Add testcase for findSole method #55115

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
Mar 22, 2025
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
13 changes: 13 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public function testFindMethod()
$this->assertSame('baz', $result);
}

public function testFindSoleMethod()
{
$builder = m::mock(Builder::class.'[sole]', [$this->getMockQueryBuilder()]);
$model = $this->getMockModel();
$builder->setModel($model);
$model->shouldReceive('getKeyType')->once()->andReturn('int');
$builder->getQuery()->shouldReceive('where')->once()->with('foo_table.foo', '=', 'bar');
$builder->shouldReceive('sole')->with(['column'])->andReturn('baz');

$result = $builder->findSole('bar', ['column']);
$this->assertSame('baz', $result);
}

public function testFindManyMethod()
{
// ids are not empty
Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\RecordsNotFoundException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -413,6 +414,29 @@ public function testFindMethodStringyKey()
$this->assertCount(2, $post->tags()->findMany(new Collection([$tag->id, $tag2->id])));
}

public function testFindSoleMethod()
{
$post = Post::create(['title' => Str::random()]);

$tag = Tag::create(['name' => Str::random()]);

$post->tags()->attach($tag);

$this->assertEquals($tag->id, $post->tags()->findSole($tag->id)->id);

$this->assertEquals($tag->id, $post->tags()->findSole($tag)->id);

// Test with no records
$post->tags()->detach($tag);

try {
$post->tags()->findSole($tag);
$this->fail('Expected RecordsNotFoundException was not thrown.');
} catch (RecordsNotFoundException $e) {
$this->assertTrue(true);
}
}

public function testFindOrFailMethod()
{
$this->expectException(ModelNotFoundException::class);
Expand Down
1 change: 1 addition & 0 deletions types/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function test(
assertType('Illuminate\Types\Builder\User', $query->forceCreate(['name' => 'John']));
assertType('Illuminate\Types\Builder\User', $query->updateOrCreate(['id' => 1], ['name' => 'John']));
assertType('Illuminate\Types\Builder\User', $query->firstOrFail());
assertType('Illuminate\Types\Builder\User', $query->findSole(1));
assertType('Illuminate\Types\Builder\User', $query->sole());
assertType('Illuminate\Support\LazyCollection<int, Illuminate\Types\Builder\User>', $query->cursor());
assertType('Illuminate\Support\LazyCollection<int, Illuminate\Types\Builder\User>', $query->cursor());
Expand Down
Loading