Skip to content

Commit

Permalink
Merge pull request #95 from fico7489/filip-patch-5
Browse files Browse the repository at this point in the history
Filip patch 5
  • Loading branch information
fico7489 authored Mar 26, 2020
2 parents 63ffbb9 + fe40292 commit df3810f
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 59 deletions.
10 changes: 9 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ protected function fetchLastLog()

protected function fetchQuery()
{
return $this->fetchLastLog()['query'];
$query = $this->fetchLastLog()['query'];
$bindings = $this->fetchLastLog()['bindings'];

foreach ($bindings as $binding) {
$binding = is_string($binding) ? ('"'.$binding.'"') : $binding;
$query = preg_replace('/\?/', $binding, $query, 1);
}

return $query;
}

protected function fetchBindings()
Expand Down
4 changes: 2 additions & 2 deletions tests/Tests/AppendRelationsCountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public function testWhere()
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
left join "locations" on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
and "locations"."is_primary" = 1
and "locations"."deleted_at" is null
left join "location_addresses" on "location_addresses"."location_id" = "locations"."id"
and "location_addresses"."is_primary" = ?
and "location_addresses"."is_primary" = 1
and "location_addresses"."deleted_at" is null
where "orders"."deleted_at" is null
group by "orders"."id"';
Expand Down
14 changes: 7 additions & 7 deletions tests/Tests/Clauses/OrWhereInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public function testWhere()
->orWhereInJoin('seller.id', [3, 4])
->get();

$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where ("sellers"."id" in (?, ?)
or
"sellers"."id" in (?, ?))
and "orders"."deleted_at" is null
$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where ("sellers"."id" in (1, 2)
or
"sellers"."id" in (3, 4))
and "orders"."deleted_at" is null
group by "orders"."id"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
Expand Down
4 changes: 2 additions & 2 deletions tests/Tests/Clauses/OrWhereNotInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function testWhere()
$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where ("sellers"."id" in (?, ?)
where ("sellers"."id" in (1, 2)
or
"sellers"."id" not in (?, ?))
"sellers"."id" not in (3, 4))
and "orders"."deleted_at" is null
group by "orders"."id"';

Expand Down
2 changes: 1 addition & 1 deletion tests/Tests/Clauses/OrWhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testWhere()
$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where ("sellers"."id" = ? or "sellers"."id" = ?)
where ("sellers"."id" = 1 or "sellers"."id" = 2)
and "orders"."deleted_at" is null
group by "orders"."id"';

Expand Down
10 changes: 5 additions & 5 deletions tests/Tests/Clauses/WhereInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public function testWhere()
->whereInJoin('seller.id', [1, 2])
->get();

$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where "sellers"."id" in (?, ?)
and "orders"."deleted_at" is null
$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where "sellers"."id" in (1, 2)
and "orders"."deleted_at" is null
group by "orders"."id"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
Expand Down
10 changes: 5 additions & 5 deletions tests/Tests/Clauses/WhereNotInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public function testWhere()
->whereNotInJoin('seller.id', [1, 2])
->get();

$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where "sellers"."id" not in (?, ?)
and "orders"."deleted_at" is null
$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where "sellers"."id" not in (1, 2)
and "orders"."deleted_at" is null
group by "orders"."id"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
Expand Down
2 changes: 1 addition & 1 deletion tests/Tests/Clauses/WhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testWhere()
$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where "sellers"."id" = ?
where "sellers"."id" = 1
and "orders"."deleted_at" is null
group by "orders"."id"';

Expand Down
30 changes: 11 additions & 19 deletions tests/Tests/ClosureOnRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,50 @@ public function testWhereOnRelationWithOrderByJoin()
$queryTest = 'select sellers.*, MAX("locations"."id") as sort from "sellers"
left join "locations"
on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
and "locations"."is_secondary" = ?
and "locations"."is_primary" = 0
and "locations"."is_secondary" = 0
and "locations"."deleted_at" is null
group by "sellers"."id"
order by sort desc';
$bindingsTest = [0, 0];

$this->assertQueryMatches($queryTest, $this->fetchQuery());
$this->assertEquals($bindingsTest, $this->fetchBindings());

//locationPrimary have one where ['is_primary => 1']
$items = Seller::orderByJoin('locationPrimary.id', 'desc')->get();
$queryTest = 'select sellers.*, MAX("locations"."id") as sort from "sellers"
left join "locations"
on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
and "locations"."is_primary" = 1
and "locations"."deleted_at" is null
group by "sellers"."id"
order by sort desc';

$bindingsTest = [1];
$this->assertQueryMatches($queryTest, $this->fetchQuery());
$this->assertEquals($bindingsTest, $this->fetchBindings());

//locationPrimary have one where ['is_secondary => 1']
$items = Seller::orderByJoin('locationSecondary.id', 'desc')->get();
$queryTest = 'select sellers.*, MAX("locations"."id") as sort from "sellers"
left join "locations"
on "locations"."seller_id" = "sellers"."id"
and "locations"."is_secondary" = ?
and "locations"."is_secondary" = 1
and "locations"."deleted_at" is null
group by "sellers"."id"
order by sort desc';

$bindingsTest = [1];
$this->assertQueryMatches($queryTest, $this->fetchQuery());
$this->assertEquals($bindingsTest, $this->fetchBindings());

