Skip to content

Commit 24fb71a

Browse files
authored
use offset() and limit() in tests (#56089)
we should not be testing the alias methods `skip()` and `take()` in our tests, but rather directly the actual methods performing the logic. if someone wanted to add a test for `skip()` and `take()` that'd be great, it'd be 1 method each ensuring they just pass along to their aliased counterparts.
1 parent 1831e26 commit 24fb71a

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,21 +1724,21 @@ public function testUnionLimitsAndOffsets()
17241724
$builder = $this->getBuilder();
17251725
$builder->select('*')->from('users');
17261726
$builder->union($this->getBuilder()->select('*')->from('dogs'));
1727-
$builder->skip(5)->take(10);
1727+
$builder->offset(5)->limit(10);
17281728
$this->assertSame('(select * from "users") union (select * from "dogs") limit 10 offset 5', $builder->toSql());
17291729

17301730
$expectedSql = '(select * from "users") union (select * from "dogs") limit 10 offset 5';
17311731
$builder = $this->getPostgresBuilder();
17321732
$builder->select('*')->from('users');
17331733
$builder->union($this->getBuilder()->select('*')->from('dogs'));
1734-
$builder->skip(5)->take(10);
1734+
$builder->offset(5)->limit(10);
17351735
$this->assertEquals($expectedSql, $builder->toSql());
17361736

17371737
$expectedSql = '(select * from "users" limit 11) union (select * from "dogs" limit 22) limit 10 offset 5';
17381738
$builder = $this->getPostgresBuilder();
17391739
$builder->select('*')->from('users')->limit(11);
17401740
$builder->union($this->getBuilder()->select('*')->from('dogs')->limit(22));
1741-
$builder->skip(5)->take(10);
1741+
$builder->offset(5)->limit(10);
17421742
$this->assertEquals($expectedSql, $builder->toSql());
17431743
}
17441744

@@ -1769,7 +1769,7 @@ public function testMySqlUnionLimitsAndOffsets()
17691769
$builder = $this->getMySqlBuilder();
17701770
$builder->select('*')->from('users');
17711771
$builder->union($this->getMySqlBuilder()->select('*')->from('dogs'));
1772-
$builder->skip(5)->take(10);
1772+
$builder->offset(5)->limit(10);
17731773
$this->assertSame('(select * from `users`) union (select * from `dogs`) limit 10 offset 5', $builder->toSql());
17741774
}
17751775

@@ -1826,14 +1826,14 @@ public function testSubSelectWhereIns()
18261826
{
18271827
$builder = $this->getBuilder();
18281828
$builder->select('*')->from('users')->whereIn('id', function ($q) {
1829-
$q->select('id')->from('users')->where('age', '>', 25)->take(3);
1829+
$q->select('id')->from('users')->where('age', '>', 25)->limit(3);
18301830
});
18311831
$this->assertSame('select * from "users" where "id" in (select "id" from "users" where "age" > ? limit 3)', $builder->toSql());
18321832
$this->assertEquals([25], $builder->getBindings());
18331833

18341834
$builder = $this->getBuilder();
18351835
$builder->select('*')->from('users')->whereNotIn('id', function ($q) {
1836-
$q->select('id')->from('users')->where('age', '>', 25)->take(3);
1836+
$q->select('id')->from('users')->where('age', '>', 25)->limit(3);
18371837
});
18381838
$this->assertSame('select * from "users" where "id" not in (select "id" from "users" where "age" > ? limit 3)', $builder->toSql());
18391839
$this->assertEquals([25], $builder->getBindings());
@@ -2070,7 +2070,7 @@ public function testOrderBysSqlServer()
20702070
$this->assertEquals(['foo'], $builder->getBindings());
20712071

20722072
$builder = $this->getSqlServerBuilder();
2073-
$builder->select('*')->from('users')->skip(25)->take(10)->orderByRaw('[email] desc');
2073+
$builder->select('*')->from('users')->offset(25)->limit(10)->orderByRaw('[email] desc');
20742074
$this->assertSame('select * from [users] order by [email] desc offset 25 rows fetch next 10 rows only', $builder->toSql());
20752075
}
20762076

@@ -2343,23 +2343,23 @@ public function testLimitsAndOffsets()
23432343
$this->assertSame('select * from "users" limit 0', $builder->toSql());
23442344

23452345
$builder = $this->getBuilder();
2346-
$builder->select('*')->from('users')->skip(5)->take(10);
2346+
$builder->select('*')->from('users')->offset(5)->limit(10);
23472347
$this->assertSame('select * from "users" limit 10 offset 5', $builder->toSql());
23482348

