Skip to content

Commit fd0242a

Browse files
mrvipchiencuong.tt
andauthored
[12.x] Add testcase for findSole method (#55115)
* add testcase for findSole method * update testcase testFindSoleMethod * fix testcase testFindSoleMethod * styleci --------- Co-authored-by: cuong.tt <[email protected]>
1 parent dd246d7 commit fd0242a

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ public function testFindMethod()
4848
$this->assertSame('baz', $result);
4949
}
5050

51+
public function testFindSoleMethod()
52+
{
53+
$builder = m::mock(Builder::class.'[sole]', [$this->getMockQueryBuilder()]);
54+
$model = $this->getMockModel();
55+
$builder->setModel($model);
56+
$model->shouldReceive('getKeyType')->once()->andReturn('int');
57+
$builder->getQuery()->shouldReceive('where')->once()->with('foo_table.foo', '=', 'bar');
58+
$builder->shouldReceive('sole')->with(['column'])->andReturn('baz');
59+
60+
$result = $builder->findSole('bar', ['column']);
61+
$this->assertSame('baz', $result);
62+
}
63+
5164
public function testFindManyMethod()
5265
{
5366
// ids are not empty

tests/Integration/Database/EloquentBelongsToManyTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\ModelNotFoundException;
88
use Illuminate\Database\Eloquent\Relations\Pivot;
9+
use Illuminate\Database\RecordsNotFoundException;
910
use Illuminate\Database\Schema\Blueprint;
1011
use Illuminate\Support\Carbon;
1112
use Illuminate\Support\Facades\DB;
@@ -413,6 +414,29 @@ public function testFindMethodStringyKey()
413414
$this->assertCount(2, $post->tags()->findMany(new Collection([$tag->id, $tag2->id])));
414415
}
415416

417+
public function testFindSoleMethod()
418+
{
419+
$post = Post::create(['title' => Str::random()]);
420+
421+
$tag = Tag::create(['name' => Str::random()]);
422+
423+
$post->tags()->attach($tag);
424+
425+
$this->assertEquals($tag->id, $post->tags()->findSole($tag->id)->id);
426+
427+
$this->assertEquals($tag->id, $post->tags()->findSole($tag)->id);
428+
429+
// Test with no records
430+
$post->tags()->detach($tag);
431+
432+
try {
433+
$post->tags()->findSole($tag);
434+
$this->fail('Expected RecordsNotFoundException was not thrown.');
435+
} catch (RecordsNotFoundException $e) {
436+
$this->assertTrue(true);
437+
}
438+
}
439+
416440
public function testFindOrFailMethod()
417441
{
418442
$this->expectException(ModelNotFoundException::class);

types/Database/Eloquent/Builder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function test(
6363
assertType('Illuminate\Types\Builder\User', $query->forceCreate(['name' => 'John']));
6464
assertType('Illuminate\Types\Builder\User', $query->updateOrCreate(['id' => 1], ['name' => 'John']));
6565
assertType('Illuminate\Types\Builder\User', $query->firstOrFail());
66+
assertType('Illuminate\Types\Builder\User', $query->findSole(1));
6667
assertType('Illuminate\Types\Builder\User', $query->sole());
6768
assertType('Illuminate\Support\LazyCollection<int, Illuminate\Types\Builder\User>', $query->cursor());
6869
assertType('Illuminate\Support\LazyCollection<int, Illuminate\Types\Builder\User>', $query->cursor());

0 commit comments

Comments
 (0)