GetFromCacheTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Tests\TestCase;
  10. class GetFromCacheTest extends TestCase
  11. {
  12. protected $newOrderCountingRecordService;
  13. protected $queryConditionDay;
  14. protected $queryConditionWeek;
  15. protected $queryConditionMonth;
  16. protected $queryConditionYear;
  17. protected $ownerIds;
  18. protected $cache_key = 'order_counting_records_';
  19. protected $step_length = 1;
  20. protected $orderCountingRecordIds = [];
  21. protected $units = ['日', '月', '年'];
  22. protected $orderIds;
  23. protected function setUp(): void
  24. {
  25. parent::setUp(); // TODO: Change the autogenerated stub
  26. cache()->flush();
  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 cache_data_empty()
  48. {
  49. $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
  50. $this->assertEquals([
  51. 0 => collect(),
  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 cache_data_all()
  63. {
  64. foreach ($this->ownerIds as $owner_id) {
  65. for ( $i = 1; $i >= 0; $i--) {
  66. $key = 'order_counting_records_' . Carbon::now()->subDays($i)->toDateString() . '_' . $owner_id . '_' . '日';
  67. cache()->put($key, collect(['aaa']));
  68. }
  69. }
  70. $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
  71. $this->assertEquals([
  72. 0 => collect(['aaa','aaa','aaa','aaa']),
  73. ['unit' => '日']], $result);
  74. }
  75. /**
  76. * 缓存命中部分数据
  77. * @test
  78. */
  79. public function cache_data_some()
  80. {
  81. foreach ($this->ownerIds as $owner_id) {
  82. for ( $i = 0; $i >= 0; $i--) {
  83. $key = 'order_counting_records_' . Carbon::now()->subDays($i)->toDateString() . '_' . $owner_id . '_' . '日';
  84. cache()->put($key, collect(['aaa']));//缓存中放入当天的数据
  85. }
  86. }
  87. $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
  88. $this->assertEquals([
  89. 0 => collect(['aaa','aaa']),
  90. ['unit' => '日',
  91. 'data' => [
  92. Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],//昨天的数据没放在缓存,应该在data中返回
  93. ]]], $result);
  94. }
  95. }