From 3bd7e0df98b8f311ec449da43d35b20ee4100ea5 Mon Sep 17 00:00:00 2001 From: jedrzejchalubek Date: Mon, 22 May 2017 14:44:07 +0200 Subject: [PATCH] Add test for multiple resolving service bounded as singleton --- src/Gin/Foundation/Theme.php | 1 - tests/Gin/Foundation/ThemeTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Gin/Foundation/Theme.php b/src/Gin/Foundation/Theme.php index b06f447..834979e 100644 --- a/src/Gin/Foundation/Theme.php +++ b/src/Gin/Foundation/Theme.php @@ -90,7 +90,6 @@ public function get($key, array $parameters = []) // Otherwise, look for service // in services collection. if (isset($this->services[$key])) { - // Resolve service jf we don't have // a deposit for this service. if (! isset($this->deposit[$key])) { diff --git a/tests/Gin/Foundation/ThemeTest.php b/tests/Gin/Foundation/ThemeTest.php index 7a9008b..625cb0a 100644 --- a/tests/Gin/Foundation/ThemeTest.php +++ b/tests/Gin/Foundation/ThemeTest.php @@ -117,6 +117,23 @@ public function it_should_return_same_object_on_factory_binding() $this->assertNotSame($theme->get('factory'), $theme->get('factory')); } + /** + * @test + */ + public function it_should_create_service_only_once_on_multiple_resolving() + { + $theme = Theme::getInstance(); + $mock = Mockery::mock(new Stub); + + $theme->bind('service', function () use ($mock) { return $mock->action(); }); + + $mock->shouldReceive('action')->once()->andReturn('value'); + + $theme->get('service'); + $theme->get('service'); + $theme->get('service'); + } + /** * @test */ @@ -128,4 +145,12 @@ public function it_should_throw_exception_on_resolving_nonexisting_binding() $theme->get('nonexsiting.binding'); } +} + +class Stub +{ + function action() + { + // + } } \ No newline at end of file