CityTest.php 5.1 KB

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