-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from MacPaw/impruveTest
Impruve test
- Loading branch information
Showing
15 changed files
with
426 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.idea | ||
build | ||
vendor | ||
var | ||
/composer.lock | ||
/.phpcs-cache | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
Oops, something went wrong.