Skip to content

Commit

Permalink
Merge d8979c9 into 9445d4d
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Sep 14, 2018
2 parents 9445d4d + d8979c9 commit 8676c0b
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 5 deletions.
2 changes: 1 addition & 1 deletion masonite/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .static import static
from .password import password
from .validator import validate
from .misc import dot
from .misc import random_string, dot
20 changes: 20 additions & 0 deletions masonite/helpers/misc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
""" Module for miscellaneous helper methods """

import random
import string


def random_string(length=4):
"""Generates a random string based on the length given
Keyword Arguments:
length {int} -- The amount of the characters to generate (default: {4})
Returns:
string
"""

return "".join(
random.choice(string.ascii_uppercase + string.digits) for _ in range(length)
)

def dot(data, compile_to=None):
notation_list = data.split('.')

Expand Down
64 changes: 62 additions & 2 deletions masonite/provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Module for the Service Provider.
"""
""" Module for the Service Provider """

from masonite.helpers import random_string


class ServiceProvider:
Expand Down Expand Up @@ -38,3 +39,62 @@ def load_app(self, app):

self.app = app
return self

def routes(self, routes):
"""Adds routes to the container
Arguments:
routes {list} -- List of routes to add to the container
"""

web_routes = self.app.make('WebRoutes')
web_routes += routes

def http_middleware(self, middleware):
"""Adds HTTP middleware to the container
Arguments:
middleware {list} -- List of middleware to add
"""

http_middleware = self.app.make('HttpMiddleware')
http_middleware += middleware

def route_middleware(self, middleware):
"""Add route middleware to the container
Arguments:
middleware {dict} -- A dictionary of route middleware to add
"""

route_middleware = self.app.make('RouteMiddleware')
route_middleware.update(middleware)

def migrations(self, *directories):
"""Add migration directories to the container
"""

for directory in directories:
self.app.bind(
'{}_MigrationDirectory'.format(random_string(4)),
directory
)

def commands(self, *commands):
"""Adds commands to the container. Pass in the commands as arguments.
"""

for command in commands:
self.app.bind(
'{}Command'.format(command.__class__.__name__.replace('Command', '')),
command
)

def assets(self, assets):
"""Add assets to the container
Arguments:
assets {dict} -- A dictionary of assets to add
"""

self.app.make('Storage').STATICFILES.update(assets)
63 changes: 61 additions & 2 deletions tests/test_service_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from masonite.app import App
from masonite.request import Request
from masonite.routes import Get
from masonite.testsuite.TestSuite import generate_wsgi
from masonite.testsuite.TestSuite import generate_wsgi, TestSuite


class ContainerTest(ServiceProvider):
Expand All @@ -15,12 +15,49 @@ def boot(self, request: Request, get: Get):
def testboot(self, request: Request, Get: Get):
return request

class Mock1Command:
pass

class Mock2Command:
pass

ROUTE1 = Get().route('/url/here', None)
ROUTE2 = Get().route('/test/url', None)

class LoadProvider(ServiceProvider):

def boot(self):
self.routes([
ROUTE1,
ROUTE2
])

self.http_middleware([
object,
object
])

self.route_middleware({
'route1': object,
'route2': object,
})

self.migrations('directory/1', 'directory/2')

self.assets({
'/some/alias': '/some/location'
})

self.commands(Mock1Command(), Mock2Command())

class TestServiceProvider:

def setup_method(self):
self.app = App()
self.app = TestSuite().create_container().container
self.provider = ServiceProvider()
self.provider.load_app(self.app).boot()
self.load_provider = LoadProvider()
self.load_provider.load_app(self.app).boot()

def test_service_provider_loads_app(self):
assert self.provider.app == self.app
Expand All @@ -44,3 +81,25 @@ def test_can_call_container_with_annotation_with_self_parameter(self):
self.app.bind('Get', Get().route('url', None))

assert self.app.resolve(ContainerTest().testboot) == self.app.make('Request')

def test_can_load_routes_into_container(self):
assert len(self.app.make('WebRoutes')) > 2
assert self.app.make('WebRoutes')[-2:] == [ROUTE1, ROUTE2]

def test_can_load_http_middleware_into_container(self):
assert self.app.make('HttpMiddleware')[-2:] == [object, object]

def test_can_load_route_middleware_into_container(self):
assert self.app.make('RouteMiddleware')['route1'] == object
assert self.app.make('RouteMiddleware')['route2'] == object

def test_can_load_migrations_into_container(self):
assert len(self.app.collect('*MigrationDirectory')) == 2

def test_can_load_assets_into_container(self):
assert self.app.make('Storage').STATICFILES['/some/alias'] == '/some/location'

def test_can_load_commands_into_container(self):
assert self.app.make('Mock1Command')
assert self.app.make('Mock2Command')

0 comments on commit 8676c0b

Please sign in to comment.