-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for services created with factories (#21)
* Add support for services with factories * Added tests * cs * Fixed tests * PHP 7.2 fix
- Loading branch information
Showing
7 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Happyr\ServiceMocking\Generator; | ||
|
||
use Laminas\Code\Generator\Exception\InvalidArgumentException; | ||
use Laminas\Code\Generator\ParameterGenerator; | ||
use Laminas\Code\Generator\PropertyGenerator; | ||
use ProxyManager\Generator\MethodGenerator; | ||
|
||
/** | ||
* The `__construct_with_factory` implementation for lazy loading proxies. This | ||
* is used for services created with service factories. | ||
* | ||
* @interal | ||
*/ | ||
class StaticConstructor extends MethodGenerator | ||
{ | ||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function generateMethod(PropertyGenerator $valueHolder): self | ||
{ | ||
$constructor = new self('__construct_with_factory'); | ||
$constructor->setStatic(true); | ||
$constructor->setParameter(new ParameterGenerator('factory', 'callable')); | ||
$parameter = new ParameterGenerator('arguments'); | ||
$parameter->setVariadic(true); | ||
$constructor->setParameter($parameter); | ||
|
||
$constructor->setBody( | ||
'static $reflection;'."\n\n" | ||
.'$reflection = $reflection ?? new \ReflectionClass(self::class);'."\n" | ||
.'$model = $reflection->newInstanceWithoutConstructor();'."\n" | ||
.'$model->'.$valueHolder->getName().' = \Closure::fromCallable($factory)->__invoke(...$arguments);'."\n" | ||
.'\Happyr\ServiceMocking\ServiceMock::initializeProxy($model);'."\n\n" | ||
.'return $model;' | ||
); | ||
|
||
return $constructor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Happyr\ServiceMocking\Tests\Resource; | ||
|
||
class ServiceWithFactory | ||
{ | ||
private $number; | ||
private $secretNumber; | ||
|
||
public function __construct(int $number) | ||
{ | ||
$this->number = $number; | ||
} | ||
|
||
public static function create(int $number, int $secret) | ||
{ | ||
$self = new self($number); | ||
$self->secretNumber = $secret; | ||
|
||
return $self; | ||
} | ||
|
||
public function getNumber(int $input = 0): int | ||
{ | ||
return $this->number + $input - $this->secretNumber; | ||
} | ||
|
||
public function getSecretNumber(): int | ||
{ | ||
return $this->secretNumber; | ||
} | ||
} |
Empty file.