1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LaravelでPOSTメソッドのテストをする

Last updated at Posted at 2019-11-04

環境・前提

Laravel Framework 5.8.35

要件

api/contactsにキーがnameで、値がTest NameのデータをPOSTしてデータベースに格納されるかテストする

準備

phpunitの設定ファイルに以下を追記する

phpunit.xml
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>

作成及び編集するファイル一覧

  1. tests/Feature/ContactsTest.php
  2. app/Contact.php
  3. routes/api.php
  4. app/Http/Controller/ContactsController.php
  5. database/migrations/2019_11_04_041040_create_contacts_table.php

1. artisanコマンドでテストをつくる

$ php artisan make:test ContactsTest
Test created successfully.

以下のように編集する

tests/Feature/ContactsTest.php
<?php

namespace Tests\Feature;

use App\Contact;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ContactsTest extends TestCase
{
    use RefreshDatabase;//マイグレーションが実行されテーブルが作成される
    public function test_a_contact_can_be_added(){
        $this->withoutExceptionHandling();//エラーが起きても例外処理をしない
        $this->post('/api/contacts', ['name'=>'Test Name']);
        $this->assertCount(1, Contact::all());
    }
}

2. ルートを定義する

以下のように編集する

routes/api.php
<?php

Route::post('/contacts', 'ContactsController@store');

3. artisanコマンドでモデルをつくる

$ php artisan make:model Contact
Model created successfully.
app/Contact.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected $guarded = [];//複数代入時に代入を許可しない属性を配列で設定
}

4. artisanコマンドでコントローラーをつくる

$ php artisan make:controller ContactsController
Controller created successfully.

以下のように編集する

app/Http/Controller/ContactsController.php
<?php

namespace App\Http\Controllers;

use App\Contact;

class ContactsController extends Controller
{
    public function store()
    {
        Contact::create([
            'name'=> request('name')
        ]);
    }
}

5. artisanコマンドでマイグレーションをつくる

$ php artisan make:migration CreateContactsTable
Created Migration: 2019_11_04_041040_create_contacts_table
database/migrations/2019_11_04_041040_create_contacts_table.php
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateContactsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contacts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');//nameコラムを追加する
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('contacts');
    }
}

テストを実行する

$ clear && vendor/bin/phpunit --filter test_a_contact_can_be_added
OK (1 test, 1 assertion)

参考

http://recipes.laravel.jp/recipe/1
https://qiita.com/kk_take/items/3e0639ed605f74c34619
https://udemy.benesse.co.jp/development/web/laravel-model.html

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?