OrderCountingRecordServiceGetTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Tests\Services\OrderCountingRecordService;
  3. use App\Order;
  4. use App\Owner;
  5. use App\Services\CacheService;
  6. use App\Services\OrderCountingRecordService;
  7. use App\User;
  8. use Carbon\Carbon;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Tests\TestCase;
  11. class OrderCountingRecordServiceGetTest extends TestCase
  12. {
  13. use RefreshDatabase;
  14. /** @var OrderCountingRecordService $orderCountingRecordService */
  15. public $orderCountingRecordService;
  16. public function setUp(): void
  17. {
  18. parent::setUp();
  19. app()->singleton('CacheService', CacheService::class);
  20. $this->orderCountingRecordService = app(OrderCountingRecordService::class);
  21. }
  22. /**
  23. * @test
  24. */
  25. public function current_date()
  26. {
  27. $start = Carbon::now()->subDays(2)->format('Y-m-d');
  28. $end = (new Carbon())->toDateString();
  29. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  30. $owner = factory(Owner::class)->create();
  31. $orders = factory(Order::class)->times(20)->create([
  32. 'created_at' => Carbon::now(),
  33. 'owner_id' => $owner->id,
  34. 'wms_status'=>'订单完成']);
  35. $orders = factory(Order::class)->times(10)->create([
  36. 'created_at' => Carbon::now()->subDays(1),
  37. 'owner_id' => $owner->id,
  38. 'wms_status'=>'订单完成']);
  39. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
  40. $this->assertEquals([0,10,20], array_column($result, 'counter'));
  41. cache()->flush();
  42. $orders = factory(Order::class)->times(20)->create([
  43. 'created_at' => Carbon::now(),
  44. 'owner_id' => $owner->id,
  45. 'wms_status'=>'订单完成']);
  46. $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
  47. $this->assertEquals([0,10,40], array_column($result, 'counter'));
  48. cache()->flush();
  49. }
  50. /**
  51. * @test
  52. */
  53. public function set_null()
  54. {
  55. $dateStr = '2020-11-26';
  56. $this->assertTrue($dateStr != Carbon::now()->format('Y-m-d'));
  57. }
  58. /**
  59. * @test
  60. */
  61. public function isNotCurrentDate()
  62. {
  63. $day_ture =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subDay()->format('Y-m-d'));
  64. $day_false =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->format('Y-m-d'));
  65. $week_ture =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subWeek()->year . '-' . Carbon::now()->subWeek()->week);
  66. $week_false =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year . '-' . Carbon::now()->week);
  67. $month_true =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subMonth()->year.'-'. Carbon::now()->subMonth()->month);
  68. $month_false =$this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year.'-'. Carbon::now()->month);
  69. $this->assertTrue($day_ture);
  70. $this->assertFalse($day_false);
  71. $this->assertTrue($week_ture);
  72. $this->assertFalse($week_false);
  73. $this->assertTrue($month_true);
  74. $this->assertFalse($month_false);
  75. }
  76. }