-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
242 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package console | ||
|
||
type Stubs struct { | ||
} | ||
|
||
func (r Stubs) Test() string { | ||
return `package DummyPackage | ||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/suite" | ||
"goravel/tests" | ||
) | ||
type DummyTestSuite struct { | ||
suite.Suite | ||
tests.TestCase | ||
} | ||
func TestDummyTestSuite(t *testing.T) { | ||
suite.Run(t, new(DummyTestSuite)) | ||
} | ||
// SetupTest will run before each test in the suite. | ||
func (s *UserTestSuite) SetupTest() { | ||
} | ||
// TearDownTest will run after each test in the suite. | ||
func (s *UserTestSuite) TearDownTest() { | ||
} | ||
func (s *UserTestSuite) TestIndex() { | ||
// TODO | ||
} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package console | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/gookit/color" | ||
|
||
"github.com/goravel/framework/contracts/console" | ||
"github.com/goravel/framework/contracts/console/command" | ||
"github.com/goravel/framework/support/file" | ||
"github.com/goravel/framework/support/str" | ||
) | ||
|
||
type TestMakeCommand struct { | ||
} | ||
|
||
func NewTestMakeCommand() *TestMakeCommand { | ||
return &TestMakeCommand{} | ||
} | ||
|
||
// Signature The name and signature of the console command. | ||
func (receiver *TestMakeCommand) Signature() string { | ||
return "make:test" | ||
} | ||
|
||
// Description The console command description. | ||
func (receiver *TestMakeCommand) Description() string { | ||
return "Create a new test class" | ||
} | ||
|
||
// Extend The console command extend. | ||
func (receiver *TestMakeCommand) Extend() command.Extend { | ||
return command.Extend{ | ||
Category: "make", | ||
} | ||
} | ||
|
||
// Handle Execute the console command. | ||
func (receiver *TestMakeCommand) Handle(ctx console.Context) error { | ||
name := ctx.Argument(0) | ||
if name == "" { | ||
return errors.New("Not enough arguments (missing: name) ") | ||
} | ||
|
||
stub := receiver.getStub() | ||
|
||
if err := file.Create(receiver.getPath(name), receiver.populateStub(stub, name)); err != nil { | ||
return err | ||
} | ||
|
||
color.Greenln("Test created successfully") | ||
|
||
return nil | ||
} | ||
|
||
func (receiver *TestMakeCommand) getStub() string { | ||
return Stubs{}.Test() | ||
} | ||
|
||
// populateStub Populate the place-holders in the command stub. | ||
func (receiver *TestMakeCommand) populateStub(stub string, name string) string { | ||
controllerName, packageName, _ := receiver.parseName(name) | ||
|
||
stub = strings.ReplaceAll(stub, "DummyTest", str.Case2Camel(controllerName)) | ||
stub = strings.ReplaceAll(stub, "DummyPackage", packageName) | ||
|
||
return stub | ||
} | ||
|
||
// getPath Get the full path to the command. | ||
func (receiver *TestMakeCommand) getPath(name string) string { | ||
pwd, _ := os.Getwd() | ||
|
||
controllerName, _, folderPath := receiver.parseName(name) | ||
|
||
return filepath.Join(pwd, "tests", folderPath, str.Camel2Case(controllerName)+".go") | ||
} | ||
|
||
// parseName Parse the name to get the controller name, package name and folder path. | ||
func (receiver *TestMakeCommand) parseName(name string) (string, string, string) { | ||
name = strings.TrimSuffix(name, ".go") | ||
|
||
segments := strings.Split(name, "/") | ||
|
||
controllerName := segments[len(segments)-1] | ||
|
||
packageName := "tests" | ||
folderPath := "" | ||
|
||
if len(segments) > 1 { | ||
folderPath = filepath.Join(segments[:len(segments)-1]...) | ||
packageName = segments[len(segments)-2] | ||
} | ||
|
||
return controllerName, packageName, folderPath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package console | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
consolemocks "github.com/goravel/framework/contracts/console/mocks" | ||
"github.com/goravel/framework/support/file" | ||
) | ||
|
||
func TestTestMakeCommand(t *testing.T) { | ||
testMakeCommand := &TestMakeCommand{} | ||
mockContext := &consolemocks.Context{} | ||
mockContext.On("Argument", 0).Return("").Once() | ||
err := testMakeCommand.Handle(mockContext) | ||
assert.EqualError(t, err, "Not enough arguments (missing: name) ") | ||
|
||
mockContext.On("Argument", 0).Return("UserTest").Once() | ||
err = testMakeCommand.Handle(mockContext) | ||
assert.Nil(t, err) | ||
assert.True(t, file.Exists("tests/user_test.go")) | ||
|
||
mockContext.On("Argument", 0).Return("user/UserTest").Once() | ||
err = testMakeCommand.Handle(mockContext) | ||
assert.Nil(t, err) | ||
assert.True(t, file.Exists("tests/user/user_test.go")) | ||
assert.True(t, file.Contain("tests/user/user_test.go", "package user")) | ||
assert.True(t, file.Contain("tests/user/user_test.go", "type UserTestSuite struct")) | ||
assert.True(t, file.Contain("tests/user/user_test.go", "func (s *UserTestSuite) SetupTest() {")) | ||
assert.Nil(t, file.Remove("tests")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package testing | ||
|
||
type TestCase struct { | ||
} |