OwnerPriceDirectLogisticServiceTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Tests\Services\OwnerPriceDirectLogisticService;
  3. use App\CarType;
  4. use App\Owner;
  5. use App\OwnerPriceDirectLogistic;
  6. use App\OwnerPriceDirectLogisticCar;
  7. use App\Services\OwnerPriceDirectLogisticService;
  8. use Ramsey\Uuid\Uuid;
  9. use Tests\TestCase;
  10. class OwnerPriceDirectLogisticServiceTest extends TestCase
  11. {
  12. /** @var OwnerPriceDirectLogisticService */
  13. public $service;
  14. public $data;
  15. protected function setUp(): void
  16. {
  17. parent::setUp();
  18. $this->service = app(OwnerPriceDirectLogisticService::class);
  19. $this->data["models"] = factory(OwnerPriceDirectLogistic::class,3)->create()->toArray();
  20. }
  21. /**
  22. * @group customer
  23. */
  24. public function testPaginate()
  25. {
  26. $models = $this->service->paginate();
  27. $this->assertGreaterThanOrEqual(3,count($models));
  28. $this->assertLessThanOrEqual(50,count($models));
  29. $models = $this->service->paginate($this->data["models"][0]["id"]);
  30. $this->assertCount(1,$models);
  31. }
  32. /**
  33. * @group customer
  34. */
  35. public function testCreate()
  36. {
  37. $model = $this->service->create([
  38. "name" => Uuid::uuid1(),
  39. "base_km" => 3
  40. ]);
  41. $this->assertNotNull($model);
  42. $this->data["models"][] = $model->toArray();
  43. }
  44. /**
  45. * @group customer
  46. */
  47. public function testDestroy()
  48. {
  49. $result = $this->service->destroy(array_column($this->data["models"],"id"));
  50. $this->assertEquals(3,$result);
  51. $this->data["models"] = [];
  52. }
  53. /**
  54. * @group customer
  55. */
  56. public function testFind()
  57. {
  58. $model = $this->service->find($this->data["models"][0]["id"]);
  59. $this->assertNotNull($model);
  60. }
  61. /**
  62. * @group customer
  63. */
  64. public function testUpdate()
  65. {
  66. $id = $this->data["models"][0]["id"];
  67. $result = $this->service->update(["id"=>$id],[
  68. "base_km" => 3
  69. ]);
  70. $this->assertEquals(1,$result);
  71. $model = OwnerPriceDirectLogistic::query()->find($id);
  72. $this->assertEquals(3,$model->base_km);
  73. }
  74. /**
  75. * @group customer
  76. */
  77. public function testUpdateDetail()
  78. {
  79. $car = factory(\App\CarType::class)->create();
  80. $model = factory(OwnerPriceDirectLogisticCar::class)->create([
  81. "owner_price_direct_logistic_id" => $this->data["models"][0]["id"], //直发车计费ID
  82. "car_type_id" => $car->id, //车型ID
  83. ]);
  84. $result = $this->service->updateDetail(["id"=>$model->id],["base_fee"=>0.01]);
  85. $this->assertEquals(1,$result);
  86. OwnerPriceDirectLogisticCar::destroy($model->id);
  87. CarType::destroy($car->id);
  88. }
  89. /**
  90. * @group customer
  91. */
  92. public function testIsExistDetail()
  93. {
  94. $car = factory(\App\CarType::class)->create();
  95. $model = factory(OwnerPriceDirectLogisticCar::class)->create([
  96. "owner_price_direct_logistic_id" => $this->data["models"][0]["id"], //直发车计费ID
  97. "car_type_id" => $car->id, //车型ID
  98. ]);
  99. $result = $this->service->isExistDetail(["owner_price_direct_logistic_id"=>$this->data["models"][0]["id"]]);
  100. $this->assertGreaterThan(0,$result);
  101. OwnerPriceDirectLogisticCar::destroy($model->id);
  102. CarType::destroy($car->id);
  103. }
  104. /**
  105. * @group customer
  106. */
  107. public function testCreatedDetail()
  108. {
  109. $model = $this->service->createDetail([
  110. "owner_price_direct_logistic_id" => $this->data["models"][0]["id"], //直发车计费ID
  111. "car_type_id" => 1,
  112. "base_fee" => 1,
  113. "additional_fee" => 1,
  114. ]);
  115. $this->assertNotNull($model);
  116. OwnerPriceDirectLogisticCar::destroy($model->id);
  117. }
  118. /**
  119. * @group customer
  120. */
  121. public function testDestroyDetail()
  122. {
  123. $car = factory(\App\CarType::class)->create();
  124. $model = factory(OwnerPriceDirectLogisticCar::class)->create([
  125. "owner_price_direct_logistic_id" => $this->data["models"][0]["id"], //直发车计费ID
  126. "car_type_id" => $car->id, //车型ID
  127. ]);
  128. $result = $this->service->destroyDetail($model->id);
  129. $this->assertEquals(1,$result);
  130. CarType::destroy($car->id);
  131. }
  132. /**
  133. * @group customer
  134. */
  135. public function testGetExistOwnerName()
  136. {
  137. $owner = factory(Owner::class)->create([
  138. "user_owner_group_id" => 1,
  139. ]);
  140. /** @var OwnerPriceDirectLogistic $model */
  141. $model = OwnerPriceDirectLogistic::query()->find($this->data["models"][0]["id"]);
  142. $model->owners()->sync([$owner->id]);
  143. $result = $this->service->getExistOwnerName($owner->id,$this->data["models"][0]["id"]);
  144. $this->assertCount(0,$result);
  145. $result = $this->service->getExistOwnerName($owner->id,null);
  146. $this->assertCount(1,$result);
  147. $this->assertEquals($owner->name,$result[0]);
  148. Owner::destroy($owner->id);
  149. $model->owners()->sync([]);
  150. }
  151. /**
  152. * @group customer
  153. */
  154. public function testMatching()
  155. {
  156. $model = factory(OwnerPriceDirectLogistic::class)->create([
  157. "base_km" => 2,
  158. ]);
  159. $this->data["models"][] = $model->toArray();
  160. $owner = factory(Owner::class)->create([
  161. "user_owner_group_id" => 1,
  162. ]);
  163. $model->owners()->sync([$owner->id]);
  164. $cars = factory(\App\CarType::class,2)->create();
  165. $item1 = factory(OwnerPriceDirectLogisticCar::class)->create([
  166. "owner_price_direct_logistic_id" => $model->id, //直发车计费ID
  167. "car_type_id" => $cars[0]->id, //车型ID
  168. "base_fee" => 1.5, //起步费
  169. "additional_fee" =>2.5, //续费(元/KM)
  170. ]);
  171. $item2 = factory(OwnerPriceDirectLogisticCar::class)->create([
  172. "owner_price_direct_logistic_id" => $model->id, //直发车计费ID
  173. "car_type_id" => $cars[1]->id, //车型ID
  174. "base_fee" =>1.3, //起步费
  175. "additional_fee" =>1.6, //续费(元/KM)
  176. ]);
  177. $result = $this->service->matching(10,$owner->id,$cars[0]->id);
  178. $this->assertEquals(23,$result);
  179. $result = $this->service->matching(10,$owner->id,$cars[1]->id);
  180. $this->assertEquals(15.4,$result);
  181. $model->owners()->sync([]);
  182. Owner::destroy($owner->id);
  183. CarType::destroy(array_column($cars->toArray(),"id"));
  184. OwnerPriceDirectLogisticCar::destroy([$item1->id,$item2->id]);
  185. }
  186. public function tearDown(): void
  187. {
  188. OwnerPriceDirectLogistic::destroy(array_column($this->data["models"],"id"));
  189. parent::tearDown();
  190. }
  191. }