CarrierTest.php 5.4 KB

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