| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace Tests\Services\OwnerPriceExpressService;
- use App\Logistic;
- use App\Owner;
- use App\OwnerPriceExpress;
- use App\OwnerPriceExpressProvince;
- use App\Province;
- use App\Services\OwnerPriceExpressService;
- use Illuminate\Support\Str;
- use Tests\TestCase;
- class OwnerPriceExpressServiceTest extends TestCase
- {
- /** @var OwnerPriceExpressService */
- public $service;
- public $data;
- protected function setUp(): void
- {
- parent::setUp();
- $this->service = app(OwnerPriceExpressService::class);
- $this->data["models"] = factory(OwnerPriceExpress::class,2)->create()->toArray();
- }
- /**
- * @group customer
- */
- public function testPaginate()
- {
- $models = $this->service->paginate();
- $this->assertGreaterThanOrEqual(2,count($models));
- $models = $this->service->paginate($this->data["models"][0]["id"]);
- $this->assertCount(1,$models);
- }
- /**
- * @group customer
- */
- public function testFind()
- {
- $model = $this->service->find($this->data["models"][0]["id"]);
- $this->assertNotNull($model);
- }
- /**
- * @group customer
- */
- public function testUpdateDetail()
- {
- $detail = factory(OwnerPriceExpressProvince::class)->create([
- "owner_price_express_id" => $this->data["models"][0]["id"], //快递价格ID
- ]);
- $result = $this->service->updateDetail(["id"=>$detail->id],["initial_weight_price"=>5]);
- $this->assertEquals(1,$result);
- OwnerPriceExpressProvince::destroy($detail->id);
- }
- /**
- * @group customer
- */
- public function testCreate()
- {
- $model = $this->service->create([
- "name" => md5(Str::random(8)),
- "initial_weight" => mt_rand(6,260) / 5,
- "additional_weight" => mt_rand(6,260) / 5,
- ]);
- $this->assertNotNull($model);
- $this->data["models"][] = $model->toArray();
- }
- /**
- * @group customer
- */
- public function testCreateDetail()
- {
- $model = $this->service->createDetail([
- "owner_price_express_id" => $this->data["models"][0]["id"],
- "province_id" => 1,
- "initial_weight_price" => mt_rand(6,260) / 5,
- "additional_weight_price"=>mt_rand(6,260) / 5,
- ]);
- $this->assertNotNull($model);
- $this->assertNotNull($model->id);
- OwnerPriceExpressProvince::destroy($model->id);
- }
- /**
- * @group customer
- */
- public function testIsExistDetail()
- {
- $province = Province::query()->first();
- $detail = factory(OwnerPriceExpressProvince::class)->create([
- "owner_price_express_id" => $this->data["models"][0]["id"],
- "province_id" => $province->id
- ]);
- $result = $this->service->isExistDetail([
- "owner_price_express_id"=>$this->data["models"][0]["id"],
- "province_id" => $province->id
- ]);
- $this->assertGreaterThan(0,$result);
- OwnerPriceExpressProvince::destroy($detail->id);
- }
- /**
- * @group customer
- */
- public function testDestroyDetail()
- {
- $detail = factory(OwnerPriceExpressProvince::class)->create([
- "owner_price_express_id" => $this->data["models"][0]["id"],
- ]);
- $result = $this->service->destroyDetail($detail->id);
- $this->assertEquals(1,$result);
- }
- /**
- * @group customer
- */
- public function testGetExistOwnerName()
- {
- $owner = factory(Owner::class)->create([
- "user_owner_group_id" => 1,
- ]);
- /** @var OwnerPriceExpress $model */
- $model = OwnerPriceExpress::query()->find($this->data["models"][0]["id"]);
- $model->owners()->sync([$owner->id]);
- $result = $this->service->getExistOwnerName([$owner->id],$this->data["models"][0]["id"]);
- $this->assertCount(0,$result);
- $result = $this->service->getExistOwnerName([$owner->id]);
- $this->assertCount(1,$result);
- $this->assertEquals($owner->name,$result[0]);
- Owner::destroy($owner->id);
- $model->owners()->sync([]);
- }
- /**
- * @group customer
- */
- public function testGetExistLogisticName()
- {
- $logistic = factory(Logistic::class)->create();
- /** @var OwnerPriceExpress $model */
- $model = OwnerPriceExpress::query()->find($this->data["models"][0]["id"]);
- $model->logistics()->sync([$logistic->id]);
- $result = $this->service->getExistLogisticName([$logistic->id],$this->data["models"][0]["id"]);
- $this->assertCount(0,$result);
- $result = $this->service->getExistLogisticName([$logistic->id]);
- $this->assertCount(1,$result);
- $this->assertEquals($logistic->name,$result[0]);
- Logistic::destroy($logistic->id);
- $model->logistics()->sync([]);
- }
- /**
- * @group customer
- */
- public function testDestroy()
- {
- $result = $this->service->destroy($this->data["models"][0]["id"]);
- $this->assertEquals(1,$result);
- unset($this->data["models"][0]);
- }
- /**
- * @group customer
- */
- public function testUpdate()
- {
- $result = $this->service->update(["id"=>$this->data["models"][0]["id"]],["initial_weight"=>3]);
- $this->assertEquals(1,$result);
- }
- /**
- * @group customer
- */
- public function testMatching()
- {
- /** @var OwnerPriceExpress $model */
- $model = factory(OwnerPriceExpress::class)->create([
- "initial_weight" => 2.4,
- "additional_weight" => 3.1,//16.2
- ]);
- $this->data["models"][] = $model->toArray();
- $detail = factory(OwnerPriceExpressProvince::class)->create([
- "owner_price_express_id" => $model->id,
- "province_id" => 1,
- "initial_weight_price" => 2, //初始单价 4.8
- "additional_weight_price"=>2.2, //续重单价
- ]);
- $model->owners()->sync([1]);
- $model->logistics()->sync([1]);
- $result = $this->service->matching(18.6,1,1,1);
- $this->assertEquals(18,$result);
- $model->owners()->sync([]);
- $model->logistics()->sync([]);
- OwnerPriceExpressProvince::destroy($detail->id);
- }
- public function tearDown(): void
- {
- OwnerPriceExpress::destroy(array_column($this->data["models"],"id"));
- parent::tearDown();
- }
- }
|