-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify Factory test by utilising a PSR container stub
Signed-off-by: George Steel <[email protected]>
- Loading branch information
Showing
2 changed files
with
55 additions
and
49 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
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AppTest; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use RuntimeException; | ||
|
||
use function array_key_exists; | ||
use function sprintf; | ||
|
||
/** | ||
* A PSR Container stub. Useful for testing factories without excessive mocking | ||
*/ | ||
class InMemoryContainer implements ContainerInterface | ||
{ | ||
/** @var array<string, mixed> */ | ||
public array $services = []; | ||
|
||
public function setService(string $name, mixed $service): void | ||
{ | ||
$this->services[$name] = $service; | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @return mixed | ||
*/ | ||
public function get($id) | ||
{ | ||
if (! $this->has($id)) { | ||
throw new RuntimeException(sprintf('Service not found "%s"', $id)); | ||
} | ||
|
||
return $this->services[$id]; | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @return bool | ||
*/ | ||
public function has($id) | ||
{ | ||
return array_key_exists($id, $this->services); | ||
} | ||
} |