From 110b061ad3b7765baaf826f83b36df381a6ba887 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 13:54:16 +0300 Subject: [PATCH 01/20] feat: update phpunit config --- phpunit.xml.dist | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6b05a77..5d05b37 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -17,11 +17,12 @@ - + + From 872421610222f126ada8abc05859d31641f82d60 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 13:54:35 +0300 Subject: [PATCH 02/20] feat: install symfony/browser-kit --- composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9f06f3c..e3dca52 100644 --- a/composer.json +++ b/composer.json @@ -33,11 +33,13 @@ "symfony/framework-bundle": "^3.4 || ^4.1.12 || ^5.0" }, "require-dev": { + "ext-json": "*", "phpstan/phpstan": "0.12.*", "squizlabs/php_codesniffer": "3.5.*", "symfony/phpunit-bridge": "^3.4 || ^4.1.12 || ^5.0", "phpunit/phpunit": "^8.5 || ^9.0", - "symfony/yaml": "^3.4 || ^4.0 || ^5.0" + "symfony/yaml": "^3.4 || ^4.0 || ^5.0", + "symfony/browser-kit": "^3.4 || ^4.4 || ^5.0" }, "autoload": { "psr-4": { From f62e18e808a64f52ae2fb91b0cc1880f486e1a9b Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 13:54:52 +0300 Subject: [PATCH 03/20] feat: add test StatusUpCheckTest --- .../Unit/Check/StatusUpCheckTest.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/Integration/Unit/Check/StatusUpCheckTest.php diff --git a/tests/Integration/Unit/Check/StatusUpCheckTest.php b/tests/Integration/Unit/Check/StatusUpCheckTest.php new file mode 100644 index 0000000..6c26e82 --- /dev/null +++ b/tests/Integration/Unit/Check/StatusUpCheckTest.php @@ -0,0 +1,21 @@ +check(); + + self::assertIsArray($result); + self::assertNotEmpty($result); + self::assertArrayHasKey('status', $result); + self::assertSame('up', $result['status']); + } +} From 3c2315168c0dd64f968c7cae60d8a96f8c331597 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 13:55:17 +0300 Subject: [PATCH 04/20] feat: Add SymfonyHealthCheckExtensionTest --- .gitignore | 1 + .../Controller/HealthControllerTest.php | 22 +++++++++ .../SymfonyHealthCheckExtensionTest.php | 2 +- tests/TestKernel.php | 46 +++++++++++++++++++ tests/config.yml | 5 ++ 5 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/Controller/HealthControllerTest.php create mode 100644 tests/TestKernel.php create mode 100644 tests/config.yml diff --git a/.gitignore b/.gitignore index b1ce44a..44c77e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .idea build vendor +var /composer.lock /.phpcs-cache diff --git a/tests/Integration/Controller/HealthControllerTest.php b/tests/Integration/Controller/HealthControllerTest.php new file mode 100644 index 0000000..ce9b1c0 --- /dev/null +++ b/tests/Integration/Controller/HealthControllerTest.php @@ -0,0 +1,22 @@ +request('GET', '/health'); + + self::assertResponseIsSuccessful(); + + $response = $client->getResponse(); + self::assertSame(200, $response->getStatusCode()); + self::assertSame(json_encode([]), $response->getContent()); + } +} diff --git a/tests/Integration/DependencyInjection/SymfonyHealthCheckExtensionTest.php b/tests/Integration/DependencyInjection/SymfonyHealthCheckExtensionTest.php index af4cebd..4045bd3 100644 --- a/tests/Integration/DependencyInjection/SymfonyHealthCheckExtensionTest.php +++ b/tests/Integration/DependencyInjection/SymfonyHealthCheckExtensionTest.php @@ -33,7 +33,7 @@ public function testWithEmptyConfig(): void public function testNotExistServiceConfig(): void { try { - $container = $this->createContainerFromFixture('error_bundle_config'); + $this->createContainerFromFixture('error_bundle_config'); } catch (Throwable $exception) { self::assertInstanceOf(ServiceNotFoundException::class, $exception); self::assertSame( diff --git a/tests/TestKernel.php b/tests/TestKernel.php new file mode 100644 index 0000000..d881789 --- /dev/null +++ b/tests/TestKernel.php @@ -0,0 +1,46 @@ +getProjectDir(); + } + + public function getCacheDir(): string + { + return dirname(__DIR__) . '/var/cache/' . $this->getEnvironment(); + } + + public function getLogDir(): string + { + return dirname(__DIR__) . '/var/logs'; + } + + public function registerContainerConfiguration(LoaderInterface $loader): void + { + $loader->load(__DIR__ . '/config.yml'); + } +} diff --git a/tests/config.yml b/tests/config.yml new file mode 100644 index 0000000..71f2dbd --- /dev/null +++ b/tests/config.yml @@ -0,0 +1,5 @@ +framework: + secret: secret + router: + resource: "%kernel.project_dir%/Resources/config/routes.xml" + test: ~ From 8f3cda8bbadf017e9b5c1a0de176de942d0c589f Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 14:34:40 +0300 Subject: [PATCH 05/20] feat: Add hide DEPRECATIONS --- phpunit.xml.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5d05b37..eb152f2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -22,6 +22,7 @@ + From 099cfac97ed29a951f671ab5fe07cf63a0c0135b Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 14:42:20 +0300 Subject: [PATCH 06/20] feat: remove useless check --- tests/Integration/Controller/HealthControllerTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Integration/Controller/HealthControllerTest.php b/tests/Integration/Controller/HealthControllerTest.php index ce9b1c0..8e3ed04 100644 --- a/tests/Integration/Controller/HealthControllerTest.php +++ b/tests/Integration/Controller/HealthControllerTest.php @@ -13,8 +13,6 @@ public function testSomething(): void $client = static::createClient(); $client->request('GET', '/health'); - self::assertResponseIsSuccessful(); - $response = $client->getResponse(); self::assertSame(200, $response->getStatusCode()); self::assertSame(json_encode([]), $response->getContent()); From 11602e32f1b8aa1d36eb8b227957c4168f2c4809 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 14:42:45 +0300 Subject: [PATCH 07/20] feat: add Symfony 5.0 --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 28810ec..8f43b37 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -18,6 +18,7 @@ jobs: symfony-versions: - '3.4.*' - '4.4.*' + - '5.0.*' - '5.1.*' - '5.2.*' include: From 8c44223646a39c55c220f18fe1716f38342e80e5 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 14:44:55 +0300 Subject: [PATCH 08/20] feat: revert Symfony 5.0 --- .github/workflows/ci.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8f43b37..28810ec 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -18,7 +18,6 @@ jobs: symfony-versions: - '3.4.*' - '4.4.*' - - '5.0.*' - '5.1.*' - '5.2.*' include: From b911ba62bc6902d59c3604a86d9180587069ea9d Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 14:54:07 +0300 Subject: [PATCH 09/20] feat: add more test HealthController --- .../Controller/HealthControllerTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/Integration/Controller/HealthControllerTest.php b/tests/Integration/Controller/HealthControllerTest.php index 8e3ed04..2518ca9 100644 --- a/tests/Integration/Controller/HealthControllerTest.php +++ b/tests/Integration/Controller/HealthControllerTest.php @@ -5,6 +5,9 @@ namespace SymfonyHealthCheckBundle\Tests\Integration\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use SymfonyHealthCheckBundle\Check\StatusUpCheck; +use SymfonyHealthCheckBundle\Controller\HealthController; +use TypeError; class HealthControllerTest extends WebTestCase { @@ -17,4 +20,23 @@ public function testSomething(): void self::assertSame(200, $response->getStatusCode()); self::assertSame(json_encode([]), $response->getContent()); } + + public function testAddCheck(): void + { + $healthController = new HealthController(); + $healthController->addHealthCheck(new StatusUpCheck()); + + $response = $healthController->healthCheckAction(); + + self::assertSame(200, $response->getStatusCode()); + self::assertSame(json_encode([['status' => 'up']]), $response->getContent()); + } + + public function testAddCheckFailed(): void + { + self::expectException(TypeError::class); + + $healthController = new HealthController(); + $healthController->addHealthCheck(new HealthController()); + } } From b4e2a2cb3bdf5fbd3b0ea6ee776b7da7ddb7c489 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 18:59:42 +0300 Subject: [PATCH 10/20] feat: update return --- src/Check/DoctrineCheck.php | 7 ++++--- src/Check/EnvironmentCheck.php | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Check/DoctrineCheck.php b/src/Check/DoctrineCheck.php index 709af72..fd10626 100644 --- a/src/Check/DoctrineCheck.php +++ b/src/Check/DoctrineCheck.php @@ -10,7 +10,7 @@ class DoctrineCheck implements CheckInterface { - private const CHECK_RESULT_KEY = 'doctrineConnection'; + private const CHECK_RESULT_KEY = 'connection'; private ContainerInterface $container; @@ -24,6 +24,7 @@ public function __construct(ContainerInterface $container) */ public function check(): array { + $result = ['name' => 'doctrine']; $entityManager = $this->container->get('doctrine.orm.entity_manager'); if ($entityManager === null) { @@ -33,9 +34,9 @@ public function check(): array try { $entityManager->getConnection()->ping(); } catch (Throwable $e) { - return [self::CHECK_RESULT_KEY => false]; + return array_merge($result, [self::CHECK_RESULT_KEY => false]); } - return [self::CHECK_RESULT_KEY => true]; + return array_merge($result, [self::CHECK_RESULT_KEY => true]); } } diff --git a/src/Check/EnvironmentCheck.php b/src/Check/EnvironmentCheck.php index 0b1fff0..d99e685 100644 --- a/src/Check/EnvironmentCheck.php +++ b/src/Check/EnvironmentCheck.php @@ -20,12 +20,14 @@ public function __construct(ContainerInterface $container) public function check(): array { + $result = ['name' => self::CHECK_RESULT_KEY]; + try { $env = $this->container->getParameter('kernel.environment'); } catch (Throwable $e) { - return [self::CHECK_RESULT_KEY => 'Could not determine']; + return array_merge($result, [self::CHECK_RESULT_KEY => 'Could not determine']); } - return [self::CHECK_RESULT_KEY => $env]; + return array_merge($result, [self::CHECK_RESULT_KEY => $env]); } } From b826a58ac2710554c251b8926c8f32f076945657 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 18:59:54 +0300 Subject: [PATCH 11/20] feat: add more test --- .../Controller/HealthControllerTest.php | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/tests/Integration/Controller/HealthControllerTest.php b/tests/Integration/Controller/HealthControllerTest.php index 2518ca9..f40dfd4 100644 --- a/tests/Integration/Controller/HealthControllerTest.php +++ b/tests/Integration/Controller/HealthControllerTest.php @@ -5,6 +5,10 @@ namespace SymfonyHealthCheckBundle\Tests\Integration\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; +use SymfonyHealthCheckBundle\Check\DoctrineCheck; +use SymfonyHealthCheckBundle\Check\EnvironmentCheck; use SymfonyHealthCheckBundle\Check\StatusUpCheck; use SymfonyHealthCheckBundle\Controller\HealthController; use TypeError; @@ -21,7 +25,7 @@ public function testSomething(): void self::assertSame(json_encode([]), $response->getContent()); } - public function testAddCheck(): void + public function testAddCheckStatusUp(): void { $healthController = new HealthController(); $healthController->addHealthCheck(new StatusUpCheck()); @@ -32,6 +36,45 @@ public function testAddCheck(): void self::assertSame(json_encode([['status' => 'up']]), $response->getContent()); } + public function testEnvironmentCheckCouldNotDetermine(): void + { + $healthController = new HealthController(); + $healthController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder())); + + $response = $healthController->healthCheckAction(); + + self::assertSame(200, $response->getStatusCode()); + self::assertSame( + json_encode([['name' => 'environment', 'environment' => 'Could not determine']]), + $response->getContent() + ); + } + + public function testDoctrineCheckServiceNotFoundException(): void + { + self::expectException(ServiceNotFoundException::class); + + $healthController = new HealthController(); + $healthController->addHealthCheck(new DoctrineCheck(new ContainerBuilder())); + + $healthController->healthCheckAction(); + } + + public function testTwoCheck(): void + { + $healthController = new HealthController(); + $healthController->addHealthCheck(new StatusUpCheck()); + $healthController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder())); + + $response = $healthController->healthCheckAction(); + + self::assertSame(200, $response->getStatusCode()); + self::assertSame( + json_encode([['status' => 'up'], ['name' => 'environment', 'environment' => 'Could not determine']]), + $response->getContent() + ); + } + public function testAddCheckFailed(): void { self::expectException(TypeError::class); From fa469e44aa88c5550b16990e4dcbbb362a0c7dbd Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 19:22:21 +0300 Subject: [PATCH 12/20] feat: add more test --- .../Controller/HealthControllerTest.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/Integration/Controller/HealthControllerTest.php b/tests/Integration/Controller/HealthControllerTest.php index f40dfd4..ae939fe 100644 --- a/tests/Integration/Controller/HealthControllerTest.php +++ b/tests/Integration/Controller/HealthControllerTest.php @@ -15,7 +15,7 @@ class HealthControllerTest extends WebTestCase { - public function testSomething(): void + public function testSuccess(): void { $client = static::createClient(); $client->request('GET', '/health'); @@ -25,7 +25,7 @@ public function testSomething(): void self::assertSame(json_encode([]), $response->getContent()); } - public function testAddCheckStatusUp(): void + public function testAddCheckStatusUpSuccess(): void { $healthController = new HealthController(); $healthController->addHealthCheck(new StatusUpCheck()); @@ -60,7 +60,7 @@ public function testDoctrineCheckServiceNotFoundException(): void $healthController->healthCheckAction(); } - public function testTwoCheck(): void + public function testTwoCheckSuccess(): void { $healthController = new HealthController(); $healthController->addHealthCheck(new StatusUpCheck()); @@ -74,6 +74,19 @@ public function testTwoCheck(): void $response->getContent() ); } + + public function testEnvironmentCheckSuccess(): void + { + $healthController = new HealthController(); + $healthController->addHealthCheck(new EnvironmentCheck(static::bootKernel()->getContainer())); + $response = $healthController->healthCheckAction(); + + self::assertSame(200, $response->getStatusCode()); + self::assertSame( + json_encode([['name' => 'environment', 'environment' => 'testing']]), + $response->getContent() + ); + } public function testAddCheckFailed(): void { From 10aae2c6bd55445b302dfd16c68dd944026fc414 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 19:22:30 +0300 Subject: [PATCH 13/20] feat: add more test --- tests/Integration/Controller/HealthControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Controller/HealthControllerTest.php b/tests/Integration/Controller/HealthControllerTest.php index ae939fe..5f94891 100644 --- a/tests/Integration/Controller/HealthControllerTest.php +++ b/tests/Integration/Controller/HealthControllerTest.php @@ -74,7 +74,7 @@ public function testTwoCheckSuccess(): void $response->getContent() ); } - + public function testEnvironmentCheckSuccess(): void { $healthController = new HealthController(); From 35d6770e6dd1ea3d56d5cf28f8e7ca3ca5c300c5 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 19:57:35 +0300 Subject: [PATCH 14/20] feat: change logic in Doctrine Check --- src/Check/DoctrineCheck.php | 5 +++++ tests/Integration/Controller/HealthControllerTest.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Check/DoctrineCheck.php b/src/Check/DoctrineCheck.php index fd10626..4116f66 100644 --- a/src/Check/DoctrineCheck.php +++ b/src/Check/DoctrineCheck.php @@ -25,6 +25,11 @@ public function __construct(ContainerInterface $container) public function check(): array { $result = ['name' => 'doctrine']; + + if ($this->container->has('doctrine.orm.entity_manager') === false) { + throw new ServiceNotFoundException('Entity Manager Not Found.'); + } + $entityManager = $this->container->get('doctrine.orm.entity_manager'); if ($entityManager === null) { diff --git a/tests/Integration/Controller/HealthControllerTest.php b/tests/Integration/Controller/HealthControllerTest.php index 5f94891..5c972b8 100644 --- a/tests/Integration/Controller/HealthControllerTest.php +++ b/tests/Integration/Controller/HealthControllerTest.php @@ -6,11 +6,11 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use SymfonyHealthCheckBundle\Check\DoctrineCheck; use SymfonyHealthCheckBundle\Check\EnvironmentCheck; use SymfonyHealthCheckBundle\Check\StatusUpCheck; use SymfonyHealthCheckBundle\Controller\HealthController; +use SymfonyHealthCheckBundle\Exception\ServiceNotFoundException; use TypeError; class HealthControllerTest extends WebTestCase From ef5e5a71bc5b2ba0b9532eb93cbd926accdf2cce Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 20:04:35 +0300 Subject: [PATCH 15/20] feat: add test to ServiceNotFoundException -> getParameters --- src/Check/DoctrineCheck.php | 7 ++++++- .../Integration/Controller/HealthControllerTest.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Check/DoctrineCheck.php b/src/Check/DoctrineCheck.php index 4116f66..3f09da2 100644 --- a/src/Check/DoctrineCheck.php +++ b/src/Check/DoctrineCheck.php @@ -27,7 +27,12 @@ public function check(): array $result = ['name' => 'doctrine']; if ($this->container->has('doctrine.orm.entity_manager') === false) { - throw new ServiceNotFoundException('Entity Manager Not Found.'); + throw new ServiceNotFoundException( + 'Entity Manager Not Found.', + [ + 'class' => 'doctrine.orm.entity_manager', + ] + ); } $entityManager = $this->container->get('doctrine.orm.entity_manager'); diff --git a/tests/Integration/Controller/HealthControllerTest.php b/tests/Integration/Controller/HealthControllerTest.php index 5c972b8..c97777f 100644 --- a/tests/Integration/Controller/HealthControllerTest.php +++ b/tests/Integration/Controller/HealthControllerTest.php @@ -60,6 +60,18 @@ public function testDoctrineCheckServiceNotFoundException(): void $healthController->healthCheckAction(); } + public function testDoctrineCheckGetParameters(): void + { + $healthController = new HealthController(); + $healthController->addHealthCheck(new DoctrineCheck(new ContainerBuilder())); + + try { + $healthController->healthCheckAction(); + } catch (ServiceNotFoundException $exception) { + self::assertSame(["class" => "doctrine.orm.entity_manager"], $exception->getParameters()); + } + } + public function testTwoCheckSuccess(): void { $healthController = new HealthController(); From e7257bae5dccc0eee52571cc757eb3e2dd096e09 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Thu, 6 May 2021 20:08:26 +0300 Subject: [PATCH 16/20] feat: update php doc --- src/Exception/AbstractException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exception/AbstractException.php b/src/Exception/AbstractException.php index 2e612c7..4bd350a 100644 --- a/src/Exception/AbstractException.php +++ b/src/Exception/AbstractException.php @@ -18,7 +18,7 @@ abstract class AbstractException extends Exception implements ParameterException * AbstractException constructor. * * @param string $message - * @param iterable[] $parameters + * @param array $parameters * @param int $code * @param Throwable|null $previous */ From 2d0660a4f5c1dabfbccc91fd2bce31513aa51a90 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Sun, 9 May 2021 14:29:34 +0300 Subject: [PATCH 17/20] feat: add new test Doctrine Check --- .../Unit/Check/DoctrineCheckTest.php | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/Integration/Unit/Check/DoctrineCheckTest.php diff --git a/tests/Integration/Unit/Check/DoctrineCheckTest.php b/tests/Integration/Unit/Check/DoctrineCheckTest.php new file mode 100644 index 0000000..1281f7c --- /dev/null +++ b/tests/Integration/Unit/Check/DoctrineCheckTest.php @@ -0,0 +1,63 @@ +check(); + + self::assertIsArray($result); + self::assertNotEmpty($result); + self::assertArrayHasKey('status', $result); + self::assertSame('up', $result['status']); + } + + public function testDoctrineHasNotFoundException(): void + { + $container = $this->createMock(ContainerInterface::class); + + $container + ->method('has') + ->with('doctrine.orm.entity_manager') + ->willReturn(false); + + $this->expectException(ServiceNotFoundException::class); + $this->expectExceptionMessage('Entity Manager Not Found.'); + + $doctrine = new DoctrineCheck($container); + + $doctrine->check(); + } + + public function testDoctrineGetNotFoundException(): void + { + $container = $this->createMock(ContainerInterface::class); + + $container + ->method('has') + ->with('doctrine.orm.entity_manager') + ->willReturn(true); + + $container + ->method('get') + ->with('doctrine.orm.entity_manager') + ->willReturn(null); + + $this->expectException(ServiceNotFoundException::class); + $this->expectExceptionMessage('Entity Manager Not Found.'); + + $doctrine = new DoctrineCheck($container); + + $doctrine->check(); + } +} From 7fe83bb1b80d168f4e964e09a41f66fcfab980b9 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Sun, 9 May 2021 14:57:07 +0300 Subject: [PATCH 18/20] feat: add new test EnvironmentCheckTest --- .../Unit/Check/EnvironmentCheckTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/Integration/Unit/Check/EnvironmentCheckTest.php diff --git a/tests/Integration/Unit/Check/EnvironmentCheckTest.php b/tests/Integration/Unit/Check/EnvironmentCheckTest.php new file mode 100644 index 0000000..98e9c3c --- /dev/null +++ b/tests/Integration/Unit/Check/EnvironmentCheckTest.php @@ -0,0 +1,40 @@ +container = $this->createMock(ContainerInterface::class); + } + + public function testStatusUpCheckSuccess(): void + { + $this->container + ->method('getParameter') + ->with('kernel.environment') + ->willReturn('test'); + + $result = (new EnvironmentCheck($this->container))->check(); + + self::assertIsArray($result); + self::assertNotEmpty($result); + self::assertArrayHasKey('name', $result); + self::assertArrayHasKey('environment', $result); + self::assertSame('test', $result['environment']); + self::assertSame('environment', $result['name']); + } +} From 1fd222e455ef72f87c852cd2ad6187e5b6bf61ef Mon Sep 17 00:00:00 2001 From: Yozhef Date: Sun, 9 May 2021 15:17:12 +0300 Subject: [PATCH 19/20] feat: add new test DoctrineCheck --- tests/Integration/Mock/ConnectionMock.php | 13 ++++ tests/Integration/Mock/EntityManagerMock.php | 13 ++++ .../Unit/Check/DoctrineCheckTest.php | 70 +++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 tests/Integration/Mock/ConnectionMock.php create mode 100644 tests/Integration/Mock/EntityManagerMock.php diff --git a/tests/Integration/Mock/ConnectionMock.php b/tests/Integration/Mock/ConnectionMock.php new file mode 100644 index 0000000..03324f2 --- /dev/null +++ b/tests/Integration/Mock/ConnectionMock.php @@ -0,0 +1,13 @@ +check(); } + + public function testDoctrineSuccess(): void + { + $container = $this->createMock(ContainerInterface::class); + $entityManager = $this->createMock(EntityManagerMock::class); + + $container + ->method('has') + ->with('doctrine.orm.entity_manager') + ->willReturn(true); + + $container + ->method('get') + ->with('doctrine.orm.entity_manager') + ->willReturn($entityManager); + + $doctrine = new DoctrineCheck($container); + + $result = $doctrine->check(); + + self::assertIsArray($result); + self::assertNotEmpty($result); + self::assertArrayHasKey('name', $result); + self::assertArrayHasKey('connection', $result); + self::assertSame('doctrine', $result['name']); + self::assertIsBool($result['connection']); + self::assertTrue($result['connection']); + } + + public function testDoctrineFailPing(): void + { + $container = $this->createMock(ContainerInterface::class); + $entityManager = $this->createMock(EntityManagerMock::class); + $connectionMock = $this->createMock(ConnectionMock::class); + + $entityManager + ->method('getConnection') + ->with() + ->willReturn($connectionMock); + + $connectionMock + ->method('ping') + ->with() + ->will(self::throwException(new Exception())); + + $container + ->method('has') + ->with('doctrine.orm.entity_manager') + ->willReturn(true); + + $container + ->method('get') + ->with('doctrine.orm.entity_manager') + ->willReturn($entityManager); + + $doctrine = new DoctrineCheck($container); + + $result = $doctrine->check(); + + self::assertIsArray($result); + self::assertNotEmpty($result); + self::assertArrayHasKey('name', $result); + self::assertArrayHasKey('connection', $result); + self::assertSame('doctrine', $result['name']); + self::assertIsBool($result['connection']); + self::assertFalse($result['connection']); + } } From f4e916e92d8deda79c568b6507073092441f8505 Mon Sep 17 00:00:00 2001 From: Yozhef Date: Sun, 9 May 2021 15:17:25 +0300 Subject: [PATCH 20/20] feat: add new test EnvironmentCheck --- .../Unit/Check/EnvironmentCheckTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Integration/Unit/Check/EnvironmentCheckTest.php b/tests/Integration/Unit/Check/EnvironmentCheckTest.php index 98e9c3c..24da47e 100644 --- a/tests/Integration/Unit/Check/EnvironmentCheckTest.php +++ b/tests/Integration/Unit/Check/EnvironmentCheckTest.php @@ -4,6 +4,7 @@ namespace SymfonyHealthCheckBundle\Tests\Integration\Unit\Check; +use Exception; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -37,4 +38,21 @@ public function testStatusUpCheckSuccess(): void self::assertSame('test', $result['environment']); self::assertSame('environment', $result['name']); } + + public function testStatusUpCheckFail(): void + { + $this->container + ->method('getParameter') + ->with('kernel.environment') + ->will(self::throwException(new Exception())); + + $result = (new EnvironmentCheck($this->container))->check(); + + self::assertIsArray($result); + self::assertNotEmpty($result); + self::assertArrayHasKey('name', $result); + self::assertArrayHasKey('environment', $result); + self::assertSame('Could not determine', $result['environment']); + self::assertSame('environment', $result['name']); + } }