//locationPrimary have one where ['is_primary => 1'] and one orWhere ['is_secondary => 1']
$items = Seller::orderByJoin('locationPrimaryOrSecondary.id', 'desc')->get();
$queryTest = 'select sellers.*, MAX("locations"."id") as sort from "sellers"
left join "locations"
on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
or "locations"."is_secondary" = ?
and "locations"."is_primary" = 1
or "locations"."is_secondary" = 1
and "locations"."deleted_at" is null
group by "sellers"."id"
order by sort desc';

$bindingsTest = [1, 1];
$this->assertQueryMatches($queryTest, $this->fetchQuery());
$this->assertEquals($bindingsTest, $this->fetchBindings());
}

public function testWhereOnRelationWithoutOrderByJoin()
Expand All @@ -74,20 +66,20 @@ public function testWhereOnRelationWithoutOrderByJoin()

$seller->locationPrimary;
$queryTest = 'select * from "locations"
where "locations"."seller_id" = ?
where "locations"."seller_id" = 1
and "locations"."seller_id" is not null
and "is_primary" = ?
and "is_primary" = 1
and "locations"."deleted_at" is null
limit 1';

$this->assertQueryMatches($queryTest, $this->fetchQuery());

$seller->locationPrimary()->where(['is_secondary' => 1])->get();
$queryTest = 'select * from "locations"
where "locations"."seller_id" = ?
where "locations"."seller_id" = 1
and "locations"."seller_id" is not null
and "is_primary" = ?
and ("is_secondary" = ?)
and "is_primary" = 1
and ("is_secondary" = 1)
and "locations"."deleted_at" is null';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
Expand Down
8 changes: 4 additions & 4 deletions tests/Tests/ClosureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testNestOne()
from "order_items"
left join "orders" on "orders"."id" = "order_items"."order_id"
and "orders"."deleted_at" is null
where ("orders"."id" = ? or "orders"."id" = ?)
where ("orders"."id" = 1 or "orders"."id" = 2)
and "order_items"."deleted_at" is null';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
Expand All @@ -42,10 +42,10 @@ public function testNestTwo()
and "orders"."deleted_at" is null
left join "sellers" on "sellers"."id" = "orders"."seller_id"
left join "locations" on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
and "locations"."is_primary" = 1
and "locations"."deleted_at" is null
where ("orders"."id" = ? or "orders"."id" = ?
and ("locations"."id" = ?))
where ("orders"."id" = 1 or "orders"."id" = 2
and ("locations"."id" = 3))
and "order_items"."deleted_at" is null';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
Expand Down
4 changes: 2 additions & 2 deletions tests/Tests/JoinTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testLeftJoin()
left join "cities"
on "cities"."id" = "sellers"."city_id"
and "cities"."deleted_at" is null
where "cities"."name" = ?';
where "cities"."name" = "test"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
Expand All @@ -31,7 +31,7 @@ public function testInnerJoin()
inner join "cities"
on "cities"."id" = "sellers"."city_id"
and "cities"."deleted_at" is null
where "cities"."name" = ?';
where "cities"."name" = "test"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Tests/Relations/BelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testBelongsToHasOne()
$queryTest = 'select orders.* from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
left join "locations" on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
and "locations"."is_primary" = 1
and "locations"."deleted_at" is null
where "orders"."deleted_at" is null
group by "orders"."id"';
Expand Down Expand Up @@ -55,7 +55,7 @@ public function testBelongsToHasOneHasMany()
$queryTest = 'select orders.* from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
left join "locations" on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ? and "locations"."deleted_at" is null
and "locations"."is_primary" = 1 and "locations"."deleted_at" is null
left join "integrations" on "integrations"."location_id" = "locations"."id"
and "integrations"."deleted_at" is null
where "orders"."deleted_at" is null
Expand All @@ -71,10 +71,10 @@ public function testBelongsToHasManyHasOne()
$queryTest = 'select orders.* from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
left join "locations" on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
and "locations"."is_primary" = 1
and "locations"."deleted_at" is null
left join "location_addresses" on "location_addresses"."location_id" = "locations"."id"
and "location_addresses"."is_primary" = ?
and "location_addresses"."is_primary" = 1
and "location_addresses"."deleted_at" is null
where "orders"."deleted_at" is null
group by "orders"."id"';
Expand Down
12 changes: 6 additions & 6 deletions tests/Tests/Relations/HasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function testHasOne()
$queryTest = 'select sellers.*
from "sellers"
left join "locations" on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
and "locations"."is_secondary" = ?
and "locations"."is_primary" = 0
and "locations"."is_secondary" = 0
and "locations"."deleted_at" is null
group by "sellers"."id"';

Expand All @@ -28,8 +28,8 @@ public function testHasOneBelongsTo()

$queryTest = 'select sellers.*
from "sellers" left join "locations" on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
and "locations"."is_secondary" = ?
and "locations"."is_primary" = 0
and "locations"."is_secondary" = 0
and "locations"."deleted_at" is null
left join "cities" on "cities"."id" = "locations"."city_id"
and "cities"."deleted_at" is null
Expand All @@ -45,8 +45,8 @@ public function testHasOneHasMany()
$queryTest = 'select sellers.*
from "sellers"
left join "locations" on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
and "locations"."is_secondary" = ?
and "locations"."is_primary" = 0
and "locations"."is_secondary" = 0
and "locations"."deleted_at" is null
left join "integrations" on "integrations"."location_id" = "locations"."id"
and "integrations"."deleted_at" is null
Expand Down

0 comments on commit df3810f

Please sign in to comment.