Skip to content

Commit

Permalink
Fix ServiceFactory calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérémy JOURDIN committed Oct 13, 2015
1 parent 23221d9 commit 5def4ba
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
21 changes: 18 additions & 3 deletions Factory/GenericServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GenericServiceFactory implements ServiceFactoryInterface
/**
* @var array
*/
protected $arguments;
protected $calls;

/**
* Constructor
Expand All @@ -32,7 +32,7 @@ class GenericServiceFactory implements ServiceFactoryInterface
public function __construct($classFQN)
{
$this->classFQN = $classFQN;
$this->arguments = array_slice(func_get_args(), 1);
$this->calls = array_slice(func_get_args(), 1);
}

/**
Expand All @@ -47,7 +47,22 @@ protected function doCreate(IdentityInterface $identity, ClientFactoryInterface
{
$reflection = new \ReflectionClass($this->classFQN);

return $reflection->newInstanceArgs(array($identity, $clientFactory));
$service = $reflection->newInstanceArgs(array($identity, $clientFactory));

$this->execCalls($service);

return $service;
}

protected function execCalls(ServiceInterface $service)
{
if ($this->calls) {
foreach ($this->calls as $definition) {
call_user_func_array(array($service, $definition[0]), array_slice($definition, 1));
}
}

return $this;
}

/**
Expand Down
15 changes: 14 additions & 1 deletion Tests/Fixtures/FakeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ class FakeService implements ServiceInterface
{
protected $identity;
protected $clientFactory;
protected $something;

/**
* {@inheritDoc}
*/
public function __construct(IdentityInterface $identity, ClientFactoryInterface $clientFactory, \Countable $test = null)
public function __construct(IdentityInterface $identity, ClientFactoryInterface $clientFactory)
{
$this->identity = $identity;
$this->clientFactory = $clientFactory;
Expand All @@ -28,4 +29,16 @@ public function getClientFactory()
{
return $this->clientFactory;
}

public function setSomething($something)
{
$this->something = $something;

return $this;
}

public function getSomething()
{
return $this->something;
}
}
4 changes: 3 additions & 1 deletion Tests/Units/Factory/GenericServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function beforeTestMethod($method)
parent::beforeTestMethod($method);

$this->fqn = "LLS\\Bundle\\AWSBundle\\Tests\\Fixtures\\FakeService";
$this->instance = new Factory\GenericServiceFactory($this->fqn);
$this->instance = new Factory\GenericServiceFactory($this->fqn, array('setSomething', 'test'));
}

public function testClass()
Expand Down Expand Up @@ -55,6 +55,8 @@ public function testCreateService()
->isIdenticalTo($data[0])
->object($service->getClientFactory())
->isIdenticalTo($data[1])
->string($service->getSomething())
->isEqualTo('test')
;
}

Expand Down

0 comments on commit 5def4ba

Please sign in to comment.