From 07eb2af13575b0a81b23023033109ce3afdbab61 Mon Sep 17 00:00:00 2001 From: Gregorio Chiko Putra Date: Fri, 10 May 2019 16:25:17 +0700 Subject: [PATCH] Updated request variable, updated api test --- resources/js/app.js | 6 +++--- routes/api.php | 7 +++---- tests/Feature/ApiTest.php | 17 +++++++++-------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/resources/js/app.js b/resources/js/app.js index 30bd15c..a783f6a 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -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'), diff --git a/routes/api.php b/routes/api.php index 58df0ad..dd755f7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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 diff --git a/tests/Feature/ApiTest.php b/tests/Feature/ApiTest.php index 01fd558..4d103f1 100644 --- a/tests/Feature/ApiTest.php +++ b/tests/Feature/ApiTest.php @@ -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');