Skip to content
This repository was archived by the owner on Jul 6, 2024. It is now read-only.

Commit ce0bf45

Browse files
author
Dominik Zogg
committed
add chubbyphp-container
1 parent 8c60e15 commit ce0bf45

File tree

5 files changed

+126
-4
lines changed

5 files changed

+126
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ composer require chubbyphp/chubbyphp-api-http "^3.3"
3535
* [RequestManager][3]
3636
* [ResponseManager][4]
3737
* [AcceptAndContentTypeMiddleware][5]
38-
* [ApiHttpServiceProvider][6]
39-
* [ApiProblemMapping (example)][7]
38+
* [ApiHttpServiceFactory][6]
39+
* [ApiHttpServiceProvider][7]
40+
* [ApiProblemMapping (example)][8]
4041

4142
## Copyright
4243

@@ -47,5 +48,6 @@ Dominik Zogg 2019
4748
[3]: doc/Manager/RequestManager.md
4849
[4]: doc/Manager/ResponseManager.md
4950
[5]: doc/Middleware/AcceptAndContentTypeMiddleware.md
50-
[6]: doc/ServiceProvider/ApiHttpServiceProvider.md
51-
[7]: doc/Serialization/ApiProblemMapping.md
51+
[6]: doc/ServiceFactory/ApiHttpServiceFactory.md
52+
[7]: doc/ServiceProvider/ApiHttpServiceProvider.md
53+
[8]: doc/Serialization/ApiProblemMapping.md

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"psr/http-server-middleware": "^1.0.1"
2020
},
2121
"require-dev": {
22+
"chubbyphp/chubbyphp-container": "^1.0",
2223
"chubbyphp/chubbyphp-mock": "^1.4.2",
2324
"friendsofphp/php-cs-fixer": "^2.16.1",
2425
"infection/infection": "^0.14.3",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ApiHttpServiceFactory
2+
3+
```php
4+
<?php
5+
6+
use Chubbyphp\ApiHttp\ServiceFactory\ApiHttpServiceFactory;
7+
use Chubbyphp\Container\Container;
8+
use Chubbyphp\Deserialization\ServiceFactory\DeserializationServiceFactory;
9+
use Chubbyphp\Serialization\ServiceFactory\SerializationServiceFactory;
10+
11+
$container = new Container();
12+
$container->factories((new ApiHttpServiceFactory())());
13+
$container->factories((new DeserializationServiceFactory())());
14+
$container->factories((new SerializationServiceFactory())());
15+
16+
$container->get('api-http.request.manager')->getDataFromRequestQuery(...);
17+
18+
$container->get('api-http.response.manager')->create(...);
19+
20+
$container->get('api-http.response.factory')->createResponse(...);
21+
// must be defined, implement the ResponseFactoryInterface
22+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Chubbyphp\ApiHttp\ServiceFactory;
6+
7+
use Chubbyphp\ApiHttp\Manager\RequestManager;
8+
use Chubbyphp\ApiHttp\Manager\ResponseManager;
9+
use Psr\Container\ContainerInterface;
10+
11+
final class ApiHttpServiceFactory
12+
{
13+
public function __invoke(): array
14+
{
15+
return [
16+
'api-http.request.manager' => static function (ContainerInterface $container) {
17+
return new RequestManager($container->get('deserializer'));
18+
},
19+
'api-http.response.manager' => static function (ContainerInterface $container) {
20+
return new ResponseManager(
21+
$container->get('deserializer'),
22+
$container->get('api-http.response.factory'),
23+
$container->get('serializer')
24+
);
25+
},
26+
'api-http.response.factory' => static function (): void {
27+
throw new \RuntimeException('Missing response factory, define service "api-http.response.factory"');
28+
},
29+
];
30+
}
31+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Chubbyphp\Tests\ApiHttp\Unit\ServiceFactory;
6+
7+
use Chubbyphp\ApiHttp\Manager\RequestManager;
8+
use Chubbyphp\ApiHttp\Manager\ResponseManager;
9+
use Chubbyphp\ApiHttp\ServiceFactory\ApiHttpServiceFactory;
10+
use Chubbyphp\Container\Container;
11+
use Chubbyphp\Container\Exceptions\ContainerException;
12+
use Chubbyphp\Deserialization\ServiceFactory\DeserializationServiceFactory;
13+
use Chubbyphp\Mock\MockByCallsTrait;
14+
use Chubbyphp\Serialization\ServiceFactory\SerializationServiceFactory;
15+
use PHPUnit\Framework\TestCase;
16+
use Psr\Http\Message\ResponseFactoryInterface;
17+
18+
/**
19+
* @covers \Chubbyphp\ApiHttp\ServiceFactory\ApiHttpServiceFactory
20+
*
21+
* @internal
22+
*/
23+
final class ApiHttpServiceFactoryTest extends TestCase
24+
{
25+
use MockByCallsTrait;
26+
27+
public function testRegister(): void
28+
{
29+
/** @var ResponseFactoryInterface $responseFactory */
30+
$responseFactory = $this->getMockByCalls(ResponseFactoryInterface::class);
31+
32+
$container = new Container();
33+
$container->factories((new ApiHttpServiceFactory())());
34+
$container->factories((new DeserializationServiceFactory())());
35+
$container->factories((new SerializationServiceFactory())());
36+
37+
$container->factory('api-http.response.factory', static function () use ($responseFactory) {
38+
return $responseFactory;
39+
});
40+
41+
self::assertTrue($container->has('api-http.response.manager'));
42+
self::assertTrue($container->has('api-http.response.factory'));
43+
44+
self::assertInstanceOf(RequestManager::class, $container->get('api-http.request.manager'));
45+
self::assertInstanceOf(ResponseManager::class, $container->get('api-http.response.manager'));
46+
self::assertSame($responseFactory, $container->get('api-http.response.factory'));
47+
}
48+
49+
public function testFactoryExpectException(): void
50+
{
51+
$container = new Container();
52+
$container->factories((new ApiHttpServiceFactory())());
53+
54+
try {
55+
$container->get('api-http.response.factory');
56+
57+
self::fail(sprintf('expected "%s"', ContainerException::class));
58+
} catch (ContainerException $exception) {
59+
self::assertSame('Could not create service with id "api-http.response.factory"', $exception->getMessage());
60+
self::assertSame(
61+
'Missing response factory, define service "api-http.response.factory"',
62+
$exception->getPrevious()->getMessage()
63+
);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)