Skip to content

Commit

Permalink
split trait ServiceContainerTrait into single purporse traits
Browse files Browse the repository at this point in the history
this allows usage only partial SS functionality
  • Loading branch information
esler committed Apr 26, 2019
1 parent 0a29efc commit 2726a60
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 68 deletions.
61 changes: 61 additions & 0 deletions src/PHPUnit/DepsProviderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

namespace IW\PHPUnit;

trait DepsProviderTrait
{
use ServiceContainerProviderTrait;

private $depsProviderClass = true;

/**
* Resolve class's annotation @depsProvider <methodName>
*
* @before
*/
public function resolveAnnotationDepsProvider(): void {
['class' => $classAnnotations, 'method' => $methodAnnotations] = $this->getAnnotations();

if ($this->depsProviderClass) {
$this->depsProviderClass = false;

if ($classProviders = $classAnnotations['depsProvider'] ?? null) {
foreach ($classProviders as $classProvider) {
$this->callDepsProvider($classProvider);
}
}
}

if ($methodProviders = $methodAnnotations['depsProvider'] ?? null) {
foreach ($methodProviders as $methodProvider) {
$this->callDepsProvider($methodProvider);
}
}
}

/**
* Resolves and calls given deps provider
*
* @param string $provider method to call
*
* @return void
*/
private function callDepsProvider(string $provider): void {
$method = \get_class($this) . '::' . $provider;

try {
$this->$provider(...$this->getServiceContainer()->resolve([$this, $provider]));
} catch (\TypeError $error) {
if (!method_exists($this, $provider)) {
throw new \InvalidArgumentException('Method ' . $method . ' does not exists');
}

$reflection = new \ReflectionMethod($this, $provider);
if (!$reflection->isPublic()) {
throw new \InvalidArgumentException('Method ' . $method . ' must be public');
}

throw $error;
}
}
}
17 changes: 17 additions & 0 deletions src/PHPUnit/ServiceContainerDataProviderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace IW\PHPUnit;

trait ServiceContainerDataProviderTrait
{
use ServiceContainerProviderTrait;

/**
* It's called before each test to resolve its dependencies defined
*
* @return void
*/
public function ServiceContainer(string $method): array {
return [$this->getServiceContainer()->resolve([$this, $method])];
}
}
16 changes: 16 additions & 0 deletions src/PHPUnit/ServiceContainerProviderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace IW\PHPUnit;

use IW\ServiceContainer;

trait ServiceContainerProviderTrait
{
/**
* Returns instance of your service container
*
* @return ServiceContainer
*/
abstract protected static function getServiceContainer(): ServiceContainer;

}
70 changes: 2 additions & 68 deletions src/PHPUnit/ServiceContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,7 @@

trait ServiceContainerTrait
{
private $depsProviderClass = true;

/**
* It's called before each test to resolve its dependencies defined
*
* @return void
*/
public function ServiceContainer(string $method): array {
return [$this->getServiceContainer()->resolve([$this, $method])];
}

/**
* Resolve class's annotation @depsProvider <methodName>
*
* @before
*/
public function resolveAnnotationDepsProvider(): void {
['class' => $classAnnotations, 'method' => $methodAnnotations] = $this->getAnnotations();

if ($this->depsProviderClass) {
$this->depsProviderClass = false;

if ($classProviders = $classAnnotations['depsProvider'] ?? null) {
foreach ($classProviders as $classProvider) {
$this->callDepsProvider($classProvider);
}
}
}

if ($methodProviders = $methodAnnotations['depsProvider'] ?? null) {
foreach ($methodProviders as $methodProvider) {
$this->callDepsProvider($methodProvider);
}
}
}

/**
* Resolves and calls given deps provider
*
* @param string $provider method to call
*
* @return void
*/
private function callDepsProvider(string $provider): void {
$method = \get_class($this) . '::' . $provider;

try {
$this->$provider(...$this->getServiceContainer()->resolve([$this, $provider]));
} catch (\TypeError $error) {
if (!method_exists($this, $provider)) {
throw new \InvalidArgumentException('Method ' . $method . ' does not exists');
}

$reflection = new \ReflectionMethod($this, $provider);
if (!$reflection->isPublic()) {
throw new \InvalidArgumentException('Method ' . $method . ' must be public');
}

throw $error;
}
}

/**
* Returns instance of your service container
*
* @return ServiceContainer
*/
abstract protected static function getServiceContainer(): ServiceContainer;
use DepsProviderTrait;
use ServiceContainerDataProviderTrait;

}

0 comments on commit 2726a60

Please sign in to comment.