forked from thephpleague/factory-muffin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EloquentTest.php
138 lines (115 loc) · 3.65 KB
/
EloquentTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* This file is part of Factory Muffin.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
/**
* This is eloquent test class.
*
* @group eloquent
*
* @author Scott Robertson <[email protected]>
* @author Graham Campbell <[email protected]>
* @license <https://github.com/thephpleague/factory-muffin/blob/master/LICENSE> MIT
*/
class EloquentTest extends AbstractTestCase
{
public static function setupBeforeClass()
{
$db = new DB();
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
$db->setAsGlobal();
$db->bootEloquent();
$db->schema()->create('users', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->timestamps();
});
$db->schema()->create('cats', function ($table) {
$table->increments('id');
$table->string('name');
$table->integer('user_id');
});
parent::setupBeforeClass();
static::$fm->seed(5, 'User');
static::$fm->seed(50, 'Cat');
}
public function testNumberOfCats()
{
$cats = [];
foreach (User::all() as $user) {
foreach ($user->cats as $cat) {
$cats[] = $cat;
}
}
$this->assertCount(50, $cats);
$this->assertInstanceOf('Cat', $cats[0]);
}
public function testNumberOfCatOwners()
{
$users = [];
foreach (Cat::all() as $cat) {
$users[] = $cat->user;
}
$this->assertCount(50, $users);
$this->assertCount(5, array_unique($users));
$this->assertInstanceOf('User', $users[0]);
}
public function testUserProperties()
{
$user = User::first();
$this->assertGreaterThan(1, strlen($user->name));
$this->assertGreaterThan(5, strlen($user->email));
$this->assertContains('@', $user->email);
$this->assertContains('.', $user->email);
$this->assertInstanceOf('DateTime', $user->created_at);
$this->assertInstanceOf('DateTime', $user->updated_at);
$this->assertSame((string) $user->created_at, (string) $user->updated_at);
$this->assertFalse($user->xyz == true);
}
public function testCatProperties()
{
$cat = Cat::first();
$this->assertGreaterThan(1, strlen($cat->name));
$this->assertTrue($cat->user_id == true);
$this->assertFalse($cat->created_at == true);
$this->assertFalse($cat->updated_at == true);
$this->assertFalse($cat->xyz == true);
}
public function testSavedObjects()
{
$this->assertCount(55, static::$fm->saved());
$this->assertCount(0, static::$fm->pending());
}
}
class User extends Eloquent
{
public $table = 'users';
public function cats()
{
return $this->hasMany('Cat');
}
}
class Cat extends Eloquent
{
public $timestamps = false;
public $table = 'cats';
public function user()
{
return $this->belongsTo('User');
}
}