WaybillPriceModelTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Authority;
  4. use App\WaybillPriceModel;
  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 WaybillPriceModelTest 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 testAddWaybillPriceModel(){
  39. $waybillPriceModel=new WaybillPriceModel([
  40. 'carrier_id'=>1,
  41. 'province_id'=>1,
  42. 'city_id'=>1,
  43. 'unit_id'=>1,
  44. 'range_min'=>0,
  45. 'range_max'=>5000,
  46. 'unit_price'=>1,
  47. 'initial_weight'=>10
  48. ]);
  49. $result=$waybillPriceModel->save();
  50. $this->assertTrue($result);
  51. $this->assertNotEmpty($waybillPriceModel);
  52. return $waybillPriceModel;
  53. }
  54. /**
  55. * @depends testAddWaybillPriceModel
  56. * @depends testUser
  57. * @depends testUserMark
  58. */
  59. public function testIndex(WaybillPriceModel $waybillPriceModel,User $user,User $userMark){
  60. $response=$this->actingAs($user)->get('maintenance/waybillPriceModel');
  61. $response->assertStatus(200)->assertSee('5000');
  62. $responseMark=$this->actingAs($userMark)->get('maintenance/waybillPriceModel');
  63. $responseMark->assertStatus(302)->assertRedirect('/');
  64. $waybillPriceModel=WaybillPriceModel::paginate(50);
  65. $this->assertNotEmpty($waybillPriceModel);
  66. }
  67. /**
  68. * @depends testUser
  69. * @depends testUserMark
  70. */
  71. public function testCreate(User $user,User $userMark){
  72. $response=$this->actingAs($user)->get('maintenance/waybillPriceModel/create');
  73. $response->assertOk();
  74. $responseMark=$this->actingAs($userMark)->get('maintenance/waybillPriceModel/create');
  75. $responseMark->assertStatus(302)->assertRedirect('/');
  76. }
  77. /**
  78. * @depends testUser
  79. * @depends testUserMark
  80. */
  81. public function testStore(User $user,User $userMark){
  82. $response=$this->actingAs($user)->post('maintenance/waybillPriceModel',['WaybillPriceModel'=>[
  83. 'carrier_id'=>12,
  84. 'province_id'=>4,
  85. 'city_id'=>1,
  86. 'unit_id'=>1,
  87. 'range_min'=>0,
  88. 'range_max'=>1000,
  89. 'unit_price'=>1,
  90. 'base_fee'=>null,
  91. 'initial_weight'=>10
  92. ]]);
  93. $response->assertStatus(302)->assertRedirect('maintenance/waybillPriceModel')->assertSessionHas('successTip');
  94. $responseMark=$this->actingAs($userMark)->post('maintenance/waybillPriceModel',['WaybillPriceModel'=>[
  95. 'carrier_id'=>12,
  96. 'province_id'=>4,
  97. 'city_id'=>3,
  98. 'unit_id'=>3,
  99. 'range_min'=>0,
  100. 'range_max'=>1000,
  101. 'unit_price'=>1,
  102. 'base_fee'=>null,
  103. 'initial_weight'=>10
  104. ]]);
  105. $responseMark->assertStatus(302)->assertRedirect('/');
  106. $waybillPriceModel=WaybillPriceModel::whereRaw('carrier_id = ? and province_id = ? and city_id=? and unit_id=? and range_min=? and range_max=? and unit_price=? and initial_weight=?',[
  107. 12,4,1,1,0.00,1000.00,0.01,10.00
  108. ])->first();
  109. $result=$waybillPriceModel->delete();
  110. $this->assertTrue($result);
  111. }
  112. /**
  113. * @depends testUser
  114. * @depends testAddWaybillPriceModel
  115. * @depends testUserMark
  116. */
  117. public function testEdit(User $user,WaybillPriceModel $waybillPriceModel,User $userMark){
  118. $response=$this->actingAs($user)->get("maintenance/waybillPriceModel/$waybillPriceModel->id/edit");
  119. $response->assertOk();
  120. $responseMark=$this->actingAs($userMark)->get("maintenance/waybillPriceModel/$waybillPriceModel->id/edit");
  121. $responseMark->assertStatus(302)->assertRedirect('/');
  122. }
  123. /**
  124. * @depends testAddWaybillPriceModel
  125. * @depends testUser
  126. * @depends testUserMark
  127. */
  128. public function testUpdate(WaybillPriceModel $waybillPriceModel,User $user,User $userMark){
  129. $response=$this->actingAs($user)->put('maintenance/waybillPriceModel/'.$waybillPriceModel->id,['WaybillPriceModel'=>[
  130. 'carrier_id'=>2,
  131. 'province_id'=>2,
  132. 'city_id'=>11,
  133. 'unit_id'=>1,
  134. 'range_min'=>500,
  135. 'range_max'=>8000,
  136. 'unit_price'=>2,
  137. 'base_fee'=>null,
  138. 'initial_weight'=>5
  139. ]]);
  140. $response->assertStatus(302)->assertRedirect('maintenance/waybillPriceModel')->assertSessionHas('successTip');
  141. $responseMark=$this->actingAs($userMark)->put('maintenance/waybillPriceModel/'.$waybillPriceModel->id,['WaybillPriceModel'=>[
  142. 'carrier_id'=>2,
  143. 'province_id'=>2,
  144. 'city_id'=>11,
  145. 'unit_id'=>2,
  146. 'range_min'=>500,
  147. 'range_max'=>8000,
  148. 'unit_price'=>2,
  149. 'base_fee'=>null,
  150. 'initial_weight'=>5
  151. ]]);
  152. $responseMark->assertStatus(302)->assertRedirect('/');
  153. }
  154. /**
  155. * @depends testAddWaybillPriceModel
  156. * @depends testUser
  157. * @depends testUserMark
  158. * @depends testRole
  159. */
  160. public function testDestroy(WaybillPriceModel $waybillPriceModel,User $user,User $userMark,Role $role){
  161. $response=$this->actingAs($user)->json('delete','maintenance/waybillPriceModel/'.$waybillPriceModel->id,['id'=>$waybillPriceModel->id]);
  162. $response->assertOk();
  163. $responseMark=$this->actingAs($userMark)->json('delete','maintenance/waybillPriceModel/'.$waybillPriceModel->id,['id'=>$waybillPriceModel->id]);
  164. $responseMark->assertStatus(302)->assertRedirect('/');
  165. $response->assertJson(['success'=>true]);
  166. $result=$user->delete();
  167. $this->assertTrue($result);
  168. $this->assertNotEmpty($userMark->id);
  169. $resultUserMark=$userMark->delete();
  170. $this->assertTrue($resultUserMark);
  171. $resultRole=$role->delete();
  172. $this->assertTrue($resultRole);
  173. DB::table('authority_role')->where('id_role','=',$role->id)->delete();
  174. DB::table('user_role')->where('id_role','=',$role->id)->delete();
  175. }
  176. }