| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace Tests\Services\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
- $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
- // $this->actingAs(factory(User::class)->create(['name' => 'yang']));
- $user = new User([
- 'name'=>'yang'
- ]);
- $this->be($user);
- $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([2, 0, 2, 0],$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],
- ]);
- }
- /**
- * @test
- */
- public function currentDateTest()
- {
- $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'日');
- $this->assertFalse($result);
- $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'月');
- $this->assertFalse($result);
- $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'年');
- $this->assertFalse($result);
- $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subDay()->toDateString(),'日');
- $this->assertTrue($result);
- // $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subMonth() ->toDateString(),'月');
- // $this->assertTrue($result);
- $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subYear() ->toDateString(),'年');
- $this->assertTrue($result);
- $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subYears()->toDateString(),'月');
- $this->assertTrue($result);
- }
- }
|