flush(); $this->newOrderCountingRecordService = new NewOrderCountingRecordService(); $this->actingAs(factory(User::class)->create(['name' => 'yang'])); } protected function tearDown(): void { cache()->flush(); OrderCountingRecord::truncate(); Order::truncate(); Owner::truncate(); parent::tearDown(); // TODO: Change the autogenerated stub } /** * @test */ public function transfersToConditions() { factory(Owner::class)->times(2)->create(); $start = Carbon::now()->subDays(2)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds(); $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds); $this->assertEquals([ Carbon::now()->subDays(2)->toDateString() => [0 => 1, 1 => 2], Carbon::now()->subDays(1)->toDateString() => [0 => 1, 1 => 2], Carbon::now()->toDateString() => [0 => 1, 1 => 2], ], $dateArray); } /** * @test */ public function dateUtils_week() { factory(Owner::class)->times(2)->create(); $start = Carbon::now()->subWeeks(2)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '周'; $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds(); $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds); dd($dateArray); // $this->assertEquals([ // Carbon::now()->subWeeks(2)->toDateString() => [0 => 1, 1 => 2], // Carbon::now()->subWeeks(1)->toDateString() => [0 => 1, 1 => 2], // Carbon::now()->toDateString() => [0 => 1, 1 => 2], // ], $dateArray); } /** * @test */ public function orderCountingRecords_remember_day() { $start = Carbon::now()->subDays(2)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; $key = 'orderCountingRecords_' . $start . '_' . $end . '_' . $unit . '_' . auth()->id(); cache()->put($key, 'This is a test value'); $result = $this->newOrderCountingRecordService->get($start, $end, $unit); $this->assertEquals('This is a test value', $result); } /** * @test */ public function dataFromCache_all_day() { $start = Carbon::now()->subDays(2)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; factory(Owner::class)->times(2)->create(); $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds(); $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds); foreach ($queryCondition as $dateStr => $ownerIds) { foreach ($ownerIds as $ownerId) { $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit . '_' . auth()->id(); cache()->put($key, collect()->push(factory(Owner::class)->create())); } } $result = $this->newOrderCountingRecordService->getFromCache($queryCondition, $unit); $this->assertCount(6, $result['dataFromCache']); } /** * @test */ public function dataFromCache_empty_day() { $start = Carbon::now()->subDays(2)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; factory(Owner::class)->times(2)->create(); $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds(); $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds); $result = $this->newOrderCountingRecordService->getFromCache($queryCondition, $unit); $this->assertEquals([ Carbon::now()->subDays(2)->toDateString() => [0 => 1, 1 => 2], Carbon::now()->subDays(1)->toDateString() => [0 => 1, 1 => 2], Carbon::now()->toDateString() => [0 => 1, 1 => 2], ], $result['unQueryCondition']); } /** * @test */ public function dataFromMiddleTable_all_day() { $start = Carbon::now()->subDays(1)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; $owner1 = factory(Owner::class)->create(); $owner2 = factory(Owner::class)->create(); factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]); factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner2->id]); factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->subDays(1)->toDateString(), 'owner_id' => $owner1->id]); factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->subDays(1)->toDateString(), 'owner_id' => $owner2->id]); $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds(); $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds); $result = $this->newOrderCountingRecordService->dataFromMiddleTable($queryCondition, $unit); $this->assertCount(8, $result['dataFromMiddleTable']); $this->assertCount(0, $result['unQueryCondition'][Carbon::now()->toDateString()]); $this->assertCount(0, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]); } /** * @test */ public function dataFromMiddleTable_empty_day() { $start = Carbon::now()->subDays(1)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; factory(Owner::class)->create(); factory(Owner::class)->create(); $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds(); $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds); $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit); $this->assertCount(0, $result['dataFromMiddleTable']); $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->toDateString()]); $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]); } /** * @test */ public function dataFromMiddleTable_common_day() { $start = Carbon::now()->subDays(1)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; $owner1 = factory(Owner::class)->create(); $owner2 = factory(Owner::class)->create(); $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds(); $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds); factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]); $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit); $this->assertCount(2, $result['dataFromMiddleTable']); $this->assertCount(1, $result['unQueryCondition'][Carbon::now()->toDateString()]); $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]); } /** * @test */ public function insertIntoCache_day() { $start = Carbon::now()->subDays(1)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; $owner1 = factory(Owner::class)->create(); $owner2 = factory(Owner::class)->create(); $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds(); $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds); $orderCountingRecord = factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]); $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit); $this->newOrderCountingRecordService->insertIntoCache($result['dataFromMiddleTable'], $unit); $key = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner1->id . '_' . $unit; $this->assertNotNull(cache()->get($key)); $this->assertNotNull($orderCountingRecord->toArray()); $this->assertEquals($orderCountingRecord->toArray(), cache()->get($key)->toArray()); } /** * @test */ public function dataFromOrder_day() { $start = Carbon::now()->subDays(1)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; $owner1 = factory(Owner::class)->create(); $owner2 = factory(Owner::class)->create(); $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds(); $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds); factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]); factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]); factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]); factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]); $result = $this->newOrderCountingRecordService->dataFromOrder($unQueryCondition, $unit); $this->assertEquals(1, $result->where('date_target', Carbon::now()->toDateString())->where('owner_id', $owner1->id)->first()->amount); $this->assertEquals(2, $result->where('date_target', Carbon::now()->toDateString())->where('owner_id', $owner2->id)->first()->amount); $this->assertEquals(3, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->where('owner_id', $owner1->id)->first()->amount); $this->assertEquals(4, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->where('owner_id', $owner2->id)->first()->amount); } /** * @test */ public function dataFromOrder_insertMiddleTable_day() { $start = Carbon::now()->subDays(1)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; $owner1 = factory(Owner::class)->create(); $owner2 = factory(Owner::class)->create(); $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds(); $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds); factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]); factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]); factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]); factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]); $this->newOrderCountingRecordService->dataFromOrder($unQueryCondition, $unit); //当天的数据不入中间表 $this->assertCount(0, OrderCountingRecord::query()->where('date_target', Carbon::now()->toDateString())->get()); $this->assertCount(2, OrderCountingRecord::query()->where('date_target', Carbon::now()->subDays(1)->toDateString())->get()); } /** * @test */ public function dataFromOrder_insertCache_day() { $start = Carbon::now()->subDays(1)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; $owner1 = factory(Owner::class)->create(); $owner2 = factory(Owner::class)->create(); $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit,$this->newOrderCountingRecordService->getCountingOwnerIds()); factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]); factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]); factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]); factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]); $this->newOrderCountingRecordService->dataFromOrder($unQueryCondition, $unit); $key1 = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner1->id . '_' . $unit; $key2 = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner2->id . '_' . $unit; $key3 = 'order_counting_records_' . Carbon::now()->subDays(1)->toDateString() . '_' . $owner1->id . '_' . $unit; $key4 = 'order_counting_records_' . Carbon::now()->subDays(1)->toDateString() . '_' . $owner2->id . '_' . $unit; $this->assertCount(1, cache()->get($key1)); $this->assertCount(1, cache()->get($key2)); $this->assertCount(1, cache()->get($key3)); $this->assertCount(1, cache()->get($key4)); } /** * @test */ public function orderCountingRecordsDay_day() { $start = Carbon::now()->subDays(1)->toDateString(); $end = Carbon::now()->toDateString(); $unit = '日'; $owner1 = factory(Owner::class)->create(); $owner2 = factory(Owner::class)->create(); factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]); factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]); factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]); factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]); $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit,$this->newOrderCountingRecordService->getCountingOwnerIds()); $result = $this->newOrderCountingRecordService->getOrderCountingRecords($unQueryCondition, $unit); $this->assertEquals(3, $result->where('date_target', Carbon::now()->toDateString())->reduce(function ($carry, $item) { return $carry + $item->amount; })); $this->assertEquals(7, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->reduce(function ($carry, $item) { return $carry + $item->amount; })); } }