Skip to content

Commit 5f33b0d

Browse files
committed
refactor tests
1 parent c1c280b commit 5f33b0d

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-query-builder` will be documented in this file
44

5+
## 1.9.2 - 2018-5-21
6+
7+
- prevent double sorting statments
8+
59
## 1.9.1 - 2018-05-15
610

711
- improvements around field selection

tests/SortTest.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Spatie\QueryBuilder\Tests;
44

5+
use DB;
56
use Illuminate\Http\Request;
67
use Spatie\QueryBuilder\QueryBuilder;
78
use Spatie\QueryBuilder\Tests\Models\TestModel;
@@ -20,31 +21,31 @@ public function setUp()
2021
{
2122
parent::setUp();
2223

24+
DB::enableQueryLog();
25+
2326
$this->models = factory(TestModel::class, 5)->create();
2427
$this->modelTableName = $this->models->first()->getTable();
2528
}
2629

2730
/** @test */
2831
public function it_can_sort_a_collection_ascending()
2932
{
30-
\DB::enableQueryLog();
3133
$sortedModels = $this
3234
->createQueryFromSortRequest('name')
3335
->get();
3436

35-
$this->assertSame(\DB::getQueryLog()[0]['query'], 'select "test_models".* from "test_models" order by "name" asc');
37+
$this->assertQueryExecuted('select "test_models".* from "test_models" order by "name" asc');
3638
$this->assertSortedAscending($sortedModels, 'name');
3739
}
3840

3941
/** @test */
4042
public function it_can_sort_a_collection_descending()
4143
{
42-
\DB::enableQueryLog();
4344
$sortedModels = $this
4445
->createQueryFromSortRequest('-name')
4546
->get();
4647

47-
$this->assertSame(\DB::getQueryLog()[0]['query'], 'select "test_models".* from "test_models" order by "name" desc');
48+
$this->assertQueryExecuted('select "test_models".* from "test_models" order by "name" desc');
4849
$this->assertSortedDescending($sortedModels, 'name');
4950
}
5051

@@ -93,26 +94,25 @@ public function it_wont_sort_if_no_sort_query_parameter_is_given()
9394
/** @test */
9495
public function it_uses_default_sort_parameter()
9596
{
96-
\DB::enableQueryLog();
9797
$sortedModels = QueryBuilder::for(TestModel::class, new Request())
9898
->allowedSorts('name')
9999
->defaultSort('name')
100100
->get();
101101

102-
$this->assertSame(\DB::getQueryLog()[0]['query'], 'select "test_models".* from "test_models" order by "name" asc');
102+
$this->assertQueryExecuted('select "test_models".* from "test_models" order by "name" asc');
103103
$this->assertSortedAscending($sortedModels, 'name');
104104
}
105105

106106
/** @test */
107107
public function it_can_allow_multiple_sort_parameters()
108108
{
109-
\DB::enableQueryLog();
109+
DB::enableQueryLog();
110110
$sortedModels = $this
111111
->createQueryFromSortRequest('name')
112112
->allowedSorts('id', 'name')
113113
->get();
114114

115-
$this->assertSame(\DB::getQueryLog()[0]['query'], 'select "test_models".* from "test_models" order by "name" asc');
115+
$this->assertQueryExecuted('select "test_models".* from "test_models" order by "name" asc');
116116
$this->assertSortedAscending($sortedModels, 'name');
117117
}
118118

@@ -131,15 +131,14 @@ public function it_can_allow_multiple_sort_parameters_as_an_array()
131131
public function it_can_sort_by_multiple_columns()
132132
{
133133
factory(TestModel::class, 3)->create(['name' => 'foo']);
134-
\DB::enableQueryLog();
135134

136135
$sortedModels = $this
137136
->createQueryFromSortRequest('name,-id')
138137
->allowedSorts('name', 'id')
139138
->get();
140139

141140
$expected = TestModel::orderBy('name')->orderByDesc('id');
142-
$this->assertSame(\DB::getQueryLog()[0]['query'], 'select "test_models".* from "test_models" order by "name" asc, "id" desc');
141+
$this->assertQueryExecuted('select "test_models".* from "test_models" order by "name" asc, "id" desc');
143142
$this->assertEquals($expected->pluck('id'), $sortedModels->pluck('id'));
144143
}
145144

@@ -151,4 +150,13 @@ protected function createQueryFromSortRequest(string $sort): QueryBuilder
151150

152151
return QueryBuilder::for(TestModel::class, $request);
153152
}
153+
154+
protected function assertQueryExecuted(string $query)
155+
{
156+
$queries = array_map(function($queryLogItem) {
157+
return $queryLogItem['query'];
158+
}, DB::getQueryLog());
159+
160+
$this->assertContains($query, $queries);
161+
}
154162
}

0 commit comments

Comments
 (0)