github.com/goravel/framework@v1.13.9/config/application_test.go (about) 1 package config 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/suite" 9 10 "github.com/goravel/framework/support/file" 11 ) 12 13 type ApplicationTestSuite struct { 14 suite.Suite 15 config *Application 16 customConfig *Application 17 } 18 19 func TestApplicationTestSuite(t *testing.T) { 20 assert.Nil(t, file.Create(".env", ` 21 APP_KEY=12345678901234567890123456789012 22 APP_DEBUG=true 23 DB_PORT=3306 24 `)) 25 temp, err := os.CreateTemp("", "goravel.env") 26 assert.Nil(t, err) 27 defer temp.Close() 28 defer os.Remove(temp.Name()) 29 30 _, err = temp.Write([]byte(` 31 APP_KEY=12345678901234567890123456789012 32 APP_DEBUG=true 33 DB_PORT=3306 34 `)) 35 assert.Nil(t, err) 36 assert.Nil(t, temp.Close()) 37 38 suite.Run(t, &ApplicationTestSuite{ 39 config: NewApplication(".env"), 40 customConfig: NewApplication(temp.Name()), 41 }) 42 43 assert.Nil(t, file.Remove(".env")) 44 } 45 46 func (s *ApplicationTestSuite) SetupTest() { 47 48 } 49 50 func (s *ApplicationTestSuite) TestEnv() { 51 s.Equal("12345678901234567890123456789012", s.config.Env("APP_KEY").(string)) 52 s.Equal("goravel", s.config.Env("APP_NAME", "goravel").(string)) 53 s.Equal("12345678901234567890123456789012", s.customConfig.Env("APP_KEY").(string)) 54 s.Equal("goravel", s.customConfig.Env("APP_NAME", "goravel").(string)) 55 } 56 57 func (s *ApplicationTestSuite) TestAdd() { 58 s.config.Add("app", map[string]any{ 59 "env": "local", 60 }) 61 s.customConfig.Add("app", map[string]any{ 62 "env": "local", 63 }) 64 65 s.Equal("local", s.config.GetString("app.env")) 66 s.Equal("local", s.customConfig.GetString("app.env")) 67 68 s.config.Add("path.with.dot.case1", "value1") 69 s.customConfig.Add("path.with.dot.case1", "value1") 70 s.Equal("value1", s.config.GetString("path.with.dot.case1")) 71 s.Equal("value1", s.customConfig.GetString("path.with.dot.case1")) 72 73 s.config.Add("path.with.dot.case2", "value2") 74 s.customConfig.Add("path.with.dot.case2", "value2") 75 s.Equal("value2", s.config.GetString("path.with.dot.case2")) 76 s.Equal("value2", s.customConfig.GetString("path.with.dot.case2")) 77 78 s.config.Add("path.with.dot", map[string]any{"case3": "value3"}) 79 s.customConfig.Add("path.with.dot", map[string]any{"case3": "value3"}) 80 s.Equal("value3", s.config.GetString("path.with.dot.case3")) 81 s.Equal("value3", s.customConfig.GetString("path.with.dot.case3")) 82 } 83 84 func (s *ApplicationTestSuite) TestGet() { 85 s.Equal("12345678901234567890123456789012", s.config.Get("APP_KEY").(string)) 86 s.Equal("goravel", s.config.Get("APP_NAME", "goravel").(string)) 87 s.Equal("12345678901234567890123456789012", s.customConfig.Get("APP_KEY").(string)) 88 s.Equal("goravel", s.customConfig.Get("APP_NAME", "goravel").(string)) 89 } 90 91 func (s *ApplicationTestSuite) TestGetString() { 92 s.config.Add("database", map[string]any{ 93 "default": s.config.Env("DB_CONNECTION", "mysql"), 94 "connections": map[string]any{ 95 "mysql": map[string]any{ 96 "host": s.config.Env("DB_HOST", "127.0.0.1"), 97 }, 98 }, 99 }) 100 s.customConfig.Add("database", map[string]any{ 101 "default": s.customConfig.Env("DB_CONNECTION", "mysql"), 102 "connections": map[string]any{ 103 "mysql": map[string]any{ 104 "host": s.customConfig.Env("DB_HOST", "127.0.0.1"), 105 }, 106 }, 107 }) 108 109 s.Equal("goravel", s.config.GetString("APP_NAME", "goravel")) 110 s.Equal("127.0.0.1", s.config.GetString("database.connections.mysql.host")) 111 s.Equal("mysql", s.config.GetString("database.default")) 112 s.Equal("goravel", s.customConfig.GetString("APP_NAME", "goravel")) 113 s.Equal("127.0.0.1", s.customConfig.GetString("database.connections.mysql.host")) 114 s.Equal("mysql", s.customConfig.GetString("database.default")) 115 } 116 117 func (s *ApplicationTestSuite) TestGetInt() { 118 s.Equal(3306, s.config.GetInt("DB_PORT")) 119 s.Equal(3306, s.customConfig.GetInt("DB_PORT")) 120 } 121 122 func (s *ApplicationTestSuite) TestGetBool() { 123 s.Equal(true, s.config.GetBool("APP_DEBUG")) 124 s.Equal(true, s.customConfig.GetBool("APP_DEBUG")) 125 } 126 127 func TestOsVariables(t *testing.T) { 128 assert.Nil(t, os.Setenv("APP_KEY", "12345678901234567890123456789013")) 129 assert.Nil(t, os.Setenv("APP_NAME", "goravel")) 130 assert.Nil(t, os.Setenv("APP_PORT", "3306")) 131 assert.Nil(t, os.Setenv("APP_DEBUG", "true")) 132 133 config := NewApplication(".env") 134 135 assert.Equal(t, "12345678901234567890123456789013", config.GetString("APP_KEY")) 136 assert.Equal(t, "goravel", config.GetString("APP_NAME")) 137 assert.Equal(t, 3306, config.GetInt("APP_PORT")) 138 assert.True(t, config.GetBool("APP_DEBUG")) 139 }