Laravel PHPUnit Validation & Update

### validation check
BookReservationTest.php

public function test_a_title_is_require(){
        $this->withoutExceptionHandling();

        $response = $this->post('/books', [
            'title' => '',
            'author' => 'victor',
        ]);

        $this->assertSessionHasErrors('title');

    }

BooksController.php

public function store(){
    	$data = request()->validate([
	    	'title' => 'required',
	    ]);
    	Book::create($data);
    }

BookReservationTest.php

public function test_a_title_is_require(){

        $response = $this->post('/books', [
            'title' => '',
            'author' => 'victor',
        ]);

        $response->assertSessionHasErrors('title');

    }

$ phpunit –filter test_a_title_is_require
PHPUnit 8.5.0 by Sebastian Bergmann and contributors.

. 1 / 1 (100%)

Time: 220 ms, Memory: 20.00 MB

OK (1 test, 2 assertions)

titleと同様にauthorもテストする

public function test_a_author_is_require(){

        $response = $this->post('/books', [
            'title' => 'title',
            'author' => '',
        ]);

        $response->assertSessionHasErrors('author');

    }

### Update method check

public function test_a_book_can_be_updated(){
        $this->post('/books', [
            'title' => 'Cool Title',
            'author' => 'Victor',
        ]);

        $response = $this->patch('/books',[
            'title' => 'New Title',
            'author' => 'New Author',
        ]);

        $this->assertEquals('New Titile', Book::first()->title);
        $this->assertEquals('New Author', Book::first()->author);

    }

$ phpunit –filter test_a_book_can_be_updated
1) Tests\Feature\BookReservationTest::test_a_book_can_be_updated
Failed asserting that two strings are equal.
— Expected
+++ Actual
@@ @@
-‘New Titile’
+’Cool Title’

route

Route::patch('/books/{book}', 'BooksController@update');
public function test_a_book_can_be_updated(){
        $this->withoutExceptionHandling();
        $this->post('/books', [
            'title' => 'Cool Title',
            'author' => 'Victor'
        ]);

        $book = Book::first();

        $response = $this->patch('/books/'. $book->id, [
            'title' => 'New Title',
            'author' => 'New Author'
        ]);

        $this->assertEquals('New Title', Book::first()->title);
        $this->assertEquals('New Author', Book::first()->author);

    }
public function update(Book $book){

    	$data = request()->validate([
	    	'title' => 'required',
	    	'author' => 'required',
	    ]);
	    $book->update($data);
    }

リファクタリング

public function store(){;
    	Book::create($this->validateRequest());
    }

    public function update(Book $book){
	    $book->update($this->validateRequest());
    }

    protected function validateRequest(){
    	return request()->validate([
    		'title' => 'required',
    		'author' => 'required',
    	]);
    }

$ git add .
$ git commit -m “php unit”

Laravelの書き方に沿ってます。こりゃフレームワークの書き方がわからずにテスト工程をコントロールするのは若干無理がありますな。