Skip to content

Commit

Permalink
Merge pull request #176 from MasoniteFramework/add-class-based-drivers
Browse files Browse the repository at this point in the history
added class based drivers
  • Loading branch information
josephmancuso committed Jun 13, 2018
2 parents 63ce3d9 + 11a135e commit 7f76d48
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions masonite/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class RequiredContainerBindingNotFound(Exception):
class MissingContainerBindingNotFound(Exception):
pass

class UnacceptableDriverType(Exception):
pass


class ContainerError(Exception):
pass
Expand Down
19 changes: 14 additions & 5 deletions masonite/managers/Manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from masonite.exceptions import DriverNotFound, MissingContainerBindingNotFound
from masonite.exceptions import DriverNotFound, MissingContainerBindingNotFound, UnacceptableDriverType
import inspect


class Manager:
Expand Down Expand Up @@ -26,12 +27,20 @@ def create_driver(self, driver=None):
if not driver:
driver = self.container.make(self.config).DRIVER.capitalize()
else:
driver = driver.capitalize()
if isinstance(driver, str):
driver = driver.capitalize()

try:
self.manage_driver = self.container.make(
'{0}{1}Driver'.format(self.driver_prefix, driver)
)
if isinstance(driver, str):
self.manage_driver = self.container.make(
'{0}{1}Driver'.format(self.driver_prefix, driver)
)
return
elif inspect.isclass(driver):
self.manage_driver = driver
return

raise UnacceptableDriverType('String or class based driver required. {} driver recieved.'.format(driver))
except MissingContainerBindingNotFound:
raise DriverNotFound(
'Could not find the {0}{1}Driver from the service container. Are you missing a service provider?'.format(self.driver_prefix, driver))
14 changes: 9 additions & 5 deletions tests/test_upload_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from config import application, storage
from masonite.app import App
from masonite.exceptions import DriverNotFound, FileTypeException
from masonite.exceptions import DriverNotFound, FileTypeException, UnacceptableDriverType
from masonite.managers.UploadManager import UploadManager
from masonite.drivers.UploadDiskDriver import UploadDiskDriver
from masonite.drivers.UploadS3Driver import UploadS3Driver
Expand All @@ -22,9 +22,6 @@ def test_static_gets_alias_with_dot_notation(self):

def test_static_gets_string_location(self):
assert self.static('s3', 'profile.py') == 'http://s3.amazon.com/bucket/profile.py'





class TestUploadManager:
Expand All @@ -39,7 +36,14 @@ def setup_method(self):

def test_upload_manager_grabs_drivers(self):
assert isinstance(self.app.make('UploadManager').driver('disk'), UploadDiskDriver)


def test_upload_manager_grabs_drivers_with_a_class(self):
assert isinstance(self.app.make('UploadManager').driver(UploadDiskDriver), UploadDiskDriver)

def test_upload_manager_throws_error_with_incorrect_file_type(self):
with pytest.raises(UnacceptableDriverType):
self.app.make('UploadManager').driver(static)

def test_upload_manager_raises_driver_not_found_error(self):
self.app = App()
self.app.bind('Test', object)
Expand Down

0 comments on commit 7f76d48

Please sign in to comment.