| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace Tests\Services\OrderCountingRecordService;
- use App\Order;
- use App\Owner;
- use App\Services\CacheService;
- use App\Services\OrderCountingRecordService;
- use App\User;
- use Carbon\Carbon;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
- class OrderCountingRecordServiceGetTest extends TestCase
- {
- use RefreshDatabase;
- /** @var OrderCountingRecordService $orderCountingRecordService */
- public $orderCountingRecordService;
- public function setUp(): void
- {
- parent::setUp();
- app()->singleton('CacheService', CacheService::class);
- $this->orderCountingRecordService = app(OrderCountingRecordService::class);
- }
- /**
- * @test
- */
- public function counting_unit_date()
- {
- cache()->flush();
- $start = Carbon::now()->subDays(2)->format('Y-m-d');
- $end = (new Carbon())->toDateString();
- $this->actingAs($user = factory(User::class)->create(['name' => 'yang']));
- $owner = factory(Owner::class)->create();
- $this->createCurrentDateOrders($owner);
- $this->createSubDayOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
- $this->assertEquals([0, 10, 20], array_column($result, 'counter'));
- cache()->flush();
- $this->createCurrentDateOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
- $this->assertEquals([0, 10, 40], array_column($result, 'counter'));
- cache()->flush();
- }
- /**
- * @test
- */
- public function counting_unit_week()
- {
- cache()->flush();
- $start = Carbon::now()->subWeek()->format('Y-m-d');
- $end = (new Carbon())->toDateString();
- $this->actingAs(factory(User::class)->create(['name' => 'yang']));
- $owner = factory(Owner::class)->create();
- $this->createCurrentDateOrders($owner);
- $this->createSubWeekOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
- $this->assertEquals([20, 20], array_column($result, 'counter'));
- cache()->flush();
- $this->createCurrentDateOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
- $this->assertEquals([20, 40], array_column($result, 'counter'));
- }
- /**
- * @test
- */
- public function counting_unit_month()
- {
- cache()->flush();
- $start = Carbon::now()->subMonth()->format('Y-m-d');
- $end = (new Carbon())->toDateString();
- $this->actingAs(factory(User::class)->create(['name' => 'yang']));
- $owner = factory(Owner::class)->create();
- $this->createCurrentDateOrders($owner);
- $this->createSubMonthOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
- $this->assertEquals([20, 20], array_column($result, 'counter'));
- cache()->flush();
- $this->createCurrentDateOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
- $this->assertEquals([20, 40], array_column($result, 'counter'));
- }
- /**
- * @test
- */
- public function current_date_ttl_day()
- {
- cache()->flush();
- $start = Carbon::now()->subDays(2)->format('Y-m-d');
- $end = (new Carbon())->toDateString();
- $this->actingAs($user = factory(User::class)->create(['name' => 'yang']));
- $owner = factory(Owner::class)->create();
- $this->createCurrentDateOrders($owner);
- $this->createSubDayOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
- $this->assertEquals([0, 10, 20], array_column($result, 'counter'));
- sleep(4);
- $this->createCurrentDateOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
- $this->assertEquals([0, 10, 40], array_column($result, 'counter'));
- cache()->flush();
- }
- /**
- * @test
- */
- public function current_date_ttl_week()
- {
- cache()->flush();
- $start = Carbon::now()->subWeek()->format('Y-m-d');
- $end = (new Carbon())->toDateString();
- $this->actingAs(factory(User::class)->create(['name' => 'yang']));
- $owner = factory(Owner::class)->create();
- $this->createCurrentDateOrders($owner);
- $this->createSubWeekOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
- $this->assertEquals([20, 20], array_column($result, 'counter'));
- sleep(4);
- $this->createCurrentDateOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
- $this->assertEquals([20, 40], array_column($result, 'counter'));
- }
- /**
- * @test
- */
- public function current_date_ttl_month()
- {
- cache()->flush();
- $start = Carbon::now()->subMonth()->format('Y-m-d');
- $end = (new Carbon())->toDateString();
- $this->actingAs(factory(User::class)->create(['name' => 'yang']));
- $owner = factory(Owner::class)->create();
- $this->createCurrentDateOrders($owner);
- $this->createSubMonthOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
- $this->assertEquals([20, 20], array_column($result, 'counter'));
- sleep(4);
- $this->createCurrentDateOrders($owner);
- $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
- $this->assertEquals([20, 40], array_column($result, 'counter'));
- }
- /**
- * @test
- */
- public function set_null()
- {
- $dateStr = '2020-11-26';
- $this->assertTrue($dateStr != Carbon::now()->format('Y-m-d'));
- }
- /**
- * @test
- */
- public function isNotCurrentDate()
- {
- $day_ture = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subDay()->format('Y-m-d'));
- $day_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->format('Y-m-d'));
- $week_ture = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subWeek()->year . '-' . Carbon::now()->subWeek()->week);
- $week_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year . '-' . Carbon::now()->week);
- $month_true = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subMonth()->year . '-' . Carbon::now()->subMonth()->month);
- $month_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year . '-' . Carbon::now()->month);
- $this->assertTrue($day_ture);
- $this->assertFalse($day_false);
- $this->assertTrue($week_ture);
- $this->assertFalse($week_false);
- $this->assertTrue($month_true);
- $this->assertFalse($month_false);
- }
- public function tearDown(): void
- {
- Order::query()->delete();
- Owner::query()->delete();
- User::query()->delete();
- }
- protected function createCurrentDateOrders($owner): void
- {
- factory(Order::class)->times(20)->create([
- 'created_at' => Carbon::now(),
- 'owner_id' => $owner->id,
- 'wms_status' => '订单完成']);
- }
- protected function createSubWeekOrders($owner): void
- {
- factory(Order::class)->times(20)->create([
- 'created_at' => Carbon::now()->subWeek(),
- 'owner_id' => $owner->id,
- 'wms_status' => '订单完成']);
- }
- protected function createSubMonthOrders($owner): void
- {
- factory(Order::class)->times(20)->create([
- 'created_at' => Carbon::now()->subMonth(),
- 'owner_id' => $owner->id,
- 'wms_status' => '订单完成']);
- }
- protected function createSubDayOrders($owner): void
- {
- factory(Order::class)->times(10)->create([
- 'created_at' => Carbon::now()->subDays(1),
- 'owner_id' => $owner->id,
- 'wms_status' => '订单完成']);
- }
- }
|