-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
EloquentTest.php
137 lines (113 loc) · 3.42 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
<?php
/*
* This file is part of Factory Muffin.
*
* (c) Graham Campbell <graham@alt-three.com>
* (c) Scott Robertson <scottymeuk@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
/**
* This is eloquent test class.
*
* @author Graham Campbell <graham@alt-three.com>
*/
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()
{
$reflection = new ReflectionClass(static::$fm);
$store = $reflection->getProperty('store');
$store->setAccessible(true);
$value = $store->getValue(static::$fm);
$this->assertCount(55, $value->saved());
$this->assertCount(0, $value->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');
}
}