From b1bdd7144881131801e40d88c58c7d36a5cfa9a0 Mon Sep 17 00:00:00 2001 From: Alex Ermashev Date: Thu, 6 Aug 2020 13:48:20 +0600 Subject: [PATCH] add a default factory --- src/Factory/InvokableFactory.php | 32 ++++++++++++++++++++++++++ test/Factory/invokableFactoryTest.php | 33 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/Factory/InvokableFactory.php create mode 100644 test/Factory/invokableFactoryTest.php diff --git a/src/Factory/InvokableFactory.php b/src/Factory/InvokableFactory.php new file mode 100644 index 0000000..c5b95f2 --- /dev/null +++ b/src/Factory/InvokableFactory.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tiny\ServiceManager\Factory; + +use Tiny\ServiceManager\ServiceManager; + +class InvokableFactory +{ + + /** + * @param ServiceManager $serviceManager + * @param string $targetClass + * + * @return object + */ + public function __invoke( + ServiceManager $serviceManager, + string $targetClass + ) { + return new $targetClass(); + } + +} diff --git a/test/Factory/invokableFactoryTest.php b/test/Factory/invokableFactoryTest.php new file mode 100644 index 0000000..e4c876d --- /dev/null +++ b/test/Factory/invokableFactoryTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace TinyTest\ServiceManager\Factory; + +use PHPUnit\Framework\TestCase; +use Tiny\ServiceManager\Factory\InvokableFactory; +use Tiny\ServiceManager\ServiceManager; +use stdClass; + +class invokableFactoryTest extends TestCase +{ + + public function testInvokeMethod() + { + $factory = new InvokableFactory(); + $instance = $factory( + $this->createMock(ServiceManager::class), + stdClass::class + ); + + $this->assertInstanceOf(stdClass::class, $instance); + } + +}