Skip to content

Commit

Permalink
QA: code cleanup (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostwriter authored May 9, 2022
2 parents 674cdfd + 7a2d41d commit 824e29e
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Latest Version on Packagist](https://badgen.net/packagist/v/ghostwriter/container)](https://packagist.org/packages/ghostwriter/container)
[![Downloads](https://badgen.net/packagist/dt/ghostwriter/container?color=blue)](https://packagist.org/packages/ghostwriter/container)

Provides an extensible [PSR-11](https://www.php-fig.org/psr/psr-11/), dependency injection service container.
Provides an extensible [PSR-11](https://www.php-fig.org/psr/psr-11/), Dependency Injection Service Container for Automated Object Composition, Interception, and Lifetime Management.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ghostwriter/container",
"description": "Provides a PSR-11, Dependency Injection Service Container",
"description": "Provides an extensible PSR-11, Dependency Injection Service Container for Automated Object Composition, Interception, and Lifetime Management.",
"license": "BSD-3-Clause",
"type": "library",
"keywords": [
Expand Down
39 changes: 21 additions & 18 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
cacheResultFile=".cache/phpunit/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
Expand All @@ -12,13 +12,14 @@
failOnWarning="true"
colors="true"
stopOnFailure="true"
testdox="true"
verbose="true">
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
<coverage cacheDirectory=".phpunit.cache/code-coverage"
<coverage cacheDirectory=".cache/phpunit/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
Expand Down
2 changes: 1 addition & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.22.0@fc2c6ab4d5fa5d644d8617089f012f3bb84b8703"/>
<files psalm-version="4.23.0@f1fe6ff483bf325c803df9f510d09a03fd796f88"/>
9 changes: 6 additions & 3 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class Container implements ContainerInterface
* providers: array<string,ServiceProviderInterface>,
* services: array<string,callable|null|object|scalar>,
* tags: array<string,array<string>>,
* } $services
* }
*/
private array $services = self::DEFAULT_SERVICES;

Expand Down Expand Up @@ -230,9 +230,9 @@ public function extend(string $class, callable $extension): void

$this->services[self::EXTENSIONS][$class] = array_key_exists($class, $extensions) ?
static fn (ContainerInterface $container, object $service): object =>
$extension($container, $extensions[$class]($container, $service)) :
$extension($container, $extensions[$class]($container, $service)) :
static fn (ContainerInterface $container, object $service): object =>
$extension($container, $service);
$extension($container, $service);
}

public function get(string $id): mixed
Expand Down Expand Up @@ -417,6 +417,9 @@ public function tag(string $id, iterable $tags): void
$this->services[self::TAGS] = $serviceTags;
}

/**
* @return iterable<string>
*/
public function tagged(string $tag): iterable
{
yield from $this->services[self::TAGS][$tag] ?? [];
Expand Down
10 changes: 8 additions & 2 deletions src/Contract/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Psr\Container\ContainerInterface as PsrContainerInterface;
use Psr\Container\NotFoundExceptionInterface as PsrNotFoundExceptionInterface;
use ReflectionException;
use Throwable;

/**
* An extendable, closure based dependency injection container.
Expand Down Expand Up @@ -87,7 +88,7 @@ interface ContainerInterface extends ArrayAccess, PsrContainerInterface
public const TAGS = 'tags';

/**
* Destroy the "static" instance of this container.
* Remove all registered services from this container and reset the default services.
*/
public function __destruct();

Expand Down Expand Up @@ -219,13 +220,18 @@ public function has(string $id): bool;
* @param array<string,mixed> $arguments optional arguments passed to $callback
*
* @throws ReflectionException
* @throws Throwable
*/
public function invoke(callable $callback, array $arguments = []): mixed;

/** @param string $offset */
public function offsetExists(mixed $offset): bool;

/** @param string $offset */
/**
* @param string $offset
*
* @throws Throwable
*/
public function offsetGet(mixed $offset): mixed;

/** @param string $offset */
Expand Down
78 changes: 39 additions & 39 deletions tests/Unit/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ static function (Container $container): void {
InvalidArgumentException::class,
InvalidArgumentException::emptyServiceId()->getMessage(),
static function (Container $container): void {
$container->extend(
'',
static fn (Container $container): Container => $container
);
$container->extend('', static fn (Container $container): Container => $container);
},
];

Expand Down Expand Up @@ -561,37 +558,6 @@ public function testContainerBuild(string $class, array $arguments = []): void
}
}

/**
* @covers \Ghostwriter\Container\Container::__construct
* @covers \Ghostwriter\Container\Container::__destruct
* @covers \Ghostwriter\Container\Container::invoke
* @covers \Ghostwriter\Container\Container::get
* @covers \Ghostwriter\Container\Container::getInstance
* @covers \Ghostwriter\Container\Container::resolve
* @covers \Ghostwriter\Container\Container::set
*
* @dataProvider dataProviderContainerCallables
*
* @param callable():void|string|TestEventListener|class-string<TestEventListener>[]|TestEventListener[]|string[] $callback
* @param array<class-string|string, mixed> $arguments
*
* @throws Throwable
*/
public function testContainerCall(callable $callback, array $arguments = []): void
{
$this->container->set(TestEvent::class, $arguments['event'] ?? new TestEvent());
$actual = random_int(10, 50);

$expectedCount = $actual;

while ($actual) {
$this->container->invoke($callback, $arguments);
--$actual;
}

self::assertCount($expectedCount, $this->container->get(TestEvent::class)->all());
}

/**
* @covers \Ghostwriter\Container\Container::__construct
* @covers \Ghostwriter\Container\Container::__destruct
Expand Down Expand Up @@ -745,6 +711,37 @@ public function testContainerImplementsPsrContainerInterface(): void
self::assertInstanceOf(Container::class, $this->container);
}

/**
* @covers \Ghostwriter\Container\Container::__construct
* @covers \Ghostwriter\Container\Container::__destruct
* @covers \Ghostwriter\Container\Container::invoke
* @covers \Ghostwriter\Container\Container::get
* @covers \Ghostwriter\Container\Container::getInstance
* @covers \Ghostwriter\Container\Container::resolve
* @covers \Ghostwriter\Container\Container::set
*
* @dataProvider dataProviderContainerCallables
*
* @param callable():void|string|TestEventListener|class-string<TestEventListener>[]|TestEventListener[]|string[] $callback
* @param array<class-string|string, mixed> $arguments
*
* @throws Throwable
*/
public function testContainerInvoke(callable|string|TestEventListener|array $callback, array $arguments = []): void
{
$this->container->set(TestEvent::class, $arguments['event'] ?? new TestEvent());
$actual = random_int(10, 50);

$expectedCount = $actual;

while ($actual) {
$this->container->invoke($callback, $arguments);
--$actual;
}

self::assertCount($expectedCount, $this->container->get(TestEvent::class)->all());
}

/**
* @covers \Ghostwriter\Container\Container::__destruct
* @covers \Ghostwriter\Container\Container::add
Expand Down Expand Up @@ -813,16 +810,19 @@ public function testContainerRemove(): void
* @covers \Ghostwriter\Container\Container::set
* @dataProvider dataProviderServices
*
* @param bool|Closure():null|float|int|stdClass|string|string|string[] $value
* @param bool|float|int|stdClass|string|string[] $expected
* @param bool|Closure():null|float|int|stdClass|string|string[] $value
* @param null|bool|float|int|stdClass|string|string[] $expected
*
* @throws PsrNotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws PsrContainerExceptionInterface
*/
public function testContainerSet(string $key, mixed $value, mixed $expected): void
{
public function testContainerSet(
string $key,
mixed $value,
null|bool|float|int|stdClass|string|array $expected
): void {
$this->container->set($key, $value);
self::assertSame($expected, $this->container->get($key));
}
Expand Down

0 comments on commit 824e29e

Please sign in to comment.