|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace BoredProgrammers\LaravelEloquentMappable\Tests\Unit; |
| 4 | + |
| 5 | +use BoredProgrammers\LaravelEloquentMappable\Tests\TestCase; |
| 6 | +use Illuminate\Database\Query\JoinClause; |
| 7 | +use Workbench\App\Models\Post; |
| 8 | + |
| 9 | +class QueryBuilderTest extends TestCase |
| 10 | +{ |
| 11 | + |
| 12 | + public function test_where() |
| 13 | + { |
| 14 | + $this->assertEquals( |
| 15 | + "select * from \"posts\" where \"title\" = 'asd'", |
| 16 | + Post::whereMappedTitle('asd')->toRawSql() |
| 17 | + ); |
| 18 | + |
| 19 | + $this->assertEquals( |
| 20 | + "select * from \"posts\" where \"title\" = 'asd'", |
| 21 | + Post::where('mapped_title', 'asd') |
| 22 | + ->toRawSql() |
| 23 | + ); |
| 24 | + |
| 25 | + $this->assertEquals( |
| 26 | + "select * from \"posts\" where \"title\" != 'asd'", |
| 27 | + Post::where('mapped_title', '!=', 'asd') |
| 28 | + ->toRawSql() |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + public function test_or_where() |
| 33 | + { |
| 34 | + $this->assertEquals( |
| 35 | + "select * from \"posts\" where \"title\" = 'asd' or \"title\" like '%testing%'", |
| 36 | + Post::where('mapped_title', 'asd') |
| 37 | + ->orWhere('mapped_title', 'like', '%testing%') |
| 38 | + ->toRawSql() |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + public function test_where_not_null() |
| 43 | + { |
| 44 | + $this->assertEquals( |
| 45 | + "select * from \"posts\" where \"title\" is not null", |
| 46 | + Post::whereNotNull('mapped_title') |
| 47 | + ->toRawSql() |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function test_join() |
| 52 | + { |
| 53 | + $this->assertEquals( |
| 54 | + "select * from \"posts\"" |
| 55 | + . " inner join \"user_accounts\" on \"user_accounts\".\"id\" = \"posts\".\"title\"" |
| 56 | + . " where \"title\" is not null", |
| 57 | + Post::whereNotNull('mapped_title') |
| 58 | + ->join('user_accounts', 'user_accounts.id', '=', 'posts.mapped_title') |
| 59 | + ->toRawSql() |
| 60 | + ); |
| 61 | + |
| 62 | + $this->assertEquals( |
| 63 | + "select * from \"posts\" inner join (\"user_accounts\" as \"ua1\"" |
| 64 | + . " inner join \"user_accounts\" as \"ua2\" on \"ua2\".\"id\" = \"posts\".\"title\")" |
| 65 | + . " where \"title\" is not null", |
| 66 | + Post::whereNotNull('mapped_title') |
| 67 | + ->join('user_accounts as ua1', function (JoinClause $join) { |
| 68 | + $join->join('user_accounts as ua2', 'ua2.id', '=', 'posts.mapped_title'); |
| 69 | + }) |
| 70 | + ->toRawSql() |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments