GetFromMiddleTableTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace NewOrderCountingRecordService;
  3. use App\Order;
  4. use App\OrderCountingRecord;
  5. use App\Owner;
  6. use App\Services\NewOrderCountingRecordService;
  7. use App\User;
  8. use Carbon\Carbon;
  9. use Illuminate\Database\Eloquent\Collection;
  10. use Tests\TestCase;
  11. class GetFromMiddleTableTest extends TestCase
  12. {
  13. protected $newOrderCountingRecordService;
  14. protected $queryConditionDay;
  15. protected $queryConditionWeek;
  16. protected $queryConditionMonth;
  17. protected $queryConditionYear;
  18. protected $ownerIds;
  19. protected $cache_key = 'order_counting_records_';
  20. protected $step_length = 1;
  21. protected $orderCountingRecordIds = [];
  22. protected $units = ['日', '月', '年'];
  23. protected $orderIds;
  24. protected function setUp(): void
  25. {
  26. parent::setUp(); // TODO: Change the autogenerated stub
  27. $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
  28. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  29. $owners = factory(Owner::class)->times(2)->create();
  30. $this->ownerIds = array_column($owners->toArray(), 'id');
  31. $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
  32. $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
  33. $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
  34. }
  35. protected function tearDown(): void
  36. {
  37. Owner::destroy($this->ownerIds);
  38. OrderCountingRecord::destroy($this->orderCountingRecordIds);
  39. Order::destroy($this->orderIds);
  40. OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
  41. parent::tearDown();
  42. }
  43. /**
  44. * 中间表没有任何数据
  45. * @test
  46. */
  47. public function data_empty()
  48. {
  49. $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
  50. $this->assertEquals([
  51. 0 => new Collection(),
  52. ['unit' => '日',
  53. 'data' => [
  54. Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
  55. Carbon::now()->subDays(0)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
  56. ]]], $result);
  57. }
  58. /**
  59. * 中间表命中全部数据
  60. * @test
  61. */
  62. public function data_all()
  63. {
  64. $orderCountingRecords = collect();
  65. foreach ($this->ownerIds as $owner_id) {
  66. for ($i = 1; $i >= 0; $i--) {
  67. $orderCountingRecords->push(factory(OrderCountingRecord::class)->create([
  68. 'date_target' => Carbon::now()->subDays($i)->toDateString(),
  69. 'owner_id' => $owner_id,
  70. ]));
  71. }
  72. }
  73. $this->orderCountingRecordIds = array_column($orderCountingRecords->toArray(), 'id');
  74. $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
  75. $orderCountingRecords = OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->orderByDesc('owner_id')->get();
  76. $this->assertEquals([
  77. 0 => $orderCountingRecords,
  78. ['unit' => '日','data'=>[]]], $result);
  79. }
  80. /**
  81. * 中间表命中部分数据
  82. * @test
  83. */
  84. public function data_some()
  85. {
  86. $orderCountingRecords = collect();
  87. foreach ($this->ownerIds as $owner_id) {
  88. for ($i = 0; $i >= 0; $i--) {
  89. $orderCountingRecords->push(factory(OrderCountingRecord::class)->create([
  90. 'date_target' => Carbon::now()->subDays($i)->toDateString(),
  91. 'owner_id' => $owner_id,
  92. ]));
  93. }
  94. }
  95. $this->orderCountingRecordIds = array_column($orderCountingRecords->toArray(), 'id');
  96. $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
  97. $this->assertEquals(
  98. ['unit' => '日','data' => [
  99. Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],//昨天的数据没放在缓存,应该在data中返回
  100. ]], $result[1]);
  101. }
  102. }