1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Capsule\Manager; |
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
5
|
|
|
use Rinvex\Tests\Stubs\EloquentPost; |
6
|
|
|
use Rinvex\Tests\Stubs\EloquentPostRepository; |
7
|
|
|
use Rinvex\Tests\Stubs\EloquentUser; |
8
|
|
|
use Rinvex\Tests\Stubs\EloquentUserRepository; |
9
|
|
|
|
10
|
|
|
abstract class AbstractEloquentTests extends PHPUnit_Framework_TestCase |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
use \Illuminate\Support\Traits\CapsuleManagerTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var \Illuminate\Container\Container |
16
|
|
|
*/ |
17
|
|
|
protected $container; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Setup the database schema. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
$this->setupContainer(); |
27
|
|
|
$this->setupDatabase(new Manager($this->getContainer())); |
|
|
|
|
28
|
|
|
$this->migrate(); |
29
|
|
|
$this->seed(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Setup the IoC container instance. |
34
|
|
|
*/ |
35
|
|
|
protected function setupContainer() |
36
|
|
|
{ |
37
|
|
|
$config = [ |
38
|
|
|
'models' => 'Models', |
39
|
|
|
'cache' => [ |
40
|
|
|
'keys_file' => '', |
41
|
|
|
'lifetime' => 0, |
42
|
|
|
'clear_on' => [ |
43
|
|
|
'create', |
44
|
|
|
'update', |
45
|
|
|
'delete', |
46
|
|
|
], |
47
|
|
|
'skip_uri' => 'skipCache', |
48
|
|
|
], |
49
|
|
|
]; |
50
|
|
|
$this->container = new \Illuminate\Container\Container(); |
51
|
|
|
$this->container->instance('config', new \Illuminate\Config\Repository()); |
52
|
|
|
$this->getContainer()['config']->offsetSet('rinvex.repository', $config); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function setupDatabase(Manager $db) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$db->addConnection([ |
58
|
|
|
'driver' => 'sqlite', |
59
|
|
|
'database' => ':memory:', |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
$db->bootEloquent(); |
63
|
|
|
$db->setAsGlobal(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create tables. |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
protected function migrate() |
72
|
|
|
{ |
73
|
|
|
$this->schema()->create('users', function ($table) { |
74
|
|
|
$table->increments('id'); |
75
|
|
|
$table->string('name')->nullable(); |
76
|
|
|
$table->string('email'); |
77
|
|
|
$table->integer('age'); |
78
|
|
|
$table->timestamps(); |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
$this->schema()->create('posts', function ($table) { |
82
|
|
|
$table->increments('id'); |
83
|
|
|
$table->integer('user_id'); |
84
|
|
|
$table->integer('parent_id')->nullable(); |
85
|
|
|
$table->string('name'); |
86
|
|
|
$table->timestamps(); |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Create test users and posts. |
92
|
|
|
*/ |
93
|
|
|
protected function seed() |
94
|
|
|
{ |
95
|
|
|
$evsign = EloquentUser::create(['name' => 'evsign', 'email' => '[email protected]', 'age' => '25']); |
96
|
|
|
$omranic = EloquentUser::create(['name' => 'omranic', 'email' => '[email protected]', 'age' => '26']); |
97
|
|
|
$ionut = EloquentUser::create(['name' => 'ionut', 'email' => '[email protected]', 'age' => '24']); |
98
|
|
|
$anotherIonut = EloquentUser::create(['name' => 'ionut', 'email' => '[email protected]', 'age' => '28']); |
99
|
|
|
|
100
|
|
|
$evsign->posts()->saveMany([ |
101
|
|
|
new EloquentPost(['name' => 'first post']), |
102
|
|
|
new EloquentPost(['name' => 'second post']), |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
$omranic->posts()->saveMany([ |
106
|
|
|
new EloquentPost(['name' => 'third post']), |
107
|
|
|
new EloquentPost(['name' => 'fourth post']), |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
$ionut->posts()->saveMany([ |
111
|
|
|
new EloquentPost(['name' => 'fifth post']), |
112
|
|
|
new EloquentPost(['name' => 'sixth post']), |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
$anotherIonut->posts()->saveMany([ |
116
|
|
|
new EloquentPost(['name' => 'seventh post']), |
117
|
|
|
new EloquentPost(['name' => 'eighth post']), |
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get Schema Builder. |
123
|
|
|
* |
124
|
|
|
* @return \Illuminate\Database\Schema\Builder |
125
|
|
|
*/ |
126
|
|
|
protected function schema() |
127
|
|
|
{ |
128
|
|
|
return Model::resolveConnection()->getSchemaBuilder(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Tear down the database schema. |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
public function tearDown() |
137
|
|
|
{ |
138
|
|
|
$this->schema()->drop('users'); |
139
|
|
|
$this->schema()->drop('posts'); |
140
|
|
|
unset($this->container); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return EloquentUserRepository |
145
|
|
|
*/ |
146
|
|
|
protected function userRepository() |
147
|
|
|
{ |
148
|
|
|
return (new EloquentUserRepository()) |
149
|
|
|
->setContainer($this->getContainer()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return EloquentPostRepository |
154
|
|
|
*/ |
155
|
|
|
protected function postRepository() |
156
|
|
|
{ |
157
|
|
|
return (new EloquentPostRepository()) |
158
|
|
|
->setContainer(new \Illuminate\Container\Container()); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.