WaybillTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Carrier;
  4. use App\WaybillAuditLog;
  5. use App\Authority;
  6. use App\WaybillPriceModel;
  7. use App\Role;
  8. use App\User;
  9. use App\Waybill;
  10. use App\WaybillPayoff;
  11. use App\WaybillFinancialExcepted;
  12. use App\WaybillFinancialSnapshot;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Request;
  15. use Tests\TestCase;
  16. use Illuminate\Foundation\Testing\WithFaker;
  17. use Illuminate\Foundation\Testing\RefreshDatabase;
  18. class WaybillTest extends TestCase
  19. {
  20. public function testUserMark(){
  21. $userMark=factory(User::class)->create();
  22. $this->assertNotEmpty($userMark->id);
  23. return $userMark;
  24. }
  25. public function testRole(){
  26. $role=Role::create([
  27. 'name'=>'测试admin'
  28. ]);
  29. $this->assertNotEmpty($role->id);
  30. $authorities= Authority::get();
  31. foreach ($authorities as $authority){
  32. DB::table('authority_role')->insert(['id_authority'=>$authority->id,'id_role'=>$role->id]);
  33. }
  34. return $role;
  35. }
  36. /**
  37. * @depends testRole
  38. */
  39. public function testUser(Role $role){
  40. $user=factory(User::class)->create();
  41. DB::table('user_role')->insert(['id_user'=>$user->id,'id_role'=>$role->id]);
  42. $this->assertNotEmpty($user->id);
  43. return $user;
  44. }
  45. public function testAddWaybill(){
  46. $waybill=new Waybill([
  47. 'type'=>'直发车',
  48. 'waybill_number'=>'TestBSZF12345848854',
  49. 'owner_id'=>1,
  50. 'wms_bill_number'=>'test1858545465',
  51. 'origination'=>'test',
  52. 'destination'=>'test',
  53. 'recipient'=>'test sister',
  54. 'recipient_mobile'=>'025487456',
  55. 'charge'=>586.5,
  56. 'collect_fee'=>30,
  57. 'ordering_remark'=>'test'
  58. ]);
  59. $result=$waybill->save();
  60. $this->assertTrue($result);
  61. $this->assertNotEmpty($waybill->charge);
  62. return $waybill;
  63. }
  64. public function testAddWaybillZX(){
  65. $waybillZX=new Waybill([
  66. 'type'=>'专线',
  67. 'status'=>'未审核',
  68. 'waybill_number'=>'testBSZX123451468',
  69. 'owner_id'=>1,
  70. 'wms_bill_number'=>'test01547860548',
  71. 'origination'=>'上海',
  72. 'destination'=>'郑州',
  73. 'recipient'=>'elder sister',
  74. 'recipient_mobile'=>'12364851478',
  75. 'charge'=>586,
  76. 'ordering_remark'=>'禁'
  77. ]);
  78. $waybillZX->save();
  79. $this->assertNotEmpty($waybillZX);
  80. return $waybillZX;
  81. }
  82. public function testAddWaybillPriceModel(){
  83. $waybillPriceModel=WaybillPriceModel::create([
  84. 'logistic_id'=>1,
  85. 'province_id'=>1,
  86. 'city_id'=>1,
  87. 'unit_id'=>1,
  88. 'range_min'=>0,
  89. 'range_max'=>2000,
  90. 'unit_price'=>5,
  91. 'base_fee'=>10,
  92. 'initial_weight'=>10
  93. ]);
  94. $this->assertNotEmpty($waybillPriceModel);
  95. return $waybillPriceModel;
  96. }
  97. public function testAddWaybillPriceModelTwo(){
  98. $waybillPriceModelTwo=WaybillPriceModel::create([
  99. 'logistic_id'=>1,
  100. 'province_id'=>1,
  101. 'unit_id'=>3,
  102. 'unit_price'=>5,
  103. 'base_fee'=>10,
  104. 'initial_weight'=>10
  105. ]);
  106. $this->assertNotEmpty($waybillPriceModelTwo);
  107. return $waybillPriceModelTwo;
  108. }
  109. public function testAddCarrier(){
  110. $carrier=Carrier::create([
  111. 'name'=>'test承运商',
  112. 'mobile'=>'000005416',
  113. 'remark'=>'test',
  114. 'delivery_fee'=>170
  115. ]);
  116. $this->assertNotEmpty($carrier);
  117. return $carrier;
  118. }
  119. /**
  120. * @depends testUser
  121. * @depends testAddWaybill
  122. * @depends testUserMark
  123. */
  124. public function testIndex(User $user,Waybill $waybill,User $userMark){
  125. $responseFalse=$this->actingAs($user)->get('waybill/index',[
  126. 'waybill_number'=>'TestBSZF12345848854'
  127. ]);
  128. $responseFalseZX=$this->actingAs($user)->get('waybill/index/ZX');
  129. $responseFalseZF=$this->actingAs($user)->get('waybill/index/ZF');
  130. $responseFalseZX->assertOk();
  131. $responseFalseZF->assertOk();
  132. $responseFalse->assertOk()->assertSee('TestBSZF12345848854');
  133. $responseMark=$this->actingAs($userMark)->get('waybill/index',[
  134. 'waybill_number'=>'TestBSZF12345848854'
  135. ]);
  136. $responseMark->assertStatus(302)->assertRedirect('/');
  137. $responseTrue=$this->actingAs($user)->get('waybill');
  138. $responseTrue->assertOk()->assertSee('TestBSZF12345848854');
  139. }
  140. /**
  141. * @depends testUser
  142. * @depends testUserMark
  143. */
  144. public function testCreate(User $user,User $userMark){
  145. $response=$this->actingAs($user)->get('waybill/create/ZX');
  146. $response->assertOk()->assertSee('WMS单号');
  147. $responseZF=$this->actingAs($user)->get('waybill/create/ZF');
  148. $responseZF->assertOk();
  149. $responseMark=$this->actingAs($userMark)->get('waybill/create/ZX');
  150. $responseMark->assertStatus(302)->assertRedirect('/');
  151. }
  152. /**
  153. * @depends testUser
  154. * @depends testUserMark
  155. */
  156. public function testStore(User $user,User $userMark){
  157. $response=$this->actingAs($user)->post('waybill',[
  158. 'type'=>'专线',
  159. 'owner_id'=>1,
  160. 'wms_bill_number'=>'ad544da6584',
  161. 'origination'=>'test上海',
  162. 'destination'=>'test商丘',
  163. 'recipient'=>'test周某',
  164. 'recipient_mobile'=>'01547896548',
  165. 'charge'=>58.6,
  166. 'ordering_remark'=>'test'
  167. ]);
  168. $response->assertStatus(302)->assertRedirect('waybill')->assertSessionHas('successTip');
  169. $responseMark=$this->actingAs($userMark)->post('waybill',[
  170. 'type'=>'专线',
  171. 'owner_id'=>1,
  172. 'wms_bill_number'=>'ad544da6584',
  173. 'origination'=>'test上海',
  174. 'destination'=>'test商丘',
  175. 'recipient'=>'test周某',
  176. 'recipient_mobile'=>'01547896548',
  177. 'charge'=>58.6,
  178. 'ordering_remark'=>'test'
  179. ]);
  180. $responseMark->assertStatus(302)->assertRedirect('/');
  181. }
  182. /**
  183. * @depends testUser
  184. * @depends testAddWaybill
  185. * @depends testUserMark
  186. */
  187. public function testEdit(User $user,Waybill $waybill,User $userMark){
  188. $response=$this->actingAs($user)->get('waybill/'.$waybill->id.'/edit');
  189. $response->assertOk()->assertSee('TestBSZF12345848854');
  190. $responseMark=$this->actingAs($userMark)->get('waybill/'.$waybill->id.'/edit');
  191. $responseMark->assertStatus(302)->assertRedirect('/');
  192. }
  193. /**
  194. * @depends testUser
  195. * @depends testAddWaybillPriceModel
  196. * @depends testAddWaybillPriceModelTwo
  197. */
  198. public function testIsWaybillPriceModel(User $user,WaybillPriceModel $waybillPriceModel,WaybillPriceModel $waybillPriceModelTwo){
  199. $response=$this->actingAs($user)->post('waybill/is/waybillPriceModel',[
  200. 'logistic_id'=>'1',
  201. 'destination_city_id'=>'1',
  202. 'carrier_weight'=>['50','20'],
  203. 'carrier_weight_unit_id'=>['1','1'],
  204. ]);
  205. $response->assertOk()->assertJson(['success'=>$waybillPriceModel->id]);
  206. $response=$this->actingAs($user)->post('waybill/is/waybillPriceModel',[
  207. 'logistic_id'=>'2',
  208. 'destination_city_id'=>'2',
  209. 'carrier_weight'=>['50','20'],
  210. 'carrier_weight_unit_id'=>['1','1'],
  211. ]);
  212. $response->assertOk()->assertJson(['success'=>false]);
  213. $response=$this->actingAs($user)->post('waybill/is/waybillPriceModel',[
  214. 'logistic_id'=>'1',
  215. 'destination_city_id'=>'2',
  216. 'carrier_weight'=>['50','20'],
  217. 'carrier_weight_unit_id'=>['1','1'],
  218. ]);
  219. $response->assertOk()->assertJson(['success'=>$waybillPriceModelTwo->id]);
  220. }
  221. /**
  222. * @depends testUser
  223. * @depends testAddWaybill
  224. * @depends testUserMark
  225. * @depends testAddWaybillZX
  226. * @depends testAddWaybillPriceModel
  227. * @depends testAddCarrier
  228. */
  229. public function testUpdate(User $user,Waybill $waybill,User $userMark,Waybill $waybillZX,WaybillPriceModel $waybillPriceModel,Carrier $carrier){
  230. //直发车数据校验通过
  231. $responseZFTrue=$this->actingAs($user)->put('waybill/'.$waybill->id,[
  232. 'type'=>'直发车',
  233. 'logistic_id'=>1,
  234. 'carrier_bill'=>'0154786548',
  235. 'carType_id'=>1,
  236. 'fee'=>500,
  237. 'other_fee'=>20,
  238. 'collect_fee'=>30,
  239. 'dispatch_remark'=>''
  240. ]);
  241. $responseZFTrue->assertStatus(302)->assertRedirect('waybill')->assertSessionHas('successTip');
  242. $responseZFMark=$this->actingAs($userMark)->put('waybill/'.$waybill->id,[
  243. 'type'=>'直发车',
  244. 'logistic_id'=>1,
  245. 'carrier_bill'=>'0154786548',
  246. 'carType_id'=>1,
  247. 'fee'=>500,
  248. 'other_fee'=>20,
  249. 'collect_fee'=>30,
  250. 'dispatch_remark'=>''
  251. ]);
  252. $responseZFMark->assertStatus(302)->assertRedirect('/');
  253. $waybillPayoffs=WaybillPayoff::where('waybill_id','=',$waybill->id)->first();
  254. $this->assertNotEmpty($waybillPayoffs);
  255. $this->assertEquals(490.00,$waybillPayoffs->total_expense);
  256. $this->assertEquals(586.50,$waybillPayoffs->total_receivable);
  257. $this->assertEquals(96.50,$waybillPayoffs->gross_margin);
  258. //直发车数据校验失败
  259. $responseZFFalse=$this->actingAs($user)->put('waybill/'.$waybill->id,[
  260. 'type'=>'直发车',
  261. 'logistic_id'=>1,
  262. 'carrier_bill'=>'20',
  263. 'other_fee'=>'a456d',
  264. 'collect_fee'=>30,
  265. 'dispatch_remark'=>''
  266. ]);
  267. $responseZFFalse->assertStatus(302)->assertRedirect('/'); //保留
  268. //无模型
  269. $responseZXFalse=$this->actingAs($user)->put('waybill/'.$waybillZX->id,[
  270. 'type'=>'专线',
  271. 'logistic_id'=>1,
  272. 'carrier_bill'=>'01547860548',
  273. 'origination_city_id'=>1,
  274. 'destination_city_id'=>2,
  275. 'warehouse_weight'=>25,
  276. 'warehouse_weight_unit_id'=>1,
  277. 'carrier_weight'=>25,
  278. 'carrier_weight_unit_id'=>1,
  279. 'pick_up_fee'=>500,
  280. 'other_fee'=>20,
  281. 'dispatch_remark'=>''
  282. ]);
  283. $responseZXFalse->assertStatus(302)->assertRedirect('waybill');
  284. $responseZXMark=$this->actingAs($userMark)->put('waybill/'.$waybillZX->id,[
  285. 'type'=>'专线',
  286. 'logistic_id'=>1,
  287. 'carrier_bill'=>'01547860548',
  288. 'origination_city_id'=>1,
  289. 'destination_city_id'=>2,
  290. 'warehouse_weight'=>25,
  291. 'warehouse_weight_unit_id'=>1,
  292. 'carrier_weight'=>25,
  293. 'carrier_weight_unit_id'=>1,
  294. 'pick_up_fee'=>500,
  295. 'other_fee'=>20,
  296. 'dispatch_remark'=>''
  297. ]);
  298. $responseZXMark->assertStatus(302)->assertRedirect('/');
  299. $fee=Waybill::find($waybillZX->id);
  300. $this->assertEquals(null,$fee->fee);
  301. //有模型
  302. $responseZX=$this->actingAs($user)->put('waybill/'.$waybillZX->id,[
  303. 'type'=>'专线',
  304. 'logistic_id'=>$carrier->id,
  305. 'carrier_bill'=>'test01547860548',
  306. 'origination_city_id'=>1,
  307. 'destination_city_id'=>1,
  308. 'warehouse_weight'=>25,
  309. 'warehouse_weight_unit_id'=>1,
  310. 'carrier_weight'=>50,
  311. 'carrier_weight_unit_id'=>1,
  312. 'carrier_weight_other'=>5,
  313. 'carrier_weight_unit_id_other'=>2,
  314. 'pick_up_fee'=>20,
  315. 'other_fee'=>20,
  316. 'waybillPriceModel'=>$waybillPriceModel->id,
  317. 'dispatch_remark'=>''
  318. ]);
  319. $responseZX->assertStatus(302)->assertRedirect('waybill');
  320. $waybillPayoff=WaybillPayoff::where('waybill_id',$waybillZX->id)->first();
  321. $this->assertNotEmpty($waybillPayoff);
  322. $this->assertEquals(460.00,$waybillPayoff->total_expense);
  323. $this->assertEquals(126.00,$waybillPayoff->gross_margin);
  324. }
  325. /**
  326. * @depends testUser
  327. * @depends testAddWaybill
  328. * @depends testUserMark
  329. */
  330. public function testWaybillUpdate(User $user,Waybill $waybill,User $userMark){
  331. $response=$this->actingAs($user)->put('waybill/'.$waybill->id,[
  332. 'type'=>'直发车',
  333. 'logistic_id'=>2,
  334. 'carrier_bill'=>'5a4d664585',
  335. 'carType_id'=>2,
  336. 'fee'=>600,
  337. 'other_fee'=>30,
  338. 'collect_fee'=>50,
  339. 'dispatch_remark'=>'测试更改'
  340. ]);
  341. $response->assertStatus(302)->assertRedirect('waybill')->assertSessionHas('successTip');
  342. $responseMark=$this->actingAs($userMark)->put('waybill/'.$waybill->id,[
  343. 'type'=>'直发车',
  344. 'logistic_id'=>2,
  345. 'carrier_bill'=>'5a4d664585',
  346. 'carType_id'=>2,
  347. 'fee'=>600,
  348. 'other_fee'=>30,
  349. 'collect_fee'=>50,
  350. 'dispatch_remark'=>'测试更改'
  351. ]);
  352. $responseMark->assertStatus(302)->assertRedirect('/');
  353. }
  354. /**
  355. * @depends testUser
  356. * @depends testAddWaybill
  357. * @depends testUserMark
  358. */
  359. public function testWaybillAudit(User $user,Waybill $waybill,User $userMark){
  360. //第一次请求成功
  361. $response=$this->actingAs($user)->json('post','waybill/waybillAudit',['id'=>$waybill->id]);
  362. $response->assertJson(['success'=>true,'status'=>'已审核']);
  363. $responseMark=$this->actingAs($userMark)->json('post','waybill/waybillAudit',['id'=>$waybill->id]);
  364. $responseMark->assertStatus(302)->assertRedirect('/');
  365. //第二次请求拦截
  366. $responseRe=$this->actingAs($user)->json('post','waybill/waybillAudit',['id'=>$waybill->id]);
  367. $responseRe->assertJson(['exception'=>'请勿重复审核!']);
  368. }
  369. /**
  370. * @depends testUser
  371. * @depends testAddWaybill
  372. * @depends testUserMark
  373. */
  374. public function testWaybillEdit(User $user,Waybill $waybill,User $userMark){
  375. $response=$this->actingAs($user)->get('waybill/waybillEdit/'.$waybill->id);
  376. $response->assertOk();
  377. $responseMark=$this->actingAs($userMark)->get('waybill/waybillEdit/'.$waybill->id);
  378. $responseMark->assertStatus(302)->assertRedirect('/');
  379. }
  380. /**
  381. * @depends testUser
  382. * @depends testAddWaybill
  383. * @depends testUserMark
  384. */
  385. public function testWaybillRetreatAudit(User $user,Waybill $waybill,User $userMark){
  386. $response=$this->actingAs($user)->json('post','waybill/waybillRetreatAudit',['id'=>$waybill->id]);
  387. $response->assertJson(['success'=>true,'status'=>'待重审']);
  388. $responseMark=$this->actingAs($userMark)->json('post','waybill/waybillRetreatAudit',['id'=>$waybill->id]);
  389. $responseMark->assertStatus(302)->assertRedirect('/');
  390. }
  391. /**
  392. * @depends testUser
  393. * @depends testAddWaybill
  394. * @depends testUserMark
  395. * @depends testAddWaybillZX
  396. */
  397. public function testWaybillEndAudit(User $user,Waybill $waybill,User $userMark,Waybill $waybillZX){
  398. //第一次请求成功
  399. $response=$this->actingAs($user)->json('post','waybill/waybillEndAudit',['id'=>$waybill->id]);
  400. $response->assertJson(['success'=>true]);
  401. $responseMark=$this->actingAs($userMark)->json('post','waybill/waybillEndAudit',['id'=>$waybill->id]);
  402. $responseMark->assertStatus(302)->assertRedirect('/');
  403. //异常时
  404. $waybillZX->waybill_price_model_id=null;
  405. $waybillZX->save();
  406. $response=$this->actingAs($user)->json('post','waybill/waybillEndAudit',['id'=>$waybillZX->id]);
  407. $response->assertJson(['success'=>true]);
  408. //第二次请求拦截
  409. $responseRe=$this->actingAs($user)->json('post','waybill/waybillEndAudit',['id'=>$waybill->id]);
  410. $responseRe->assertJson(['exception'=>'请勿重复审核!']);
  411. }
  412. /**
  413. * @depends testAddWaybill
  414. * @depends testUser
  415. * @depends testUserMark
  416. * @depends testRole
  417. * @depends testAddWaybillZX
  418. * @depends testAddWaybillZX
  419. * @depends testAddWaybillPriceModel
  420. * @depends testAddCarrier
  421. * @depends testAddWaybillPriceModelTwo
  422. */
  423. public function testDestroy(Waybill $waybill,User $user,User $userMark,Role $role,Waybill $waybillZX,WaybillPriceModel $waybillPriceModel,Carrier $carrier,WaybillPriceModel $waybillPriceModelTwo){
  424. $this->assertNotEmpty($waybill);
  425. $result=$waybill->delete();
  426. $this->assertTrue($result);
  427. $waybillPayoffs=WaybillPayoff::where('waybill_id','=',$waybill->id)->first();
  428. $this->assertNotEmpty($waybillPayoffs);
  429. $resultWaybillPayoffs=$waybillPayoffs->delete();
  430. $this->assertTrue($resultWaybillPayoffs);
  431. $waybillStore=Waybill::where('wms_bill_number','=','ad544da6584')->first();
  432. $this->assertNotEmpty($waybillStore);
  433. $resultWaybillStore=$waybillStore->delete();
  434. $this->assertTrue($resultWaybillStore);
  435. $waybillPayoffsZX=WaybillPayoff::where('waybill_id','=',$waybillZX->id)->first();
  436. $this->assertNotEmpty($waybillPayoffsZX);
  437. $waybillPayoffsZX->delete();
  438. $result=$waybillZX->delete();
  439. $this->assertTrue($result);
  440. $this->assertNotEmpty($waybillPriceModel);
  441. $resultWaybillPriceModel=$waybillPriceModel->delete();
  442. $this->assertTrue($resultWaybillPriceModel);
  443. $this->assertNotEmpty($waybillPriceModelTwo);
  444. $resultWaybillPriceModelTwo=$waybillPriceModelTwo->delete();
  445. $this->assertTrue($resultWaybillPriceModelTwo);
  446. $this->assertNotEmpty($carrier);
  447. $resultCarrier=$carrier->delete();
  448. $this->assertTrue($resultCarrier);
  449. $waybillFinancialSnapshots=WaybillFinancialSnapshot::where('waybill_id','=',$waybill->id)->first();
  450. $r=$waybillFinancialSnapshots->delete();
  451. $this->assertTrue($r);
  452. $waybillFinancialSnapshotsZX=WaybillFinancialExcepted::where('waybill_id','=',$waybillZX->id)->first();
  453. $resultZX=$waybillFinancialSnapshotsZX->delete();
  454. $this->assertTrue($resultZX);
  455. $isAudit=WaybillAuditLog::withTrashed()->whereRaw('waybill_id = ? and audit_stage = ?',[$waybill->id,"运单阶段"])->first();
  456. $resultWaybillAuditLogWaybill=$isAudit->forceDelete();
  457. $this->assertTrue($resultWaybillAuditLogWaybill);
  458. $waybillAuditLogDispatch=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$waybill->id,"调度阶段"])->first();
  459. $resultWaybillAuditLogDispatch=$waybillAuditLogDispatch->forceDelete();
  460. $this->assertTrue($resultWaybillAuditLogDispatch);
  461. $result=$user->delete();
  462. $this->assertTrue($result);
  463. $this->assertNotEmpty($userMark->id);
  464. $resultUserMark=$userMark->delete();
  465. $this->assertTrue($resultUserMark);
  466. $resultRole=$role->delete();
  467. $this->assertTrue($resultRole);
  468. DB::table('authority_role')->where('id_role','=',$role->id)->delete();
  469. DB::table('user_role')->where('id_role','=',$role->id)->delete();
  470. }
  471. }