<?php

namespace Tests\Feature;

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

class ApplicationTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function testCariDataSiswaSalahGagal()
    {
        $response = $this->json('POST', '/api/siswa', ['nisn' => '1234567890', 'tanggalLahir' => '20190510']);

        $response
            ->assertStatus(404)
            ->assertJson([
                'errors' => [
                    'siswa' => ['Siswa tidak ditemukan.'],
                ],
            ]);
    }

    public function testCariDataSiswaTidakDiisiGagal()
    {
        $response = $this->json('POST', '/api/siswa', ['nisn' => '', 'tanggalLahir' => '']);

        $response
            ->assertStatus(422)
            ->assertJson([
                'message' => 'The given data was invalid.',
                'errors' => [
                    'nisn' => ['Kolom nisn harus diisi.'],
                    'tanggalLahir' => ['Kolom tanggal lahir harus diisi.'],
                ],
            ]);
    }

    public function testCariDataSiswaTidakSesuaiGagal()
    {
        $response = $this->json('POST', '/api/siswa', ['nisn' => '1abcdefghij9', 'tanggalLahir' => '2019-05-10']);

        $response
            ->assertStatus(422)
            ->assertJson([
                'message' => 'The given data was invalid.',
                'errors' => [
                    'nisn' => ['Kolom nisn tidak sesuai.'],
                    'tanggalLahir' => ['Kolom tanggal lahir tidak sesuai.'],
                ],
            ]);
    }

    public function testCariDataSiswaBenarBerhasil()
    {
        $siswa = factory(\App\Siswa::class)->create();

        $response = $this->json('POST', '/api/siswa', ['nisn' => $siswa->nisn, 'tanggalLahir' => $siswa->tanggal_lahir->format('Ymd')]);
        $this->assertDatabaseHas('siswas', $siswa->toArray());

        $response
            ->assertStatus(200)
            ->assertJson([
                'nama' => $siswa->nama,
                'nisn' => $siswa->nisn,
                'kelas' => $siswa->kelas,
                'tempat_lahir' => $siswa->tempat_lahir,
                'tanggal_lahir' => $siswa->tanggal_lahir->toDateString(),
                'lulus' => true,
            ]);
    }

    public function testCariDataSiswaBenarMasukLog()
    {
        $siswa = factory(\App\Siswa::class)->create();

        $response = $this->json('POST', '/api/siswa', ['nisn' => $siswa->nisn, 'tanggalLahir' => $siswa->tanggal_lahir->format('Ymd')]);

        $this->assertDatabaseHas('access_logs', [
            'siswa_id' => $siswa->id,
        ]);
    }

    public function testLihatJumlahLogBerhasil()
    {
        $siswas = factory(\App\Siswa::class, 50)->create();

        $request = $this->json('POST', '/api/siswa', ['nisn' => $siswas[0]->nisn, 'tanggalLahir' => $siswas[0]->tanggal_lahir->format('Ymd')]);

        $response = $this->json('GET', '/api/access_log');

        $response
            //->assertStatus(200)
            ->assertJson([
                'accessed' => 1,
                'total' => $siswas->count()
            ]);
    }
}
