| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?php
- namespace NewOrderCountingRecordService;
- use App\Order;
- use App\OrderCountingRecord;
- use App\Owner;
- use App\Services\NewOrderCountingRecordService;
- use App\User;
- use Carbon\Carbon;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Arr;
- use Tests\TestCase;
- class NewOrderCountingRecordServiceTest extends TestCase
- {
- use RefreshDatabase;
- protected $newOrderCountingRecordService;
- 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']));
- }
- 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->orderCountingRecords($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;
- }));
- }
- }
|