ContactsTest.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
 
namespace Tests\Feature;
 
use App\Contact;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
 
class ContactsTest extends TestCase
{
    use RefreshDatabase;
    /** @test */
    public function a_contact_can_be_added()
    {
        //예외처리 핸들링
        $this->withoutExceptionHandling();
 
        //이름을 전달하는 테스트 코드
        $this->post('/api/contacts', $this->data());
 
            //데이터베이스의 첫번째 것 가져오기
            $contact = Contact::first();
 
        $this->assertEquals('Test Name', $contact->name);
        $this->assertEquals('test@email.com', $contact->email);
        $this->assertEquals('05/14/1988', $contact->birthday);
        $this->assertEquals('ABC String', $contact->company);
    }
 
    /** @test */
    public function fields_are_required()
    {
        collect(['name', 'email', 'birthday', 'company'])
            ->each(function($field){
 
       //이름을 전달하는 테스트 코드
       $response = $this->post('/api/contacts',
       array_merge($this->data(),[$field => '']));
 
        $response->assertSessionHasErrors($field);
        $this->assertCount(0, Contact::all());
 
            });
    }
 
    private function data()
    {
        return[
            'name' => 'Test Name',
            'email' => 'test@email.com',
            'birthday' => '05/14/1988',
            'company' => 'ABC String',
        ];
    }
}
 
cs


ContactsController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
 
namespace App\Http\Controllers;
 
use App\Contact;
use Illuminate\Http\Request;
 
class ContactsController extends Controller
{
    public function store()
    {
 
        $data = request()->validate([
 
            'name' => 'required',
 
            'email' => 'required',
 
            'birthday' => 'required',
 
            'company' => 'required',
 
        ]);
 
        Contact::create($data);
    }
}
 
cs


'라라벨 > jot' 카테고리의 다른 글

10. tailwindcss설치  (2) 2020.01.15

+ Recent posts