github.com/goravel/framework@v1.13.9/foundation/container_test.go (about)

     1  package foundation
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/suite"
     8  
     9  	"github.com/goravel/framework/contracts/foundation"
    10  )
    11  
    12  type ContainerTestSuite struct {
    13  	suite.Suite
    14  	container *Container
    15  }
    16  
    17  func TestContainerTestSuite(t *testing.T) {
    18  	suite.Run(t, new(ContainerTestSuite))
    19  }
    20  
    21  func (s *ContainerTestSuite) SetupTest() {
    22  	s.container = NewContainer()
    23  }
    24  
    25  func (s *ContainerTestSuite) TestBind() {
    26  	callback := func(app foundation.Application) (any, error) {
    27  		return 1, nil
    28  	}
    29  	s.container.Bind("Bind", callback)
    30  
    31  	concrete, exist := s.container.bindings.Load("Bind")
    32  	s.True(exist)
    33  	ins, ok := concrete.(instance)
    34  	s.True(ok)
    35  	s.False(ins.shared)
    36  	s.NotNil(ins.concrete)
    37  	switch concrete := ins.concrete.(type) {
    38  	case func(app foundation.Application) (any, error):
    39  		concreteImpl, err := concrete(nil)
    40  		s.Equal(1, concreteImpl)
    41  		s.Nil(err)
    42  	default:
    43  		s.T().Errorf("error")
    44  	}
    45  }
    46  
    47  func (s *ContainerTestSuite) TestBindWith() {
    48  	callback := func(app foundation.Application, parameters map[string]any) (any, error) {
    49  		return parameters["name"], nil
    50  	}
    51  	s.container.BindWith("BindWith", callback)
    52  
    53  	concrete, exist := s.container.bindings.Load("BindWith")
    54  	s.True(exist)
    55  	ins, ok := concrete.(instance)
    56  	s.True(ok)
    57  	s.False(ins.shared)
    58  	s.NotNil(ins.concrete)
    59  	switch concrete := ins.concrete.(type) {
    60  	case func(app foundation.Application, parameters map[string]any) (any, error):
    61  		concreteImpl, err := concrete(nil, map[string]any{"name": "goravel"})
    62  		s.Equal("goravel", concreteImpl)
    63  		s.Nil(err)
    64  	default:
    65  		s.T().Errorf("error")
    66  	}
    67  }
    68  
    69  func (s *ContainerTestSuite) TestInstance() {
    70  	impl := 1
    71  	s.container.Instance("Instance", impl)
    72  
    73  	concrete, exist := s.container.bindings.Load("Instance")
    74  	s.True(exist)
    75  	ins, ok := concrete.(instance)
    76  	s.True(ok)
    77  	s.True(ins.shared)
    78  	s.NotNil(ins.concrete)
    79  	s.Equal(impl, ins.concrete)
    80  }
    81  
    82  func (s *ContainerTestSuite) TestSingleton() {
    83  	callback := func(app foundation.Application) (any, error) {
    84  		return 1, nil
    85  	}
    86  	s.container.Singleton("Singleton", callback)
    87  
    88  	concrete, exist := s.container.bindings.Load("Singleton")
    89  	s.True(exist)
    90  	ins, ok := concrete.(instance)
    91  	s.True(ok)
    92  	s.True(ins.shared)
    93  	s.NotNil(ins.concrete)
    94  	switch concrete := ins.concrete.(type) {
    95  	case func(app foundation.Application) (any, error):
    96  		concreteImpl, err := concrete(nil)
    97  		s.Equal(1, concreteImpl)
    98  		s.Nil(err)
    99  	default:
   100  		s.T().Errorf("error")
   101  	}
   102  }
   103  
   104  func (s *ContainerTestSuite) TestMake() {
   105  	tests := []struct {
   106  		name       string
   107  		key        string
   108  		parameters map[string]any
   109  		setup      func()
   110  		expectImpl any
   111  		expectErr  error
   112  	}{
   113  		{
   114  			name:      "not found binding",
   115  			key:       "no",
   116  			setup:     func() {},
   117  			expectErr: fmt.Errorf("binding not found: %+v", "no"),
   118  		},
   119  		{
   120  			name: "found Singleton",
   121  			key:  "Singleton",
   122  			setup: func() {
   123  				s.container.Singleton("Singleton", func(app foundation.Application) (any, error) {
   124  					return 1, nil
   125  				})
   126  			},
   127  			expectImpl: 1,
   128  		},
   129  		{
   130  			name: "found Bind",
   131  			key:  "Bind",
   132  			setup: func() {
   133  				s.container.Bind("Bind", func(app foundation.Application) (any, error) {
   134  					return 1, nil
   135  				})
   136  			},
   137  			expectImpl: 1,
   138  		},
   139  		{
   140  			name: "found BindWith",
   141  			key:  "BindWith",
   142  			parameters: map[string]any{
   143  				"name": "goravel",
   144  			},
   145  			setup: func() {
   146  				s.container.BindWith("BindWith", func(app foundation.Application, parameters map[string]any) (any, error) {
   147  					return parameters["name"], nil
   148  				})
   149  			},
   150  			expectImpl: "goravel",
   151  		},
   152  		{
   153  			name: "found Instance",
   154  			key:  "Instance",
   155  			parameters: map[string]any{
   156  				"name": "goravel",
   157  			},
   158  			setup: func() {
   159  				s.container.Instance("Instance", 1)
   160  			},
   161  			expectImpl: 1,
   162  		},
   163  	}
   164  
   165  	for _, test := range tests {
   166  		s.Run(test.name, func() {
   167  			test.setup()
   168  			impl, err := s.container.make(test.key, test.parameters)
   169  			s.Equal(test.expectImpl, impl)
   170  			s.Equal(test.expectErr, err)
   171  		})
   172  	}
   173  }