WaybillTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace Tests\Unit;
  3. use App\WaybillAuditLog;
  4. use App\Authority;
  5. use App\WaybillPriceModel;
  6. use App\Role;
  7. use App\User;
  8. use App\Waybill;
  9. use App\WaybillPayoff;
  10. use App\WaybillFinancialExcepted;
  11. use App\WaybillFinancialSnapshot;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Request;
  14. use Tests\TestCase;
  15. use Illuminate\Foundation\Testing\WithFaker;
  16. use Illuminate\Foundation\Testing\RefreshDatabase;
  17. class WaybillTest extends TestCase
  18. {
  19. public function testUserMark(){
  20. $userMark=factory(User::class)->create();
  21. $this->assertNotEmpty($userMark->id);
  22. return $userMark;
  23. }
  24. public function testRole(){
  25. $role=Role::create([
  26. 'name'=>'测试admin'
  27. ]);
  28. $this->assertNotEmpty($role->id);
  29. $authorities= Authority::get();
  30. foreach ($authorities as $authority){
  31. DB::table('authority_role')->insert(['id_authority'=>$authority->id,'id_role'=>$role->id]);
  32. }
  33. return $role;
  34. }
  35. /**
  36. * @depends testRole
  37. */
  38. public function testUser(Role $role){
  39. $user=factory(User::class)->create();
  40. DB::table('user_role')->insert(['id_user'=>$user->id,'id_role'=>$role->id]);
  41. $this->assertNotEmpty($user->id);
  42. return $user;
  43. }
  44. public function testAddWaybill(){
  45. $waybill=new Waybill([
  46. 'type'=>'直发车',
  47. 'state'=>'未审核',
  48. 'waybill_number'=>'BSZX12345848854',
  49. 'owner_id'=>2,
  50. 'wms_bill_number'=>'zx1858545465',
  51. 'origination'=>'上海松江',
  52. 'destination'=>'郑州中原',
  53. 'recipient'=>'elder sister',
  54. 'recipient_mobile'=>'18458741254',
  55. 'charge'=>586,
  56. 'ordering_remark'=>'禁'
  57. ]);
  58. $result=$waybill->save();
  59. $this->assertTrue($result);
  60. $this->assertNotEmpty($waybill);
  61. return $waybill;
  62. }
  63. /**
  64. * @depends testUser
  65. * @depends testAddWaybill
  66. * @depends testUserMark
  67. */
  68. public function testIndex(User $user,Waybill $waybill,User $userMark){
  69. $responseFalse=$this->actingAs($user)->get('waybill',[
  70. 'waybill_number'=>'BSZX12345848854'
  71. ]);
  72. $responseFalse->assertOk()->assertSee('BSZX12345848854');
  73. $responseMark=$this->actingAs($userMark)->get('waybill',[
  74. 'waybill_number'=>'BSZX12345848854'
  75. ]);
  76. $responseMark->assertStatus(302)->assertRedirect('/');
  77. $responseTrue=$this->actingAs($user)->get('waybill');
  78. $responseTrue->assertOk()->assertSee('BSZX12345848854');
  79. }
  80. /**
  81. * @depends testUser
  82. * @depends testUserMark
  83. */
  84. public function testCreate(User $user,User $userMark){
  85. $response=$this->actingAs($user)->get('waybill/createZX');
  86. $response->assertOk()->assertSee('WMS单号');
  87. $responseMark=$this->actingAs($userMark)->get('waybill/createZX');
  88. $responseMark->assertStatus(302)->assertRedirect('/');
  89. }
  90. /**
  91. * @depends testUser
  92. * @depends testUserMark
  93. */
  94. public function testStore(User $user,User $userMark){
  95. $response=$this->actingAs($user)->post('waybill',[
  96. 'type'=>'专线',
  97. 'owner_id'=>1,
  98. 'wms_bill_number'=>'ad544da6584',
  99. 'origination'=>'上海',
  100. 'destination'=>'商丘',
  101. 'recipient'=>'周某',
  102. 'recipient_mobile'=>'01547896548',
  103. 'charge'=>58.6,
  104. 'ordering_remark'=>''
  105. ]);
  106. $response->assertStatus(302)->assertRedirect('waybill')->assertSessionHas('successTip');
  107. $responseMark=$this->actingAs($userMark)->post('waybill',[
  108. 'type'=>'专线',
  109. 'owner_id'=>1,
  110. 'wms_bill_number'=>'ad544da6584',
  111. 'origination'=>'上海',
  112. 'destination'=>'商丘',
  113. 'recipient'=>'周某',
  114. 'recipient_mobile'=>'01547896548',
  115. 'charge'=>58.6,
  116. 'ordering_remark'=>''
  117. ]);
  118. $responseMark->assertStatus(302)->assertRedirect('/');
  119. }
  120. /**
  121. * @depends testUser
  122. * @depends testAddWaybill
  123. * @depends testUserMark
  124. */
  125. public function testEdit(User $user,Waybill $waybill,User $userMark){
  126. $response=$this->actingAs($user)->get('waybill/'.$waybill->id.'/edit');
  127. $response->assertOk()->assertSee('BSZX12345848854');
  128. $responseMark=$this->actingAs($userMark)->get('waybill/'.$waybill->id.'/edit');
  129. $responseMark->assertStatus(302)->assertRedirect('/');
  130. }
  131. /**
  132. * @depends testUser
  133. * @depends testAddWaybill
  134. * @depends testUserMark
  135. */
  136. public function testUpdate(User $user,Waybill $waybill,User $userMark){
  137. //直发车数据校验通过
  138. $responseZFTrue=$this->actingAs($user)->put('waybill/'.$waybill->id,[
  139. 'type'=>'直发车',
  140. 'carrier_id'=>1,
  141. 'carrier_bill'=>'0154786548',
  142. 'carType_id'=>1,
  143. 'fee'=>500,
  144. 'other_fee'=>20,
  145. 'collect_fee'=>30,
  146. 'dispatch_remark'=>''
  147. ]);
  148. $responseZFTrue->assertStatus(302)->assertRedirect('waybill')->assertSessionHas('successTip');
  149. $responseZFMark=$this->actingAs($userMark)->put('waybill/'.$waybill->id,[
  150. 'type'=>'直发车',
  151. 'carrier_id'=>1,
  152. 'carrier_bill'=>'0154786548',
  153. 'carType_id'=>1,
  154. 'fee'=>500,
  155. 'other_fee'=>20,
  156. 'collect_fee'=>30,
  157. 'dispatch_remark'=>''
  158. ]);
  159. $responseZFMark->assertStatus(302)->assertRedirect('/');
  160. $waybillPayoffs=WaybillPayoff::where('waybill_id','=',$waybill->id)->first();
  161. $this->assertNotEmpty($waybillPayoffs);
  162. $this->assertEquals(490.00,$waybillPayoffs->total_expense);
  163. $this->assertEquals(586.00,$waybillPayoffs->total_receivable);
  164. $this->assertEquals(96.00,$waybillPayoffs->gross_margin);
  165. //直发车数据校验失败
  166. $responseZFFalse=$this->actingAs($user)->put('waybill/'.$waybill->id,[
  167. 'type'=>'直发车',
  168. 'carrier_id'=>1,
  169. 'carrier_bill'=>'20',
  170. 'other_fee'=>'a456d',
  171. 'collect_fee'=>30,
  172. 'dispatch_remark'=>''
  173. ]);
  174. $responseZFFalse->assertStatus(302)->assertRedirect('/'); //保留
  175. //专线
  176. $waybillZX=new Waybill([
  177. 'type'=>'专线',
  178. 'state'=>'未审核',
  179. 'waybill_number'=>'BSZX12345146854854',
  180. 'owner_id'=>2,
  181. 'wms_bill_number'=>'01547860548',
  182. 'origination'=>'上海',
  183. 'destination'=>'郑州',
  184. 'recipient'=>'elder sister',
  185. 'recipient_mobile'=>'12364851478',
  186. 'charge'=>586,
  187. 'ordering_remark'=>'禁'
  188. ]);
  189. $waybillZX->save();
  190. $this->assertNotEmpty($waybillZX);
  191. //未定义计费模型
  192. $responseZXFalse=$this->actingAs($user)->put('waybill/'.$waybillZX->id,[
  193. 'type'=>'专线',
  194. 'carrier_id'=>1,
  195. 'carrier_bill'=>'01547860548',
  196. 'origination_city_id'=>1,
  197. 'destination_city_id'=>2,
  198. 'warehouse_weight'=>25,
  199. 'warehouse_weight_unit_id'=>1,
  200. 'carrier_weight'=>25,
  201. 'carrier_weight_unit_id'=>1,
  202. 'pick_up_fee'=>500,
  203. 'other_fee'=>20,
  204. 'dispatch_remark'=>''
  205. ]);
  206. $responseZXFalse->assertStatus(302)->assertRedirect('waybill');
  207. $responseZXMark=$this->actingAs($userMark)->put('waybill/'.$waybillZX->id,[
  208. 'type'=>'专线',
  209. 'carrier_id'=>1,
  210. 'carrier_bill'=>'01547860548',
  211. 'origination_city_id'=>1,
  212. 'destination_city_id'=>2,
  213. 'warehouse_weight'=>25,
  214. 'warehouse_weight_unit_id'=>1,
  215. 'carrier_weight'=>25,
  216. 'carrier_weight_unit_id'=>1,
  217. 'pick_up_fee'=>500,
  218. 'other_fee'=>20,
  219. 'dispatch_remark'=>''
  220. ]);
  221. $responseZXMark->assertStatus(302)->assertRedirect('/');
  222. $fee=Waybill::find($waybillZX->id);
  223. $this->assertEquals(null,$fee->fee);
  224. $waybillPayoffsZX=WaybillPayoff::where('waybill_id','=',$waybillZX->id)->first();
  225. $this->assertNotEmpty($waybillPayoffsZX);
  226. $waybillPayoffsZX->delete();
  227. //清除冗余数据
  228. $result=$waybillZX->delete();
  229. $this->assertTrue($result);
  230. }
  231. /**
  232. * @depends testUser
  233. * @depends testAddWaybill
  234. * @depends testUserMark
  235. */
  236. public function testWaybillUpdate(User $user,Waybill $waybill,User $userMark){
  237. $response=$this->actingAs($user)->put('waybill/'.$waybill->id,[
  238. 'type'=>'直发车',
  239. 'carrier_id'=>2,
  240. 'carrier_bill'=>'5a4d664585',
  241. 'carType_id'=>2,
  242. 'fee'=>600,
  243. 'other_fee'=>30,
  244. 'collect_fee'=>50,
  245. 'dispatch_remark'=>'测试更改'
  246. ]);
  247. $response->assertStatus(302)->assertRedirect('waybill')->assertSessionHas('successTip');
  248. $responseMark=$this->actingAs($userMark)->put('waybill/'.$waybill->id,[
  249. 'type'=>'直发车',
  250. 'carrier_id'=>2,
  251. 'carrier_bill'=>'5a4d664585',
  252. 'carType_id'=>2,
  253. 'fee'=>600,
  254. 'other_fee'=>30,
  255. 'collect_fee'=>50,
  256. 'dispatch_remark'=>'测试更改'
  257. ]);
  258. $responseMark->assertStatus(302)->assertRedirect('/');
  259. }
  260. /**
  261. * @depends testUser
  262. * @depends testAddWaybill
  263. * @depends testUserMark
  264. */
  265. public function testWaybillAudit(User $user,Waybill $waybill,User $userMark){
  266. //第一次请求成功
  267. $response=$this->actingAs($user)->json('post','waybill/waybillAudit',['id'=>$waybill->id]);
  268. $response->assertJson(['success'=>true,'state'=>'已审核']);
  269. $responseMark=$this->actingAs($userMark)->json('post','waybill/waybillAudit',['id'=>$waybill->id]);
  270. $responseMark->assertStatus(302)->assertRedirect('/');
  271. //第二次请求拦截
  272. $responseRe=$this->actingAs($user)->json('post','waybill/waybillAudit',['id'=>$waybill->id]);
  273. $responseRe->assertJson(['exception'=>'请勿重复审核!']);
  274. }
  275. /**
  276. * @depends testUser
  277. * @depends testAddWaybill
  278. * @depends testUserMark
  279. */
  280. public function testWaybillEdit(User $user,Waybill $waybill,User $userMark){
  281. $response=$this->actingAs($user)->get('waybill/waybillEdit/'.$waybill->id);
  282. $response->assertOk()->assertSee('上海松江');
  283. $responseMark=$this->actingAs($userMark)->get('waybill/waybillEdit/'.$waybill->id);
  284. $responseMark->assertStatus(302)->assertRedirect('/');
  285. }
  286. /**
  287. * @depends testUser
  288. * @depends testAddWaybill
  289. * @depends testUserMark
  290. */
  291. public function testWaybillRetreatAudit(User $user,Waybill $waybill,User $userMark){
  292. $response=$this->actingAs($user)->json('post','waybill/waybillRetreatAudit',['id'=>$waybill->id]);
  293. $response->assertJson(['success'=>true,'state'=>'待重审']);
  294. $responseMark=$this->actingAs($userMark)->json('post','waybill/waybillRetreatAudit',['id'=>$waybill->id]);
  295. $responseMark->assertStatus(302)->assertRedirect('/');
  296. }
  297. /**
  298. * @depends testUser
  299. * @depends testAddWaybill
  300. * @depends testUserMark
  301. */
  302. public function testWaybillEndAudit(User $user,Waybill $waybill,User $userMark){
  303. //第一次请求成功
  304. $response=$this->actingAs($user)->json('post','waybill/waybillEndAudit',['id'=>$waybill->id]);
  305. $response->assertJson(['success'=>true,'state'=>'未定义计费模型']);
  306. $responseMark=$this->actingAs($userMark)->json('post','waybill/waybillEndAudit',['id'=>$waybill->id]);
  307. $responseMark->assertStatus(302)->assertRedirect('/');
  308. //第二次请求拦截
  309. $responseRe=$this->actingAs($user)->json('post','waybill/waybillEndAudit',['id'=>$waybill->id]);
  310. $responseRe->assertJson(['exception'=>'请勿重复审核!']);
  311. }
  312. /**
  313. * @depends testAddWaybill
  314. * @depends testUser
  315. * @depends testUserMark
  316. * @depends testRole
  317. */
  318. public function testDestroy(Waybill $waybill,User $user,User $userMark,Role $role){
  319. $this->assertNotEmpty($waybill);
  320. $result=$waybill->delete();
  321. $this->assertTrue($result);
  322. $waybillPayoffs=WaybillPayoff::where('waybill_id','=',$waybill->id)->first();
  323. $this->assertNotEmpty($waybillPayoffs);
  324. $resultWaybillPayoffs=$waybillPayoffs->delete();
  325. $this->assertTrue($resultWaybillPayoffs);
  326. $waybillStore=Waybill::where('wms_bill_number','=','ad544da6584')->first();
  327. $this->assertNotEmpty($waybillStore);
  328. $resultWaybillStore=$waybillStore->delete();
  329. $this->assertTrue($resultWaybillStore);
  330. $waybillFinancialSnapshots=WaybillFinancialExcepted::where('waybill_id','=',$waybill->id)->first();
  331. $r=$waybillFinancialSnapshots->delete();
  332. $this->assertTrue($r);
  333. $isAudit=WaybillAuditLog::withTrashed()->whereRaw('waybill_id = ? and audit_stage = ?',[$waybill->id,"运单阶段"])->first();
  334. $resultWaybillAuditLogWaybill=$isAudit->forceDelete();
  335. $this->assertTrue($resultWaybillAuditLogWaybill);
  336. $waybillAuditLogDispatch=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$waybill->id,"调度阶段"])->first();
  337. $resultWaybillAuditLogDispatch=$waybillAuditLogDispatch->forceDelete();
  338. $this->assertTrue($resultWaybillAuditLogDispatch);
  339. $result=$user->delete();
  340. $this->assertTrue($result);
  341. $this->assertNotEmpty($userMark->id);
  342. $resultUserMark=$userMark->delete();
  343. $this->assertTrue($resultUserMark);
  344. $resultRole=$role->delete();
  345. $this->assertTrue($resultRole);
  346. DB::table('authority_role')->where('id_role','=',$role->id)->delete();
  347. DB::table('user_role')->where('id_role','=',$role->id)->delete();
  348. }
  349. }