Skip to content

Commit 21c89ea

Browse files
committed
add test and doc
1 parent c3d5037 commit 21c89ea

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ This package adds functionalities to the Eloquent model and Query builder for Mo
2020
- [Configuration](#configuration)
2121
- [Eloquent](#eloquent)
2222
- [Extending the base model](#extending-the-base-model)
23+
- [Extending the Authenticable base model](#extending-the-authenticable-base-model)
2324
- [Soft Deletes](#soft-deletes)
24-
- [Dates](#dates)
2525
- [Guarding attributes](#guarding-attributes)
26+
- [Dates](#dates)
2627
- [Basic Usage](#basic-usage)
2728
- [MongoDB-specific operators](#mongodb-specific-operators)
2829
- [MongoDB-specific Geo operations](#mongodb-specific-geo-operations)
@@ -44,9 +45,10 @@ This package adds functionalities to the Eloquent model and Query builder for Mo
4445
- [Authentication](#authentication)
4546
- [Queues](#queues)
4647
- [Laravel specific](#laravel-specific)
47-
- [Lumen specific](#Lumen-specific)
48+
- [Lumen specific](#lumen-specific)
4849
- [Upgrading](#upgrading)
4950
- [Upgrading from version 2 to 3](#upgrading-from-version-2-to-3)
51+
- [Security contact information](#security-contact-information)
5052

5153
Installation
5254
------------
@@ -356,6 +358,14 @@ $posts = Post::whereBetween('votes', [1, 100])->get();
356358
$users = User::whereNull('age')->get();
357359
```
358360

361+
**whereDate**
362+
363+
```php
364+
$users = User::whereDate('birthday', '2021-5-12')->get();
365+
```
366+
The usage is the same as `whereMonth` / `whereDay` / `whereYear` / `whereTime`
367+
368+
359369
**Advanced wheres**
360370

361371
```php

tests/QueryTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ public function setUp(): void
1818
User::create(['name' => 'Tommy Toe', 'age' => 33, 'title' => 'user']);
1919
User::create(['name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin']);
2020
User::create(['name' => 'Error', 'age' => null, 'title' => null]);
21+
Birthday::create(['name' => 'Mark Moe', 'birthday' => '2020-04-10', 'day' => '10', 'month' => '04', "year" => '2020', 'time' => '10:53:11']);
22+
Birthday::create(['name' => 'Jane Doe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', "year" => '2021', 'time' => '10:53:12']);
23+
Birthday::create(['name' => 'Harry Hoe', 'birthday' => '2021-05-11', 'day' => '11', 'month' => '05', "year" => '2021', 'time' => '10:53:13']);
24+
Birthday::create(['name' => 'Robert Doe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', "year" => '2021', 'time' => '10:53:14']);
25+
Birthday::create(['name' => 'Mark Moe', 'birthday' => '2021-05-12', 'day' => '12', 'month' => '05', "year" => '2021', 'time' => '10:53:15']);
26+
Birthday::create(['name' => 'Mark Moe', 'birthday' => '2022-05-12', 'day' => '12', 'month' => '05', "year" => '2022', 'time' => '10:53:16']);
2127
}
2228

2329
public function tearDown(): void
2430
{
2531
User::truncate();
2632
Scoped::truncate();
33+
Birthday::truncate();
2734
parent::tearDown();
2835
}
2936

@@ -163,6 +170,54 @@ public function testWhereNotNull(): void
163170
$this->assertCount(8, $users);
164171
}
165172

173+
public function testWhereDate(): void
174+
{
175+
$birthdayCount = Birthday::whereDate('birthday', '2021-05-12')->get();
176+
$this->assertCount(3, $birthdayCount);
177+
178+
$birthdayCount = Birthday::whereDate('birthday', '2021-05-11')->get();
179+
$this->assertCount(1, $birthdayCount);
180+
}
181+
182+
public function testWhereDay(): void
183+
{
184+
$day = Birthday::whereDay('day', '12')->get();
185+
$this->assertCount(4, $day);
186+
187+
$day = Birthday::whereDay('day', '11')->get();
188+
$this->assertCount(1, $day);
189+
}
190+
191+
public function testWhereMonth(): void
192+
{
193+
$month = Birthday::whereMonth('month', '04')->get();
194+
$this->assertCount(1, $month);
195+
196+
$month = Birthday::whereMonth('month', '05')->get();
197+
$this->assertCount(5, $month);
198+
}
199+
200+
public function testWhereYear(): void
201+
{
202+
$year = Birthday::whereYear('year', '2021')->get();
203+
$this->assertCount(4, $year);
204+
205+
$year = Birthday::whereYear('year', '2022')->get();
206+
$this->assertCount(1, $year);
207+
208+
$year = Birthday::whereYear('year', '<', '2021')->get();
209+
$this->assertCount(1, $year);
210+
}
211+
212+
public function testWhereTime(): void
213+
{
214+
$time = Birthday::whereTime('time', '10:53:11')->get();
215+
$this->assertCount(1, $time);
216+
217+
$time = Birthday::whereTime('time', '>=', '10:53:14')->get();
218+
$this->assertCount(3, $time);
219+
}
220+
166221
public function testOrder(): void
167222
{
168223
$user = User::whereNotNull('age')->orderBy('age', 'asc')->first();

tests/models/Birthday.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
6+
7+
/**
8+
* Class Birthday.
9+
* @property string $name
10+
* @property string $birthday
11+
* @property string $day
12+
* @property string $month
13+
* @property string $year
14+
* @property string $time
15+
*/
16+
class Birthday extends Eloquent
17+
{
18+
protected $connection = 'mongodb';
19+
protected $collection = 'birthday';
20+
protected $fillable = ['name', 'birthday', 'day', 'month', 'year', 'time'];
21+
}

0 commit comments

Comments
 (0)