|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Entity; |
| 4 | + |
| 5 | +use BookStack\Entities\Models\Book; |
| 6 | +use Illuminate\Database\Eloquent\Builder; |
| 7 | +use Tests\TestCase; |
| 8 | + |
| 9 | +class EntityQueryTest extends TestCase |
| 10 | +{ |
| 11 | + public function test_basic_entity_query_has_join_and_type_applied() |
| 12 | + { |
| 13 | + $query = Book::query(); |
| 14 | + $expected = 'select * from `entities` left join `entity_container_data` on `entity_container_data`.`entity_id` = `entities`.`id` and `entity_container_data`.`entity_type` = ? where `type` = ? and `entities`.`deleted_at` is null'; |
| 15 | + $this->assertEquals($expected, $query->toSql()); |
| 16 | + $this->assertEquals(['book', 'book'], $query->getBindings()); |
| 17 | + } |
| 18 | + |
| 19 | + public function test_joins_in_sub_queries_use_alias_names() |
| 20 | + { |
| 21 | + $query = Book::query()->whereHas('chapters', function (Builder $query) { |
| 22 | + $query->where('name', '=', 'a'); |
| 23 | + }); |
| 24 | + |
| 25 | + // Probably from type limits on relation where not needed? |
| 26 | + $expected = 'select * from `entities` left join `entity_container_data` on `entity_container_data`.`entity_id` = `entities`.`id` and `entity_container_data`.`entity_type` = ? where exists (select * from `entities` as `laravel_reserved_%d` left join `entity_container_data` on `entity_container_data`.`entity_id` = `laravel_reserved_%d`.`id` and `entity_container_data`.`entity_type` = ? where `entities`.`id` = `laravel_reserved_%d`.`book_id` and `name` = ? and `type` = ? and `laravel_reserved_%d`.`deleted_at` is null) and `type` = ? and `entities`.`deleted_at` is null'; |
| 27 | + $this->assertStringMatchesFormat($expected, $query->toSql()); |
| 28 | + $this->assertEquals(['book', 'chapter', 'a', 'chapter', 'book'], $query->getBindings()); |
| 29 | + } |
| 30 | + |
| 31 | + public function test_book_chapter_relation_applies_type_condition() |
| 32 | + { |
| 33 | + $book = $this->entities->book(); |
| 34 | + $query = $book->chapters(); |
| 35 | + $expected = 'select * from `entities` left join `entity_container_data` on `entity_container_data`.`entity_id` = `entities`.`id` and `entity_container_data`.`entity_type` = ? where `entities`.`book_id` = ? and `entities`.`book_id` is not null and `type` = ? and `entities`.`deleted_at` is null'; |
| 36 | + $this->assertEquals($expected, $query->toSql()); |
| 37 | + $this->assertEquals(['chapter', $book->id, 'chapter'], $query->getBindings()); |
| 38 | + |
| 39 | + $query = Book::query()->whereHas('chapters'); |
| 40 | + $expected = 'select * from `entities` left join `entity_container_data` on `entity_container_data`.`entity_id` = `entities`.`id` and `entity_container_data`.`entity_type` = ? where exists (select * from `entities` as `laravel_reserved_%d` left join `entity_container_data` on `entity_container_data`.`entity_id` = `laravel_reserved_%d`.`id` and `entity_container_data`.`entity_type` = ? where `entities`.`id` = `laravel_reserved_%d`.`book_id` and `type` = ? and `laravel_reserved_%d`.`deleted_at` is null) and `type` = ? and `entities`.`deleted_at` is null'; |
| 41 | + $this->assertStringMatchesFormat($expected, $query->toSql()); |
| 42 | + $this->assertEquals(['book', 'chapter', 'chapter', 'book'], $query->getBindings()); |
| 43 | + } |
| 44 | +} |
0 commit comments