Skip to content

Commit

Permalink
add a default factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Ermashev committed Aug 6, 2020
1 parent 282ac55 commit b1bdd71
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Factory/InvokableFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Tiny package.
*
* (c) Alex Ermashev <alexermashevn@gmail.com>
*
* 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();
}

}
33 changes: 33 additions & 0 deletions test/Factory/invokableFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Tiny package.
*
* (c) Alex Ermashev <alexermashevn@gmail.com>
*
* 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);
}

}

0 comments on commit b1bdd71

Please sign in to comment.