|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace LaminasTest\Cache\Service; |
| 6 | + |
| 7 | +use Generator; |
| 8 | +use InvalidArgumentException; |
| 9 | +use Laminas\Cache\ConfigProvider; |
| 10 | +use Laminas\ServiceManager\ServiceManager; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Psr\Container\ContainerInterface; |
| 13 | + |
| 14 | +use function array_keys; |
| 15 | +use function array_merge; |
| 16 | +use function array_unique; |
| 17 | +use function is_array; |
| 18 | +use function is_string; |
| 19 | + |
| 20 | +final class ConfigProviderIntegrationTest extends TestCase |
| 21 | +{ |
| 22 | + private ContainerInterface $container; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + $this->container = $this->createContainer(); |
| 28 | + } |
| 29 | + |
| 30 | + private function createContainer(): ContainerInterface |
| 31 | + { |
| 32 | + return new ServiceManager((new ConfigProvider())->getDependencyConfig()); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @dataProvider servicesProvidedByConfigProvider |
| 37 | + */ |
| 38 | + public function testContainerCanProvideRegisteredServices(string $serviceName): void |
| 39 | + { |
| 40 | + $instance = $this->container->get($serviceName); |
| 41 | + self::assertIsObject($instance); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return Generator<string, array{string}> |
| 46 | + */ |
| 47 | + public function servicesProvidedByConfigProvider(): Generator |
| 48 | + { |
| 49 | + $provider = new ConfigProvider(); |
| 50 | + $dependencies = $provider->getDependencyConfig(); |
| 51 | + |
| 52 | + $factories = $dependencies['factories'] ?? []; |
| 53 | + self::assertArrayIsMappedWithStrings($factories); |
| 54 | + $invokables = $dependencies['invokables'] ?? []; |
| 55 | + self::assertArrayIsMappedWithStrings($invokables); |
| 56 | + $services = $dependencies['services'] ?? []; |
| 57 | + self::assertArrayIsMappedWithStrings($services); |
| 58 | + $aliases = $dependencies['aliases'] ?? []; |
| 59 | + self::assertArrayIsMappedWithStrings($aliases); |
| 60 | + |
| 61 | + $serviceNames = array_unique( |
| 62 | + array_merge( |
| 63 | + array_keys($factories), |
| 64 | + array_keys($invokables), |
| 65 | + array_keys($services), |
| 66 | + array_keys($aliases), |
| 67 | + ), |
| 68 | + ); |
| 69 | + |
| 70 | + foreach ($serviceNames as $serviceName) { |
| 71 | + yield $serviceName => [$serviceName]; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @psalm-assert array<string,mixed> $array |
| 77 | + */ |
| 78 | + private static function assertArrayIsMappedWithStrings(mixed $array): void |
| 79 | + { |
| 80 | + if (! is_array($array)) { |
| 81 | + throw new InvalidArgumentException('Expecting value to be an array.'); |
| 82 | + } |
| 83 | + |
| 84 | + foreach (array_keys($array) as $value) { |
| 85 | + if (is_string($value)) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + throw new InvalidArgumentException('Expecting all values to are mapped with a string.'); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments