diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a2bf7a9..1949d22 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -44,11 +44,6 @@ jobs: - name: Checkout uses: actions/checkout@v2 - - uses: actions/cache@v2 - with: - path: ~/.composer/cache/files - key: ${{ matrix.php }}-${{ matrix.symfony-versions }} - - name: Setup PHP uses: shivammathur/setup-php@v2 with: @@ -58,17 +53,6 @@ jobs: - name: Add PHPUnit matcher run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - - name: Set composer cache directory - id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - - name: Cache composer - uses: actions/cache@v2.1.2 - with: - path: ${{ steps.composer-cache.outputs.dir }} - key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.symfony-versions }}-composer-${{ hashFiles('composer.json') }} - restore-keys: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.symfony-versions }}-composer - - name: Update Symfony version if: matrix.symfony-versions != '' run: | @@ -89,8 +73,9 @@ jobs: if: matrix.coverage == 'xdebug' - name: Run codecov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v5 if: matrix.coverage == 'xdebug' with: - file: './coverage.xml' + token: ${{ secrets.CODECOV_TOKEN }} + files: './coverage.xml' fail_ci_if_error: true diff --git a/README.md b/README.md index ec54b56..07c9e05 100644 --- a/README.md +++ b/README.md @@ -13,61 +13,35 @@ Step 1: Download the Bundle ---------------------------------- Open a command console, enter your project directory and execute: -### Applications that use Symfony Flex - ```console -$ composer require macpaw/symfony-health-check-bundle +composer require macpaw/symfony-health-check-bundle ``` ### Applications that don't use Symfony Flex -Open a command console, enter your project directory and execute the -following command to download the latest stable version of this bundle: - -```console -$ composer require macpaw/symfony-health-check-bundle -``` - -This command requires you to have Composer installed globally, as explained -in the [installation chapter](https://getcomposer.org/doc/00-intro.md) -of the Composer documentation. - -Step 2: Enable the Bundle ----------------------------------- -Then, enable the bundle by adding it to the list of registered bundles -in the `app/AppKernel.php` file of your project: +enable the bundle by adding it to the list of registered bundles in config/bundles.php ```php +// config/bundles.php ['all' => true], - ); // ... - } - - // ... -} + ]; ``` Create Symfony Health Check Bundle Config: ---------------------------------- -`config/packages/symfony_health_check.yaml` Configurating health check - all available you can see [here](https://github.com/MacPaw/symfony-health-check-bundle/tree/master/src/Check). ```yaml +# config/packages/symfony_health_check.yaml` symfony_health_check: health_checks: - - id: symfony_health_check.doctrine_check + - id: symfony_health_check.doctrine_orm_check ping_checks: - id: symfony_health_check.status_up_check ``` @@ -77,7 +51,7 @@ Change response code: ```yaml symfony_health_check: health_checks: - - id: symfony_health_check.doctrine_check + - id: symfony_health_check.doctrine_orm_check ping_checks: - id: symfony_health_check.status_up_check ping_error_response_code: 500 @@ -98,11 +72,11 @@ Step 3: Configuration Security Optional: ---------------------------------- -`config/packages/security.yaml` If you are using [symfony/security](https://symfony.com/doc/current/security.html) and your health check is to be used anonymously, add a new firewall to the configuration ```yaml +# config/packages/security.yaml firewalls: healthcheck: pattern: ^/health @@ -142,14 +116,15 @@ Then we add our custom health check to collection ```yaml symfony_health_check: health_checks: - - id: symfony_health_check.doctrine_check + - id: symfony_health_check.doctrine_orm_check - id: custom_health_check // custom service check id ``` How Change Route: ---------------------------------- -You can change the default behavior with a light configuration, remember to return to Step 3 after that: +You can change the default behavior with a light configuration, remember to modify the routes. ```yaml +# config/routes/symfony_health_check.yaml health: path: /your/custom/url methods: GET diff --git a/composer.json b/composer.json index 5939d62..bfc7c32 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ }, "require-dev": { "ext-json": "*", - "phpstan/phpstan": "1.9.*", + "phpstan/phpstan": "1.11.*", "squizlabs/php_codesniffer": "3.7.*", "symfony/phpunit-bridge": "^3.4 || ^4.1.12 || ^5.0 || ^6.0 || ^7.0", "phpunit/phpunit": "^8.5 || ^9.0", diff --git a/src/Check/DoctrineODMCheck.php b/src/Check/DoctrineODMCheck.php new file mode 100644 index 0000000..54ce235 --- /dev/null +++ b/src/Check/DoctrineODMCheck.php @@ -0,0 +1,47 @@ +container = $container; + } + + public function check(): Response + { + /** + * @var object|null $documentManager + */ + $documentManager = $this->container->get( + 'doctrine_mongodb.odm.document_manager', + ContainerInterface::NULL_ON_INVALID_REFERENCE, + ); + + if (!$documentManager) { + return new Response(self::CHECK_RESULT_NAME, false, 'Document Manager Not Found.'); + } + + $client = $documentManager->getClient(); + $databaseName = $documentManager->getConfiguration()->getDefaultDB() ?? 'admin'; + + try { + $client->selectDatabase($databaseName)->command(['ping' => 1]); + } catch (Throwable $e) { + return new Response(self::CHECK_RESULT_NAME, false, $e->getMessage()); + } + + return new Response(self::CHECK_RESULT_NAME, true, 'ok'); + } +} diff --git a/src/Check/DoctrineCheck.php b/src/Check/DoctrineORMCheck.php similarity index 96% rename from src/Check/DoctrineCheck.php rename to src/Check/DoctrineORMCheck.php index 767f243..d81e6d2 100644 --- a/src/Check/DoctrineCheck.php +++ b/src/Check/DoctrineORMCheck.php @@ -8,7 +8,7 @@ use SymfonyHealthCheckBundle\Dto\Response; use Throwable; -class DoctrineCheck implements CheckInterface +class DoctrineORMCheck implements CheckInterface { private const CHECK_RESULT_NAME = 'doctrine'; diff --git a/src/Resources/config/health_checks.xml b/src/Resources/config/health_checks.xml index 7fce6e1..20acf51 100644 --- a/src/Resources/config/health_checks.xml +++ b/src/Resources/config/health_checks.xml @@ -5,7 +5,14 @@ http://symfony.com/schema/dic/services/services-1.0.xsd"> - + + + The "%service_id%" service alias is deprecated, use symfony_health_check.doctrine_orm_check instead + + + + + diff --git a/tests/Integration/Controller/HealthControllerTest.php b/tests/Integration/Controller/HealthControllerTest.php index 0e00374..5690d2f 100644 --- a/tests/Integration/Controller/HealthControllerTest.php +++ b/tests/Integration/Controller/HealthControllerTest.php @@ -6,7 +6,7 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; -use SymfonyHealthCheckBundle\Check\DoctrineCheck; +use SymfonyHealthCheckBundle\Check\DoctrineORMCheck; use SymfonyHealthCheckBundle\Check\EnvironmentCheck; use SymfonyHealthCheckBundle\Check\StatusUpCheck; use SymfonyHealthCheckBundle\Controller\HealthController; @@ -62,7 +62,7 @@ public function testEnvironmentCheckCouldNotDetermine(): void public function testDoctrineCheckServiceNotFoundException(): void { $healthController = new HealthController(); - $healthController->addHealthCheck(new DoctrineCheck(new ContainerBuilder())); + $healthController->addHealthCheck(new DoctrineORMCheck(new ContainerBuilder())); $response = $healthController->check(); self::assertSame(200, $response->getStatusCode()); diff --git a/tests/Integration/Controller/PingControllerTest.php b/tests/Integration/Controller/PingControllerTest.php index 24fb1dd..655f36b 100644 --- a/tests/Integration/Controller/PingControllerTest.php +++ b/tests/Integration/Controller/PingControllerTest.php @@ -6,7 +6,7 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; -use SymfonyHealthCheckBundle\Check\DoctrineCheck; +use SymfonyHealthCheckBundle\Check\DoctrineORMCheck; use SymfonyHealthCheckBundle\Check\EnvironmentCheck; use SymfonyHealthCheckBundle\Check\StatusUpCheck; use SymfonyHealthCheckBundle\Controller\PingController; @@ -62,7 +62,7 @@ public function testEnvironmentCheckCouldNotDetermine(): void public function testDoctrineCheckServiceNotFoundException(): void { $pingController = new PingController(); - $pingController->addHealthCheck(new DoctrineCheck(new ContainerBuilder())); + $pingController->addHealthCheck(new DoctrineORMCheck(new ContainerBuilder())); $response = $pingController->check(); self::assertSame(200, $response->getStatusCode()); diff --git a/tests/Integration/DependencyInjection/SymfonyHealthCheckExtensionTest.php b/tests/Integration/DependencyInjection/SymfonyHealthCheckExtensionTest.php index 9be03bd..f6dff8f 100644 --- a/tests/Integration/DependencyInjection/SymfonyHealthCheckExtensionTest.php +++ b/tests/Integration/DependencyInjection/SymfonyHealthCheckExtensionTest.php @@ -50,10 +50,12 @@ public function testWithFullConfig(): void { $container = $this->createContainerFromFixture('filled_bundle_config'); - self::assertCount(6, $container->getDefinitions()); + self::assertCount(8, $container->getDefinitions()); self::assertArrayHasKey(HealthController::class, $container->getDefinitions()); self::assertArrayHasKey(PingController::class, $container->getDefinitions()); - self::assertArrayHasKey('symfony_health_check.doctrine_check', $container->getDefinitions()); + self::assertArrayHasKey('symfony_health_check.doctrine_check', $container->getDefinitions()); #deprecated + self::assertArrayHasKey('symfony_health_check.doctrine_orm_check', $container->getDefinitions()); + self::assertArrayHasKey('symfony_health_check.doctrine_odm_check', $container->getDefinitions()); self::assertArrayHasKey('symfony_health_check.environment_check', $container->getDefinitions()); self::assertArrayHasKey('symfony_health_check.status_up_check', $container->getDefinitions()); } diff --git a/tests/Mock/DocumentManager/ClientMock.php b/tests/Mock/DocumentManager/ClientMock.php new file mode 100644 index 0000000..f99e759 --- /dev/null +++ b/tests/Mock/DocumentManager/ClientMock.php @@ -0,0 +1,13 @@ +createMock(ContainerInterface::class); + + $container + ->method('get') + ->with('doctrine_mongodb.odm.document_manager') + ->willReturn(null); + + $doctrine = new DoctrineODMCheck($container); + + $result = $doctrine->check()->toArray(); + + self::assertIsArray($result); + self::assertNotEmpty($result); + + self::assertArrayHasKey('name', $result); + self::assertArrayHasKey('result', $result); + self::assertArrayHasKey('message', $result); + self::assertArrayHasKey('params', $result); + + self::assertSame('doctrine_odm_check', $result['name']); + self::assertFalse($result['result']); + self::assertSame('Document Manager Not Found.', $result['message']); + self::assertIsArray($result['params']); + } + + public function testDoctrineODMGetNotFoundException(): void + { + $container = $this->createMock(ContainerInterface::class); + + $container + ->method('get') + ->with('doctrine_mongodb.odm.document_manager') + ->willReturn(null); + + $doctrine = new DoctrineODMCheck($container); + + $result = $doctrine->check()->toArray(); + + self::assertIsArray($result); + self::assertNotEmpty($result); + + self::assertArrayHasKey('name', $result); + self::assertArrayHasKey('result', $result); + self::assertArrayHasKey('message', $result); + self::assertArrayHasKey('params', $result); + + self::assertSame('doctrine_odm_check', $result['name']); + self::assertFalse($result['result']); + self::assertSame('Document Manager Not Found.', $result['message']); + self::assertIsArray($result['params']); + } + + public function testDoctrineODMSuccess(): void + { + $container = $this->createMock(ContainerInterface::class); + $documentManager = $this->createMock(DocumentManagerMock::class); + + $container + ->method('get') + ->with('doctrine_mongodb.odm.document_manager') + ->willReturn($documentManager); + + $doctrine = new DoctrineODMCheck($container); + + $result = $doctrine->check()->toArray(); + + self::assertIsArray($result); + self::assertNotEmpty($result); + + self::assertArrayHasKey('name', $result); + self::assertArrayHasKey('result', $result); + self::assertArrayHasKey('message', $result); + self::assertArrayHasKey('params', $result); + + self::assertSame('doctrine_odm_check', $result['name']); + self::assertTrue($result['result']); + self::assertSame('ok', $result['message']); + self::assertIsArray($result['params']); + } + + public function testDoctrineFailPing(): void + { + $container = $this->createMock(ContainerInterface::class); + $documentManager = $this->createMock(DocumentManagerMock::class); + $client = $this->createMock(ClientMock::class); + $database = $this->createMock(DatabaseMock::class); + $configuration = $this->createMock(ConfigurationMock::class); + + $configuration + ->method('getDefaultDB') + ->willReturn('default'); + + $documentManager + ->method('getClient') + ->with() + ->willReturn($client); + + $documentManager + ->method('getConfiguration') + ->with() + ->willReturn($configuration); + + $client + ->method('selectDatabase') + ->with('default') + ->willReturn($database); + + $database + ->method('command') + ->with(['ping' => 1]) + ->will(self::throwException(new Exception('No suitable servers found'))); + + $container + ->method('get') + ->with('doctrine_mongodb.odm.document_manager') + ->willReturn($documentManager); + + $doctrine = new DoctrineODMCheck($container); + + $result = $doctrine->check()->toArray(); + + self::assertIsArray($result); + self::assertNotEmpty($result); + + self::assertArrayHasKey('name', $result); + self::assertArrayHasKey('result', $result); + self::assertArrayHasKey('message', $result); + self::assertArrayHasKey('params', $result); + + self::assertSame('doctrine_odm_check', $result['name']); + self::assertFalse($result['result']); + self::assertSame('No suitable servers found', $result['message']); + self::assertIsArray($result['params']); + } +} diff --git a/tests/Unit/Check/DoctrineCheckTest.php b/tests/Unit/Check/DoctrineORMCheckTest.php similarity index 88% rename from tests/Unit/Check/DoctrineCheckTest.php rename to tests/Unit/Check/DoctrineORMCheckTest.php index 0ac7969..7c82f0c 100644 --- a/tests/Unit/Check/DoctrineCheckTest.php +++ b/tests/Unit/Check/DoctrineORMCheckTest.php @@ -7,13 +7,13 @@ use Exception; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerInterface; -use SymfonyHealthCheckBundle\Check\DoctrineCheck; +use SymfonyHealthCheckBundle\Check\DoctrineORMCheck; use SymfonyHealthCheckBundle\Tests\Mock\ConnectionMock; use SymfonyHealthCheckBundle\Tests\Mock\EntityManagerMock; -class DoctrineCheckTest extends TestCase +class DoctrineORMCheckTest extends TestCase { - public function testDoctrineHasNotFoundException(): void + public function testDoctrineORMHasNotFoundException(): void { $container = $this->createMock(ContainerInterface::class); @@ -22,7 +22,7 @@ public function testDoctrineHasNotFoundException(): void ->with('doctrine.orm.entity_manager') ->willReturn(false); - $doctrine = new DoctrineCheck($container); + $doctrine = new DoctrineORMCheck($container); $result = $doctrine->check()->toArray(); @@ -40,7 +40,7 @@ public function testDoctrineHasNotFoundException(): void self::assertIsArray($result['params']); } - public function testDoctrineGetNotFoundException(): void + public function testDoctrineORMGetNotFoundException(): void { $container = $this->createMock(ContainerInterface::class); @@ -54,7 +54,7 @@ public function testDoctrineGetNotFoundException(): void ->with('doctrine.orm.entity_manager') ->willReturn(null); - $doctrine = new DoctrineCheck($container); + $doctrine = new DoctrineORMCheck($container); $result = $doctrine->check()->toArray(); @@ -72,7 +72,7 @@ public function testDoctrineGetNotFoundException(): void self::assertIsArray($result['params']); } - public function testDoctrineSuccess(): void + public function testDoctrineORMSuccess(): void { $container = $this->createMock(ContainerInterface::class); $entityManager = $this->createMock(EntityManagerMock::class); @@ -87,7 +87,7 @@ public function testDoctrineSuccess(): void ->with('doctrine.orm.entity_manager') ->willReturn($entityManager); - $doctrine = new DoctrineCheck($container); + $doctrine = new DoctrineORMCheck($container); $result = $doctrine->check()->toArray(); @@ -105,7 +105,7 @@ public function testDoctrineSuccess(): void self::assertIsArray($result['params']); } - public function testDoctrineFailPing(): void + public function testDoctrineORMFailPing(): void { $container = $this->createMock(ContainerInterface::class); $entityManager = $this->createMock(EntityManagerMock::class); @@ -131,7 +131,7 @@ public function testDoctrineFailPing(): void ->with('doctrine.orm.entity_manager') ->willReturn($entityManager); - $doctrine = new DoctrineCheck($container); + $doctrine = new DoctrineORMCheck($container); $result = $doctrine->check()->toArray();