23492349
$builder = $this->getBuilder();
2350-
$builder->select('*')->from('users')->skip(0)->take(0);
2350+
$builder->select('*')->from('users')->offset(0)->limit(0);
23512351
$this->assertSame('select * from "users" limit 0 offset 0', $builder->toSql());
23522352

23532353
$builder = $this->getBuilder();
2354-
$builder->select('*')->from('users')->skip(-5)->take(-10);
2354+
$builder->select('*')->from('users')->offset(-5)->limit(-10);
23552355
$this->assertSame('select * from "users" offset 0', $builder->toSql());
23562356

23572357
$builder = $this->getBuilder();
2358-
$builder->select('*')->from('users')->skip(null)->take(null);
2358+
$builder->select('*')->from('users')->offset(null)->limit(null);
23592359
$this->assertSame('select * from "users" offset 0', $builder->toSql());
23602360

23612361
$builder = $this->getBuilder();
2362-
$builder->select('*')->from('users')->skip(5)->take(null);
2362+
$builder->select('*')->from('users')->offset(5)->limit(null);
23632363
$this->assertSame('select * from "users" offset 5', $builder->toSql());
23642364
}
23652365

@@ -2475,7 +2475,7 @@ public function testGetCountForPaginationWithUnionOrders()
24752475
public function testGetCountForPaginationWithUnionLimitAndOffset()
24762476
{
24772477
$builder = $this->getBuilder();
2478-
$builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id'))->take(15)->skip(1);
2478+
$builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id'))->limit(15)->offset(1);
24792479

24802480
$builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from ((select "id" from "posts") union (select "id" from "videos")) as "temp_table"', [], true)->andReturn([['aggregate' => 1]]);
24812481
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
@@ -4310,12 +4310,12 @@ public function testDeleteMethod()
43104310

43114311
$builder = $this->getSqliteBuilder();
43124312
$builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "rowid" in (select "users"."rowid" from "users" where "email" = ? order by "id" asc limit 1)', ['foo'])->andReturn(1);
4313-
$result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->take(1)->delete();
4313+
$result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->limit(1)->delete();
43144314
$this->assertEquals(1, $result);
43154315

43164316
$builder = $this->getMySqlBuilder();
43174317
$builder->getConnection()->shouldReceive('delete')->once()->with('delete from `users` where `email` = ? order by `id` asc limit 1', ['foo'])->andReturn(1);
4318-
$result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->take(1)->delete();
4318+
$result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->limit(1)->delete();
43194319
$this->assertEquals(1, $result);
43204320

43214321
$builder = $this->getSqlServerBuilder();
@@ -4325,7 +4325,7 @@ public function testDeleteMethod()
43254325

43264326
$builder = $this->getSqlServerBuilder();
43274327
$builder->getConnection()->shouldReceive('delete')->once()->with('delete top (1) from [users] where [email] = ?', ['foo'])->andReturn(1);
4328-
$result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->take(1)->delete();
4328+
$result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->limit(1)->delete();
43294329
$this->assertEquals(1, $result);
43304330
}
43314331

@@ -4353,7 +4353,7 @@ public function testDeleteWithJoinMethod()
43534353

43544354
$builder = $this->getMySqlBuilder();
43554355
$builder->getConnection()->shouldReceive('delete')->once()->with('delete `users` from `users` inner join `contacts` on `users`.`id` = `contacts`.`id` where `users`.`id` = ?', [1])->andReturn(1);
4356-
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->orderBy('id')->take(1)->delete(1);
4356+
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->orderBy('id')->limit(1)->delete(1);
43574357
$this->assertEquals(1, $result);
43584358

43594359
$builder = $this->getSqlServerBuilder();
@@ -4383,7 +4383,7 @@ public function testDeleteWithJoinMethod()
43834383

43844384
$builder = $this->getPostgresBuilder();
43854385
$builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "ctid" in (select "users"."ctid" from "users" inner join "contacts" on "users"."id" = "contacts"."id" where "users"."id" = ? order by "id" asc limit 1)', [1])->andReturn(1);
4386-
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->orderBy('id')->take(1)->delete(1);
4386+
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->orderBy('id')->limit(1)->delete(1);
43874387
$this->assertEquals(1, $result);
43884388

