| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace Tests\Unit;
- use App\Authority;
- use App\Province;
- use App\Role;
- use App\User;
- use Illuminate\Support\Facades\DB;
- use Tests\TestCase;
- use Illuminate\Foundation\Testing\WithFaker;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- class ProvinceTest extends TestCase
- {
- public function testUserMark(){
- $userMark=factory(User::class)->create();
- $this->assertNotEmpty($userMark->id);
- return $userMark;
- }
- public function testRole(){
- $role=Role::create([
- 'name'=>'测试admin'
- ]);
- $this->assertNotEmpty($role->id);
- $authorities= Authority::get();
- foreach ($authorities as $authority){
- DB::table('authority_role')->insert(['id_authority'=>$authority->id,'id_role'=>$role->id]);
- }
- return $role;
- }
- /**
- * @depends testRole
- */
- public function testUser(Role $role){
- $user=factory(User::class)->create();
- DB::table('user_role')->insert(['id_user'=>$user->id,'id_role'=>$role->id]);
- $this->assertNotEmpty($user->id);
- return $user;
- }
- public function testAddProvince(){
- $province=new Province([
- 'name'=>'汉城',
- ]);
- $result=$province->save();
- $this->assertTrue($result);
- $this->assertNotEmpty($province->id);
- return $province;
- }
- /**
- * @depends testAddProvince
- * @depends testUser
- * @depends testUserMark
- */
- public function testIndex(Province $province,User $user,User $userMark){
- $response=$this->actingAs($user)->get('maintenance/province');
- $response->assertOk()->assertSee('汉城');
- $responseMark=$this->actingAs($userMark)->get('maintenance/province');
- $responseMark->assertStatus(302)->assertRedirect('/');
- $province=Province::paginate(10);
- $this->assertNotEmpty($province);
- }
- /**
- * @depends testUser
- * @depends testUserMark
- */
- public function testCreate(User $user,User $userMark){
- $response=$this->actingAs($user)->get('maintenance/province/create');
- $response->assertOk()->assertSee('省份名称');
- $responseMark=$this->actingAs($userMark)->get('maintenance/province/create');
- $responseMark->assertStatus(302)->assertRedirect('/');
- }
- /**
- * @depends testUser
- * @depends testUserMark
- */
- public function testStore(User $user,User $userMark){
- $response=$this->actingAs($user)->post('maintenance/province',['Province'=>[
- 'name'=>'帝都',
- ]]);
- $response->assertStatus(302)->assertRedirect('maintenance/province')->assertSessionHas('successTip');
- $responseMark=$this->actingAs($userMark)->post('maintenance/province',['Province'=>[
- 'name'=>'帝都',
- ]]);
- $responseMark->assertStatus(302)->assertRedirect('/');
- $province=Province::where('name','=','帝都')->first();
- $result=$province->delete();
- $this->assertTrue($result);
- }
- /**
- * @depends testAddProvince
- * @depends testUser
- * @depends testUserMark
- */
- public function testEdit(Province $province,User $user,User $userMark){
- $response=$this->actingAs($user)->get("maintenance/province/$province->id/edit");
- $response->assertOk()->assertSee('汉城');
- $responseMark=$this->actingAs($userMark)->get("maintenance/province/$province->id/edit");
- $responseMark->assertStatus(302)->assertRedirect('/');
- }
- /**
- * @depends testAddProvince
- * @depends testUser
- * @depends testUserMark
- */
- public function testUpdate(Province $province,User $user,User $userMark){
- $response=$this->actingAs($user)->put("maintenance/province/$province->id",['Province'=>[
- 'name'=>'圣都'
- ]]);
- $response->assertStatus(302)->assertRedirect('maintenance/province')->assertSessionHas('successTip');
- $responseMark=$this->actingAs($userMark)->put("maintenance/province/$province->id",['Province'=>[
- 'name'=>'圣都'
- ]]);
- $responseMark->assertStatus(302)->assertRedirect('/');
- }
- /**
- * @depends testAddProvince
- * @depends testUser
- * @depends testUserMark
- * @depends testRole
- */
- public function testDestroy(Province $province,User $user,User $userMark,Role $role){
- $response=$this->actingAs($user)->json('delete','maintenance/province/'.$province->id,['id'=>$province->id]);
- $response->assertOk()->assertJson(['success'=>true]);
- $responseMark=$this->actingAs($userMark)->json('delete','maintenance/province/'.$province->id,['id'=>$province->id]);
- $responseMark->assertStatus(302)->assertRedirect('/');
- $result=$user->delete();
- $this->assertTrue($result);
- $this->assertNotEmpty($userMark->id);
- $resultUserMark=$userMark->delete();
- $this->assertTrue($resultUserMark);
- $resultRole=$role->delete();
- $this->assertTrue($resultRole);
- DB::table('authority_role')->where('id_role','=',$role->id)->delete();
- DB::table('user_role')->where('id_role','=',$role->id)->delete();
- }
- }
|