From 4b6150be96808d95e9f1bce53247dd42f09d04a3 Mon Sep 17 00:00:00 2001 From: jedrzejchalubek Date: Mon, 27 Feb 2017 15:05:33 +0100 Subject: [PATCH] Throw exception when resolving nonexisting binding --- .../Exception/BindingResolutionException.php | 10 ++++++ src/Gin/Foundation/Theme.php | 16 ++++++++-- tests/Gin/Foundation/ThemeTest.php | 31 ++++++++++++------- 3 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 src/Gin/Foundation/Exception/BindingResolutionException.php diff --git a/src/Gin/Foundation/Exception/BindingResolutionException.php b/src/Gin/Foundation/Exception/BindingResolutionException.php new file mode 100644 index 0000000..102db9b --- /dev/null +++ b/src/Gin/Foundation/Exception/BindingResolutionException.php @@ -0,0 +1,10 @@ +registry[$key])) { - return call_user_func_array($callable, $parameters); + if (! isset($this->registry[$key])) { + throw new BindingResolutionException("Unresolvable resolution. The [{$key}] binding is not registered."); + } + + if (is_callable($abstract = $this->registry[$key])) { + if (is_array($parameters)) { + return call_user_func_array($abstract, $parameters); + } + + return call_user_func($abstract, $parameters); } - return $this->registry[$key]; + return $abstract; } } diff --git a/tests/Gin/Foundation/ThemeTest.php b/tests/Gin/Foundation/ThemeTest.php index ea7bf72..311cc08 100644 --- a/tests/Gin/Foundation/ThemeTest.php +++ b/tests/Gin/Foundation/ThemeTest.php @@ -17,31 +17,40 @@ public function it_return_same_instance_on_every_access() /** * @test */ - public function test_binding_and_resolving_value_from_theme_container() + public function test_binding_and_resolving_value_from_the_theme_container() { $theme = Theme::getInstance(); $theme->bind('key', 'value'); - $this->assertEquals(Theme::getInstance()->get('key'), 'value'); + $this->assertEquals($theme->get('key'), 'value'); } /** * @test */ - public function test_binding_and_resolving_closure_from_theme_container() + public function test_binding_and_resolving_callable_from_the_theme_container() { $theme = Theme::getInstance(); - $theme->bind('key.without.parameters', function () { - return 'value'; - }); + $theme->bind('callable.without.parameters', function () { return 'value'; }); + $theme->bind('callable.with.parameter', function ($param) { return $param; }); + $theme->bind('callable.with.parameters', function ($param1, $param2) { return $param1.','.$param2; }); - $theme->bind('key.with.parameters', function ($param1, $param2) { - return $param1.','.$param2; - }); + $this->assertEquals($theme->get('callable.without.parameters'), 'value'); + $this->assertEquals($theme->get('callable.with.parameter', 'value'), 'value'); + $this->assertEquals($theme->get('callable.with.parameters', ['value1', 'value2']), 'value1,value2'); + } + + /** + * @test + */ + public function it_should_throw_exception_on_resolving_nonexisting_binding() + { + $theme = Theme::getInstance(); + + $this->expectException('Tonik\Gin\Foundation\Exception\BindingResolutionException'); - $this->assertEquals(Theme::getInstance()->get('key.without.parameters'), 'value'); - $this->assertEquals(Theme::getInstance()->get('key.with.parameters', ['value1', 'value2']), 'value1,value2'); + $theme->get('nonexsiting.binding'); } } \ No newline at end of file