43894389
$builder = $this->getPostgresBuilder();
@@ -4955,35 +4955,35 @@ public function testSQLiteOrderBy()
49554955
public function testSqlServerLimitsAndOffsets()
49564956
{
49574957
$builder = $this->getSqlServerBuilder();
4958-
$builder->select('*')->from('users')->take(10);
4958+
$builder->select('*')->from('users')->limit(10);
49594959
$this->assertSame('select top 10 * from [users]', $builder->toSql());
49604960

49614961
$builder = $this->getSqlServerBuilder();
4962-
$builder->select('*')->from('users')->skip(10)->orderBy('email', 'desc');
4962+
$builder->select('*')->from('users')->offset(10)->orderBy('email', 'desc');
49634963
$this->assertSame('select * from [users] order by [email] desc offset 10 rows', $builder->toSql());
49644964

49654965
$builder = $this->getSqlServerBuilder();
4966-
$builder->select('*')->from('users')->skip(10)->take(10);
4966+
$builder->select('*')->from('users')->offset(10)->limit(10);
49674967
$this->assertSame('select * from [users] order by (SELECT 0) offset 10 rows fetch next 10 rows only', $builder->toSql());
49684968

49694969
$builder = $this->getSqlServerBuilder();
4970-
$builder->select('*')->from('users')->skip(11)->take(10)->orderBy('email', 'desc');
4970+
$builder->select('*')->from('users')->offset(11)->limit(10)->orderBy('email', 'desc');
49714971
$this->assertSame('select * from [users] order by [email] desc offset 11 rows fetch next 10 rows only', $builder->toSql());
49724972

49734973
$builder = $this->getSqlServerBuilder();
49744974
$subQuery = function ($query) {
49754975
return $query->select('created_at')->from('logins')->where('users.name', 'nameBinding')->whereColumn('user_id', 'users.id')->limit(1);
49764976
};
4977-
$builder->select('*')->from('users')->where('email', 'emailBinding')->orderBy($subQuery)->skip(10)->take(10);
4977+
$builder->select('*')->from('users')->where('email', 'emailBinding')->orderBy($subQuery)->offset(10)->limit(10);
49784978
$this->assertSame('select * from [users] where [email] = ? order by (select top 1 [created_at] from [logins] where [users].[name] = ? and [user_id] = [users].[id]) asc offset 10 rows fetch next 10 rows only', $builder->toSql());
49794979
$this->assertEquals(['emailBinding', 'nameBinding'], $builder->getBindings());
49804980

49814981
$builder = $this->getSqlServerBuilder();
4982-
$builder->select('*')->from('users')->take('foo');
4982+
$builder->select('*')->from('users')->limit('foo');
49834983
$this->assertSame('select * from [users]', $builder->toSql());
49844984

49854985
$builder = $this->getSqlServerBuilder();
4986-
$builder->select('*')->from('users')->take('foo')->offset('bar');
4986+
$builder->select('*')->from('users')->limit('foo')->offset('bar');
49874987
$this->assertSame('select * from [users]', $builder->toSql());
49884988

49894989
$builder = $this->getSqlServerBuilder();

tests/Foundation/FoundationInteractsWithDatabaseTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function testSeeInDatabaseFindsNotMatchingResults()
8686

8787
$builder = $this->mockCountBuilder(false);
8888

89-
$builder->shouldReceive('take')->andReturnSelf();
89+
$builder->shouldReceive('limit')->andReturnSelf();
9090
$builder->shouldReceive('get')->andReturn(collect([['title' => 'Forge']]));
9191

9292
$this->assertDatabaseHas($this->table, $this->data);
@@ -100,7 +100,7 @@ public function testSeeInDatabaseFindsManyNotMatchingResults()
100100

101101
$builder = $this->mockCountBuilder(false, countResult: [5, 5]);
102102

103-
$builder->shouldReceive('take')->andReturnSelf();
103+
$builder->shouldReceive('limit')->andReturnSelf();
104104
$builder->shouldReceive('get')->andReturn(
105105
collect(array_fill(0, 3, 'data'))
106106
);
@@ -142,7 +142,7 @@ public function testDontSeeInDatabaseFindsResults()
142142

143143
$builder = $this->mockCountBuilder(true);
144144

145-
$builder->shouldReceive('take')->andReturnSelf();
145+
$builder->shouldReceive('limit')->andReturnSelf();
146146
$builder->shouldReceive('get')->andReturn(collect([$this->data]));
147147

148148
$this->assertDatabaseMissing($this->table, $this->data);

0 commit comments

Comments
 (0)