Skip to content

Commit f1a0883

Browse files
authored
Merge pull request #74 from MacPaw/develop
Release
2 parents 43d2c80 + ac83ba1 commit f1a0883

15 files changed

+300
-74
lines changed

.github/workflows/ci.yaml

+3-18
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ jobs:
4444
- name: Checkout
4545
uses: actions/checkout@v2
4646

47-
- uses: actions/cache@v2
48-
with:
49-
path: ~/.composer/cache/files
50-
key: ${{ matrix.php }}-${{ matrix.symfony-versions }}
51-
5247
- name: Setup PHP
5348
uses: shivammathur/setup-php@v2
5449
with:
@@ -58,17 +53,6 @@ jobs:
5853
- name: Add PHPUnit matcher
5954
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
6055

61-
- name: Set composer cache directory
62-
id: composer-cache
63-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
64-
65-
- name: Cache composer
66-
uses: actions/[email protected]
67-
with:
68-
path: ${{ steps.composer-cache.outputs.dir }}
69-
key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.symfony-versions }}-composer-${{ hashFiles('composer.json') }}
70-
restore-keys: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.symfony-versions }}-composer
71-
7256
- name: Update Symfony version
7357
if: matrix.symfony-versions != ''
7458
run: |
@@ -89,8 +73,9 @@ jobs:
8973
if: matrix.coverage == 'xdebug'
9074

9175
- name: Run codecov
92-
uses: codecov/codecov-action@v1
76+
uses: codecov/codecov-action@v5
9377
if: matrix.coverage == 'xdebug'
9478
with:
95-
file: './coverage.xml'
79+
token: ${{ secrets.CODECOV_TOKEN }}
80+
files: './coverage.xml'
9681
fail_ci_if_error: true

README.md

+12-37
Original file line numberDiff line numberDiff line change
@@ -13,61 +13,35 @@ Step 1: Download the Bundle
1313
----------------------------------
1414
Open a command console, enter your project directory and execute:
1515

16-
### Applications that use Symfony Flex
17-
1816
```console
19-
$ composer require macpaw/symfony-health-check-bundle
17+
composer require macpaw/symfony-health-check-bundle
2018
```
2119

2220
### Applications that don't use Symfony Flex
2321

24-
Open a command console, enter your project directory and execute the
25-
following command to download the latest stable version of this bundle:
26-
27-
```console
28-
$ composer require macpaw/symfony-health-check-bundle
29-
```
30-
31-
This command requires you to have Composer installed globally, as explained
32-
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
33-
of the Composer documentation.
34-
35-
Step 2: Enable the Bundle
36-
----------------------------------
37-
Then, enable the bundle by adding it to the list of registered bundles
38-
in the `app/AppKernel.php` file of your project:
22+
enable the bundle by adding it to the list of registered bundles in config/bundles.php
3923

4024
```php
25+
// config/bundles.php
4126
<?php
42-
// app/AppKernel.php
4327

44-
// ...
45-
class AppKernel extends Kernel
46-
{
47-
public function registerBundles()
48-
{
49-
$bundles = array(
50-
// ...
28+
return [
5129
SymfonyHealthCheckBundle\SymfonyHealthCheckBundle::class => ['all' => true],
52-
);
5330

5431
// ...
55-
}
56-
57-
// ...
58-
}
32+
];
5933
```
6034

6135
Create Symfony Health Check Bundle Config:
6236
----------------------------------
63-
`config/packages/symfony_health_check.yaml`
6437

