|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\CompositeKeys; |
| 4 | + |
| 5 | +use Illuminate\Database\Capsule\Manager as DB; |
| 6 | +use Tests\Models\Task; |
| 7 | +use Tests\TestCase; |
| 8 | + |
| 9 | +class HasOneJsonTest extends TestCase |
| 10 | +{ |
| 11 | + public function testLazyLoading() |
| 12 | + { |
| 13 | + $employee = Task::find(101)->employee; |
| 14 | + |
| 15 | + $this->assertEquals(121, $employee->id); |
| 16 | + } |
| 17 | + |
| 18 | + public function testLazyLoadingWithObjects() |
| 19 | + { |
| 20 | + if (in_array($this->connection, ['sqlite', 'sqlsrv'])) { |
| 21 | + $this->markTestSkipped(); |
| 22 | + } |
| 23 | + |
| 24 | + $employee = Task::find(101)->employeeWithObjects; |
| 25 | + |
| 26 | + $this->assertEquals(121, $employee->id); |
| 27 | + $this->assertEquals(['work_stream' => ['active' => true]], $employee->pivot->getAttributes()); |
| 28 | + } |
| 29 | + |
| 30 | + public function testEmptyLazyLoading() |
| 31 | + { |
| 32 | + DB::enableQueryLog(); |
| 33 | + |
| 34 | + $employee = (new Task())->employee; |
| 35 | + |
| 36 | + $this->assertNull($employee); |
| 37 | + $this->assertEmpty(DB::getQueryLog()); |
| 38 | + } |
| 39 | + |
| 40 | + public function testEagerLoading() |
| 41 | + { |
| 42 | + $tasks = Task::with('employee')->get(); |
| 43 | + |
| 44 | + $this->assertEquals(121, $tasks[0]->employee->id); |
| 45 | + $this->assertEquals(122, $tasks[1]->employee->id); |
| 46 | + $this->assertNull($tasks[5]->employee); |
| 47 | + } |
| 48 | + |
| 49 | + public function testEagerLoadingWithObjects() |
| 50 | + { |
| 51 | + if (in_array($this->connection, ['sqlite', 'sqlsrv'])) { |
| 52 | + $this->markTestSkipped(); |
| 53 | + } |
| 54 | + |
| 55 | + $tasks = Task::with('employeeWithObjects')->get(); |
| 56 | + |
| 57 | + $this->assertEquals(121, $tasks[0]->employeeWithObjects->id); |
| 58 | + $this->assertEquals(122, $tasks[1]->employeeWithObjects->id); |
| 59 | + $this->assertNull($tasks[5]->employeeWithObjects); |
| 60 | + $this->assertEquals(['work_stream' => ['active' => true]], $tasks[0]->employeeWithObjects->pivot->getAttributes()); |
| 61 | + } |
| 62 | + |
| 63 | + public function testLazyEagerLoading() |
| 64 | + { |
| 65 | + $tasks = Task::all()->load('employee'); |
| 66 | + |
| 67 | + $this->assertEquals(121, $tasks[0]->employee->id); |
| 68 | + $this->assertEquals(122, $tasks[1]->employee->id); |
| 69 | + $this->assertNull($tasks[5]->employee); |
| 70 | + } |
| 71 | + |
| 72 | + public function testLazyEagerLoadingWithObjects() |
| 73 | + { |
| 74 | + if (in_array($this->connection, ['sqlite', 'sqlsrv'])) { |
| 75 | + $this->markTestSkipped(); |
| 76 | + } |
| 77 | + |
| 78 | + $tasks = Task::all()->load('employeeWithObjects'); |
| 79 | + |
| 80 | + $this->assertEquals(121, $tasks[0]->employeeWithObjects->id); |
| 81 | + $this->assertEquals(122, $tasks[1]->employeeWithObjects->id); |
| 82 | + $this->assertNull($tasks[5]->employeeWithObjects); |
| 83 | + $this->assertEquals(['work_stream' => ['active' => true]], $tasks[0]->employeeWithObjects->pivot->getAttributes()); |
| 84 | + } |
| 85 | +} |
0 commit comments