Skip to content

Commit 76e3352

Browse files
committed
Add first test case, prepare database and model
1 parent a752106 commit 76e3352

File tree

7 files changed

+197
-0
lines changed

7 files changed

+197
-0
lines changed

phpunit.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false">
3+
<testsuites>
4+
<testsuite name="Unit">
5+
<directory suffix="Test.php">./tests/Unit</directory>
6+
</testsuite>
7+
8+
<testsuite name="Feature">
9+
<directory suffix="Test.php">./tests/Feature</directory>
10+
</testsuite>
11+
</testsuites>
12+
<filter>
13+
<whitelist processUncoveredFilesFromWhitelist="true">
14+
<directory suffix=".php">./src</directory>
15+
</whitelist>
16+
</filter>
17+
<php>
18+
<server name="APP_ENV" value="testing"/>
19+
<server name="BCRYPT_ROUNDS" value="4"/>
20+
<server name="CACHE_DRIVER" value="array"/>
21+
<server name="MAIL_DRIVER" value="array"/>
22+
<server name="QUEUE_CONNECTION" value="sync"/>
23+
<server name="SESSION_DRIVER" value="array"/>
24+
</php>
25+
</phpunit>

tests/CreatesDatabase.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace PPSpaces\Tests;
4+
5+
use Illuminate\Contracts\Console\Kernel;
6+
7+
trait CreatesDatabase
8+
{
9+
/**
10+
* Setup the test environment.
11+
*/
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
16+
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
17+
18+
$this->withFactories(__DIR__.'/database/factories');
19+
}
20+
21+
/**
22+
* Define environment setup.
23+
*
24+
* @param \Illuminate\Foundation\Application $app
25+
* @return void
26+
*/
27+
protected function getEnvironmentSetUp($app)
28+
{
29+
// Setup default database to use sqlite :memory:
30+
$app['config']->set('database.default', 'testbench');
31+
$app['config']->set('database.connections.testbench', [
32+
'driver' => 'sqlite',
33+
'database' => ':memory:',
34+
'prefix' => '',
35+
]);
36+
}
37+
}

tests/TestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace PPSpaces\Tests;
4+
5+
use Orchestra\Testbench\TestCase as BaseTestCase;
6+
7+
abstract class TestCase extends BaseTestCase
8+
{
9+
use CreatesDatabase;
10+
}

tests/Unit/DatabaseTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace PPSpaces\Tests\Unit;
4+
5+
use PPSpaces\Tests\TestCase;
6+
use PPSpaces\Tests\App\User;
7+
8+
/**
9+
* Class DatabaseTest
10+
*/
11+
class DatabaseTest extends TestCase
12+
{
13+
public function test_it_uses_the_default_database_connection()
14+
{
15+
factory(User::class)->create();
16+
17+
$connection = config('database.default');
18+
19+
$user = User::first();
20+
21+
22+
$this->assertEquals($connection, $user->getConnectionName());
23+
}
24+
}

tests/app/User.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace PPSpaces\Tests\App;
4+
5+
use Illuminate\Notifications\Notifiable;
6+
use Illuminate\Contracts\Auth\MustVerifyEmail;
7+
use Illuminate\Foundation\Auth\User as Authenticatable;
8+
9+
class User extends Authenticatable
10+
{
11+
use Notifiable;
12+
13+
/**
14+
* The attributes that are mass assignable.
15+
*
16+
* @var array
17+
*/
18+
protected $fillable = [
19+
'name', 'email', 'password',
20+
];
21+
22+
/**
23+
* The attributes that should be hidden for arrays.
24+
*
25+
* @var array
26+
*/
27+
protected $hidden = [
28+
'password', 'remember_token',
29+
];
30+
31+
/**
32+
* The attributes that should be cast to native types.
33+
*
34+
* @var array
35+
*/
36+
protected $casts = [
37+
'email_verified_at' => 'datetime',
38+
];
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use PPSpaces\Tests\App\User;
4+
use Illuminate\Support\Str;
5+
use Faker\Generator as Faker;
6+
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Model Factories
10+
|--------------------------------------------------------------------------
11+
|
12+
| This directory should contain each of the model factory definitions for
13+
| your application. Factories provide a convenient way to generate new
14+
| model instances for testing / seeding your application's database.
15+
|
16+
*/
17+
18+
$factory->define(User::class, function (Faker $faker) {
19+
return [
20+
'name' => $faker->name,
21+
'email' => $faker->unique()->safeEmail,
22+
'email_verified_at' => now(),
23+
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
24+
'remember_token' => Str::random(10),
25+
];
26+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateUsersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('users', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->string('name');
19+
$table->string('email')->unique();
20+
$table->timestamp('email_verified_at')->nullable();
21+
$table->string('password');
22+
$table->rememberToken();
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('users');
35+
}
36+
}

0 commit comments

Comments
 (0)