CityTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Authority;
  4. use App\City;
  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 CityTest 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 testAddCity(){
  39. $city=new City([
  40. 'name'=>'测试',
  41. 'province_id'=>1,
  42. ]);
  43. $result=$city->save();
  44. $this->assertTrue($result);
  45. $this->assertNotEmpty($city);
  46. return $city;
  47. }
  48. /**
  49. * @depends testAddCity
  50. * @depends testUser
  51. * @depends testUserMark
  52. */
  53. public function testIndex(City $city,User $user,User $userMark){
  54. $response=$this->actingAs($user)->get('maintenance/city');
  55. $response->assertOk();
  56. $responseMark=$this->actingAs($userMark)->get('maintenance/city');
  57. $responseMark->assertStatus(302)->assertRedirect('/');
  58. $city=City::paginate(50);
  59. $this->assertNotEmpty($city);
  60. }
  61. /**
  62. * @depends testUser
  63. * @depends testUserMark
  64. */
  65. public function testCreate(User $user,User $userMark){
  66. $response=$this->actingAs($user)->get('maintenance/city/create');
  67. $response->assertOk()->assertSee('城市名称');
  68. $responseMark=$this->actingAs($userMark)->get('maintenance/city/create');
  69. $responseMark->assertStatus(302)->assertRedirect('/');
  70. }
  71. /**
  72. * @depends testUser
  73. * @depends testUserMark
  74. * */
  75. public function testStore(User $user,User $userMark){
  76. $response=$this->actingAs($user)->post('maintenance/city',['City'=>[
  77. 'name'=>'测试Two',
  78. 'province_id'=>1,
  79. ]]);
  80. $response->assertStatus(302)->assertRedirect('maintenance/city')->assertSessionHas('successTip');
  81. $responseMark=$this->actingAs($userMark)->post('maintenance/city',['City'=>[
  82. 'name'=>'测试Two',
  83. 'province_id'=>1,
  84. ]]);
  85. $responseMark->assertStatus(302)->assertRedirect('/');
  86. $city=City::where('name','=','测试Two')->first();
  87. $result=$city->delete();
  88. $this->assertTrue($result);
  89. }
  90. /**
  91. * @depends testAddCity
  92. * @depends testUser
  93. * @depends testUserMark
  94. */
  95. public function testEdit(City $city,User $user,User $userMark){
  96. $response=$this->actingAs($user)->get("maintenance/city/$city->id/edit");
  97. $response->assertOk()->assertSee('测试');
  98. $responseMark=$this->actingAs($userMark)->get("maintenance/city/$city->id/edit");
  99. $responseMark->assertStatus(302)->assertRedirect('/');
  100. }
  101. /**
  102. * @depends testAddCity
  103. * @depends testUser
  104. * @depends testUserMark
  105. */
  106. public function testUpdate(City $city,User $user,User $userMark){
  107. $response=$this->actingAs($user)->put("maintenance/city/$city->id",['City'=>[
  108. 'name'=>'测试Update',
  109. 'province_id'=>1,
  110. ]]);
  111. $response->assertStatus(302)->assertRedirect('maintenance/city')->assertSessionHas('successTip');
  112. $responseMark=$this->actingAs($userMark)->put("maintenance/city/$city->id",['City'=>[
  113. 'name'=>'测试Update',
  114. 'province_id'=>1,
  115. ]]);
  116. $responseMark->assertStatus(302)->assertRedirect('/');
  117. }
  118. /**
  119. * @depends testAddCity
  120. * @depends testUser
  121. * @depends testUserMark
  122. * @depends testRole
  123. */
  124. public function testDestroy(City $city,User $user,User $userMark,Role $role){
  125. $response=$this->actingAs($user)->json('delete','maintenance/city/'.$city->id,['id'=>$city->id]);
  126. $response->assertOk()->assertJson(['success'=>true]);
  127. $responseMark=$this->actingAs($userMark)->json('delete','maintenance/city/'.$city->id,['id'=>$city->id]);
  128. $responseMark->assertStatus(302)->assertRedirect('/');
  129. $result=$user->delete();
  130. $this->assertTrue($result);
  131. $this->assertNotEmpty($userMark->id);
  132. $resultUserMark=$userMark->delete();
  133. $this->assertTrue($resultUserMark);
  134. $resultRole=$role->delete();
  135. $this->assertTrue($resultRole);
  136. DB::table('authority_role')->where('id_role','=',$role->id)->delete();
  137. DB::table('user_role')->where('id_role','=',$role->id)->delete();
  138. }
  139. }