Skip to content

Commit

Permalink
Merge pull request #5 from MacPaw/impruveTest
Browse files Browse the repository at this point in the history
Impruve test
  • Loading branch information
Yozhef authored May 9, 2021
2 parents 11a33d7 + f4e916e commit 5a33d14
Show file tree
Hide file tree
Showing 15 changed files with 426 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea
build
vendor
var
/composer.lock
/.phpcs-cache

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
<php>
<ini name="error_reporting" value="-1"/>
<ini name="memory_limit" value="-1"/>
<server name="APP_ENV" value="test" force="true"/>
<server name="APP_ENV" value="testing" force="true"/>
<server name="APP_DEBUG" value="0" force="true"/>
<server name="SHELL_VERBOSITY" value="-1"/>
<server name="SYMFONY_PHPUNIT_REMOVE" value=""/>
<server name="SYMFONY_PHPUNIT_VERSION" value="9"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled" />
<env name="KERNEL_CLASS" value="SymfonyHealthCheckBundle\Tests\TestKernel" />
</php>
<testsuites>
<testsuite name="Symfony Health Check Bundle Test Suite">
Expand Down
17 changes: 14 additions & 3 deletions src/Check/DoctrineCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DoctrineCheck implements CheckInterface
{
private const CHECK_RESULT_KEY = 'doctrineConnection';
private const CHECK_RESULT_KEY = 'connection';

private ContainerInterface $container;

Expand All @@ -24,6 +24,17 @@ 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.',
[
'class' => 'doctrine.orm.entity_manager',
]
);
}

$entityManager = $this->container->get('doctrine.orm.entity_manager');

if ($entityManager === null) {
Expand All @@ -33,9 +44,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]);
}
}
6 changes: 4 additions & 2 deletions src/Check/EnvironmentCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
2 changes: 1 addition & 1 deletion src/Exception/AbstractException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class AbstractException extends Exception implements ParameterException
* AbstractException constructor.
*
* @param string $message
* @param iterable[] $parameters
* @param array<mixed> $parameters
* @param int $code
* @param Throwable|null $previous
*/
Expand Down
110 changes: 110 additions & 0 deletions tests/Integration/Controller/HealthControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Tests\Integration\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
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
{
public function testSuccess(): void
{
$client = static::createClient();
$client->request('GET', '/health');

$response = $client->getResponse();
self::assertSame(200, $response->getStatusCode());
self::assertSame(json_encode([]), $response->getContent());
}

public function testAddCheckStatusUpSuccess(): 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 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 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();
$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 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
{
self::expectException(TypeError::class);

$healthController = new HealthController();
$healthController->addHealthCheck(new HealthController());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions tests/Integration/Mock/ConnectionMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Tests\Integration\Mock;

class ConnectionMock
{
public function ping(): bool
{
return true;
}
}
13 changes: 13 additions & 0 deletions tests/Integration/Mock/EntityManagerMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Tests\Integration\Mock;

class EntityManagerMock
{
public function getConnection(): ConnectionMock
{
return new ConnectionMock();
}
}
133 changes: 133 additions & 0 deletions tests/Integration/Unit/Check/DoctrineCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Tests\Integration\Unit\Check;

use Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use SymfonyHealthCheckBundle\Check\DoctrineCheck;
use SymfonyHealthCheckBundle\Check\StatusUpCheck;
use SymfonyHealthCheckBundle\Exception\ServiceNotFoundException;
use SymfonyHealthCheckBundle\Tests\Integration\Mock\ConnectionMock;
use SymfonyHealthCheckBundle\Tests\Integration\Mock\EntityManagerMock;

class DoctrineCheckTest extends TestCase
{
public function testStatusUpCheckSuccess(): void
{
$result = (new StatusUpCheck())->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();
}

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']);
}
}
Loading

0 comments on commit 5a33d14

Please sign in to comment.