Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Throw exception when resolving nonexisting binding
Browse files Browse the repository at this point in the history
  • Loading branch information
jedrzejchalubek committed Feb 27, 2017
1 parent 15707ee commit 4b6150b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
10 changes: 10 additions & 0 deletions src/Gin/Foundation/Exception/BindingResolutionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tonik\Gin\Foundation\Exception;

use Exception;

class BindingResolutionException extends Exception
{
//
}
16 changes: 13 additions & 3 deletions src/Gin/Foundation/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tonik\Gin\Foundation;

use Tonik\Gin\Foundation\Exception\BindingResolutionException;

class Theme extends Singleton
{
/**
Expand Down Expand Up @@ -36,10 +38,18 @@ public function bind($key, $value)
*/
public function get($key, $parameters = [])
{
if (is_callable($callable = $this->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;
}
}
31 changes: 20 additions & 11 deletions tests/Gin/Foundation/ThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit 4b6150b

Please sign in to comment.