Modified API and created its tests
This commit is contained in:
parent
b0553839d0
commit
a82b6f0af5
@ -9,8 +9,16 @@ class AccessLog extends Model
|
|||||||
{
|
{
|
||||||
use SoftDeletes;
|
use SoftDeletes;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'siswa_id',
|
||||||
|
];
|
||||||
|
|
||||||
protected $with = [ 'siswa' ];
|
protected $with = [ 'siswa' ];
|
||||||
|
|
||||||
|
protected $date = [
|
||||||
|
'created_at', 'updated_at', 'deleted_at',
|
||||||
|
];
|
||||||
|
|
||||||
public function siswa()
|
public function siswa()
|
||||||
{
|
{
|
||||||
return $this->belongsTo('App\Siswa');
|
return $this->belongsTo('App\Siswa');
|
||||||
|
@ -8,4 +8,17 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
|||||||
class Siswa extends Model
|
class Siswa extends Model
|
||||||
{
|
{
|
||||||
use SoftDeletes;
|
use SoftDeletes;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'nama', 'nisn', 'kelas', 'tempat_lahir', 'tanggal_lahir', 'lulus',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $dates = [
|
||||||
|
'created_at', 'updated_at', 'deleted_at',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'tanggal_lahir' => 'datetime:Y-m-d',
|
||||||
|
'lulus' => 'boolean',
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ class CreateSiswasTable extends Migration
|
|||||||
$table->string('nisn');
|
$table->string('nisn');
|
||||||
$table->string('kelas');
|
$table->string('kelas');
|
||||||
$table->string('tempat_lahir');
|
$table->string('tempat_lahir');
|
||||||
$table->string('tanggal_lahir');
|
$table->date('tanggal_lahir');
|
||||||
$table->boolean('lulus');
|
$table->boolean('lulus');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
$table->softDeletes();
|
$table->softDeletes();
|
||||||
|
@ -15,11 +15,12 @@ class CreateAccessLogsTable extends Migration
|
|||||||
{
|
{
|
||||||
Schema::create('access_logs', function (Blueprint $table) {
|
Schema::create('access_logs', function (Blueprint $table) {
|
||||||
$table->bigIncrements('id');
|
$table->bigIncrements('id');
|
||||||
$table->unsignedInteger('siswa_id');
|
$table->unsignedBigInteger('siswa_id');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
$table->softDeletes();
|
$table->softDeletes();
|
||||||
|
|
||||||
$table->foreign('siswa_id')
|
$table->foreign('siswa_id')
|
||||||
|
->references('id')
|
||||||
->on('siswas')
|
->on('siswas')
|
||||||
->onUpdate('cascade')
|
->onUpdate('cascade')
|
||||||
->onDelete('restrict');
|
->onDelete('restrict');
|
||||||
|
@ -30,9 +30,7 @@ Route::post('/siswa', function (Request $request) {
|
|||||||
|
|
||||||
// Redirect with error if not found
|
// Redirect with error if not found
|
||||||
if (!$siswa || $siswa == null) {
|
if (!$siswa || $siswa == null) {
|
||||||
return redirect()->back()
|
return response()->json(['siswa' => 'Siswa tidak ditemukan.'], 404);
|
||||||
->withErrors(['siswa' => 'Siswa tidak ditemukan.'])
|
|
||||||
->withInput();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write to log
|
// Write to log
|
||||||
|
81
tests/Feature/ApplicationTest.php
Normal file
81
tests/Feature/ApplicationTest.php
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
<?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', ['nama' => 'Sally', 'nisn' => '1234567890']);
|
||||||
|
|
||||||
|
$response
|
||||||
|
->assertStatus(404)
|
||||||
|
->assertJson([
|
||||||
|
'siswa' => 'Siswa tidak ditemukan.'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCariDataSiswaTidakDiisiGagal()
|
||||||
|
{
|
||||||
|
$response = $this->json('POST', '/api/siswa', ['nama' => '', 'nisn' => '']);
|
||||||
|
|
||||||
|
$response
|
||||||
|
->assertStatus(422)
|
||||||
|
->assertJson([
|
||||||
|
'message' => 'The given data was invalid.',
|
||||||
|
'errors' => [
|
||||||
|
'nama' => ['Kolom nama harus diisi.'],
|
||||||
|
'nisn' => ['Kolom nisn harus diisi.'],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCariDataSiswaBenarBerhasil()
|
||||||
|
{
|
||||||
|
$siswa = \App\Siswa::create([
|
||||||
|
'nama' => 'Sally',
|
||||||
|
'nisn' => '1234567890',
|
||||||
|
'kelas' => 'xii mm 1',
|
||||||
|
'tempat_lahir' => $this->faker->city(),
|
||||||
|
'tanggal_lahir' => $this->faker->dateTime('now', 'Asia/Jakarta'),
|
||||||
|
'lulus' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->json('POST', '/api/siswa', ['nama' => 'Sally', 'nisn' => '1234567890']);
|
||||||
|
|
||||||
|
$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 = \App\Siswa::create([
|
||||||
|
'nama' => 'Sally',
|
||||||
|
'nisn' => '1234567890',
|
||||||
|
'kelas' => 'xii mm 1',
|
||||||
|
'tempat_lahir' => $this->faker->city(),
|
||||||
|
'tanggal_lahir' => $this->faker->dateTime('now', 'Asia/Jakarta'),
|
||||||
|
'lulus' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->json('POST', '/api/siswa', ['nama' => 'Sally', 'nisn' => '1234567890']);
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('access_logs', [
|
||||||
|
'siswa_id' => $siswa->id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
48
tests/Feature/SiswaTest.php
Normal file
48
tests/Feature/SiswaTest.php
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use Tests\TestCase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
|
||||||
|
class SiswaTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase, WithFaker;
|
||||||
|
|
||||||
|
public function testCariDataSiswaGagal()
|
||||||
|
{
|
||||||
|
$response = $this->json('POST', '/api/siswa', ['nama' => 'Sally', 'nisn' => '1234567890']);
|
||||||
|
|
||||||
|
$response
|
||||||
|
->assertStatus(404)
|
||||||
|
->assertJson([
|
||||||
|
'siswa' => 'Siswa tidak ditemukan.'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCariDataSiswaBerhasil()
|
||||||
|
{
|
||||||
|
$siswa = \App\Siswa::create([
|
||||||
|
'nama' => 'Sally',
|
||||||
|
'nisn' => '1234567890',
|
||||||
|
'kelas' => 'xii mm 1',
|
||||||
|
'tempat_lahir' => $this->faker->city(),
|
||||||
|
'tanggal_lahir' => $this->faker->dateTime('now', 'Asia/Jakarta'),
|
||||||
|
'lulus' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->json('POST', '/api/siswa', ['nama' => 'Sally', 'nisn' => '1234567890']);
|
||||||
|
|
||||||
|
$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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user