|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kirschbaum\PowerJoins\Tests; |
| 4 | + |
| 5 | +use Kirschbaum\PowerJoins\Tests\Models\Comment; |
| 6 | +use Kirschbaum\PowerJoins\Tests\Models\Post; |
| 7 | +use Kirschbaum\PowerJoins\Tests\Models\User; |
| 8 | + |
| 9 | +class FromRawWithJoinRelationshipTest extends TestCase |
| 10 | +{ |
| 11 | + /** @test */ |
| 12 | + public function test_left_join_relationship_with_from_raw_cte() |
| 13 | + { |
| 14 | + $user = factory(User::class)->create(['name' => 'John Doe']); |
| 15 | + factory(Post::class)->create([ |
| 16 | + 'user_id' => $user->id, |
| 17 | + 'title' => 'Test Post', |
| 18 | + 'published' => true, |
| 19 | + ]); |
| 20 | + |
| 21 | + // Using fromRaw with a CTE (Common Table Expression) |
| 22 | + $query = Post::fromRaw('( |
| 23 | + WITH active_posts AS ( |
| 24 | + SELECT * FROM posts WHERE published = 1 |
| 25 | + ) |
| 26 | + SELECT * FROM active_posts |
| 27 | + ) as posts') |
| 28 | + ->leftJoinRelationship('user'); |
| 29 | + |
| 30 | + // Should not throw TypeError |
| 31 | + $result = $query->get(); |
| 32 | + |
| 33 | + $this->assertCount(1, $result); |
| 34 | + $this->assertEquals('Test Post', $result->first()->title); |
| 35 | + } |
| 36 | + |
| 37 | + /** @test */ |
| 38 | + public function test_join_relationship_with_from_raw_subquery() |
| 39 | + { |
| 40 | + $user = factory(User::class)->create(['name' => 'Jane Doe']); |
| 41 | + factory(Post::class)->create([ |
| 42 | + 'user_id' => $user->id, |
| 43 | + 'title' => 'Another Test', |
| 44 | + 'published' => true, |
| 45 | + ]); |
| 46 | + |
| 47 | + // Using fromRaw with a subquery |
| 48 | + $query = Post::fromRaw('( |
| 49 | + SELECT * FROM posts WHERE published = 1 |
| 50 | + ) as posts') |
| 51 | + ->joinRelationship('user'); |
| 52 | + |
| 53 | + // Should not throw TypeError |
| 54 | + $result = $query->get(); |
| 55 | + |
| 56 | + $this->assertCount(1, $result); |
| 57 | + $this->assertEquals('Another Test', $result->first()->title); |
| 58 | + } |
| 59 | + |
| 60 | + /** @test */ |
| 61 | + public function test_order_by_left_power_joins_with_from_raw() |
| 62 | + { |
| 63 | + $user = factory(User::class)->create(['name' => 'Alice']); |
| 64 | + factory(Post::class)->create([ |
| 65 | + 'user_id' => $user->id, |
| 66 | + 'title' => 'First Post', |
| 67 | + 'published' => true, |
| 68 | + ]); |
| 69 | + |
| 70 | + // Using fromRaw with orderByLeftPowerJoins |
| 71 | + $query = Post::fromRaw('( |
| 72 | + SELECT * FROM posts WHERE published = 1 |
| 73 | + ) as posts') |
| 74 | + ->orderByLeftPowerJoins('user.name', 'asc'); |
| 75 | + |
| 76 | + // Should not throw TypeError |
| 77 | + $result = $query->get(); |
| 78 | + |
| 79 | + $this->assertCount(1, $result); |
| 80 | + $this->assertEquals('First Post', $result->first()->title); |
| 81 | + } |
| 82 | + |
| 83 | + /** @test */ |
| 84 | + public function test_right_join_relationship_with_from_raw() |
| 85 | + { |
| 86 | + $user = factory(User::class)->create(['name' => 'Bob']); |
| 87 | + factory(Post::class)->create([ |
| 88 | + 'user_id' => $user->id, |
| 89 | + 'title' => 'Right Join Test', |
| 90 | + 'published' => true, |
| 91 | + ]); |
| 92 | + |
| 93 | + // Using fromRaw with rightJoinRelationship |
| 94 | + $query = Post::fromRaw('( |
| 95 | + SELECT * FROM posts WHERE published = 1 |
| 96 | + ) as posts') |
| 97 | + ->rightJoinRelationship('user'); |
| 98 | + |
| 99 | + // Should not throw TypeError |
| 100 | + $result = $query->get(); |
| 101 | + |
| 102 | + $this->assertGreaterThanOrEqual(1, $result->count()); |
| 103 | + } |
| 104 | + |
| 105 | + /** @test */ |
| 106 | + public function test_nested_join_relationship_with_from_raw() |
| 107 | + { |
| 108 | + $user = factory(User::class)->create(['name' => 'Charlie']); |
| 109 | + $post = factory(Post::class)->create([ |
| 110 | + 'user_id' => $user->id, |
| 111 | + 'title' => 'Nested Test', |
| 112 | + 'published' => true, |
| 113 | + ]); |
| 114 | + factory(Comment::class)->create([ |
| 115 | + 'post_id' => $post->id, |
| 116 | + 'user_id' => $user->id, |
| 117 | + 'body' => 'Test comment', |
| 118 | + ]); |
| 119 | + |
| 120 | + // Using fromRaw with nested relationship |
| 121 | + $query = Post::fromRaw('( |
| 122 | + SELECT * FROM posts WHERE published = 1 |
| 123 | + ) as posts') |
| 124 | + ->leftJoinRelationship('comments.user'); |
| 125 | + |
| 126 | + // Should not throw TypeError |
| 127 | + $result = $query->get(); |
| 128 | + |
| 129 | + $this->assertCount(1, $result); |
| 130 | + $this->assertEquals('Nested Test', $result->first()->title); |
| 131 | + } |
| 132 | +} |
0 commit comments