Skip to content

Commit f807caf

Browse files
committed
update PSR + new test for loadLimit
1 parent 325bfd2 commit f807caf

5 files changed

+84
-24
lines changed

tests/LaravelSubQueryWithAvgTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,9 @@ public function testSortingScopes()
126126

127127
$result = Invoice::withAvg('items:price')->toSql();
128128

129-
$this->assertSame(
130-
'select "invoices".*, (select avg(price) from "items"
131-
where "invoices"."id" = "items"."invoice_id") as "items_price_avg"
132-
from "invoices"',
133-
$result
134-
);
129+
$query = 'select "invoices".*, (select avg(price) from "items"';
130+
$query .= ' where "invoices"."id" = "items"."invoice_id") as "items_price_avg"';
131+
$query .= ' from "invoices"';
132+
$this->assertSame($query, $result);
135133
}
136134
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Alexmg86\LaravelSubQuery\Tests;
4+
5+
use Alexmg86\LaravelSubQuery\Facades\LaravelSubQuery;
6+
use Alexmg86\LaravelSubQuery\ServiceProvider;
7+
use Alexmg86\LaravelSubQuery\Tests\Models\Good;
8+
use Alexmg86\LaravelSubQuery\Tests\Models\Invoice;
9+
use Alexmg86\LaravelSubQuery\Tests\Models\Item;
10+
11+
class LaravelSubQueryWithLimitTest extends DatabaseTestCase
12+
{
13+
protected function getPackageProviders($app)
14+
{
15+
return [ServiceProvider::class];
16+
}
17+
18+
protected function getPackageAliases($app)
19+
{
20+
return [
21+
'laravel-sub-query' => LaravelSubQuery::class,
22+
];
23+
}
24+
25+
private function createBasic()
26+
{
27+
for ($id = 1; $id < 5; $id++) {
28+
$invoice = Invoice::create(['id' => $id, 'name' => 'text_name']);
29+
30+
for ($i = 1; $i < 11; $i++) {
31+
Item::create(['invoice_id' => $id, 'price' => $i, 'price2' => $i + 1]);
32+
Good::create(['invoice_id' => $id, 'price' => $i, 'price2' => $i + 1]);
33+
}
34+
}
35+
}
36+
37+
public function testBasic()
38+
{
39+
$this->createBasic();
40+
41+
$results = Invoice::all();
42+
$results->loadLimit('items:1', 'goods:2');
43+
44+
foreach ($results as $result) {
45+
$this->assertEquals(count($result->items), 1);
46+
$this->assertEquals(count($result->goods), 2);
47+
}
48+
}
49+
50+
public function testWithConditions()
51+
{
52+
$this->createBasic();
53+
54+
$results = Invoice::all();
55+
$results->loadLimit(['items:1', 'goods:2' => function ($query) {
56+
$query->orderBy('id', 'desc')->where('price', '<', 6);
57+
}]);
58+
59+
foreach ($results as $result) {
60+
$this->assertEquals(count($result->items), 1);
61+
$this->assertEquals(count($result->goods), 2);
62+
63+
foreach ($result->goods as $good) {
64+
$this->assertLessThan(6, $good->price);
65+
}
66+
}
67+
}
68+
}

tests/LaravelSubQueryWithMaxTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,9 @@ public function testSortingScopes()
126126

127127
$result = Invoice::withMax('items:price')->toSql();
128128

129-
$this->assertSame(
130-
'select "invoices".*, (select max(price) from "items"
131-
where "invoices"."id" = "items"."invoice_id") as "items_price_max"
132-
from "invoices"',
133-
$result
134-
);
129+
$query = 'select "invoices".*, (select max(price) from "items"';
130+
$query .= ' where "invoices"."id" = "items"."invoice_id") as "items_price_max"';
131+
$query .= ' from "invoices"';
132+
$this->assertSame($query, $result);
135133
}
136134
}

tests/LaravelSubQueryWithMinTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@ public function testSortingScopes()
120120

121121
$result = Invoice::withMin('items:price')->toSql();
122122

123-
$this->assertSame(
124-
'select "invoices".*, (select min(price) from "items"
125-
where "invoices"."id" = "items"."invoice_id") as "items_price_min"
126-
from "invoices"',
127-
$result
128-
);
123+
$query = 'select "invoices".*, (select min(price) from "items"';
124+
$query .= ' where "invoices"."id" = "items"."invoice_id") as "items_price_min"';
125+
$query .= ' from "invoices"';
126+
$this->assertSame($query, $result);
129127
}
130128
}

tests/LaravelSubQueryWithSumTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,9 @@ public function testSortingScopes()
126126

127127
$result = Invoice::withSum('items:price')->toSql();
128128

129-
$this->assertSame(
130-
'select "invoices".*, (select sum(price) from "items"
131-
where "invoices"."id" = "items"."invoice_id") as "items_price_sum"
132-
from "invoices"',
133-
$result
134-
);
129+
$query = 'select "invoices".*, (select sum(price) from "items"';
130+
$query .= ' where "invoices"."id" = "items"."invoice_id") as "items_price_sum"';
131+
$query .= ' from "invoices"';
132+
$this->assertSame($query, $result);
135133
}
136134
}

0 commit comments

Comments
 (0)