Skip to content

Commit 9f4b8c8

Browse files
committed
test: add container build test verifying all services resolve
Compiles a full Nette DI container with the extension and stub PSR-18/17 implementations, then asserts TheConfig, SignatureService, ApiServiceInterface, GateServiceInterface, and TheClient all resolve.
1 parent cba4f51 commit 9f4b8c8

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

tests/Cases/ContainerTest.phpt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Cases;
4+
5+
use Contributte\Tester\Environment;
6+
use Contributte\Tester\Toolkit;
7+
use Contributte\ThePay\DI\ThePayExtension;
8+
use Nette\DI\Compiler;
9+
use Nette\DI\Container;
10+
use Nette\DI\ContainerLoader;
11+
use Psr\Http\Client\ClientInterface;
12+
use Psr\Http\Message\RequestFactoryInterface;
13+
use Psr\Http\Message\StreamFactoryInterface;
14+
use Tester\Assert;
15+
use Tests\Fixtures\PsrStubFactory;
16+
use ThePay\ApiClient\Service\ApiServiceInterface;
17+
use ThePay\ApiClient\Service\GateServiceInterface;
18+
use ThePay\ApiClient\Service\SignatureService;
19+
use ThePay\ApiClient\TheClient;
20+
use ThePay\ApiClient\TheConfig;
21+
22+
require __DIR__ . '/../bootstrap.php';
23+
24+
Toolkit::test(function (): void {
25+
$loader = new ContainerLoader(Environment::getTestDir());
26+
27+
$class = $loader->load(function (Compiler $compiler): void {
28+
$compiler->addExtension('thepay', new ThePayExtension());
29+
$compiler->addConfig([
30+
'thepay' => [
31+
'demo' => true,
32+
'merchantId' => 'test-merchant-id',
33+
'projectId' => 3,
34+
'apiPassword' => 'secret',
35+
],
36+
'services' => [
37+
'httpClient' => [
38+
'type' => ClientInterface::class,
39+
'factory' => PsrStubFactory::class . '::createHttpClient',
40+
],
41+
'requestFactory' => [
42+
'type' => RequestFactoryInterface::class,
43+
'factory' => PsrStubFactory::class . '::createRequestFactory',
44+
],
45+
'streamFactory' => [
46+
'type' => StreamFactoryInterface::class,
47+
'factory' => PsrStubFactory::class . '::createStreamFactory',
48+
],
49+
],
50+
]);
51+
}, 'thepay-container-test');
52+
53+
/** @var Container $container */
54+
$container = new $class();
55+
56+
Assert::type(TheConfig::class, $container->getByType(TheConfig::class));
57+
Assert::type(SignatureService::class, $container->getByType(SignatureService::class));
58+
Assert::type(ApiServiceInterface::class, $container->getByType(ApiServiceInterface::class));
59+
Assert::type(GateServiceInterface::class, $container->getByType(GateServiceInterface::class));
60+
Assert::type(TheClient::class, $container->getByType(TheClient::class));
61+
});

tests/Fixtures/PsrStubFactory.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Fixtures;
4+
5+
use Psr\Http\Client\ClientInterface;
6+
use Psr\Http\Message\RequestFactoryInterface;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
use Psr\Http\Message\StreamFactoryInterface;
10+
use Psr\Http\Message\StreamInterface;
11+
12+
final class PsrStubFactory
13+
{
14+
15+
public static function createHttpClient(): ClientInterface
16+
{
17+
return new class implements ClientInterface {
18+
19+
public function sendRequest(RequestInterface $request): ResponseInterface
20+
{
21+
throw new \RuntimeException('Stub');
22+
}
23+
24+
};
25+
}
26+
27+
public static function createRequestFactory(): RequestFactoryInterface
28+
{
29+
return new class implements RequestFactoryInterface {
30+
31+
public function createRequest(string $method, $uri): RequestInterface // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint
32+
{
33+
throw new \RuntimeException('Stub');
34+
}
35+
36+
};
37+
}
38+
39+
public static function createStreamFactory(): StreamFactoryInterface
40+
{
41+
return new class implements StreamFactoryInterface {
42+
43+
public function createStream(string $content = ''): StreamInterface
44+
{
45+
throw new \RuntimeException('Stub');
46+
}
47+
48+
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
49+
{
50+
throw new \RuntimeException('Stub');
51+
}
52+
53+
public function createStreamFromResource($resource): StreamInterface // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint
54+
{
55+
throw new \RuntimeException('Stub');
56+
}
57+
58+
};
59+
}
60+
61+
}

0 commit comments

Comments
 (0)