-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split trait ServiceContainerTrait into single purporse traits
this allows usage only partial SS functionality
- Loading branch information
Showing
4 changed files
with
96 additions
and
68 deletions.
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
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; | ||
} | ||
} | ||
} |
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,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])]; | ||
} | ||
} |
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,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; | ||
|
||
} |
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