|
|
@@ -0,0 +1,181 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace NewOrderCountingRecordService;
|
|
|
+
|
|
|
+
|
|
|
+use App\Order;
|
|
|
+use App\OrderCountingRecord;
|
|
|
+use App\Owner;
|
|
|
+use App\Services\NewOrderCountingRecordService;
|
|
|
+use App\User;
|
|
|
+use Carbon\Carbon;
|
|
|
+use Tests\TestCase;
|
|
|
+
|
|
|
+class GetFromLowerUnitTest extends TestCase
|
|
|
+{
|
|
|
+ protected $newOrderCountingRecordService;
|
|
|
+ protected $queryConditionDay;
|
|
|
+ protected $queryConditionWeek;
|
|
|
+ protected $queryConditionMonth;
|
|
|
+ protected $queryConditionYear;
|
|
|
+ protected $ownerIds;
|
|
|
+ protected $cache_key = 'order_counting_records_';
|
|
|
+ protected $step_length = 1;
|
|
|
+ protected $orderCountingRecordIds = [];
|
|
|
+ protected $units = ['日', '月', '年'];
|
|
|
+ protected $orderIds;
|
|
|
+
|
|
|
+
|
|
|
+ protected function setUp(): void
|
|
|
+ {
|
|
|
+ parent::setUp(); // TODO: Change the autogenerated stub
|
|
|
+ cache()->flush();
|
|
|
+ $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
|
|
|
+ $this->actingAs(factory(User::class)->create(['name' => 'yang']));
|
|
|
+ $owners = factory(Owner::class)->times(2)->create();
|
|
|
+ $this->ownerIds = array_column($owners->toArray(), 'id');
|
|
|
+ $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
|
|
|
+ $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
|
|
|
+ $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function tearDown(): void
|
|
|
+ {
|
|
|
+ Owner::destroy($this->ownerIds);
|
|
|
+ OrderCountingRecord::destroy($this->orderCountingRecordIds);
|
|
|
+ Order::destroy($this->orderIds);
|
|
|
+ OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
|
|
|
+ parent::tearDown();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * unit为月,中间表日为空,查询orders
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function unit_month_from_order()
|
|
|
+ {
|
|
|
+ $orders = collect();
|
|
|
+ foreach ($this->ownerIds as $ownerId) {
|
|
|
+ for ($i = 1; $i >= 0; $i--) {
|
|
|
+ $orders->push(factory(Order::class)->create([
|
|
|
+ 'created_at' => Carbon::now()->subMonths($i)->toDateString(),
|
|
|
+ 'owner_id' => $ownerId,
|
|
|
+ 'wms_status' => '订单完成',
|
|
|
+ ]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $this->orderIds = array_column($orders->toArray(), 'id');
|
|
|
+
|
|
|
+ $result = $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionMonth);
|
|
|
+ $this->assertEquals([1, 1, 1, 1],$result->pluck('amount')->toArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * unit为年,中间表日为空,查询orders
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function unit_year_from_order()
|
|
|
+ {
|
|
|
+ $orders = collect();
|
|
|
+ foreach ($this->ownerIds as $ownerId) {
|
|
|
+ for ($i = 1; $i >= 0; $i--) {
|
|
|
+ $orders->push(factory(Order::class)->create([
|
|
|
+ 'created_at' => Carbon::now()->subYears($i)->toDateString(),
|
|
|
+ 'owner_id' => $ownerId,
|
|
|
+ 'wms_status' => '订单完成',
|
|
|
+ ]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $this->orderIds = array_column($orders->toArray(), 'id');
|
|
|
+
|
|
|
+ $result = $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionYear);
|
|
|
+ $this->assertEquals([1, 1, 1, 1],$result->pluck('amount')->toArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 插入中间表测试 月
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function unit_month_from_order_insert()
|
|
|
+ {
|
|
|
+ $orders = collect();
|
|
|
+ foreach ($this->ownerIds as $ownerId) {
|
|
|
+ for ($i = 1; $i >= 0; $i--) {
|
|
|
+ $orders->push(factory(Order::class)->create([
|
|
|
+ 'created_at' => Carbon::now()->subMonths($i)->toDateString(),
|
|
|
+ 'owner_id' => $ownerId,
|
|
|
+ 'wms_status' => '订单完成',
|
|
|
+ ]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $this->orderIds = array_column($orders->toArray(), 'id');
|
|
|
+
|
|
|
+ $this->assertDatabaseMissing('order_counting_records', [
|
|
|
+ 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
|
|
|
+ 'counting_unit' => '月',
|
|
|
+ 'owner_id' => $this->ownerIds[0],
|
|
|
+ ]);
|
|
|
+ $this->assertDatabaseMissing('order_counting_records', [
|
|
|
+ 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
|
|
|
+ 'counting_unit' => '月',
|
|
|
+ 'owner_id' => $this->ownerIds[1],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionMonth);
|
|
|
+
|
|
|
+ $this->assertDatabaseHas('order_counting_records', [
|
|
|
+ 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
|
|
|
+ 'counting_unit' => '月',
|
|
|
+ 'owner_id' => $this->ownerIds[0],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $this->assertDatabaseHas('order_counting_records', [
|
|
|
+ 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
|
|
|
+ 'counting_unit' => '月',
|
|
|
+ 'owner_id' => $this->ownerIds[1],
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 插入中间表测试 年
|
|
|
+ * @test
|
|
|
+ */
|
|
|
+ public function unit_year_from_order_insert()
|
|
|
+ {
|
|
|
+ $orders = collect();
|
|
|
+ foreach ($this->ownerIds as $ownerId) {
|
|
|
+ for ($i = 1; $i >= 0; $i--) {
|
|
|
+ $orders->push(factory(Order::class)->create([
|
|
|
+ 'created_at' => Carbon::now()->subYears($i)->toDateString(),
|
|
|
+ 'owner_id' => $ownerId,
|
|
|
+ 'wms_status' => '订单完成',
|
|
|
+ ]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $this->orderIds = array_column($orders->toArray(), 'id');
|
|
|
+
|
|
|
+ $this->assertDatabaseMissing('order_counting_records', [
|
|
|
+ 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
|
|
|
+ 'counting_unit' => '年',
|
|
|
+ 'owner_id' => $this->ownerIds[0],
|
|
|
+ ]);
|
|
|
+ $this->assertDatabaseMissing('order_counting_records', [
|
|
|
+ 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
|
|
|
+ 'counting_unit' => '年',
|
|
|
+ 'owner_id' => $this->ownerIds[1],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionYear);
|
|
|
+ $this->assertDatabaseHas('order_counting_records', [
|
|
|
+ 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
|
|
|
+ 'counting_unit' => '年',
|
|
|
+ 'owner_id' => $this->ownerIds[0],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $this->assertDatabaseHas('order_counting_records', [
|
|
|
+ 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
|
|
|
+ 'counting_unit' => '年',
|
|
|
+ 'owner_id' => $this->ownerIds[1],
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+}
|