ProvinceTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Authority;
  4. use App\Province;
  5. use App\Role;
  6. use App\User;
  7. use Illuminate\Support\Facades\DB;
  8. use Tests\TestCase;
  9. use Illuminate\Foundation\Testing\WithFaker;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. class ProvinceTest extends TestCase
  12. {
  13. public function testUserMark(){
  14. $userMark=factory(User::class)->create();
  15. $this->assertNotEmpty($userMark->id);
  16. return $userMark;
  17. }
  18. public function testRole(){
  19. $role=Role::create([
  20. 'name'=>'测试admin'
  21. ]);
  22. $this->assertNotEmpty($role->id);
  23. $authorities= Authority::get();
  24. foreach ($authorities as $authority){
  25. DB::table('authority_role')->insert(['id_authority'=>$authority->id,'id_role'=>$role->id]);
  26. }
  27. return $role;
  28. }
  29. /**
  30. * @depends testRole
  31. */
  32. public function testUser(Role $role){
  33. $user=factory(User::class)->create();
  34. DB::table('user_role')->insert(['id_user'=>$user->id,'id_role'=>$role->id]);
  35. $this->assertNotEmpty($user->id);
  36. return $user;
  37. }
  38. public function testAddProvince(){
  39. $province=new Province([
  40. 'name'=>'汉城',
  41. ]);
  42. $result=$province->save();
  43. $this->assertTrue($result);
  44. $this->assertNotEmpty($province->id);
  45. return $province;
  46. }
  47. /**
  48. * @depends testAddProvince
  49. * @depends testUser
  50. * @depends testUserMark
  51. */
  52. public function testIndex(Province $province,User $user,User $userMark){
  53. $response=$this->actingAs($user)->get('maintenance/province');
  54. $response->assertOk()->assertSee('汉城');
  55. $responseMark=$this->actingAs($userMark)->get('maintenance/province');
  56. $responseMark->assertStatus(302)->assertRedirect('/');
  57. $province=Province::paginate(10);
  58. $this->assertNotEmpty($province);
  59. }
  60. /**
  61. * @depends testUser
  62. * @depends testUserMark
  63. */
  64. public function testCreate(User $user,User $userMark){
  65. $response=$this->actingAs($user)->get('maintenance/province/create');
  66. $response->assertOk()->assertSee('省份名称');
  67. $responseMark=$this->actingAs($userMark)->get('maintenance/province/create');
  68. $responseMark->assertStatus(302)->assertRedirect('/');
  69. }
  70. /**
  71. * @depends testUser
  72. * @depends testUserMark
  73. */
  74. public function testStore(User $user,User $userMark){
  75. $response=$this->actingAs($user)->post('maintenance/province',['Province'=>[
  76. 'name'=>'帝都',
  77. ]]);
  78. $response->assertStatus(302)->assertRedirect('maintenance/province')->assertSessionHas('successTip');
  79. $responseMark=$this->actingAs($userMark)->post('maintenance/province',['Province'=>[
  80. 'name'=>'帝都',
  81. ]]);
  82. $responseMark->assertStatus(302)->assertRedirect('/');
  83. $province=Province::where('name','=','帝都')->first();
  84. $result=$province->delete();
  85. $this->assertTrue($result);
  86. }
  87. /**
  88. * @depends testAddProvince
  89. * @depends testUser
  90. * @depends testUserMark
  91. */
  92. public function testEdit(Province $province,User $user,User $userMark){
  93. $response=$this->actingAs($user)->get("maintenance/province/$province->id/edit");
  94. $response->assertOk()->assertSee('汉城');
  95. $responseMark=$this->actingAs($userMark)->get("maintenance/province/$province->id/edit");
  96. $responseMark->assertStatus(302)->assertRedirect('/');
  97. }
  98. /**
  99. * @depends testAddProvince
  100. * @depends testUser
  101. * @depends testUserMark
  102. */
  103. public function testUpdate(Province $province,User $user,User $userMark){
  104. $response=$this->actingAs($user)->put("maintenance/province/$province->id",['Province'=>[
  105. 'name'=>'圣都'
  106. ]]);
  107. $response->assertStatus(302)->assertRedirect('maintenance/province')->assertSessionHas('successTip');
  108. $responseMark=$this->actingAs($userMark)->put("maintenance/province/$province->id",['Province'=>[
  109. 'name'=>'圣都'
  110. ]]);
  111. $responseMark->assertStatus(302)->assertRedirect('/');
  112. }
  113. /**
  114. * @depends testAddProvince
  115. * @depends testUser
  116. * @depends testUserMark
  117. * @depends testRole
  118. */
  119. public function testDestroy(Province $province,User $user,User $userMark,Role $role){
  120. $response=$this->actingAs($user)->json('delete','maintenance/province/'.$province->id,['id'=>$province->id]);
  121. $response->assertOk()->assertJson(['success'=>true]);
  122. $responseMark=$this->actingAs($userMark)->json('delete','maintenance/province/'.$province->id,['id'=>$province->id]);
  123. $responseMark->assertStatus(302)->assertRedirect('/');
  124. $result=$user->delete();
  125. $this->assertTrue($result);
  126. $this->assertNotEmpty($userMark->id);
  127. $resultUserMark=$userMark->delete();
  128. $this->assertTrue($resultUserMark);
  129. $resultRole=$role->delete();
  130. $this->assertTrue($resultRole);
  131. DB::table('authority_role')->where('id_role','=',$role->id)->delete();
  132. DB::table('user_role')->where('id_role','=',$role->id)->delete();
  133. }
  134. }