Skip to content

Commit

Permalink
chore: Improve artisan about commmand unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
almas1992 committed Dec 28, 2024
1 parent bbdf9e3 commit c48e461
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
5 changes: 4 additions & 1 deletion foundation/console/about_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type information struct {

var appInformation = &information{section: make(map[string]int)}
var customInformationResolvers []func()
var getGoVersion = func() string {
return strings.TrimPrefix(runtime.Version(), "go")

Check warning on line 26 in foundation/console/about_command.go

View check run for this annotation

Codecov / codecov/patch

foundation/console/about_command.go#L25-L26

Added lines #L25 - L26 were not covered by tests
}

func NewAboutCommand(app foundation.Application) *AboutCommand {
return &AboutCommand{
Expand Down Expand Up @@ -71,7 +74,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: getGoVersion()},
{Key: "Environment", Value: configFacade.GetString("app.env")},
{Key: "Debug Mode", Value: func() string {
mode := "OFF"
Expand Down
67 changes: 54 additions & 13 deletions foundation/console/about_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
{"<fg=green;op=bold>Environment</>", ""},
{"Application Name", "test"},
{"Goravel Version", "test_version"},
{"Go Version", "test_version"},
{"Environment", "test"},
{"Debug Mode", "<fg=yellow;op=bold>ENABLED</>"},
{"URL", "test_url"},
{"HTTP Host", "test_host"},
{"HTTP Port", "test_port"},
{"GRPC Host", "test_host"},
{"GRPC Port", "test_port"},
{"<fg=green;op=bold>Drivers</>", ""},
{"Cache", "test_cache"},
{"Database", "test_database"},
{"Hashing", "test_hashing"},
{"Http", "test_http"},
{"Logs", "<fg=yellow;op=bold>stack</> <fg=gray;op=bold>/</> test"},
{"Mail", "test_mail"},
{"Queue", "test_queue"},
{"Session", "test_session"},
{"<fg=green;op=bold>Custom</>", ""},
{"Test Info", "<fg=cyan>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: "<fg=cyan>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) {
Expand Down

0 comments on commit c48e461

Please sign in to comment.