|
9 | 9 | use Illuminate\Support\Facades\Date;
|
10 | 10 | use Illuminate\Support\Facades\DB;
|
11 | 11 | use Illuminate\Support\LazyCollection;
|
| 12 | +use Illuminate\Support\Str; |
12 | 13 | use Illuminate\Testing\Assert;
|
13 | 14 | use MongoDB\BSON\ObjectId;
|
14 | 15 | use MongoDB\BSON\Regex;
|
@@ -895,4 +896,64 @@ public function testCursor()
|
895 | 896 | $this->assertEquals($data[$i]['name'], $result['name']);
|
896 | 897 | }
|
897 | 898 | }
|
| 899 | + |
| 900 | + public function testStringableColumn() |
| 901 | + { |
| 902 | + DB::collection('users')->insert([ |
| 903 | + ['name' => 'Jane Doe', 'age' => 36, 'birthday' => new UTCDateTime(new \DateTime('1987-01-01 00:00:00'))], |
| 904 | + ['name' => 'John Doe', 'age' => 28, 'birthday' => new UTCDateTime(new \DateTime('1995-01-01 00:00:00'))], |
| 905 | + ]); |
| 906 | + |
| 907 | + $nameColumn = Str::of('name'); |
| 908 | + $this->assertInstanceOf(\Stringable::class, $nameColumn, 'Ensure we are testing the feature with a Stringable instance'); |
| 909 | + |
| 910 | + $user = DB::collection('users')->where($nameColumn, 'John Doe')->first(); |
| 911 | + $this->assertEquals('John Doe', $user['name']); |
| 912 | + |
| 913 | + // Test this other document to be sure this is not a random success to data order |
| 914 | + $user = DB::collection('users')->where($nameColumn, 'Jane Doe')->orderBy('natural')->first(); |
| 915 | + $this->assertEquals('Jane Doe', $user['name']); |
| 916 | + |
| 917 | + // With an operator |
| 918 | + $user = DB::collection('users')->where($nameColumn, '!=', 'Jane Doe')->first(); |
| 919 | + $this->assertEquals('John Doe', $user['name']); |
| 920 | + |
| 921 | + // whereIn and whereNotIn |
| 922 | + $user = DB::collection('users')->whereIn($nameColumn, ['John Doe'])->first(); |
| 923 | + $this->assertEquals('John Doe', $user['name']); |
| 924 | + |
| 925 | + $user = DB::collection('users')->whereNotIn($nameColumn, ['John Doe'])->first(); |
| 926 | + $this->assertEquals('Jane Doe', $user['name']); |
| 927 | + |
| 928 | + // whereBetween and whereNotBetween |
| 929 | + $ageColumn = Str::of('age'); |
| 930 | + $user = DB::collection('users')->whereBetween($ageColumn, [30, 40])->first(); |
| 931 | + $this->assertEquals('Jane Doe', $user['name']); |
| 932 | + |
| 933 | + // whereBetween and whereNotBetween |
| 934 | + $ageColumn = Str::of('age'); |
| 935 | + $user = DB::collection('users')->whereNotBetween($ageColumn, [30, 40])->first(); |
| 936 | + $this->assertEquals('John Doe', $user['name']); |
| 937 | + |
| 938 | + // whereDate |
| 939 | + $birthdayColumn = Str::of('birthday'); |
| 940 | + $user = DB::collection('users')->whereDate($birthdayColumn, '1995-01-01')->first(); |
| 941 | + $this->assertEquals('John Doe', $user['name']); |
| 942 | + |
| 943 | + $user = DB::collection('users')->whereDate($birthdayColumn, '<', '1990-01-01') |
| 944 | + ->orderBy($birthdayColumn, 'desc')->first(); |
| 945 | + $this->assertEquals('Jane Doe', $user['name']); |
| 946 | + |
| 947 | + $user = DB::collection('users')->whereDate($birthdayColumn, '>', '1990-01-01') |
| 948 | + ->orderBy($birthdayColumn, 'asc')->first(); |
| 949 | + $this->assertEquals('John Doe', $user['name']); |
| 950 | + |
| 951 | + $user = DB::collection('users')->whereDate($birthdayColumn, '!=', '1987-01-01')->first(); |
| 952 | + $this->assertEquals('John Doe', $user['name']); |
| 953 | + |
| 954 | + // increment |
| 955 | + DB::collection('users')->where($ageColumn, 28)->increment($ageColumn, 1); |
| 956 | + $user = DB::collection('users')->where($ageColumn, 29)->first(); |
| 957 | + $this->assertEquals('John Doe', $user['name']); |
| 958 | + } |
898 | 959 | }
|
0 commit comments