From 380a71a02867997ef9dc10e9674af4e9529557c0 Mon Sep 17 00:00:00 2001 From: ALMAS Date: Sun, 29 Dec 2024 09:30:56 +0800 Subject: [PATCH] chore: Improve artisan about commmand unit test (#794) Co-authored-by: Wenbo Han --- foundation/console/about_command.go | 3 +- foundation/console/about_command_test.go | 67 +++++++++++++++++++----- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/foundation/console/about_command.go b/foundation/console/about_command.go index cfcc90243..db0f6460e 100644 --- a/foundation/console/about_command.go +++ b/foundation/console/about_command.go @@ -22,6 +22,7 @@ type information struct { var appInformation = &information{section: make(map[string]int)} var customInformationResolvers []func() +var getGoVersion = runtime.Version func NewAboutCommand(app foundation.Application) *AboutCommand { return &AboutCommand{ @@ -71,7 +72,7 @@ func (r *AboutCommand) gatherApplicationInformation() { appInformation.addToSection("Environment", []foundation.AboutItem{ {Key: "Application Name", Value: configFacade.GetString("app.name")}, {Key: "Goravel Version", Value: strings.TrimPrefix(r.app.Version(), "v")}, - {Key: "Go Version", Value: strings.TrimPrefix(runtime.Version(), "go")}, + {Key: "Go Version", Value: strings.TrimPrefix(getGoVersion(), "go")}, {Key: "Environment", Value: configFacade.GetString("app.env")}, {Key: "Debug Mode", Value: func() string { mode := "OFF" diff --git a/foundation/console/about_command_test.go b/foundation/console/about_command_test.go index 0b50da298..f3f85af20 100644 --- a/foundation/console/about_command_test.go +++ b/foundation/console/about_command_test.go @@ -2,10 +2,10 @@ package console import ( "io" + "strings" "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" "github.com/goravel/framework/contracts/foundation" mocksconfig "github.com/goravel/framework/mocks/config" @@ -18,26 +18,67 @@ func TestAboutCommand(t *testing.T) { mockApp := mocksfoundation.NewApplication(t) mockConfig := mocksconfig.NewConfig(t) mockApp.EXPECT().MakeConfig().Return(mockConfig).Once() - mockApp.EXPECT().Version().Return("") + mockApp.EXPECT().Version().Return("test_version").Once() + mockConfig.EXPECT().GetString("app.name").Return("test").Once() + mockConfig.EXPECT().GetString("app.env").Return("test").Once() + mockConfig.EXPECT().GetBool("app.debug").Return(true).Once() + mockConfig.EXPECT().GetString("http.url").Return("test_url").Once() + mockConfig.EXPECT().GetString("http.host").Return("test_host").Once() + mockConfig.EXPECT().GetString("http.port").Return("test_port").Once() + mockConfig.EXPECT().GetString("grpc.host").Return("test_host").Once() + mockConfig.EXPECT().GetString("grpc.port").Return("test_port").Once() + mockConfig.EXPECT().GetString("cache.default").Return("test_cache").Once() + mockConfig.EXPECT().GetString("database.default").Return("test_database").Once() + mockConfig.EXPECT().GetString("hashing.driver").Return("test_hashing").Once() + mockConfig.EXPECT().GetString("http.default").Return("test_http").Once() mockConfig.EXPECT().GetString("logging.default").Return("stack").Once() mockConfig.EXPECT().GetString("logging.channels.stack.driver").Return("stack").Once() mockConfig.EXPECT().Get("logging.channels.stack.channels").Return([]string{"test"}).Once() - mockConfig.EXPECT().GetString(mock.Anything).Return("") - mockConfig.EXPECT().GetString(mock.Anything, mock.Anything).Return("") - mockConfig.EXPECT().GetBool(mock.Anything).Return(true) + mockConfig.EXPECT().GetString("mail.default", "smtp").Return("test_mail").Once() + mockConfig.EXPECT().GetString("queue.default").Return("test_queue").Once() + mockConfig.EXPECT().GetString("session.driver").Return("test_session").Once() aboutCommand := NewAboutCommand(mockApp) mockContext := &consolemocks.Context{} - mockContext.EXPECT().NewLine().Return() + mockContext.EXPECT().NewLine().Return().Times(4) mockContext.EXPECT().Option("only").Return("").Once() - mockContext.EXPECT().TwoColumnDetail(mock.Anything, mock.Anything).Return() + getGoVersion = func() string { + return "test_version" + } + var expected []string + for _, ex := range [][2]string{ + {"Environment", ""}, + {"Application Name", "test"}, + {"Goravel Version", "test_version"}, + {"Go Version", "test_version"}, + {"Environment", "test"}, + {"Debug Mode", "ENABLED"}, + {"URL", "test_url"}, + {"HTTP Host", "test_host"}, + {"HTTP Port", "test_port"}, + {"GRPC Host", "test_host"}, + {"GRPC Port", "test_port"}, + {"Drivers", ""}, + {"Cache", "test_cache"}, + {"Database", "test_database"}, + {"Hashing", "test_hashing"}, + {"Http", "test_http"}, + {"Logs", "stack / test"}, + {"Mail", "test_mail"}, + {"Queue", "test_queue"}, + {"Session", "test_session"}, + {"Custom", ""}, + {"Test Info", "OK"}, + } { + mockContext.EXPECT().TwoColumnDetail(ex[0], ex[1]). + Run(func(first string, second string, _ ...rune) { + expected = append(expected, color.Default().Sprintf("%s %s\n", first, second)) + color.Default().Printf("%s %s\n", first, second) + }).Return().Once() + } AddAboutInformation("Custom", foundation.AboutItem{Key: "Test Info", Value: "OK"}) - color.CaptureOutput(func(w io.Writer) { + assert.Contains(t, color.CaptureOutput(func(w io.Writer) { assert.Nil(t, aboutCommand.Handle(mockContext)) - }) - appInformation.Range("", func(section string, details []foundation.AboutItem) { - assert.Contains(t, []string{"Environment", "Drivers", "Custom"}, section) - assert.NotEmpty(t, details) - }) + }), strings.Join(expected, "")) } func TestAddToSection(t *testing.T) {