|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Alexmg86\LaravelSubQuery\Tests; |
| 4 | + |
| 5 | +use Alexmg86\LaravelSubQuery\Facades\LaravelSubQuery; |
| 6 | +use Alexmg86\LaravelSubQuery\ServiceProvider; |
| 7 | +use Illuminate\Database\Eloquent\Builder; |
| 8 | +use Illuminate\Database\Schema\Blueprint; |
| 9 | +use Illuminate\Support\Facades\Schema; |
| 10 | + |
| 11 | +class LaravelSubQueryWithAvgTest 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 | + protected function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + Schema::create('invoices', function (Blueprint $table) { |
| 30 | + $table->increments('id'); |
| 31 | + $table->string('name', 100); |
| 32 | + }); |
| 33 | + |
| 34 | + Schema::create('items', function (Blueprint $table) { |
| 35 | + $table->increments('id'); |
| 36 | + $table->integer('invoice_id'); |
| 37 | + $table->integer('price'); |
| 38 | + $table->integer('price2'); |
| 39 | + }); |
| 40 | + |
| 41 | + Schema::create('goods', function (Blueprint $table) { |
| 42 | + $table->increments('id'); |
| 43 | + $table->integer('invoice_id'); |
| 44 | + $table->integer('price'); |
| 45 | + $table->integer('price2'); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + public function testBasic() |
| 50 | + { |
| 51 | + $invoice = Invoice::create(['id' => 1, 'name' => 'text_name']); |
| 52 | + for ($i = 1; $i < 11; $i++) { |
| 53 | + Item::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]); |
| 54 | + Good::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]); |
| 55 | + } |
| 56 | + |
| 57 | + $results = Invoice::withAvg('items:price,price2'); |
| 58 | + |
| 59 | + $this->assertEquals([ |
| 60 | + ['id' => 1, 'name' => 'text_name', 'items_price_avg' => 5.5, 'items_price2_avg' => 6.5], |
| 61 | + ], $results->get()->toArray()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testWithConditions() |
| 65 | + { |
| 66 | + $invoice = Invoice::create(['id' => 1, 'name' => 'text_name']); |
| 67 | + for ($i = 1; $i < 11; $i++) { |
| 68 | + Item::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]); |
| 69 | + Good::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]); |
| 70 | + } |
| 71 | + |
| 72 | + $results = Invoice::withAvg(['items:price', 'goods:price,price2' => function (Builder $query) { |
| 73 | + $query->where('price', '>', 6); |
| 74 | + }]); |
| 75 | + |
| 76 | + $this->assertEquals([ |
| 77 | + ['id' => 1, 'name' => 'text_name', 'items_price_avg' => 5.5, 'goods_price_avg' => 8.5, 'goods_price2_avg' => 9.5], |
| 78 | + ], $results->get()->toArray()); |
| 79 | + } |
| 80 | + |
| 81 | + public function testWithSelect() |
| 82 | + { |
| 83 | + $invoice = Invoice::create(['id' => 1, 'name' => 'text_name']); |
| 84 | + for ($i = 1; $i < 11; $i++) { |
| 85 | + Item::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]); |
| 86 | + } |
| 87 | + |
| 88 | + $results = Invoice::select(['id'])->withAvg('items:price'); |
| 89 | + |
| 90 | + $this->assertEquals([ |
| 91 | + ['id' => 1, 'items_price_avg' => 5.5], |
| 92 | + ], $results->get()->toArray()); |
| 93 | + } |
| 94 | + |
| 95 | + public function testLoadAvg() |
| 96 | + { |
| 97 | + $invoice = Invoice::create(['id' => 1, 'name' => 'text_name']); |
| 98 | + for ($i = 1; $i < 11; $i++) { |
| 99 | + Item::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]); |
| 100 | + } |
| 101 | + |
| 102 | + $results = Invoice::first(); |
| 103 | + $results->loadAvg('items:price'); |
| 104 | + |
| 105 | + $this->assertEquals(['id' => 1, 'name' => 'text_name', 'items_price_avg' => 5.5], $results->toArray()); |
| 106 | + } |
| 107 | + |
| 108 | + public function testLoadAvgWithConditions() |
| 109 | + { |
| 110 | + $invoice = Invoice::create(['id' => 1, 'name' => 'text_name']); |
| 111 | + for ($i = 1; $i < 11; $i++) { |
| 112 | + Item::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]); |
| 113 | + } |
| 114 | + |
| 115 | + $results = Invoice::first(); |
| 116 | + $results->loadAvg(['items:price' => function ($query) { |
| 117 | + $query->where('price', '>', 5); |
| 118 | + }]); |
| 119 | + |
| 120 | + $this->assertEquals(['id' => 1, 'name' => 'text_name', 'items_price_avg' => 8.0], $results->toArray()); |
| 121 | + } |
| 122 | + |
| 123 | + public function testGlobalScopes() |
| 124 | + { |
| 125 | + $invoice = Invoice::create(['id' => 1, 'name' => 'text_name']); |
| 126 | + for ($i = 1; $i < 11; $i++) { |
| 127 | + Good::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]); |
| 128 | + } |
| 129 | + |
| 130 | + $result = Invoice::withAvg('goods:price')->first(); |
| 131 | + $this->assertEquals(8.0, $result->goods_price_avg); |
| 132 | + |
| 133 | + $result = Invoice::withAvg('allGoods:price')->first(); |
| 134 | + $this->assertEquals(5.5, $result->all_goods_price_avg); |
| 135 | + } |
| 136 | + |
| 137 | + public function testSortingScopes() |
| 138 | + { |
| 139 | + $invoice = Invoice::create(['id' => 1, 'name' => 'text_name']); |
| 140 | + for ($i = 1; $i < 11; $i++) { |
| 141 | + Item::create(['invoice_id' => $invoice->id, 'price' => $i, 'price2' => $i + 1]); |
| 142 | + } |
| 143 | + |
| 144 | + $result = Invoice::withAvg('items:price')->toSql(); |
| 145 | + |
| 146 | + $this->assertSame('select "invoices".*, (select avg(price) from "items" where "invoices"."id" = "items"."invoice_id") as "items_price_avg" from "invoices"', $result); |
| 147 | + } |
| 148 | +} |
0 commit comments