Updated request variable, updated api test

This commit is contained in:
Gregorio Chiko Putra 2019-05-10 16:25:17 +07:00
parent 4bb4e7c56b
commit 07eb2af135
3 changed files with 15 additions and 15 deletions

6
resources/js/app.js vendored
View File

@ -28,7 +28,7 @@ m.mount(document.body.querySelector('.container'), {
e.preventDefault();
Siswa.cariData({
nisn: e.target.elements.nisn.value,
tglLahir: e.target.elements.tglLahir.value,
tanggalLahir: e.target.elements.tanggalLahir.value,
});
}
},
@ -39,8 +39,8 @@ m.mount(document.body.querySelector('.container'), {
m('p.input-helper', 'Nomor Induk Siswa Nasional.'),
]),
m('.form-group', [
m('label.form-label[for=input-tglLahir]', 'Tanggal Lahir'),
m('input.form-input.input-text#input-tglLahir[name=tglLahir][type=text][autocomplete=off][required]'),
m('label.form-label[for=input-tanggalLahir]', 'Tanggal Lahir'),
m('input.form-input.input-text#input-tanggalLahir[name=tanggalLahir][type=text][autocomplete=off][required]'),
m('p.input-helper', 'Tanggal lahir dengan format YYYYMMDD. Contoh: untuk tanggal 29 Mei 2000 ditulis 20000529'),
]),
m('button.form-submit[type=submit]', 'Lihat'),

View File

@ -18,17 +18,16 @@ Route::post('/siswa', function (Request $request) {
// Auto redirect on fail
Validator::make($request->all(), [
'nisn' => 'required|regex:/^[0-9]+$/',
'tglLahir' => 'required|regex:/^[0-9]+$/',
'tanggalLahir' => 'required|regex:/^[0-9]+$/',
], [
'required' => 'Kolom :attribute harus diisi.',
'string' => 'Kolom :attribute tidak sesuai.',
'regex' => 'Kolom :attribute tidak sesuai.',
])->validate();
// Look for the given inputs in the resource
$tglLahir = Carbon\Carbon::parse($request->tglLahir);
$tanggalLahir = Carbon\Carbon::parse($request->tanggalLahir)->format('Y-m-d');
$siswa = App\Siswa::where('nisn', $request->nisn)
->where('tanggal_lahir', $tglLahir)
->where('tanggal_lahir', $tanggalLahir)
->first();
// Redirect with error if not found

View File

@ -12,7 +12,7 @@ class ApplicationTest extends TestCase
public function testCariDataSiswaSalahGagal()
{
$response = $this->json('POST', '/api/siswa', ['nama' => 'Sally', 'nisn' => '1234567890']);
$response = $this->json('POST', '/api/siswa', ['nisn' => '1234567890', 'tanggalLahir' => '20190510']);
$response
->assertStatus(404)
@ -23,30 +23,30 @@ class ApplicationTest extends TestCase
public function testCariDataSiswaTidakDiisiGagal()
{
$response = $this->json('POST', '/api/siswa', ['nama' => '', 'nisn' => '']);
$response = $this->json('POST', '/api/siswa', ['nisn' => '', 'tanggalLahir' => '']);
$response
->assertStatus(422)
->assertJson([
'message' => 'The given data was invalid.',
'errors' => [
'nama' => ['Kolom nama harus diisi.'],
'nisn' => ['Kolom nisn harus diisi.'],
'tanggalLahir' => ['Kolom tanggal lahir harus diisi.'],
],
]);
}
public function testCariDataSiswaTidakSesuaiGagal()
{
$response = $this->json('POST', '/api/siswa', ['nama' => ['Sally'], 'nisn' => 'abcdefghij']);
$response = $this->json('POST', '/api/siswa', ['nisn' => '1abcdefghij9', 'tanggalLahir' => '2019-05-10']);
$response
->assertStatus(422)
->assertJson([
'message' => 'The given data was invalid.',
'errors' => [
'nama' => ['Kolom nama tidak sesuai.'],
'nisn' => ['Kolom nisn tidak sesuai.'],
'tanggalLahir' => ['Kolom tanggal lahir tidak sesuai.'],
],
]);
}
@ -55,7 +55,8 @@ class ApplicationTest extends TestCase
{
$siswa = factory(\App\Siswa::class)->create();
$response = $this->json('POST', '/api/siswa', ['nama' => $siswa->nama, 'nisn' => $siswa->nisn]);
$response = $this->json('POST', '/api/siswa', ['nisn' => $siswa->nisn, 'tanggalLahir' => $siswa->tanggal_lahir->format('Ymd')]);
$this->assertDatabaseHas('siswas', $siswa->toArray());
$response
->assertStatus(200)
@ -73,7 +74,7 @@ class ApplicationTest extends TestCase
{
$siswa = factory(\App\Siswa::class)->create();
$response = $this->json('POST', '/api/siswa', ['nama' => $siswa->nama, 'nisn' => $siswa->nisn]);
$response = $this->json('POST', '/api/siswa', ['nisn' => $siswa->nisn, 'tanggalLahir' => $siswa->tanggal_lahir->format('Ymd')]);
$this->assertDatabaseHas('access_logs', [
'siswa_id' => $siswa->id,
@ -84,7 +85,7 @@ class ApplicationTest extends TestCase
{
$siswas = factory(\App\Siswa::class, 50)->create();
$request = $this->json('POST', '/api/siswa', ['nama' => $siswas[0]->nama, 'nisn' => $siswas[0]->nisn]);
$request = $this->json('POST', '/api/siswa', ['nisn' => $siswas[0]->nisn, 'tanggalLahir' => $siswas[0]->tanggal_lahir->format('Ymd')]);
$response = $this->json('GET', '/api/access_log');