class TestMailManager(unittest.TestCase):
    def setUp(self):
        self.app = App()
        self.app = self.app.bind('Container', self.app)

        self.app.bind('Test', object)
        self.app.bind('MailSmtpDriver', object)
        self.app.bind('MailConfig', mail)
        self.app.bind('View', View(self.app).render)
        self.app.bind('ViewClass', View(self.app))

    def test_mail_manager_loads_container(self):
        mailManager = MailManager(self.app)
        self.assertTrue(mailManager.load_container(self.app))

    def test_mail_manager_resolves_from_contract(self):
        self.app.singleton('MailManager', MailManager)
        self.assertEqual(self.app.resolve(self._test_resolve),
                         self.app.make('MailManager'))

    def _test_resolve(self, mail: MailManagerContract):
        return mail

    def test_creates_driver(self):
        mailManager = MailManager(self.app)

        self.assertIsInstance(mailManager.manage_driver, object)

    def test_does_not_create_driver_with_initilization_container(self):

        mailManager = MailManager(self.app)

        self.assertEqual(mailManager.manage_driver, None)

    def test_does_not_raise_drivernotfound_exception(self):
        MailManager(self.app)

    def test_manager_sets_driver(self):
        self.app.bind('MailMailtrapDriver', Mailgun)
        MailManager(self.app).driver('mailtrap')

    def test_manager_sets_driver_throws_driver_not_found_exception(self):
        with self.assertRaises(DriverNotFound):
            MailManager(self.app).driver('mailtrap')

    def test_drivers_are_resolvable_by_container(self):
        self.app.bind('MailSmtpDriver', MailDriver)

        self.assertIsInstance(MailManager(self.app).driver('smtp'), MailDriver)

    def test_driver_loads_template(self):
        self.app.bind('MailSmtpDriver', MailDriver)

        driver = MailManager(self.app).driver('smtp')

        self.assertEqual(
            driver.template('test', {
                'test': 'test'
            }).message_body, 'test')

    def test_send_mail(self):
        self.app.bind('MailSmtpDriver', MailDriver)
        self.assertTrue(
            MailManager(self.app).driver('smtp').to('*****@*****.**'))

    def test_send_mail_with_from(self):
        self.app.bind('MailSmtpDriver', MailDriver)

        self.assertEqual(
            MailManager(
                self.app).driver('smtp').to('*****@*****.**').send_from(
                    '*****@*****.**').from_address,
            '*****@*****.**')

    def test_send_mail_sends(self):
        if env('RUN_MAIL'):
            self.app.bind('MailSmtpDriver', MailDriver)

            self.assertTrue(
                MailManager(self.app).driver('smtp').to(
                    '*****@*****.**').send('hi'))

    def test_send_mail_sends_with_queue(self):
        if env('RUN_MAIL'):
            self.app.bind('MailSmtpDriver', MailDriver)

            self.assertEqual(
                MailManager(self.app).driver('smtp').to(
                    '*****@*****.**').queue().send('hi'), None)

    def test_send_mail_with_subject(self):
        self.app.bind('MailSmtpDriver', MailDriver)

        self.assertEqual(
            MailManager(self.app).driver('smtp').to('').subject(
                'test').message_subject, 'test')

    def test_send_mail_with_callable(self):
        self.app.bind('MailSmtpDriver', MailDriver)
        user = User
        user.email = '*****@*****.**'
        self.assertTrue(MailManager(self.app).driver('smtp').to(User))

    def test_switch_mail_manager(self):
        self.app.bind('MailSmtpDriver', MailDriver)
        self.app.bind('MailTestDriver', Mailgun)

        mail_driver = MailManager(self.app).driver('smtp')

        self.assertIsInstance(mail_driver.driver('test'), Mailgun)

    def test_mail_helper_method_resolves_a_driver(self):
        self.assertIsInstance(mail_helper(), MailContract)
Example #2
0
class TestApp(unittest.TestCase):
    def setUp(self):
        self.app = App()

    def test_app_binds(self):
        self.app.bind('test1', object)
        self.app.bind('test2', object)
        self.assertEqual(self.app.providers, {
            'test1': object,
            'test2': object
        })

    def test_app_makes(self):
        self.app.bind('Request', REQUEST)
        self.assertEqual(self.app.make('Request').cookies, [])

    def test_app_makes_and_resolves(self):
        self.app.bind('Request', REQUEST)
        self.app.bind('MockMail', MockMail)
        mockmail = self.app.make('MockMail')
        self.assertIsInstance(mockmail.request, Request)

    def test_app_makes_different_instances(self):
        self.app.bind('MockMail', MockMail)
        self.app.bind('Request', REQUEST)
        m1 = self.app.make('MockMail')
        m2 = self.app.make('MockMail')

        self.assertNotEqual(id(m1), id(m2))

    def test_app_makes_singleton_instance(self):
        self.app.bind('Request', REQUEST)
        self.app.singleton('MockMail', MockMail)
        m1 = self.app.make('MockMail')
        m2 = self.app.make('MockMail')

        self.assertEqual(id(m1), id(m2))
        self.assertEqual(id(m1.request), id(m2.request))

        m1.request.test = 'test'
        self.assertEqual(m2.request.test, 'test')

    def test_can_set_container_hook(self):
        self.app.on_bind('Request', self._func_on_bind)
        self.app.bind('Request', REQUEST)
        self.assertEqual(self.app.make('Request').path, '/test/on/bind')

    def _func_on_bind(self, request, container):
        request.path = '/test/on/bind'

    def test_can_set_container_hook_with_obj_binding(self):
        self.app.on_bind(Request, self._func_on_bind_with_obj)
        self.app.bind('Request', REQUEST)
        self.assertEqual(self.app.make('Request').path, '/test/on/bind/obj')

    def _func_on_bind_with_obj(self, request, container):
        request.path = '/test/on/bind/obj'

    def test_can_fire_container_hook_on_make(self):
        self.app.on_make(Request, self._func_on_make)
        self.app.bind('Request', REQUEST)
        self.assertEqual(self.app.make('Request').path, '/test/on/make')
        self.assertEqual(self.app.make('Request').path, '/test/on/make')

    def _func_on_make(self, request, container):
        request.path = '/test/on/make'

    def test_can_fire_hook_on_resolve(self):
        self.app.on_resolve(Request, self._func_on_resolve)
        self.app.bind('Request', REQUEST)
        self.assertEqual(
            self.app.resolve(self._resolve_request).path, '/on/resolve')
        self.assertEqual(
            self.app.resolve(self._resolve_request).path, '/on/resolve')

    def test_can_fire_hook_on_resolve_class(self):
        self.app.on_resolve(Request, self._func_on_resolve_class)
        self.app.bind('Request', REQUEST)
        self.assertEqual(
            self.app.resolve(self._resolve_reques_class).path,
            '/on/resolve/class')
        self.assertEqual(
            self.app.resolve(self._resolve_reques_class).path,
            '/on/resolve/class')

    def test_can_resolve_parameter_with_keyword_argument_setting(self):
        self.app.bind('Request', REQUEST)
        self.app.resolve_parameters = True
        self.assertEqual(self.app.resolve(self._resolve_parameter), REQUEST)
        self.assertEqual(self.app.resolve(self._resolve_parameter), REQUEST)

    def _func_on_resolve(self, request, container):
        request.path = '/on/resolve'

    def _func_on_resolve_class(self, request, container):
        request.path = '/on/resolve/class'

    def _resolve_request(self, request: Request):
        return request

    def _resolve_parameter(self, Request):
        return Request

    def _resolve_reques_class(self, request: Request):
        return request