forked from thephpleague/factory-muffin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEloquentTest.php
122 lines (99 loc) · 2.98 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
<?php
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
use League\FactoryMuffin\Facade as FactoryMuffin;
/**
* @group eloquent
*/
class EloquentTest extends AbstractTestCase
{
public static function setupBeforeClass()
{
$db = new DB();
$db->addConnection(array(
'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();
FactoryMuffin::seed(5, 'User');
FactoryMuffin::seed(50, 'Cat');
}
public function testNumberOfCats()
{
$cats = array();
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 = array();
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, FactoryMuffin::saved());
$this->assertCount(0, FactoryMuffin::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');
}
}