6538
Configurating health check - all available you can see [here](https://github.com/MacPaw/symfony-health-check-bundle/tree/master/src/Check).
6639

6740
```yaml
41+
# config/packages/symfony_health_check.yaml`
6842
symfony_health_check:
6943
health_checks:
70-
- id: symfony_health_check.doctrine_check
44+
- id: symfony_health_check.doctrine_orm_check
7145
ping_checks:
7246
- id: symfony_health_check.status_up_check
7347
```
@@ -77,7 +51,7 @@ Change response code:
7751
```yaml
7852
symfony_health_check:
7953
health_checks:
80-
- id: symfony_health_check.doctrine_check
54+
- id: symfony_health_check.doctrine_orm_check
8155
ping_checks:
8256
- id: symfony_health_check.status_up_check
8357
ping_error_response_code: 500
@@ -98,11 +72,11 @@ Step 3: Configuration
9872

9973
Security Optional:
10074
----------------------------------
101-
`config/packages/security.yaml`
10275

10376
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
10477

10578
```yaml
79+
# config/packages/security.yaml
10680
firewalls:
10781
healthcheck:
10882
pattern: ^/health
@@ -142,14 +116,15 @@ Then we add our custom health check to collection
142116
```yaml
143117
symfony_health_check:
144118
health_checks:
145-
- id: symfony_health_check.doctrine_check
119+
- id: symfony_health_check.doctrine_orm_check
146120
- id: custom_health_check // custom service check id
147121
```
148122

149123
How Change Route:
150124
----------------------------------
151-
You can change the default behavior with a light configuration, remember to return to Step 3 after that:
125+
You can change the default behavior with a light configuration, remember to modify the routes.
152126
```yaml
127+
# config/routes/symfony_health_check.yaml
153128
health:
154129
path: /your/custom/url
155130
methods: GET

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"require-dev": {
3636
"ext-json": "*",
37-
"phpstan/phpstan": "1.9.*",
37+
"phpstan/phpstan": "1.11.*",
3838
"squizlabs/php_codesniffer": "3.7.*",
3939
"symfony/phpunit-bridge": "^3.4 || ^4.1.12 || ^5.0 || ^6.0 || ^7.0",
4040
"phpunit/phpunit": "^8.5 || ^9.0",

src/Check/DoctrineODMCheck.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyHealthCheckBundle\Check;
6+
7+
use Symfony\Component\DependencyInjection\ContainerInterface;
8+
use SymfonyHealthCheckBundle\Dto\Response;
9+
use Throwable;
10+
11+
class DoctrineODMCheck implements CheckInterface
12+
{
13+
private const CHECK_RESULT_NAME = 'doctrine_odm_check';
14+
15+
private ContainerInterface $container;
16+
17+
public function __construct(ContainerInterface $container)
18+
{
19+
$this->container = $container;
20+
}
21+
22+
public function check(): Response
23+
{
24+
/**
25+
* @var object|null $documentManager
26+
*/
27+
$documentManager = $this->container->get(
28+
'doctrine_mongodb.odm.document_manager',
29+
ContainerInterface::NULL_ON_INVALID_REFERENCE,
30+
);
31+
32+
if (!$documentManager) {
33+
return new Response(self::CHECK_RESULT_NAME, false, 'Document Manager Not Found.');
34+
}
35+
36+
$client = $documentManager->getClient();
37+
$databaseName = $documentManager->getConfiguration()->getDefaultDB() ?? 'admin';
38+
39+
try {
40+
$client->selectDatabase($databaseName)->command(['ping' => 1]);
41+
} catch (Throwable $e) {
42+
return new Response(self::CHECK_RESULT_NAME, false, $e->getMessage());
43+
}
44+
45+
return new Response(self::CHECK_RESULT_NAME, true, 'ok');
46+
}
47+
}

src/Check/DoctrineCheck.php renamed to src/Check/DoctrineORMCheck.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use SymfonyHealthCheckBundle\Dto\Response;
99
use Throwable;
1010

11-
class DoctrineCheck implements CheckInterface
11+
class DoctrineORMCheck implements CheckInterface
1212
{
1313
private const CHECK_RESULT_NAME = 'doctrine';
1414

src/Resources/config/health_checks.xml

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
http://symfony.com/schema/dic/services/services-1.0.xsd">
66
<services>
77
<defaults autowire="true" autoconfigure="true" public="true" />
8-
<service id="symfony_health_check.doctrine_check" class="SymfonyHealthCheckBundle\Check\DoctrineCheck">
8+
<service id="symfony_health_check.doctrine_check" class="SymfonyHealthCheckBundle\Check\DoctrineORMCheck">
9+
<argument type="service" id="service_container"/>
10+
<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>
11+
</service>
12+
<service id="symfony_health_check.doctrine_orm_check" class="SymfonyHealthCheckBundle\Check\DoctrineORMCheck">
13+
<argument type="service" id="service_container"/>
14+
</service>
15+
<service id="symfony_health_check.doctrine_odm_check" class="SymfonyHealthCheckBundle\Check\DoctrineODMCheck">
916
<argument type="service" id="service_container"/>
1017
</service>
1118
<service id="symfony_health_check.environment_check" class="SymfonyHealthCheckBundle\Check\EnvironmentCheck">

tests/Integration/Controller/HealthControllerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
9-
use SymfonyHealthCheckBundle\Check\DoctrineCheck;
9+
use SymfonyHealthCheckBundle\Check\DoctrineORMCheck;
1010
use SymfonyHealthCheckBundle\Check\EnvironmentCheck;
1111
use SymfonyHealthCheckBundle\Check\StatusUpCheck;
1212
use SymfonyHealthCheckBundle\Controller\HealthController;
@@ -62,7 +62,7 @@ public function testEnvironmentCheckCouldNotDetermine(): void
6262
public function testDoctrineCheckServiceNotFoundException(): void
6363
{
6464
$healthController = new HealthController();
65-
$healthController->addHealthCheck(new DoctrineCheck(new ContainerBuilder()));
65+
$healthController->addHealthCheck(new DoctrineORMCheck(new ContainerBuilder()));
6666

6767
$response = $healthController->check();
6868
self::assertSame(200, $response->getStatusCode());

tests/Integration/Controller/PingControllerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
9-
use SymfonyHealthCheckBundle\Check\DoctrineCheck;
9+
use SymfonyHealthCheckBundle\Check\DoctrineORMCheck;
1010
use SymfonyHealthCheckBundle\Check\EnvironmentCheck;
1111
use SymfonyHealthCheckBundle\Check\StatusUpCheck;
1212
use SymfonyHealthCheckBundle\Controller\PingController;
@@ -62,7 +62,7 @@ public function testEnvironmentCheckCouldNotDetermine(): void
6262
public function testDoctrineCheckServiceNotFoundException(): void
6363
{
6464
$pingController = new PingController();
65-
$pingController->addHealthCheck(new DoctrineCheck(new ContainerBuilder()));
65+
$pingController->addHealthCheck(new DoctrineORMCheck(new ContainerBuilder()));
6666

6767
$response = $pingController->check();
6868
self::assertSame(200, $response->getStatusCode());

tests/Integration/DependencyInjection/SymfonyHealthCheckExtensionTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ public function testWithFullConfig(): void
5050
{
5151
$container = $this->createContainerFromFixture('filled_bundle_config');
5252

53-
self::assertCount(6, $container->getDefinitions());
53+
self::assertCount(8, $container->getDefinitions());
5454
self::assertArrayHasKey(HealthController::class, $container->getDefinitions());
5555
self::assertArrayHasKey(PingController::class, $container->getDefinitions());
56-
self::assertArrayHasKey('symfony_health_check.doctrine_check', $container->getDefinitions());
56+
self::assertArrayHasKey('symfony_health_check.doctrine_check', $container->getDefinitions()); #deprecated
57+
self::assertArrayHasKey('symfony_health_check.doctrine_orm_check', $container->getDefinitions());
58+
self::assertArrayHasKey('symfony_health_check.doctrine_odm_check', $container->getDefinitions());
5759
self::assertArrayHasKey('symfony_health_check.environment_check', $container->getDefinitions());
5860
self::assertArrayHasKey('symfony_health_check.status_up_check', $container->getDefinitions());
5961
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyHealthCheckBundle\Tests\Mock\DocumentManager;
6+
7+
class ClientMock
8+
{
9+
public function selectDatabase(string $databaseName): DatabaseMock
10+
{
11+
return new DatabaseMock();
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyHealthCheckBundle\Tests\Mock\DocumentManager;
6+
7+
class ConfigurationMock
8+
{
9+
public function getDefaultDB(): ?string
10+
{
11+
return 'default';
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyHealthCheckBundle\Tests\Mock\DocumentManager;
6+
7+
class DatabaseMock
8+
{
9+
public function command(array $command): void
10+
{
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyHealthCheckBundle\Tests\Mock\DocumentManager;
6+
7+
class DocumentManagerMock
8+
{
9+
public function getConfiguration(): ConfigurationMock
10+
{
11+
return new ConfigurationMock();
12+
}
13+
14+
public function getClient(): ClientMock
15+
{
16+
return new ClientMock();
17+
}
18+
}

0 commit comments

Comments
 (0)