Skip to content

Commit

Permalink
Merge 2500f7a into 540c27c
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrukowski committed Aug 14, 2017
2 parents 540c27c + 2500f7a commit 6675b59
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 5 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
@@ -1,5 +1,7 @@
language: php

dist: trusty

php:
- 5.4
- 5.5
Expand All @@ -22,8 +24,8 @@ before_script:
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi;

script:
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then phpunit --coverage-clover clover.xml ; fi
- if [[ "$TRAVIS_PHP_VERSION" != '5.6' ]]; then phpunit ; fi
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then vendor/bin/phpunit --coverage-clover clover.xml ; fi
- if [[ "$TRAVIS_PHP_VERSION" != '5.6' ]]; then vendor/bin/phpunit ; fi
- vendor/bin/athletic --path tests/Benchmark --formatter GroupedFormatter
after_script:
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then php vendor/bin/coveralls -v ; fi
20 changes: 18 additions & 2 deletions src/Invoker.php
Expand Up @@ -60,6 +60,22 @@ public function call($callable, array $parameters = array())
));
}

return call_user_func_array($callable, $this->getArgs($callable, $parameters));
}

/**
* {@inheritdoc}
*/
public function create($className, array $parameters = array())
{
$className = (string) $className;
$classReflection = new \ReflectionClass($className);

return $classReflection->newInstanceArgs($this->getArgs(array($className, '__construct'), $parameters));
}

private function getArgs($callable, array $parameters)
{
$callableReflection = CallableReflection::create($callable);

$args = $this->parameterResolver->getParameters($callableReflection, $parameters, array());
Expand All @@ -78,8 +94,8 @@ public function call($callable, array $parameters = array())
$parameter->name
));
}

return call_user_func_array($callable, $args);
return $args;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/InvokerInterface.php
Expand Up @@ -26,4 +26,13 @@ interface InvokerInterface
* @throws NotEnoughParametersException
*/
public function call($callable, array $parameters = array());

/**
* Create instance of the given class using the given parameters.
*
* @param string $className
* @param array $parameters
* @return object
*/
public function create($className, array $parameters = array());
}
6 changes: 6 additions & 0 deletions src/Reflection/CallableReflection.php
Expand Up @@ -32,6 +32,12 @@ public static function create($callable)
list($class, $method) = $callable;

if (! method_exists($class, $method)) {
if ('__construct' === $method) {
return new \ReflectionFunction(function () use ($class) {
return new $class;
});
}

throw NotCallableException::fromInvalidCallable($callable);
}

Expand Down
33 changes: 33 additions & 0 deletions tests/InvokerTest.php
Expand Up @@ -377,6 +377,39 @@ public function should_throw_if_no_value_for_optional_parameter_2()
));
}

/**
* @test
*/
public function should_create_object()
{
$className = 'Invoker\Test\TestObject';
$parameters = array(
'd' => 'd value',
'b' => new \stdClass(),
'a' => 'a value',
'c' => 'c value',
);

$invoker = new Invoker();
/** @var TestObject $testObject */
$testObject = $invoker->create($className, $parameters);
$this->assertInstanceOf($className, $testObject);
$this->assertSame($parameters['a'], $testObject->getA());
$this->assertSame($parameters['b'], $testObject->getB());
$this->assertSame($parameters['c'], $testObject->getC());
}

/**
* @test
*/
public function should_create_object_without_constructor()
{
$className = 'Invoker\Test\TestObjectWithoutConstructor';
$invoker = new Invoker();
$object = $invoker->create($className);
$this->assertInstanceOf($className, $object);
}

private function assertWasCalled(CallableSpy $callableSpy)
{
$this->assertEquals(1, $callableSpy->getCallCount(), 'The callable should be called once');
Expand Down
1 change: 0 additions & 1 deletion tests/Mock/NotFound.php
@@ -1,5 +1,4 @@
<?php
declare(strict_types = 1);

namespace Invoker\Test\Mock;

Expand Down
34 changes: 34 additions & 0 deletions tests/TestObject.php
@@ -0,0 +1,34 @@
<?php

namespace Invoker\Test;

class TestObject
{
private $a;

private $b;

private $c;

public function __construct($a, $b, $c)
{
$this->a = $a;
$this->b = $b;
$this->c = $c;
}

public function getA()
{
return $this->a;
}

public function getB()
{
return $this->b;
}

public function getC()
{
return $this->c;
}
}
7 changes: 7 additions & 0 deletions tests/TestObjectWithoutConstructor.php
@@ -0,0 +1,7 @@
<?php

namespace Invoker\Test;

class TestObjectWithoutConstructor
{
}

0 comments on commit 6675b59

Please sign in to comment.