From 1ce656705373b7af60a04a69fc01719a9c99f533 Mon Sep 17 00:00:00 2001 From: Sarai Misidjan Date: Wed, 21 Jun 2023 11:32:47 +0200 Subject: [PATCH 1/2] Unit Test GatewayResourceService + renamed logger --- src/Service/GatewayResourceService.php | 20 +- .../Service/GatewayResourceServiceTest.php | 362 ++++++++++++++++++ 2 files changed, 368 insertions(+), 14 deletions(-) create mode 100644 tests/CommonGateway/CoreBundle/Service/GatewayResourceServiceTest.php diff --git a/src/Service/GatewayResourceService.php b/src/Service/GatewayResourceService.php index 60b03613..6894dc14 100644 --- a/src/Service/GatewayResourceService.php +++ b/src/Service/GatewayResourceService.php @@ -9,7 +9,7 @@ * @license EUPL * * @package commongateway/corebundle - * + * * @category Service */ @@ -25,7 +25,6 @@ class GatewayResourceService { - /** * @var EntityManagerInterface */ @@ -45,9 +44,8 @@ class GatewayResourceService public function __construct(EntityManagerInterface $entityManager, LoggerInterface $pluginLogger) { $this->entityManager = $entityManager; - $this->pluginLogger = $pluginLogger; - - }//end __construct() + $this->pluginLogger = $pluginLogger; + } /** * Get a schema by reference. @@ -65,7 +63,6 @@ public function getSchema(string $reference, string $pluginName): ?Entity }//end if return $entity; - }//end getSchema() /** @@ -84,7 +81,6 @@ public function getMapping(string $reference, string $pluginName): ?Mapping }//end if return $mapping; - }//end getMapping() /** @@ -103,7 +99,6 @@ public function getSource(string $reference, string $pluginName): ?Source }//end if return $source; - }//end getSource() /** @@ -117,7 +112,7 @@ public function getSource(string $reference, string $pluginName): ?Source */ public function findSourcesForUrl(string $url, string $pluginName): ?array { - $sources = []; + $sources = []; $allSources = $this->entityManager->getRepository('App:Gateway')->findAll(); foreach ($allSources as $source) { @@ -132,7 +127,6 @@ public function findSourcesForUrl(string $url, string $pluginName): ?array }//end if return $sources; - }//end findSourcesForUrl() /** @@ -151,7 +145,6 @@ public function getEndpoint(string $reference, string $pluginName): ?Endpoint }//end if return $endpoint; - }//end getEndpoint() /** @@ -166,10 +159,9 @@ public function getAction(string $reference, string $pluginName): ?Action { $action = $this->entityManager->getRepository('App:Action')->findOneBy(['reference' => $reference]); if ($action === null) { - $this->logger->error("No action found for $reference.", ['plugin' => $pluginName]); + $this->pluginLogger->error("No action found for $reference.", ['plugin' => $pluginName]); }//end if return $action; - }//end getAction() -}//end class +} diff --git a/tests/CommonGateway/CoreBundle/Service/GatewayResourceServiceTest.php b/tests/CommonGateway/CoreBundle/Service/GatewayResourceServiceTest.php new file mode 100644 index 00000000..8befbe7c --- /dev/null +++ b/tests/CommonGateway/CoreBundle/Service/GatewayResourceServiceTest.php @@ -0,0 +1,362 @@ +entityManager = $this->createMock(EntityManagerInterface::class); + $this->pluginLogger = $this->createMock(LoggerInterface::class); + $this->gatewayResourceService = new GatewayResourceService($this->entityManager, $this->pluginLogger); + } + + /** + * Test the 'getSchema' method with an existing entity. + */ + public function testGetSchemaWithExistingEntity(): void + { + $reference = 'test-reference'; + $pluginName = 'test-plugin'; + $entity = $this->createMock(Entity::class); + + $entityRepositoryMock = $this->createMock(EntityRepository::class); + $entityRepositoryMock->expects($this->once()) + ->method('findOneBy') + ->with(['reference' => $reference]) + ->willReturn($entity); + + $this->entityManager->expects($this->once()) + ->method('getRepository') + ->with('App:Entity') + ->willReturn($entityRepositoryMock); + + $result = $this->gatewayResourceService->getSchema($reference, $pluginName); + + $this->assertSame($entity, $result); + } + + /** + * Test the 'getSchema' method with a non-existing entity. + */ + public function testGetSchemaWithNonExistingEntity(): void + { + $reference = 'test-reference'; + $pluginName = 'test-plugin'; + + + $entityRepositoryMock = $this->createMock(EntityRepository::class); + $entityRepositoryMock->expects($this->once()) + ->method('findOneBy') + ->with(['reference' => $reference]) + ->willReturn(null); + + $this->entityManager->expects($this->once()) + ->method('getRepository') + ->with('App:Entity') + ->willReturn($entityRepositoryMock); + + $this->pluginLogger->expects($this->once()) + ->method('error') + ->with("No entity found for $reference.", ['plugin' => $pluginName]); + + $result = $this->gatewayResourceService->getSchema($reference, $pluginName); + + $this->assertNull($result); + } + + /** + * Test the 'getMapping' method with an existing mapping. + */ + public function testGetMappingWithExistingMapping(): void + { + $reference = 'test-reference'; + $pluginName = 'test-plugin'; + $mapping = $this->createMock(Mapping::class); + + + $mappingRepositoryMock = $this->createMock(MappingRepository::class); + $mappingRepositoryMock->expects($this->once()) + ->method('findOneBy') + ->with(['reference' => $reference]) + ->willReturn($mapping); + + $this->entityManager->expects($this->once()) + ->method('getRepository') + ->with('App:Mapping') + ->willReturn($mappingRepositoryMock); + + $result = $this->gatewayResourceService->getMapping($reference, $pluginName); + + $this->assertSame($mapping, $result); + } + + /** + * Test the 'getMapping' method with a non-existing mapping. + */ + public function testGetMappingWithNonExistingMapping(): void + { + $reference = 'test-reference'; + $pluginName = 'test-plugin'; + + $mappingRepositoryMock = $this->createMock(MappingRepository::class); + $mappingRepositoryMock->expects($this->once()) + ->method('findOneBy') + ->with(['reference' => $reference]) + ->willReturn(null); + + $this->entityManager->expects($this->once()) + ->method('getRepository') + ->with('App:Mapping') + ->willReturn($mappingRepositoryMock); + + $this->pluginLogger->expects($this->once()) + ->method('error') + ->with("No mapping found for $reference.", ['plugin' => $pluginName]); + + $result = $this->gatewayResourceService->getMapping($reference, $pluginName); + + $this->assertNull($result); + } + + /** + * Test the 'getSource' method with an existing source. + */ + public function testGetSourceWithExistingSource(): void + { + $reference = 'test-reference'; + $pluginName = 'test-plugin'; + $source = $this->createMock(Source::class); + + $sourceRepositoryMock = $this->createMock(GatewayRepository::class); + $sourceRepositoryMock->expects($this->once()) + ->method('findOneBy') + ->with(['reference' => $reference]) + ->willReturn($source); + + $this->entityManager->expects($this->once()) + ->method('getRepository') + ->with('App:Gateway') + ->willReturn($sourceRepositoryMock); + + $result = $this->gatewayResourceService->getSource($reference, $pluginName); + + $this->assertSame($source, $result); + } + + /** + * Test the 'getSource' method with a non-existing source. + */ + public function testGetSourceWithNonExistingSource(): void + { + $reference = 'test-reference'; + $pluginName = 'test-plugin'; + + $sourceRepositoryMock = $this->createMock(GatewayRepository::class); + $sourceRepositoryMock->expects($this->once()) + ->method('findOneBy') + ->with(['reference' => $reference]) + ->willReturn(null); + + $this->entityManager->expects($this->once()) + ->method('getRepository') + ->with('App:Gateway') + ->willReturn($sourceRepositoryMock); + + $this->pluginLogger->expects($this->once()) + ->method('error') + ->with("No source found for $reference.", ['plugin' => $pluginName]); + + $result = $this->gatewayResourceService->getSource($reference, $pluginName); + + $this->assertNull($result); + } + + /** + * Test the 'findSourcesForUrl' method with matching sources. + */ +// public function testFindSourcesForUrlWithMatchingSources(): void +// { +// $url = 'http://example.com'; +// $pluginName = 'test-plugin'; +// $source1 = $this->createMock(Source::class); +// $source2 = $this->createMock(Source::class); +// $allSources = [$source1, $source2]; +// +// $this->entityManager->expects($this->once()) +// ->method('getRepository') +// ->with('App:Gateway') +// ->willReturnSelf(); +// +// $this->entityManager->expects($this->once()) +// ->method('findAll') +// ->willReturn($allSources); +// +// $result = $this->gatewayResourceService->findSourcesForUrl($url, $pluginName); +// +// $this->assertSame($allSources, $result); +// } +// +// /** +// * Test the 'findSourcesForUrl' method with no matching sources. +// */ +// public function testFindSourcesForUrlWithNoMatchingSources(): void +// { +// $url = 'http://example.com'; +// $pluginName = 'test-plugin'; +// $allSources = []; +// +// $this->entityManager->expects($this->once()) +// ->method('getRepository') +// ->with('App:Gateway') +// ->willReturnSelf(); +// +// $this->entityManager->expects($this->once()) +// ->method('findAll') +// ->willReturn($allSources); +// +// $this->pluginLogger->expects($this->once()) +// ->method('error') +// ->with("No sources found for $url.", ['plugin' => $pluginName]); +// +// $result = $this->gatewayResourceService->findSourcesForUrl($url, $pluginName); +// +// $this->assertNull($result); +// } + + /** + * Test the 'getEndpoint' method with an existing endpoint. + */ + public function testGetEndpointWithExistingEndpoint(): void + { + $reference = 'test-reference'; + $pluginName = 'test-plugin'; + $endpoint = $this->createMock(Endpoint::class); + + $endpointRepositoryMock = $this->createMock(EndpointRepository::class); + $endpointRepositoryMock->expects($this->once()) + ->method('findOneBy') + ->with(['reference' => $reference]) + ->willReturn($endpoint); + + $this->entityManager->expects($this->once()) + ->method('getRepository') + ->with('App:Endpoint') + ->willReturn($endpointRepositoryMock); + + $result = $this->gatewayResourceService->getEndpoint($reference, $pluginName); + + $this->assertSame($endpoint, $result); + } + + /** + * Test the 'getEndpoint' method with a non-existing endpoint. + */ + public function testGetEndpointWithNonExistingEndpoint(): void + { + $reference = 'test-reference'; + $pluginName = 'test-plugin'; + + $endpointRepositoryMock = $this->createMock(EndpointRepository::class); + $endpointRepositoryMock->expects($this->once()) + ->method('findOneBy') + ->with(['reference' => $reference]) + ->willReturn(null); + + $this->entityManager->expects($this->once()) + ->method('getRepository') + ->with('App:Endpoint') + ->willReturn($endpointRepositoryMock); + + $this->pluginLogger->expects($this->once()) + ->method('error') + ->with("No endpoint found for $reference.", ['plugin' => $pluginName]); + + $result = $this->gatewayResourceService->getEndpoint($reference, $pluginName); + + $this->assertNull($result); + } + + /** + * Test the 'getAction' method with an existing action. + */ + public function testGetActionWithExistingAction(): void + { + $reference = 'test-reference'; + $pluginName = 'test-plugin'; + $action = $this->createMock(Action::class); + + $actionRepositoryMock = $this->createMock(ActionRepository::class); + $actionRepositoryMock->expects($this->once()) + ->method('findOneBy') + ->with(['reference' => $reference]) + ->willReturn($action); + + $this->entityManager->expects($this->once()) + ->method('getRepository') + ->with('App:Action') + ->willReturn($actionRepositoryMock); + + $result = $this->gatewayResourceService->getAction($reference, $pluginName); + + $this->assertSame($action, $result); + } + + /** + * Test the 'getAction' method with a non-existing action. + */ + public function testGetActionWithNonExistingAction(): void + { + $reference = 'test-reference'; + $pluginName = 'test-plugin'; + + $actionRepositoryMock = $this->createMock(ActionRepository::class); + $actionRepositoryMock->expects($this->once()) + ->method('findOneBy') + ->with(['reference' => $reference]) + ->willReturn(null); + + $this->entityManager->expects($this->once()) + ->method('getRepository') + ->with('App:Action') + ->willReturn($actionRepositoryMock); + + $this->pluginLogger->expects($this->once()) + ->method('error') + ->with("No action found for $reference.", ['plugin' => $pluginName]); + + $result = $this->gatewayResourceService->getAction($reference, $pluginName); + + $this->assertNull($result); + } +} From 5d34a57f1d9aef4885ad8569283d907eac0df037 Mon Sep 17 00:00:00 2001 From: GitHub Actions <> Date: Wed, 21 Jun 2023 09:33:20 +0000 Subject: [PATCH 2/2] Update src from PHP Codesniffer --- src/Service/GatewayResourceService.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Service/GatewayResourceService.php b/src/Service/GatewayResourceService.php index 6894dc14..84cf0b80 100644 --- a/src/Service/GatewayResourceService.php +++ b/src/Service/GatewayResourceService.php @@ -9,7 +9,7 @@ * @license EUPL * * @package commongateway/corebundle - * + * * @category Service */ @@ -25,6 +25,7 @@ class GatewayResourceService { + /** * @var EntityManagerInterface */ @@ -44,8 +45,9 @@ class GatewayResourceService public function __construct(EntityManagerInterface $entityManager, LoggerInterface $pluginLogger) { $this->entityManager = $entityManager; - $this->pluginLogger = $pluginLogger; - } + $this->pluginLogger = $pluginLogger; + + }//end __construct() /** * Get a schema by reference. @@ -63,6 +65,7 @@ public function getSchema(string $reference, string $pluginName): ?Entity }//end if return $entity; + }//end getSchema() /** @@ -81,6 +84,7 @@ public function getMapping(string $reference, string $pluginName): ?Mapping }//end if return $mapping; + }//end getMapping() /** @@ -99,6 +103,7 @@ public function getSource(string $reference, string $pluginName): ?Source }//end if return $source; + }//end getSource() /** @@ -112,7 +117,7 @@ public function getSource(string $reference, string $pluginName): ?Source */ public function findSourcesForUrl(string $url, string $pluginName): ?array { - $sources = []; + $sources = []; $allSources = $this->entityManager->getRepository('App:Gateway')->findAll(); foreach ($allSources as $source) { @@ -127,6 +132,7 @@ public function findSourcesForUrl(string $url, string $pluginName): ?array }//end if return $sources; + }//end findSourcesForUrl() /** @@ -145,6 +151,7 @@ public function getEndpoint(string $reference, string $pluginName): ?Endpoint }//end if return $endpoint; + }//end getEndpoint() /** @@ -163,5 +170,6 @@ public function getAction(string $reference, string $pluginName): ?Action }//end if return $action; + }//end getAction() -} +}//end class