Loading module/Import/src/Import/Service/DbService.php +2 −2 Changes for module/Import/src/Import/Service/DbService.php: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -256,7 +256,7 @@ class DbService $message = sprintf("Interrogation du service '%s' de l'établissement '%s', en %s seconde(s).", $serviceName, $this->etablissement->getStructure()->getCode(), $this->etablissement->getCode(), $duration ); Loading @@ -277,7 +277,7 @@ class DbService try { $connection->executeQuery($sql, [ 'message' => $message, 'etab' => $this->etablissement->getStructure()->getCode(), 'etab' => $this->etablissement->getCode(), 'status' => $status, 'route' => $route, 'service' => $serviceName, Loading module/Import/src/Import/Service/Factory/FetcherServiceFactory.php +2 −1 Changes for module/Import/src/Import/Service/Factory/FetcherServiceFactory.php: 2 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -26,7 +26,8 @@ class FetcherServiceFactory throw new LogicException("La config du FetcherService est incorrecte.", null, $e); } $service = new FetcherService($config); $service = new FetcherService(); $service->setConfig($config); $service->setCallService($callService); $service->setDbService($dbService); Loading module/Import/src/Import/Service/FetcherService.php +10 −21 Changes for module/Import/src/Import/Service/FetcherService.php: 10 added lines, 21 removed lines. Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ use Assert\AssertionFailedException; use DateTime; use Import\Service\Traits\CallServiceAwareTrait; use Import\Service\Traits\DbServiceAwareTrait; use stdClass; use UnicaenApp\Exception\LogicException; use UnicaenApp\Exception\RuntimeException; use Zend\Log\LoggerAwareTrait; Loading Loading @@ -37,16 +38,6 @@ class FetcherService */ protected $etablissement; /** * Constructor * * @param array $config */ public function __construct(array $config) { $this->config = $config; } /** * Set logger object * Loading Loading @@ -86,15 +77,16 @@ class FetcherService } /** * @return \stdClass * @return stdClass */ public function version() { $config = $this->getConfigForEtablissement(); $config['timeout'] = 10; return $this->callService->setConfig($config)->getVersion(); $this->callService->setConfig($config); return $this->callService->getVersion(); } /** Loading Loading @@ -158,7 +150,7 @@ class FetcherService $this->logger->info(sprintf("Interrogations du WS '%s'...", $serviceName)); $this->callService->setConfig($this->getConfigForEtablissement()); $apiFilters = $this->prepareFiltersForWebServiceRequest($filters); $apiFilters = $this->prepareFiltersForAPIRequest($filters); $jsonEntities = []; $page = 1; do { Loading @@ -167,6 +159,7 @@ class FetcherService if (count($params) > 0) { $uri .= '?' . http_build_query($params); } $_deb = microtime(true); $json = $this->callService->get($uri); $_fin = microtime(true); Loading Loading @@ -220,16 +213,12 @@ class FetcherService */ private function getConfigForEtablissement() { if ($this->etablissement === null) { throw new LogicException("Le code établissement courant est null."); } $codeEtablissement = $this->etablissement->getStructure()->getCode(); $codeEtablissement = $this->etablissement->getCode(); try { Assertion::keyIsset($this->config, $codeEtablissement); } catch (AssertionFailedException $e) { throw new LogicException("Le code établissement '{$codeEtablissement}' est introuvable dans la config.", null, $e); throw new RuntimeException("Aucune clé de config ne correspond au code établissement '{$codeEtablissement}'.", null, $e); } return $this->config[$codeEtablissement]; Loading @@ -239,7 +228,7 @@ class FetcherService * @param array $filters * @return array */ private function prepareFiltersForWebServiceRequest(array $filters) private function prepareFiltersForAPIRequest(array $filters) { if (empty($filters)) { return $filters; Loading module/Import/tests/Import/Service/DbServiceTest.phpdeleted 100644 → 0 +0 −78 Changes for module/Import/tests/Import/Service/DbServiceTest.php: 0 added lines, 78 removed lines. Original line number Diff line number Diff line <?php namespace ImportTest\Service; use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping\ClassMetadata; use Import\Service\DbService; use Import\Service\SQLGenerator; use Zend\Log\LoggerInterface; class DbServiceTest extends \PHPUnit_Framework_TestCase { /** * @var EntityManager|\PHPUnit_Framework_MockObject_MockObject */ private $entityManager; /** * @var ClassMetadata|\PHPUnit_Framework_MockObject_MockObject */ private $entityClassMetadata; /** * @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ private $logger; /** * @var Connection|\PHPUnit_Framework_MockObject_MockObject */ private $connection; /** * @var SQLGenerator|\PHPUnit_Framework_MockObject_MockObject */ private $sqlGenerator; protected function setUp() { $this->entityClassMetadata = $this->createMock(ClassMetadata::class); $this->entityClassMetadata->name = 'EntityClass'; $this->entityClassMetadata->table = ['name' => 'TABLE']; $this->entityClassMetadata->columnNames = ['one'=>'ONE', 'two'=>'TWO']; $this->entityClassMetadata->fieldMappings = [ 'one' => ['type' => 'string'], 'two' => ['type' => 'string'], ]; $this->connection = $this->createMock(Connection::class); $this->entityManager = $this->createMock(EntityManager::class); $this->entityManager->expects($this->once())->method('getClassMetadata')->willReturn($this->entityClassMetadata); $this->entityManager->expects($this->once())->method('getConnection')->willReturn($this->connection); $this->logger = $this->createMock(LoggerInterface::class); $this->sqlGenerator = $this->createMock(SQLGenerator::class); } public function testCanSaveEntities() { $jsonEntity = new \stdClass(); $this->entityClassMetadata->columnNames = [ 'one' => 'ONE', 'two' => 'TWO', ]; $this->entityClassMetadata->fieldMappings = [ 'one' => ['type' => 'string'], 'two' => ['type' => 'string'], ]; $service = new DbService(); $service->save([$jsonEntity]); } } module/Import/tests/ImportTest/Service/DbServiceTest.php +64 −2 Changes for module/Import/tests/ImportTest/Service/DbServiceTest.php: 64 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -3,10 +3,13 @@ namespace ImportTest\Service; use Application\Entity\Db\Etablissement; use Application\Entity\Db\These; use DateTime; use Doctrine\DBAL\Connection; use Doctrine\DBAL\ConnectionException; use Doctrine\DBAL\DBALException; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Mapping\ClassMetadata; use Import\Service\DbService; use Import\Service\DbServiceJSONHelper; Loading Loading @@ -87,6 +90,7 @@ class DbServiceTest extends \PHPUnit_Framework_TestCase /** @var Etablissement|\PHPUnit_Framework_MockObject_MockObject $etablissement */ $this->etablissement = $this->createMock(Etablissement::class); $this->etablissement->expects($this->any())->method('getCode')->willReturn('ETAB'); $this->dbService = new DbService(); $this->dbService->setEntityManager($this->entityManager); Loading @@ -96,7 +100,17 @@ class DbServiceTest extends \PHPUnit_Framework_TestCase $this->dbService->setEtablissement($this->etablissement); } public function testClearMakesNoCommit() public function testSettingServiceNameResetsMetadata() { $this->entityManager->expects($this->once())->method('getClassMetadata')->willReturn('not null'); $this->dbService->clear(); $this->dbService->setServiceName('whatever'); $this->assertNull($this->getObjectAttribute($this->dbService, 'entityClassMetadata')); } public function testClearDoesNotPerformAnyCommit() { $this->connection->expects($this->never())->method('commit'); Loading @@ -105,13 +119,44 @@ class DbServiceTest extends \PHPUnit_Framework_TestCase public function testClearAddsMandatoryEtablissementFilter() { $this->etablissement->expects($this->any())->method('getCode')->willReturn('ETAB'); $this->sqlGenerator->expects($this->once())->method('generateSQLQueryForClearingExistingData') ->with('TABLE', ['critere' => 'valeur', 'etablissement_id' => 'ETAB']); $this->dbService->clear(['critere' => 'valeur']); } public function testClearReplacesTheseIdFilterValueByTheSourceCode() { /** @var EntityRepository|\PHPUnit_Framework_MockObject_MockObject $repository */ $repository = $this->createMock(EntityRepository::class); /** @var These|\PHPUnit_Framework_MockObject_MockObject $repository */ $these = $this->createMock(These::class); $these->expects($this->once())->method('getSourceCode')->willReturn('source code'); $this->entityManager->expects($this->once())->method('getRepository')->willReturn($repository); $repository->expects($this->once())->method('find')->with(12)->willReturn($these); $this->sqlGenerator->expects($this->once())->method('generateSQLQueryForClearingExistingData') ->with('TABLE', ['these_id' => 'source code', 'etablissement_id' => 'ETAB']); $this->dbService->clear(['these_id' => 12]); } /** * @expectedException \UnicaenApp\Exception\RuntimeException */ public function testClearThrowsExceptionIfNoTheseFoundWithTheseIdFilterValue() { /** @var EntityRepository|\PHPUnit_Framework_MockObject_MockObject $repository */ $repository = $this->createMock(EntityRepository::class); $this->entityManager->expects($this->once())->method('getRepository')->willReturn($repository); $repository->expects($this->once())->method('find')->with(12)->willReturn(null); $this->dbService->clear(['these_id' => 12]); } /** * @expectedException \UnicaenApp\Exception\RuntimeException */ Loading @@ -122,6 +167,13 @@ class DbServiceTest extends \PHPUnit_Framework_TestCase $this->dbService->clear([]); } public function testSaveDoesNotPerformAnyCommit() { $this->connection->expects($this->never())->method('commit'); $this->dbService->save([]); } public function testSaveCutsQueriesIntoChunks() { $this->dbService->setInsertQueriesChunkSize(20); Loading Loading @@ -178,4 +230,14 @@ class DbServiceTest extends \PHPUnit_Framework_TestCase $this->dbService->commit(); } public function testInsertLogDoesNotPerformAnyCommit() { $this->connection->expects($this->never())->method('commit'); /** @var DateTime $startDate */ $startDate = $this->createMock(DateTime::class); $this->dbService->insertLog('', $startDate, 0, '', ''); } } Loading
module/Import/src/Import/Service/DbService.php +2 −2 Changes for module/Import/src/Import/Service/DbService.php: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -256,7 +256,7 @@ class DbService $message = sprintf("Interrogation du service '%s' de l'établissement '%s', en %s seconde(s).", $serviceName, $this->etablissement->getStructure()->getCode(), $this->etablissement->getCode(), $duration ); Loading @@ -277,7 +277,7 @@ class DbService try { $connection->executeQuery($sql, [ 'message' => $message, 'etab' => $this->etablissement->getStructure()->getCode(), 'etab' => $this->etablissement->getCode(), 'status' => $status, 'route' => $route, 'service' => $serviceName, Loading
module/Import/src/Import/Service/Factory/FetcherServiceFactory.php +2 −1 Changes for module/Import/src/Import/Service/Factory/FetcherServiceFactory.php: 2 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -26,7 +26,8 @@ class FetcherServiceFactory throw new LogicException("La config du FetcherService est incorrecte.", null, $e); } $service = new FetcherService($config); $service = new FetcherService(); $service->setConfig($config); $service->setCallService($callService); $service->setDbService($dbService); Loading
module/Import/src/Import/Service/FetcherService.php +10 −21 Changes for module/Import/src/Import/Service/FetcherService.php: 10 added lines, 21 removed lines. Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ use Assert\AssertionFailedException; use DateTime; use Import\Service\Traits\CallServiceAwareTrait; use Import\Service\Traits\DbServiceAwareTrait; use stdClass; use UnicaenApp\Exception\LogicException; use UnicaenApp\Exception\RuntimeException; use Zend\Log\LoggerAwareTrait; Loading Loading @@ -37,16 +38,6 @@ class FetcherService */ protected $etablissement; /** * Constructor * * @param array $config */ public function __construct(array $config) { $this->config = $config; } /** * Set logger object * Loading Loading @@ -86,15 +77,16 @@ class FetcherService } /** * @return \stdClass * @return stdClass */ public function version() { $config = $this->getConfigForEtablissement(); $config['timeout'] = 10; return $this->callService->setConfig($config)->getVersion(); $this->callService->setConfig($config); return $this->callService->getVersion(); } /** Loading Loading @@ -158,7 +150,7 @@ class FetcherService $this->logger->info(sprintf("Interrogations du WS '%s'...", $serviceName)); $this->callService->setConfig($this->getConfigForEtablissement()); $apiFilters = $this->prepareFiltersForWebServiceRequest($filters); $apiFilters = $this->prepareFiltersForAPIRequest($filters); $jsonEntities = []; $page = 1; do { Loading @@ -167,6 +159,7 @@ class FetcherService if (count($params) > 0) { $uri .= '?' . http_build_query($params); } $_deb = microtime(true); $json = $this->callService->get($uri); $_fin = microtime(true); Loading Loading @@ -220,16 +213,12 @@ class FetcherService */ private function getConfigForEtablissement() { if ($this->etablissement === null) { throw new LogicException("Le code établissement courant est null."); } $codeEtablissement = $this->etablissement->getStructure()->getCode(); $codeEtablissement = $this->etablissement->getCode(); try { Assertion::keyIsset($this->config, $codeEtablissement); } catch (AssertionFailedException $e) { throw new LogicException("Le code établissement '{$codeEtablissement}' est introuvable dans la config.", null, $e); throw new RuntimeException("Aucune clé de config ne correspond au code établissement '{$codeEtablissement}'.", null, $e); } return $this->config[$codeEtablissement]; Loading @@ -239,7 +228,7 @@ class FetcherService * @param array $filters * @return array */ private function prepareFiltersForWebServiceRequest(array $filters) private function prepareFiltersForAPIRequest(array $filters) { if (empty($filters)) { return $filters; Loading
module/Import/tests/Import/Service/DbServiceTest.phpdeleted 100644 → 0 +0 −78 Changes for module/Import/tests/Import/Service/DbServiceTest.php: 0 added lines, 78 removed lines. Original line number Diff line number Diff line <?php namespace ImportTest\Service; use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping\ClassMetadata; use Import\Service\DbService; use Import\Service\SQLGenerator; use Zend\Log\LoggerInterface; class DbServiceTest extends \PHPUnit_Framework_TestCase { /** * @var EntityManager|\PHPUnit_Framework_MockObject_MockObject */ private $entityManager; /** * @var ClassMetadata|\PHPUnit_Framework_MockObject_MockObject */ private $entityClassMetadata; /** * @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ private $logger; /** * @var Connection|\PHPUnit_Framework_MockObject_MockObject */ private $connection; /** * @var SQLGenerator|\PHPUnit_Framework_MockObject_MockObject */ private $sqlGenerator; protected function setUp() { $this->entityClassMetadata = $this->createMock(ClassMetadata::class); $this->entityClassMetadata->name = 'EntityClass'; $this->entityClassMetadata->table = ['name' => 'TABLE']; $this->entityClassMetadata->columnNames = ['one'=>'ONE', 'two'=>'TWO']; $this->entityClassMetadata->fieldMappings = [ 'one' => ['type' => 'string'], 'two' => ['type' => 'string'], ]; $this->connection = $this->createMock(Connection::class); $this->entityManager = $this->createMock(EntityManager::class); $this->entityManager->expects($this->once())->method('getClassMetadata')->willReturn($this->entityClassMetadata); $this->entityManager->expects($this->once())->method('getConnection')->willReturn($this->connection); $this->logger = $this->createMock(LoggerInterface::class); $this->sqlGenerator = $this->createMock(SQLGenerator::class); } public function testCanSaveEntities() { $jsonEntity = new \stdClass(); $this->entityClassMetadata->columnNames = [ 'one' => 'ONE', 'two' => 'TWO', ]; $this->entityClassMetadata->fieldMappings = [ 'one' => ['type' => 'string'], 'two' => ['type' => 'string'], ]; $service = new DbService(); $service->save([$jsonEntity]); } }
module/Import/tests/ImportTest/Service/DbServiceTest.php +64 −2 Changes for module/Import/tests/ImportTest/Service/DbServiceTest.php: 64 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -3,10 +3,13 @@ namespace ImportTest\Service; use Application\Entity\Db\Etablissement; use Application\Entity\Db\These; use DateTime; use Doctrine\DBAL\Connection; use Doctrine\DBAL\ConnectionException; use Doctrine\DBAL\DBALException; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Mapping\ClassMetadata; use Import\Service\DbService; use Import\Service\DbServiceJSONHelper; Loading Loading @@ -87,6 +90,7 @@ class DbServiceTest extends \PHPUnit_Framework_TestCase /** @var Etablissement|\PHPUnit_Framework_MockObject_MockObject $etablissement */ $this->etablissement = $this->createMock(Etablissement::class); $this->etablissement->expects($this->any())->method('getCode')->willReturn('ETAB'); $this->dbService = new DbService(); $this->dbService->setEntityManager($this->entityManager); Loading @@ -96,7 +100,17 @@ class DbServiceTest extends \PHPUnit_Framework_TestCase $this->dbService->setEtablissement($this->etablissement); } public function testClearMakesNoCommit() public function testSettingServiceNameResetsMetadata() { $this->entityManager->expects($this->once())->method('getClassMetadata')->willReturn('not null'); $this->dbService->clear(); $this->dbService->setServiceName('whatever'); $this->assertNull($this->getObjectAttribute($this->dbService, 'entityClassMetadata')); } public function testClearDoesNotPerformAnyCommit() { $this->connection->expects($this->never())->method('commit'); Loading @@ -105,13 +119,44 @@ class DbServiceTest extends \PHPUnit_Framework_TestCase public function testClearAddsMandatoryEtablissementFilter() { $this->etablissement->expects($this->any())->method('getCode')->willReturn('ETAB'); $this->sqlGenerator->expects($this->once())->method('generateSQLQueryForClearingExistingData') ->with('TABLE', ['critere' => 'valeur', 'etablissement_id' => 'ETAB']); $this->dbService->clear(['critere' => 'valeur']); } public function testClearReplacesTheseIdFilterValueByTheSourceCode() { /** @var EntityRepository|\PHPUnit_Framework_MockObject_MockObject $repository */ $repository = $this->createMock(EntityRepository::class); /** @var These|\PHPUnit_Framework_MockObject_MockObject $repository */ $these = $this->createMock(These::class); $these->expects($this->once())->method('getSourceCode')->willReturn('source code'); $this->entityManager->expects($this->once())->method('getRepository')->willReturn($repository); $repository->expects($this->once())->method('find')->with(12)->willReturn($these); $this->sqlGenerator->expects($this->once())->method('generateSQLQueryForClearingExistingData') ->with('TABLE', ['these_id' => 'source code', 'etablissement_id' => 'ETAB']); $this->dbService->clear(['these_id' => 12]); } /** * @expectedException \UnicaenApp\Exception\RuntimeException */ public function testClearThrowsExceptionIfNoTheseFoundWithTheseIdFilterValue() { /** @var EntityRepository|\PHPUnit_Framework_MockObject_MockObject $repository */ $repository = $this->createMock(EntityRepository::class); $this->entityManager->expects($this->once())->method('getRepository')->willReturn($repository); $repository->expects($this->once())->method('find')->with(12)->willReturn(null); $this->dbService->clear(['these_id' => 12]); } /** * @expectedException \UnicaenApp\Exception\RuntimeException */ Loading @@ -122,6 +167,13 @@ class DbServiceTest extends \PHPUnit_Framework_TestCase $this->dbService->clear([]); } public function testSaveDoesNotPerformAnyCommit() { $this->connection->expects($this->never())->method('commit'); $this->dbService->save([]); } public function testSaveCutsQueriesIntoChunks() { $this->dbService->setInsertQueriesChunkSize(20); Loading Loading @@ -178,4 +230,14 @@ class DbServiceTest extends \PHPUnit_Framework_TestCase $this->dbService->commit(); } public function testInsertLogDoesNotPerformAnyCommit() { $this->connection->expects($this->never())->method('commit'); /** @var DateTime $startDate */ $startDate = $this->createMock(DateTime::class); $this->dbService->insertLog('', $startDate, 0, '', ''); } }