-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathTestCase.php
109 lines (91 loc) · 3.09 KB
/
TestCase.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
<?php
declare(strict_types=1);
namespace Thomasjohnkane\Snooze\Tests;
use File;
use Illuminate\Database\Schema\Blueprint;
use Orchestra\Testbench\TestCase as Orchestra;
use Thomasjohnkane\Snooze\Facades\Snooze;
use Thomasjohnkane\Snooze\ServiceProvider;
use Thomasjohnkane\Snooze\Tests\Models\Child;
use Thomasjohnkane\Snooze\Tests\Models\User;
class TestCase extends Orchestra
{
public function setUp(): void
{
parent::setUp();
$this->initializeDirectory($this->getTempDirectory());
$this->setUpDatabase($this->app);
}
protected function getPackageProviders($app)
{
return [ServiceProvider::class];
}
protected function getPackageAliases($app)
{
return [
'snooze' => Snooze::class,
];
}
public function getTempDirectory($suffix = '')
{
return __DIR__.DIRECTORY_SEPARATOR.'temp'.($suffix == '' ? '' : DIRECTORY_SEPARATOR.$suffix);
}
protected function initializeDirectory($directory)
{
if (File::isDirectory($directory)) {
File::deleteDirectory($directory);
}
File::makeDirectory($directory);
}
/**
* @param \Illuminate\Foundation\Application $app
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('mail.driver', 'log');
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
'database' => $this->getTempDirectory().'/database.sqlite',
'prefix' => '',
]);
$app['config']->set('app.key', '6rE9Nz59bGRbeMATftriyQjrpF7DcOQm');
}
/**
* @param \Illuminate\Foundation\Application $app
*/
protected function setUpDatabase($app)
{
file_put_contents($this->getTempDirectory().'/database.sqlite', null);
$this->artisan('migrate')->run();
$app['db']->connection()->getSchemaBuilder()->create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
$app['db']->connection()->getSchemaBuilder()->create('children', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
foreach (range(1, 5) as $index) {
$user = User::create([
'name' => "user{$index}",
'email' => "user{$index}@example.com",
'password' => "password{$index}",
]);
// Create two children for each user
foreach (range(1, 2) as $childIndex) {
Child::create([
'name' => "child{$childIndex}_user{$index}",
'user_id' => $user->id,
]);
}
}
}
}