SetLastRecordsByRedisTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Tests\Services\OrderService;
  3. use App\OracleDOCASNHeader;
  4. use App\Services\StoreService;
  5. use Illuminate\Support\Facades\Cache;
  6. use Tests\TestCase;
  7. class SetLastRecordsByRedisTest extends TestCase
  8. {
  9. /** @var StoreService $service */
  10. public $service;
  11. public $data;
  12. public $create_set;
  13. public $create_keys;
  14. public $create_key;
  15. public $update_set;
  16. public $update_keys;
  17. public $update_key;
  18. public $createRecord;
  19. public $updateRecord;
  20. public function setUp(): void
  21. {
  22. parent::setUp(); // TODO: Change the autogenerated stub
  23. $this->service = app(StoreService::class);
  24. $this->create_set = config('sync.asn_sync.cache_prefix.create_set');
  25. $this->create_keys = config('sync.asn_sync.cache_prefix.create_keys');
  26. $this->create_key = config('sync.asn_sync.cache_prefix.create');
  27. $this->update_set = config('sync.asn_sync.cache_prefix.update_set');
  28. $this->update_keys = config('sync.asn_sync.cache_prefix.update_keys');
  29. $this->update_key = config('sync.asn_sync.cache_prefix.update');
  30. }
  31. /**
  32. * @test
  33. */
  34. public function testSetLastRecordsByRedisCreated()
  35. {
  36. $this->createRecord=collect();
  37. $last_record=OracleDOCASNHeader::query()->orderByDesc('addtime')->first();
  38. $this->createRecord->add($last_record);
  39. if (!Cache::get($this->create_set)){
  40. $this->assertNull(Cache::get($this->create_set));
  41. $this->assertNotNull($this->createRecord);
  42. return;
  43. }
  44. $this->service->setLastRecordsByRedis($this->create_key,$this->create_set,$this->create_keys,$this->createRecord);
  45. $this->assertNotNull($this->createRecord);
  46. $this->assertEquals(true,Cache::get($this->create_set));
  47. $this->assertNotNull(Cache::get($this->create_keys));
  48. }
  49. /**
  50. * @test
  51. */
  52. public function testSetLastRecordsByRedisUpdated()
  53. {
  54. $this->updateRecord=collect();
  55. $last_record=OracleDOCASNHeader::query()->orderByDesc('edittime')->first();
  56. $this->updateRecord->add($last_record);
  57. if (!Cache::get($this->update_set)){
  58. $this->assertNull(Cache::get($this->update_set));
  59. $this->assertNotNull($this->updateRecord);
  60. return;
  61. }
  62. $this->service->setLastRecordsByRedis($this->update_key,$this->update_set,$this->update_keys,$this->updateRecord);
  63. $this->assertNotNull($this->updateRecord);
  64. $this->assertEquals(true,Cache::get($this->update_set));
  65. $this->assertNotNull(Cache::get($this->update_keys));
  66. }
  67. public function tearDown(): void
  68. {
  69. if (!Cache::get($this->create_set)) return;
  70. $cacheKeys = Cache::get($this->create_keys);
  71. if (!$cacheKeys) return;
  72. foreach ($cacheKeys as $cacheKey) {
  73. Cache::forget($cacheKey);
  74. }
  75. Cache::forget($this->create_keys);
  76. if (!Cache::get($this->update_set)) return;
  77. $cacheKeys = Cache::get($this->update_keys);
  78. if (!$cacheKeys) return;
  79. foreach ($cacheKeys as $cacheKey) {
  80. Cache::forget($cacheKey);
  81. }
  82. Cache::forget($this->update_keys);
  83. parent::tearDown(); // TODO: Change the autogenerated stub
  84. }
  85. }