CarrierTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Authority;
  4. use App\Carrier;
  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 CarrierTest 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 testAddCarrier(){
  39. $carrier=new Carrier([
  40. 'name'=>'货拉拉',
  41. 'mobile'=>'18601677143',
  42. 'remark'=>'测试用例'
  43. ]);
  44. $result=$carrier->save();
  45. $this->assertTrue($result);
  46. $this->assertNotEmpty($carrier);
  47. return $carrier;
  48. }
  49. /**
  50. * @depends testUser
  51. * @depends testAddCarrier
  52. * @depends testUserMark
  53. */
  54. public function testIndex(User $user,Carrier $carrier,User $userMark){
  55. $response=$this->actingAs($user)->get('maintenance/carrier');
  56. $response->assertOk()->assertSee('货拉拉');
  57. $responseMark=$this->actingAs($userMark)->get('maintenance/carrier');
  58. $responseMark->assertStatus(302)->assertRedirect('/');
  59. $carrier=Carrier::paginate(10);
  60. $this->assertNotEmpty($carrier);
  61. }
  62. /**
  63. * @depends testUser
  64. * @depends testUserMark
  65. */
  66. public function testCreate(User $user,User $userMark){
  67. $response=$this->actingAs($user)->get('maintenance/carrier/create');
  68. $response->assertOk();
  69. $responseMark=$this->actingAs($userMark)->get('maintenance/carrier/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/carrier',['Carrier'=>[
  78. 'name'=>'德邦',
  79. 'mobile'=>'18452365478',
  80. 'remark'=>'测试'
  81. ]]);
  82. $response->assertStatus(302)->assertRedirect('maintenance/carrier')->assertSessionHas('successTip');
  83. $responseMark=$this->actingAs($userMark)->post('maintenance/carrier',['Carrier'=>[
  84. 'name'=>'德邦',
  85. 'mobile'=>'18452365478',
  86. 'remark'=>'测试'
  87. ]]);
  88. $responseMark->assertStatus(302)->assertRedirect('/');
  89. $carrier=Carrier::where('name','=','德邦')->first();
  90. $result=$carrier->delete();
  91. $this->assertTrue($result);
  92. }
  93. /**
  94. * @depends testUser
  95. * @depends testAddCarrier
  96. * @depends testUserMark
  97. */
  98. public function testEdit(User $user,Carrier $carrier,User $userMark){
  99. $response=$this->actingAs($user)->get("maintenance/carrier/$carrier->id/edit");
  100. $response->assertOk();
  101. $responseMark=$this->actingAs($userMark)->get("maintenance/carrier/$carrier->id/edit");
  102. $responseMark->assertStatus(302)->assertRedirect('/');
  103. }
  104. /**
  105. * @depends testUser
  106. * @depends testAddCarrier
  107. * @depends testUserMark
  108. */
  109. public function testUpdate(User $user,Carrier $carrier,User $userMark){
  110. $response=$this->actingAs($user)->put("maintenance/carrier/$carrier->id",['Carrier'=>[
  111. 'name'=>'辉腾',
  112. 'mobile'=>'021587468547',
  113. 'remark'=>'测试更改'
  114. ]]);
  115. $response->assertStatus(302)->assertRedirect('maintenance/carrier')->assertSessionHas('successTip');
  116. $responseMark=$this->actingAs($userMark)->put("maintenance/carrier/$carrier->id",['Carrier'=>[
  117. 'name'=>'辉腾',
  118. 'mobile'=>'021587468547',
  119. 'remark'=>'测试更改'
  120. ]]);
  121. $responseMark->assertStatus(302)->assertRedirect('/');
  122. }
  123. /**
  124. * @depends testUser
  125. * @depends testAddCarrier
  126. * @depends testUserMark
  127. * @depends testRole
  128. */
  129. public function testDestroy(User $user,Carrier $carrier,User $userMark,Role $role){
  130. $response=$this->actingAs($user)->json('delete','/maintenance/carrier/'.$carrier->id,['id'=>$carrier->id]);
  131. $response->assertOk();
  132. $responseMark=$this->actingAs($userMark)->json('delete','/maintenance/carrier/'.$carrier->id,['id'=>$carrier->id]);
  133. $responseMark->assertStatus(302)->assertRedirect('/');
  134. $response->assertJson(['success'=>true]);
  135. $result=$user->delete();
  136. $this->assertTrue($result);
  137. $this->assertNotEmpty($userMark->id);
  138. $resultUserMark=$userMark->delete();
  139. $this->assertTrue($resultUserMark);
  140. $resultRole=$role->delete();
  141. $this->assertTrue($resultRole);
  142. DB::table('authority_role')->where('id_role','=',$role->id)->delete();
  143. DB::table('user_role')->where('id_role','=',$role->id)->delete();
  144. }
  145. }