|
4 | 4 |
|
5 | 5 | use Parental\Tests\Models\Admin; |
6 | 6 | use Parental\Tests\Models\Car; |
| 7 | +use Parental\Tests\Models\ChildNode; |
7 | 8 | use Parental\Tests\Models\Driver; |
| 9 | +use Parental\Tests\Models\NodeEdge; |
| 10 | +use Parental\Tests\Models\ParentNode; |
8 | 11 | use Parental\Tests\Models\Passenger; |
9 | 12 | use Parental\Tests\Models\Trip; |
10 | 13 | use Parental\Tests\Models\User; |
@@ -71,4 +74,57 @@ function child_is_scoped_when_accessed_from_belongs_to_many() |
71 | 74 | $this->assertCount(1, $trip->cars); |
72 | 75 | $this->assertCount(2, $trip->vehicles); |
73 | 76 | } |
| 77 | + |
| 78 | + /** @test */ |
| 79 | + function child_is_scoped_when_accessed_from_has_one_through() |
| 80 | + { |
| 81 | + // Create root with children |
| 82 | + $rootA = ParentNode::create(['name' => 'Root A']); |
| 83 | + $childA = ChildNode::create(['name' => 'Child 1']); |
| 84 | + $childB = ChildNode::create(['name' => 'Child 2']); |
| 85 | + $childC = ChildNode::create(['name' => 'Child 3']); |
| 86 | + NodeEdge::create(['parent_node_id' => $rootA->id, 'child_node_id' => $childA->id]); |
| 87 | + NodeEdge::create(['parent_node_id' => $rootA->id, 'child_node_id' => $childB->id]); |
| 88 | + |
| 89 | + $this->assertInstanceOf(ParentNode::class, $childA->parent); |
| 90 | + $this->assertInstanceOf(ParentNode::class, $childB->parent); |
| 91 | + $this->assertNull($childC->parent); |
| 92 | + |
| 93 | + $this->assertCount(2, ChildNode::whereHas('parent')->get()); |
| 94 | + $this->assertTrue(ChildNode::whereId($childA->id)->whereHas('parent')->exists()); |
| 95 | + $this->assertTrue(ChildNode::whereId($childB->id)->whereHas('parent')->exists()); |
| 96 | + $this->assertFalse(ChildNode::whereId($childC->id)->whereHas('parent')->exists()); |
| 97 | + } |
| 98 | + |
| 99 | + /** @test */ |
| 100 | + function child_is_scoped_when_accessed_from_has_many_through() |
| 101 | + { |
| 102 | + // Create root with children |
| 103 | + $rootA = ParentNode::create(['name' => 'Root A']); |
| 104 | + $childA = ChildNode::create(['name' => 'Child 1']); |
| 105 | + $childB = ChildNode::create(['name' => 'Child 2']); |
| 106 | + $childC = ChildNode::create(['name' => 'Child 3']); |
| 107 | + NodeEdge::create(['parent_node_id' => $rootA->id, 'child_node_id' => $childA->id]); |
| 108 | + NodeEdge::create(['parent_node_id' => $rootA->id, 'child_node_id' => $childB->id]); |
| 109 | + NodeEdge::create(['parent_node_id' => $rootA->id, 'child_node_id' => $childC->id]); |
| 110 | + |
| 111 | + // Create different root with different children |
| 112 | + $rootB = ParentNode::create(['name' => 'Root B']); |
| 113 | + $childX = ChildNode::create(['name' => 'Child X']); |
| 114 | + NodeEdge::create(['parent_node_id' => $rootB->id, 'child_node_id' => $childX->id]); |
| 115 | + |
| 116 | + // Create different root children any children |
| 117 | + $rootC = ParentNode::create(['name' => 'Root C']); |
| 118 | + |
| 119 | + $this->assertCount(3, $rootA->children); |
| 120 | + $this->assertContainsOnlyInstancesOf(ChildNode::class, $rootA->children); |
| 121 | + |
| 122 | + $this->assertCount(1, $rootB->children); |
| 123 | + $this->assertContainsOnlyInstancesOf(ChildNode::class, $rootB->children); |
| 124 | + |
| 125 | + $this->assertCount(2, ParentNode::whereHas('children')->get()); |
| 126 | + $this->assertTrue(ParentNode::whereId($rootA->id)->whereHas('children')->exists()); |
| 127 | + $this->assertTrue(ParentNode::whereId($rootB->id)->whereHas('children')->exists()); |
| 128 | + $this->assertFalse(ParentNode::whereId($rootC->id)->whereHas('children')->exists()); |
| 129 | + } |
74 | 130 | } |
0 commit comments