Skip to content

Commit

Permalink
Merge pull request #74 from MacPaw/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
Yozhef authored Jan 7, 2025
2 parents 43d2c80 + ac83ba1 commit f1a0883
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 74 deletions.
21 changes: 3 additions & 18 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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/[email protected]
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: |
Expand All @@ -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
49 changes: 12 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
return [
SymfonyHealthCheckBundle\SymfonyHealthCheckBundle::class => ['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
```
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
47 changes: 47 additions & 0 deletions src/Check/DoctrineODMCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Check;

use Symfony\Component\DependencyInjection\ContainerInterface;
use SymfonyHealthCheckBundle\Dto\Response;
use Throwable;

class DoctrineODMCheck implements CheckInterface
{
private const CHECK_RESULT_NAME = 'doctrine_odm_check';

private ContainerInterface $container;

public function __construct(ContainerInterface $container)
{
$this->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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use SymfonyHealthCheckBundle\Dto\Response;
use Throwable;

class DoctrineCheck implements CheckInterface
class DoctrineORMCheck implements CheckInterface
{
private const CHECK_RESULT_NAME = 'doctrine';

Expand Down
9 changes: 8 additions & 1 deletion src/Resources/config/health_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<defaults autowire="true" autoconfigure="true" public="true" />
<service id="symfony_health_check.doctrine_check" class="SymfonyHealthCheckBundle\Check\DoctrineCheck">
<service id="symfony_health_check.doctrine_check" class="SymfonyHealthCheckBundle\Check\DoctrineORMCheck">
<argument type="service" id="service_container"/>
<deprecated package="macpaw/symfony-health-check-bundle" version="1.4.2">The "%service_id%" service alias is deprecated, use symfony_health_check.doctrine_orm_check instead</deprecated>
</service>
<service id="symfony_health_check.doctrine_orm_check" class="SymfonyHealthCheckBundle\Check\DoctrineORMCheck">
<argument type="service" id="service_container"/>
</service>
<service id="symfony_health_check.doctrine_odm_check" class="SymfonyHealthCheckBundle\Check\DoctrineODMCheck">
<argument type="service" id="service_container"/>
</service>
<service id="symfony_health_check.environment_check" class="SymfonyHealthCheckBundle\Check\EnvironmentCheck">
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Controller/HealthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Controller/PingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Mock/DocumentManager/ClientMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Tests\Mock\DocumentManager;

class ClientMock
{
public function selectDatabase(string $databaseName): DatabaseMock
{
return new DatabaseMock();
}
}
13 changes: 13 additions & 0 deletions tests/Mock/DocumentManager/ConfigurationMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Tests\Mock\DocumentManager;

class ConfigurationMock
{
public function getDefaultDB(): ?string
{
return 'default';
}
}
12 changes: 12 additions & 0 deletions tests/Mock/DocumentManager/DatabaseMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Tests\Mock\DocumentManager;

class DatabaseMock
{
public function command(array $command): void
{
}
}
18 changes: 18 additions & 0 deletions tests/Mock/DocumentManager/DocumentManagerMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Tests\Mock\DocumentManager;

class DocumentManagerMock
{
public function getConfiguration(): ConfigurationMock
{
return new ConfigurationMock();
}

public function getClient(): ClientMock
{
return new ClientMock();
}
}
Loading

0 comments on commit f1a0883

Please sign in to comment.