テストをしましょう
This commit is contained in:
parent
73ee9f108b
commit
695f457505
12
database/factories/EjaculationFactory.php
Normal file
12
database/factories/EjaculationFactory.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||
|
||||
use App\Ejaculation;
|
||||
use Faker\Generator as Faker;
|
||||
|
||||
$factory->define(Ejaculation::class, function (Faker $faker) {
|
||||
return [
|
||||
'ejaculated_date' => $faker->date('Y-m-d H:i:s'),
|
||||
'note' => $faker->text,
|
||||
];
|
||||
});
|
10
database/factories/LikeFactory.php
Normal file
10
database/factories/LikeFactory.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||
|
||||
use Faker\Generator as Faker;
|
||||
|
||||
$factory->define(App\Like::class, function (Faker $faker) {
|
||||
return [
|
||||
//
|
||||
];
|
||||
});
|
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Model Factories
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here you may define all of your model factories. Model factories give
|
||||
| you a convenient way to create models for testing and seeding your
|
||||
| database. Just tell the factory how a default model should look.
|
||||
|
|
||||
*/
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||
$factory->define(App\User::class, function (Faker\Generator $faker) {
|
||||
static $password;
|
||||
|
||||
return [
|
||||
'name' => $faker->name,
|
||||
'email' => $faker->unique()->safeEmail,
|
||||
'password' => $password ?: $password = bcrypt('secret'),
|
||||
'remember_token' => str_random(10),
|
||||
];
|
||||
});
|
21
database/factories/UserFactory.php
Normal file
21
database/factories/UserFactory.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||
|
||||
$factory->define(App\User::class, function (Faker\Generator $faker) {
|
||||
static $password;
|
||||
|
||||
return [
|
||||
'name' => substr($faker->userName, 0, 15),
|
||||
'email' => $faker->unique()->safeEmail,
|
||||
'password' => $password ?: $password = bcrypt('secret'),
|
||||
'remember_token' => str_random(10),
|
||||
'display_name' => substr($faker->name, 0, 20),
|
||||
'is_protected' => false,
|
||||
'accept_analytics' => false,
|
||||
'private_likes' => false,
|
||||
];
|
||||
});
|
||||
|
||||
$factory->state(App\User::class, 'protected', [
|
||||
'is_protected' => true,
|
||||
]);
|
63
tests/Feature/SettingTest.php
Normal file
63
tests/Feature/SettingTest.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Ejaculation;
|
||||
use App\Like;
|
||||
use App\User;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SettingTest extends TestCase
|
||||
{
|
||||
public function testDestroyUser()
|
||||
{
|
||||
$user = factory(User::class)->create();
|
||||
$ejaculation = factory(Ejaculation::class)->create(['user_id' => $user->id]);
|
||||
|
||||
$anotherUser = factory(User::class)->create();
|
||||
$anotherEjaculation = factory(Ejaculation::class)->create(['user_id' => $anotherUser->id]);
|
||||
|
||||
$like = factory(Like::class)->create([
|
||||
'user_id' => $user->id,
|
||||
'ejaculation_id' => $anotherEjaculation->id,
|
||||
]);
|
||||
$anotherLike = factory(Like::class)->create([
|
||||
'user_id' => $anotherUser->id,
|
||||
'ejaculation_id' => $ejaculation->id,
|
||||
]);
|
||||
|
||||
$token = $this->getCsrfToken($user, '/setting/deactivate');
|
||||
$response = $this->actingAs($user)
|
||||
->followingRedirects()
|
||||
->post('/setting/deactivate', [
|
||||
'_token' => $token,
|
||||
'password' => 'secret',
|
||||
]);
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertViewIs('setting.deactivated');
|
||||
$this->assertGuest();
|
||||
$this->assertDatabaseMissing('users', ['id' => $user->id]);
|
||||
$this->assertDatabaseMissing('ejaculations', ['id' => $ejaculation->id]);
|
||||
$this->assertDatabaseMissing('likes', ['id' => $like->id]);
|
||||
$this->assertDatabaseMissing('likes', ['id' => $anotherLike->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* テスト対象を呼び出す前にGETリクエストを行い、CSRFトークンを得る
|
||||
* @param Authenticatable $user 認証情報
|
||||
* @param string $uri リクエスト先
|
||||
* @return string CSRFトークン
|
||||
*/
|
||||
private function getCsrfToken(Authenticatable $user, string $uri): string
|
||||
{
|
||||
$response = $this->actingAs($user)->get($uri);
|
||||
$crawler = new Crawler($response->getContent());
|
||||
|
||||
return $crawler->filter('input[name=_token]')->attr('value');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user