Skip to content

Commit

Permalink
Add ExamModuleTest
Browse files Browse the repository at this point in the history
Add exam module feature test
  • Loading branch information
changeweb committed Apr 19, 2019
1 parent 0a5694d commit c819681
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
10 changes: 6 additions & 4 deletions app/Http/Controllers/ExamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ public function store(Request $request)
$request->validate([
'exam_name' => 'required|string',
'term' => 'required|string',
'exam_start_date' => 'required|string',
'exam_end_date' => 'required|string',
'start_date' => 'required|string',
'end_date' => 'required|string',
]);

\DB::transaction(function () use ($request) {
$exam = new \App\Exam;
$exam->exam_name = $request->exam_name;
$exam->active = 1;
$exam->term = $request->term;
$exam->start_date = $request->exam_start_date;
$exam->end_date = $request->exam_end_date;
$exam->start_date = $request->start_date;
$exam->end_date = $request->end_date;
$exam->notice_published = 0;
$exam->result_published = 0;
$exam->school_id = \Auth::user()->school_id;
$exam->user_id = \Auth::user()->id;
$exam->save();
Expand Down
5 changes: 5 additions & 0 deletions database/factories/ExamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
return $faker->randomElement(App\School::pluck('id')->toArray());
}
},
'term' => $faker->sentence,
'active' => $faker->randomElement([0,1]),
'start_date' => $faker->dateTime()->format('Y-m-d H:i:s'),
'end_date' => $faker->dateTime()->format('Y-m-d H:i:s'),
'notice_published' => $faker->randomElement([0,1]),
'result_published' => $faker->randomElement([0,1]),
'user_id' => function () use ($faker) {
Expand All @@ -22,5 +25,7 @@
return $faker->randomElement(App\User::pluck('id')->toArray());
}
},
'created_at' => Carbon\Carbon::now(),
'updated_at' => Carbon\Carbon::now(),
];
});
20 changes: 10 additions & 10 deletions resources/views/components/add-exam-form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@
@endif
</div>
</div>
<div class="form-group{{ $errors->has('exam_start_date') ? ' has-error' : '' }}">
<label for="exam_start_date" class="col-md-4 control-label">Start Date</label>
<div class="form-group{{ $errors->has('start_date') ? ' has-error' : '' }}">
<label for="start_date" class="col-md-4 control-label">Start Date</label>

<div class="col-md-6">
<input id="exam_start_date" type="text" class="form-control" name="exam_start_date" value="{{ old('exam_start_date') }}" placeholder="5th April..." required>
<input id="start_date" type="text" class="form-control" name="start_date" value="{{ old('start_date') }}" placeholder="5th April..." required>

@if ($errors->has('exam_start_date'))
@if ($errors->has('start_date'))
<span class="help-block">
<strong>{{ $errors->first('exam_start_date') }}</strong>
<strong>{{ $errors->first('start_date') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('exam_end_date') ? ' has-error' : '' }}">
<label for="exam_end_date" class="col-md-4 control-label">End Date</label>
<div class="form-group{{ $errors->has('end_date') ? ' has-error' : '' }}">
<label for="end_date" class="col-md-4 control-label">End Date</label>

<div class="col-md-6">
<input id="exam_end_date" type="text" class="form-control" name="exam_end_date" value="{{ old('exam_end_date') }}" placeholder="20th April..." required>
<input id="end_date" type="text" class="form-control" name="end_date" value="{{ old('end_date') }}" placeholder="20th April..." required>

@if ($errors->has('exam_end_date'))
@if ($errors->has('end_date'))
<span class="help-block">
<strong>{{ $errors->first('exam_end_date') }}</strong>
<strong>{{ $errors->first('end_date') }}</strong>
</span>
@endif
</div>
Expand Down
56 changes: 56 additions & 0 deletions tests/Feature/Manage/ExamModuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Tests\Feature\Manage;

use App\User;
use App\Exam;
use App\School;
use App\Myclass;
use App\Section;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExamModuleTest extends TestCase
{
use WithFaker, RefreshDatabase;

public function setUp() {
parent::setUp();
$admin = factory(User::class)->states('admin')->create();
$this->actingAs($admin);
$this->withoutExceptionHandling();
}
/** @test */
public function view_is(){
$this->get('exams')
->assertViewIs('exams.all');
}
/** @test */
public function it_shows_the_exam_list() {
$this->get('exams')
->assertStatus(200)
->assertViewHas('exams');
}
/** @test */
public function admin_can_create_exam() {
$exam = [
'exam_name' => $this->faker->words(3, true),
'term' => $this->faker->sentence,
'start_date' => $this->faker->dateTime()->format('Y-m-d H:i:s'),
'end_date' => $this->faker->dateTime()->format('Y-m-d H:i:s'),
];
$classes = [
'classes' => [1,2,3]//id
];
$request = array_merge($exam, $classes);
$this->followingRedirects()
->post('exams/create', $request)
->assertStatus(200);

$this->assertDatabaseHas('exams', $exam);

$this->get('exams')
->assertSee($exam['exam_name']);
}
}

0 comments on commit c819681

Please sign in to comment.