0
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でテストしてみる

Last updated at Posted at 2020-03-14

概要

前回作った環境をテストする

環境

Laravel 7.0.8
Valet 2.8.1

Laravelの構造

テストをする前に前回のおさらいを踏まえて、構造を整理

MVC

Model、View、Controllerにプログラムの役割を分割した設計手法を採用

 Model
  データ処理全般、DBとの受け渡し:app/

 View
  ユーザーインターフェス、画面関連:resources/views/

 Controller
  全体の制御、モデルとビューをコントロール:app/Http/Controllers/

MVCのフォルダだけを表示すると、

.
├── app
│   └── Http
│      └── app/Http/Controllers
└── resources
    └── views

ただし、Modelについては、どのフォルダに配置してもよい

modelsディレクトリはどこにある?

Laravelを学習し始めるとき、多くの開発者はmodelsディレクトリが存在しないことに戸惑います。しかし、意図的にこのディレクトリを用意していません。多くの別々の人達にとって、その意味合いはさまざまなため、"models"という言葉の定義は曖昧であることに私達は気づきました。ある開発者たちはすべてのビジネスロジックを総称してアプリケーションの「モデル」と呼び、一方で別の人達はリレーショナルデータベースに関連するクラスを「モデル」として参照しています。
このため、私達はEloquentモデルをデフォルトではappディレクトリ下へ設置することを選択し、開発者自分が選んだどこか別の場所へ設置してもらうことにしました

Webサイトを開いてからの処理の流れは、ざっと以下の流れ

ユーザーからのリクエスト
 │
ルーティング
 │
コントローラー
 │
 ├── ビュー 
 │
モデル
 │
データベース

詳しくてわかりやすい解説は https://miyabi-lab.space/blog/22

前回は、モデルは触らずに使わずにルーティングで設定したコントローラーからビューを表示させた。今回は、モデルからデータベースの読み書きを行う。

====
MVC参考
https://www.ritolab.com/entry/48
====

テストもご一緒に

テストは先に書いてもいいし後でもいいけれど、テストも書きながら、
サイト作りを進めます。

PHPUnit

Laravelに用意されているテスト用フレームワークPHPUnitを使います。

.
└── tests
    ├── Feature
    │   └── ExampleTest.php
    └── Unit
        └── ExampleTest.php|    

テストには一つのメソッドを対象とするユニットテストと、コードを広く対象とした機能(Feature)テストがあります。testsディレクトリにそれぞれのディレクトリがあり、サンプルがあるので見てみると、

ExampleTest.php
<?php

namespace Tests\Feature;

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

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

これを流用できそうですが、artisan コマンドでテストが作れるので試します。

$ php artisan make:test UserTest
Test created successfully.

中身はサンプルとほぼ一緒でした。

UserTest.php
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class UserTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testExample()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

これを以下を満たすように変更します。

 サイトが正常に表示:
  HTTP レスポンスステータスコードが200
  画面に「Hello World」の文字列がある

UserTest.php
//del
    public function testExample()
    {
        $response = $this->get('/');

//add
    public function testRespose()
    {
        $response = $this->get('/user');

    public function testText()
    {
        $text = $this->get('/user');

        $text->assertSeeText("Hello World");
    }

で早速実行してみる

$ php vendor/bin/phpunit tests/Feature/UserTest.php 
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 788 ms, Memory: 18.00 MB

OK (2 tests, 2 assertions)

OKを確認。2つのテストで2つの検証が成功したという意味。ちなみにテストが失敗すると

DIR/blog/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:432
DIR/blog/tests/Feature/UserTest.php:27

FAILURES!
Tests: 2, Assertions: 2, Failures: 2.

参考
https://readouble.com/laravel/6.x/ja/testing.html

次回は、データベースを使う簡単なサイトを作ります。

0
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
0
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?