github.com/goravel/framework@v1.13.9/cache/driver_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/goravel/framework/contracts/cache"
    11  	configmock "github.com/goravel/framework/contracts/config/mocks"
    12  	logmock "github.com/goravel/framework/contracts/log/mocks"
    13  )
    14  
    15  type DriverTestSuite struct {
    16  	suite.Suite
    17  	driver     *DriverImpl
    18  	mockConfig *configmock.Config
    19  	mockLog    *logmock.Log
    20  }
    21  
    22  func TestDriverTestSuite(t *testing.T) {
    23  	suite.Run(t, new(DriverTestSuite))
    24  }
    25  
    26  func (s *DriverTestSuite) SetupTest() {
    27  	s.mockConfig = &configmock.Config{}
    28  	s.mockLog = &logmock.Log{}
    29  	s.driver = NewDriverImpl(s.mockConfig)
    30  }
    31  
    32  func (s *DriverTestSuite) TestMemory() {
    33  	s.mockConfig.On("GetString", "cache.prefix").Return("goravel_cache").Once()
    34  	memory, err := s.driver.memory()
    35  	s.NotNil(memory)
    36  	s.Nil(err)
    37  }
    38  
    39  func (s *DriverTestSuite) TestCustom() {
    40  	s.mockConfig.On("Get", "cache.stores.store.via").Return(&Store{}).Once()
    41  
    42  	store, err := s.driver.custom("store")
    43  	s.NotNil(store)
    44  	s.Nil(err)
    45  	s.Equal("name", store.Get("name", "Goravel").(string))
    46  
    47  	s.mockConfig.AssertExpectations(s.T())
    48  }
    49  
    50  func (s *DriverTestSuite) TestStore() {
    51  	s.mockConfig.On("GetString", "cache.stores.memory.driver").Return("memory").Once()
    52  	s.mockConfig.On("GetString", "cache.prefix").Return("goravel_cache").Once()
    53  
    54  	memory, err := NewApplication(s.mockConfig, s.mockLog, "memory")
    55  	s.NotNil(memory)
    56  	s.Nil(err)
    57  	s.True(memory.Add("hello", "goravel", 5*time.Second))
    58  	s.Equal("goravel", memory.GetString("hello"))
    59  
    60  	s.mockConfig.On("GetString", "cache.stores.custom.driver").Return("custom").Once()
    61  	s.mockConfig.On("Get", "cache.stores.custom.via").Return(&Store{}).Once()
    62  
    63  	custom := memory.Store("custom")
    64  	s.NotNil(custom)
    65  	s.Equal("", custom.GetString("hello"))
    66  	s.True(custom.Add("hello", "world", 5*time.Second))
    67  	s.Equal("", custom.GetString("hello"))
    68  
    69  	s.Equal("goravel", memory.GetString("hello"))
    70  
    71  	s.mockConfig.AssertExpectations(s.T())
    72  }
    73  
    74  type Store struct {
    75  }
    76  
    77  //Add Store an item in the cache if the key does not exist.
    78  func (r *Store) Add(key string, value any, seconds time.Duration) bool {
    79  	return true
    80  }
    81  
    82  func (r *Store) Decrement(key string, value ...int) (int, error) {
    83  	return 1, nil
    84  }
    85  
    86  //Forever Store an item in the cache indefinitely.
    87  func (r *Store) Forever(key string, value any) bool {
    88  	return true
    89  }
    90  
    91  //Forget Remove an item from the cache.
    92  func (r *Store) Forget(key string) bool {
    93  	return true
    94  }
    95  
    96  //Flush Remove all items from the cache.
    97  func (r *Store) Flush() bool {
    98  	return true
    99  }
   100  
   101  //Get Retrieve an item from the cache by key.
   102  func (r *Store) Get(key string, def ...any) any {
   103  	return key
   104  }
   105  
   106  //Get Retrieve an item from the cache by key.
   107  func (r *Store) GetBool(key string, def ...bool) bool {
   108  	return false
   109  }
   110  
   111  //Get Retrieve an item from the cache by key.
   112  func (r *Store) GetInt(key string, def ...int) int {
   113  	return 1
   114  }
   115  
   116  //Get Retrieve an item from the cache by key.
   117  func (r *Store) GetInt64(key string, def ...int64) int64 {
   118  	return 1
   119  }
   120  
   121  //Get Retrieve an item from the cache by key.
   122  func (r *Store) GetString(key string, def ...string) string {
   123  	return ""
   124  }
   125  
   126  //Has Check an item exists in the cache.
   127  func (r *Store) Has(key string) bool {
   128  	return true
   129  }
   130  
   131  func (r *Store) Increment(key string, value ...int) (int, error) {
   132  	return 1, nil
   133  }
   134  
   135  func (r *Store) Lock(key string, second ...time.Duration) cache.Lock {
   136  	return nil
   137  }
   138  
   139  //Pull Retrieve an item from the cache and delete it.
   140  func (r *Store) Pull(key string, def ...any) any {
   141  	return def
   142  }
   143  
   144  //Put Store an item in the cache for a given number of seconds.
   145  func (r *Store) Put(key string, value any, seconds time.Duration) error {
   146  	return nil
   147  }
   148  
   149  //Remember Get an item from the cache, or execute the given Closure and store the result.
   150  func (r *Store) Remember(key string, ttl time.Duration, callback func() (any, error)) (any, error) {
   151  	return "", nil
   152  }
   153  
   154  //RememberForever Get an item from the cache, or execute the given Closure and store the result forever.
   155  func (r *Store) RememberForever(key string, callback func() (any, error)) (any, error) {
   156  	return "", nil
   157  }
   158  
   159  func (r *Store) WithContext(ctx context.Context) cache.Driver {
   160  	return r
   161  }
   162  
   163  var _ cache.Driver = &Store{}