Loading module/Import/src/Import/Service/CallService.php +7 −18 Changes for module/Import/src/Import/Service/CallService.php: 7 added lines, 18 removed lines. Original line number Diff line number Diff line Loading @@ -32,21 +32,6 @@ class CallService */ protected $client; /** * @var string $url : le chemin d'acces au web service * @var string $user : l'identifiant pour l'authentification * @var string $password : le mot de passe pour l'authentification * @var string|null $proxy : le champ proxy * @var boolean|string $verify : le champ pour le mode https * @var float $timeout : timeout of the request in seconds */ protected $url; protected $user; protected $password; protected $proxy; protected $verify = true; protected $timeout = 0; /** * @param array $config * @return self Loading @@ -63,6 +48,8 @@ class CallService } /** * Interroge le ws pour connaître sa version. * * @return \stdClass */ public function getVersion() Loading @@ -71,7 +58,7 @@ class CallService } /** * Envoie une requête au web service, à l'URI spécifiée. * Envoie une requête quelconque au web service, ex: 'version/current'. * * @param string $uri * @return \stdClass Loading Loading @@ -185,10 +172,12 @@ class CallService throw CallException::unexpectedResponse($uri, $response); } $json = json_decode($response->getBody()); $body = $response->getBody(); $json = json_decode($body); if ($json === null) { // NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. throw CallException::invalidJSONResponse($uri, (string)$response->getBody()); throw CallException::invalidJSONResponse($uri, (string)$body); } return $json; Loading module/Import/src/Import/Service/DbService.php +65 −95 Changes for module/Import/src/Import/Service/DbService.php: 65 added lines, 95 removed lines. Original line number Diff line number Diff line Loading @@ -66,105 +66,58 @@ class DbService */ private $tableName; /** * Récupération des infos et des champs (i.e. propriétés/colonnes) */ private function loadEntityClassMetadata() { if ($this->entityClassMetadata !== null) { return; } $entityClass = $this->entityNameForService($this->serviceName); $this->entityClassMetadata = $this->entityManager->getClassMetadata($entityClass); $this->tableName = $this->entityClassMetadata->table['name']; $this->logMetadata($this->serviceName, $entityClass); } /** * Demande au WS d'import un enregistrement particulier d'un service. * NB: AUCUN COMMIT N'EST FAIT. * * @param stdClass $json * @param string $sourceCode Source code de l'enregistrement à importer * @param stdClass $jsonEntity */ public function saveEntityForService(stdClass $json, $sourceCode) public function saveEntity(stdClass $jsonEntity) { $this->loadEntityClassMetadata(); $tableColumns = $this->entityClassMetadata->columnNames; /** Etablissement de la transaction Oracle */ $connection = $this->entityManager->getConnection(); $connection->beginTransaction(); $statement = null; $queries = []; /** Remplissage avec les données retournées par le Web Services */ $colonnes = implode(", ", $tableColumns); $values = implode(", ", $this->generateValueArray($json, $this->entityClassMetadata)); $query = "INSERT INTO " . $this->tableName . " (" . $colonnes . ") VALUES (" . $values . ")"; $queries[] = $query; /** Execution des requetes */ $_debut = microtime(true); foreach ($queries as $query) { $this->logger->debug("Execution de la requête : " . $query); try { $connection->executeQuery($query); } catch (DBALException $e) { throw new RuntimeException("Erreur lors de l'insert dans la table '$this->tableName'.'", null, $e); } } $_fin = microtime(true); $this->logger->debug(sprintf("Exécution des %d requêtes INSERT : %s secondes.", count($queries), $_fin - $_debut)); $this->saveEntities([$jsonEntity]); } /** * Demande au WS d'import tous les enregistrements d'un service, éventuellement filtrés. * NB: AUCUN COMMIT N'EST FAIT. * * @param array $jsonEntities * @param stdClass[] $jsonEntities */ public function saveEntitiesForService($jsonEntities) public function saveEntities(array $jsonEntities) { $this->loadEntityClassMetadata(); $tableColumns = $this->entityClassMetadata->columnNames; /** Etablissement de la transaction Oracle */ $connection = $this->entityManager->getConnection(); $connection->beginTransaction(); $statement = null; $tableColumns = $this->entityClassMetadata->columnNames; /** Construction des requêtes d'INSERTion **/ $queries = []; foreach ($jsonEntities as $entry) { foreach ($jsonEntities as $jsonEntity) { $colonnes = implode(", ", $tableColumns); $values = implode(", ", $this->generateValueArray($entry, $this->entityClassMetadata)); $values = implode(", ", $this->generateValueArray($jsonEntity)); $query = "INSERT INTO " . $this->tableName . " (" . $colonnes . ") VALUES (" . $values . ")"; $queries[] = $query; } /** Execution des requetes d'INSERTion groupées */ /** Execution des requetes d'INSERTion par groupes de N */ $_debut = microtime(true); $queriesChunks = array_chunk($queries, self::INSERT_QUERIES_CHUNK_SIZE); foreach ($queriesChunks as $queryChunk) { $sql = implode(';' . PHP_EOL, $queryChunk) . ';'; $this->logger->debug(sprintf("Execution de %s requêtes d'insert.", count($queryChunk))); $this->logger->debug(sprintf("Execution de %s requête(s) INSERT.", count($queryChunk))); try { $connection->executeQuery('BEGIN' . PHP_EOL . $sql . PHP_EOL . ' END;'); } catch (DBALException $e) { throw new RuntimeException("Erreur lors des inserts dans la table '$this->tableName'.'", null, $e); throw new RuntimeException("Erreur lors des requêtes INSERT dans la table '$this->tableName'.'", null, $e); } } $_fin = microtime(true); $this->logger->debug(sprintf("Exécution des %d requêtes INSERT : %s secondes.", count($queries), $_fin - $_debut)); $this->logger->debug(sprintf("%d requête(s) INSERT effectuée(s) en %s secondes.", count($queries), $_fin - $_debut)); } /** Loading @@ -175,7 +128,7 @@ class DbService * * @param array $filters */ public function deleteExistingDataForService(array $filters = []) public function deleteExistingData(array $filters = []) { $this->loadEntityClassMetadata(); Loading @@ -190,6 +143,46 @@ class DbService } } /** * Fait un commit en bdd. */ public function commit() { $connection = $this->entityManager->getConnection(); $_debut = microtime(true); try { $connection->commit(); } catch (\Exception $e) { try { $connection->rollBack(); } catch (ConnectionException $e) { throw new RuntimeException("Le rollback a échoué!", null, $e); } throw new RuntimeException("Le commit a échoué, un rollback a été effectué.", null, $e); } $_fin = microtime(true); $this->logger->debug("Commit : " . ($_fin - $_debut) . " secondes."); } /** * Récupération des infos et des champs (i.e. propriétés/colonnes) */ private function loadEntityClassMetadata() { if ($this->entityClassMetadata !== null) { return; } $entityClass = $this->entityNameForService($this->serviceName); $this->entityClassMetadata = $this->entityManager->getClassMetadata($entityClass); $this->tableName = $this->entityClassMetadata->table['name']; $this->logMetadata($this->serviceName); } /** * Fonction écrivant dans la table des logs API_LOGS. * NB: AUCUN COMMIT N'EST FAIT. Loading Loading @@ -239,29 +232,6 @@ class DbService $this->logger->info($message); } /** * Fait un commit en bdd. */ public function commit() { $connection = $this->entityManager->getConnection(); $_debut = microtime(true); try { $connection->commit(); } catch (\Exception $e) { try { $connection->rollBack(); } catch (ConnectionException $e) { throw new RuntimeException("Le rollback a échoué!", null, $e); } throw new RuntimeException("Le commit a échoué, un rollback a été effectué.", null, $e); } $_fin = microtime(true); $this->logger->debug("Commit : " . ($_fin - $_debut) . " secondes."); } /** * @param string service * @return string Loading Loading @@ -323,15 +293,15 @@ class DbService * Mise sous forme de table des données appartenant à une entité * * @param stdClass $jsonEntity : le json associé à une entité * @param ClassMetadata $metadata : les metadonnées associées à l'entité * @return array les données mises sous forme d'un tableau */ private function generateValueArray(stdClass $jsonEntity, $metadata) private function generateValueArray(stdClass $jsonEntity) { $f = new EtablissementPrefixFilter(); $valuesArray = []; foreach ($metadata->columnNames as $propriete => $colonne) { $type = $metadata->fieldMappings[$propriete]["type"]; foreach ($this->entityClassMetadata->columnNames as $propriete => $colonne) { $type = $this->entityClassMetadata->fieldMappings[$propriete]["type"]; $value = null; switch ($propriete) { case "etablissementId": // UCN, URN, ULHN, INSA, rien d'autre Loading Loading @@ -433,20 +403,20 @@ class DbService * Ajoute aux logs les metadonnées associées à une entity * * @param string $dataName : le nom de l'entité * @param string $entityClass : le chemin vers l'entité (namespace) */ private function logMetadata($dataName, $entityClass) private function logMetadata($dataName) { $metadata = $this->entityManager->getClassMetadata($entityClass); $tableName = $metadata->table['name']; $tableRelation = $metadata->columnNames; $entityClass = $this->entityClassMetadata->name; $tableName = $this->entityClassMetadata->table['name']; $tableColumns = $this->entityClassMetadata->columnNames; $str = <<<EOS La table associée à l'entité [$dataName][$entityClass] est [$tableName]" Relation dans propriétés/colonnes: EOS; foreach ($tableRelation as $propriete => $colonne) { $str .= " - " . $propriete . " => " . $colonne . " [" . $metadata->fieldMappings[$propriete]["type"] . "]" . PHP_EOL; foreach ($tableColumns as $propriete => $colonne) { $type = $this->entityClassMetadata->fieldMappings[$propriete]["type"]; $str .= " - " . $propriete . " => " . $colonne . " [" . $type . "]" . PHP_EOL; } $this->logger->debug($str); Loading module/Import/src/Import/Service/FetcherService.php +4 −4 Changes for module/Import/src/Import/Service/FetcherService.php: 4 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -124,8 +124,8 @@ class FetcherService $_deb = microtime(true); $this->dbService->setServiceName($serviceName); $this->dbService->setEtablissement($this->etablissement); $this->dbService->deleteExistingDataForService(['id' => $sourceCode]); $this->dbService->saveEntityForService($jsonEntity, $sourceCode); $this->dbService->deleteExistingData(['id' => $sourceCode]); $this->dbService->saveEntity($jsonEntity, $sourceCode); $this->dbService->commit(); $_fin = microtime(true); $this->logger->info(sprintf("Enregistrement en base de données en %.2f secondes.", $_fin - $_deb)); Loading Loading @@ -175,8 +175,8 @@ class FetcherService $_deb = microtime(true); $this->dbService->setServiceName($serviceName); $this->dbService->setEtablissement($this->etablissement); $this->dbService->deleteExistingDataForService($filters); $this->dbService->saveEntitiesForService($jsonEntities); $this->dbService->deleteExistingData($filters); $this->dbService->saveEntities($jsonEntities); $this->dbService->commit(); $_fin = microtime(true); $this->logger->info(sprintf("Enregistrement des %d entités en %.2f secondes.", count($jsonEntities), $_fin - $_deb)); Loading module/Import/tests/Import/Service/CallServiceTest.php +64 −1 Changes for module/Import/tests/Import/Service/CallServiceTest.php: 64 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace ImportTest\Service; use GuzzleHttp\Client; use GuzzleHttp\Exception\TransferException; use Import\Service\CallService; use stdClass; use Zend\Http\Response; use Exception; use GuzzleHttp\Exception\ClientException; Loading Loading @@ -129,6 +130,18 @@ class CallServiceTest extends \PHPUnit_Framework_TestCase $this->assertSame($this->client, $service->getClient()); } public function testCanCreateClientIfConfigIsProvided() { $service = new CallService(); $service->setConfig([ 'url' => 'url value', 'user' => 'user value', 'password' => 'password value', ]); $this->assertInstanceOf(Client::class, $service->getClient()); } public function getSendRequestPossibleError() { return [ Loading Loading @@ -160,7 +173,7 @@ class CallServiceTest extends \PHPUnit_Framework_TestCase public function testSendRequestThrowsCallExceptionWhenResponseStatusCodeIsNot200() { $response = $this->createMock(Response::class); $response->expects($this->any())->method('getStatusCode')->willReturn(123); $response->expects($this->exactly(2))->method('getStatusCode')->willReturn(123); $this->client->expects($this->once())->method('request')->willReturn($response); Loading @@ -169,4 +182,54 @@ class CallServiceTest extends \PHPUnit_Framework_TestCase $service->setLogger($this->logger); $service->get('peu/importe'); } /** * @expectedException \Import\Exception\CallException */ public function testSendRequestThrowsExceptionWhenResponseIsInvalidJSON() { $response = $this->createMock(Response::class); $response->expects($this->once())->method('getStatusCode')->willReturn(Response::STATUS_CODE_200); $response->expects($this->once())->method('getBody')->willReturn('invalid JSON'); $this->client->expects($this->once())->method('request')->willReturn($response); $service = new CallService(); $service->setClient($this->client); $service->setLogger($this->logger); $service->get('peu/importe'); } public function testCanGetVersion() { $response = $this->createMock(Response::class); $response->expects($this->once())->method('getStatusCode')->willReturn(Response::STATUS_CODE_200); $response->expects($this->once())->method('getBody')->willReturn('{"valid": "JSON"}'); $this->client->expects($this->once())->method('request')->with('GET', 'version/current')->willReturn($response); $service = new CallService(); $service->setClient($this->client); $service->setLogger($this->logger); $service->getVersion(); } public function testSendRequestReturnsJSON() { $response = $this->createMock(Response::class); $response->expects($this->once())->method('getStatusCode')->willReturn(Response::STATUS_CODE_200); $response->expects($this->once())->method('getBody')->willReturn('{"valid": "JSON"}'); $this->client->expects($this->once())->method('request')->willReturn($response); $service = new CallService(); $service->setClient($this->client); $service->setLogger($this->logger); $json = $service->get('peu/importe'); $entity = new stdClass(); $entity->valid = "JSON"; $this->assertEquals($entity, $json); } } module/Import/tests/Import/Service/DbServiceTest.php 0 → 100644 +57 −0 Changes for module/Import/tests/Import/Service/DbServiceTest.php: 57 added lines, 0 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 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; 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); } public function testCanSaveEntity() { } } Loading
module/Import/src/Import/Service/CallService.php +7 −18 Changes for module/Import/src/Import/Service/CallService.php: 7 added lines, 18 removed lines. Original line number Diff line number Diff line Loading @@ -32,21 +32,6 @@ class CallService */ protected $client; /** * @var string $url : le chemin d'acces au web service * @var string $user : l'identifiant pour l'authentification * @var string $password : le mot de passe pour l'authentification * @var string|null $proxy : le champ proxy * @var boolean|string $verify : le champ pour le mode https * @var float $timeout : timeout of the request in seconds */ protected $url; protected $user; protected $password; protected $proxy; protected $verify = true; protected $timeout = 0; /** * @param array $config * @return self Loading @@ -63,6 +48,8 @@ class CallService } /** * Interroge le ws pour connaître sa version. * * @return \stdClass */ public function getVersion() Loading @@ -71,7 +58,7 @@ class CallService } /** * Envoie une requête au web service, à l'URI spécifiée. * Envoie une requête quelconque au web service, ex: 'version/current'. * * @param string $uri * @return \stdClass Loading Loading @@ -185,10 +172,12 @@ class CallService throw CallException::unexpectedResponse($uri, $response); } $json = json_decode($response->getBody()); $body = $response->getBody(); $json = json_decode($body); if ($json === null) { // NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. throw CallException::invalidJSONResponse($uri, (string)$response->getBody()); throw CallException::invalidJSONResponse($uri, (string)$body); } return $json; Loading
module/Import/src/Import/Service/DbService.php +65 −95 Changes for module/Import/src/Import/Service/DbService.php: 65 added lines, 95 removed lines. Original line number Diff line number Diff line Loading @@ -66,105 +66,58 @@ class DbService */ private $tableName; /** * Récupération des infos et des champs (i.e. propriétés/colonnes) */ private function loadEntityClassMetadata() { if ($this->entityClassMetadata !== null) { return; } $entityClass = $this->entityNameForService($this->serviceName); $this->entityClassMetadata = $this->entityManager->getClassMetadata($entityClass); $this->tableName = $this->entityClassMetadata->table['name']; $this->logMetadata($this->serviceName, $entityClass); } /** * Demande au WS d'import un enregistrement particulier d'un service. * NB: AUCUN COMMIT N'EST FAIT. * * @param stdClass $json * @param string $sourceCode Source code de l'enregistrement à importer * @param stdClass $jsonEntity */ public function saveEntityForService(stdClass $json, $sourceCode) public function saveEntity(stdClass $jsonEntity) { $this->loadEntityClassMetadata(); $tableColumns = $this->entityClassMetadata->columnNames; /** Etablissement de la transaction Oracle */ $connection = $this->entityManager->getConnection(); $connection->beginTransaction(); $statement = null; $queries = []; /** Remplissage avec les données retournées par le Web Services */ $colonnes = implode(", ", $tableColumns); $values = implode(", ", $this->generateValueArray($json, $this->entityClassMetadata)); $query = "INSERT INTO " . $this->tableName . " (" . $colonnes . ") VALUES (" . $values . ")"; $queries[] = $query; /** Execution des requetes */ $_debut = microtime(true); foreach ($queries as $query) { $this->logger->debug("Execution de la requête : " . $query); try { $connection->executeQuery($query); } catch (DBALException $e) { throw new RuntimeException("Erreur lors de l'insert dans la table '$this->tableName'.'", null, $e); } } $_fin = microtime(true); $this->logger->debug(sprintf("Exécution des %d requêtes INSERT : %s secondes.", count($queries), $_fin - $_debut)); $this->saveEntities([$jsonEntity]); } /** * Demande au WS d'import tous les enregistrements d'un service, éventuellement filtrés. * NB: AUCUN COMMIT N'EST FAIT. * * @param array $jsonEntities * @param stdClass[] $jsonEntities */ public function saveEntitiesForService($jsonEntities) public function saveEntities(array $jsonEntities) { $this->loadEntityClassMetadata(); $tableColumns = $this->entityClassMetadata->columnNames; /** Etablissement de la transaction Oracle */ $connection = $this->entityManager->getConnection(); $connection->beginTransaction(); $statement = null; $tableColumns = $this->entityClassMetadata->columnNames; /** Construction des requêtes d'INSERTion **/ $queries = []; foreach ($jsonEntities as $entry) { foreach ($jsonEntities as $jsonEntity) { $colonnes = implode(", ", $tableColumns); $values = implode(", ", $this->generateValueArray($entry, $this->entityClassMetadata)); $values = implode(", ", $this->generateValueArray($jsonEntity)); $query = "INSERT INTO " . $this->tableName . " (" . $colonnes . ") VALUES (" . $values . ")"; $queries[] = $query; } /** Execution des requetes d'INSERTion groupées */ /** Execution des requetes d'INSERTion par groupes de N */ $_debut = microtime(true); $queriesChunks = array_chunk($queries, self::INSERT_QUERIES_CHUNK_SIZE); foreach ($queriesChunks as $queryChunk) { $sql = implode(';' . PHP_EOL, $queryChunk) . ';'; $this->logger->debug(sprintf("Execution de %s requêtes d'insert.", count($queryChunk))); $this->logger->debug(sprintf("Execution de %s requête(s) INSERT.", count($queryChunk))); try { $connection->executeQuery('BEGIN' . PHP_EOL . $sql . PHP_EOL . ' END;'); } catch (DBALException $e) { throw new RuntimeException("Erreur lors des inserts dans la table '$this->tableName'.'", null, $e); throw new RuntimeException("Erreur lors des requêtes INSERT dans la table '$this->tableName'.'", null, $e); } } $_fin = microtime(true); $this->logger->debug(sprintf("Exécution des %d requêtes INSERT : %s secondes.", count($queries), $_fin - $_debut)); $this->logger->debug(sprintf("%d requête(s) INSERT effectuée(s) en %s secondes.", count($queries), $_fin - $_debut)); } /** Loading @@ -175,7 +128,7 @@ class DbService * * @param array $filters */ public function deleteExistingDataForService(array $filters = []) public function deleteExistingData(array $filters = []) { $this->loadEntityClassMetadata(); Loading @@ -190,6 +143,46 @@ class DbService } } /** * Fait un commit en bdd. */ public function commit() { $connection = $this->entityManager->getConnection(); $_debut = microtime(true); try { $connection->commit(); } catch (\Exception $e) { try { $connection->rollBack(); } catch (ConnectionException $e) { throw new RuntimeException("Le rollback a échoué!", null, $e); } throw new RuntimeException("Le commit a échoué, un rollback a été effectué.", null, $e); } $_fin = microtime(true); $this->logger->debug("Commit : " . ($_fin - $_debut) . " secondes."); } /** * Récupération des infos et des champs (i.e. propriétés/colonnes) */ private function loadEntityClassMetadata() { if ($this->entityClassMetadata !== null) { return; } $entityClass = $this->entityNameForService($this->serviceName); $this->entityClassMetadata = $this->entityManager->getClassMetadata($entityClass); $this->tableName = $this->entityClassMetadata->table['name']; $this->logMetadata($this->serviceName); } /** * Fonction écrivant dans la table des logs API_LOGS. * NB: AUCUN COMMIT N'EST FAIT. Loading Loading @@ -239,29 +232,6 @@ class DbService $this->logger->info($message); } /** * Fait un commit en bdd. */ public function commit() { $connection = $this->entityManager->getConnection(); $_debut = microtime(true); try { $connection->commit(); } catch (\Exception $e) { try { $connection->rollBack(); } catch (ConnectionException $e) { throw new RuntimeException("Le rollback a échoué!", null, $e); } throw new RuntimeException("Le commit a échoué, un rollback a été effectué.", null, $e); } $_fin = microtime(true); $this->logger->debug("Commit : " . ($_fin - $_debut) . " secondes."); } /** * @param string service * @return string Loading Loading @@ -323,15 +293,15 @@ class DbService * Mise sous forme de table des données appartenant à une entité * * @param stdClass $jsonEntity : le json associé à une entité * @param ClassMetadata $metadata : les metadonnées associées à l'entité * @return array les données mises sous forme d'un tableau */ private function generateValueArray(stdClass $jsonEntity, $metadata) private function generateValueArray(stdClass $jsonEntity) { $f = new EtablissementPrefixFilter(); $valuesArray = []; foreach ($metadata->columnNames as $propriete => $colonne) { $type = $metadata->fieldMappings[$propriete]["type"]; foreach ($this->entityClassMetadata->columnNames as $propriete => $colonne) { $type = $this->entityClassMetadata->fieldMappings[$propriete]["type"]; $value = null; switch ($propriete) { case "etablissementId": // UCN, URN, ULHN, INSA, rien d'autre Loading Loading @@ -433,20 +403,20 @@ class DbService * Ajoute aux logs les metadonnées associées à une entity * * @param string $dataName : le nom de l'entité * @param string $entityClass : le chemin vers l'entité (namespace) */ private function logMetadata($dataName, $entityClass) private function logMetadata($dataName) { $metadata = $this->entityManager->getClassMetadata($entityClass); $tableName = $metadata->table['name']; $tableRelation = $metadata->columnNames; $entityClass = $this->entityClassMetadata->name; $tableName = $this->entityClassMetadata->table['name']; $tableColumns = $this->entityClassMetadata->columnNames; $str = <<<EOS La table associée à l'entité [$dataName][$entityClass] est [$tableName]" Relation dans propriétés/colonnes: EOS; foreach ($tableRelation as $propriete => $colonne) { $str .= " - " . $propriete . " => " . $colonne . " [" . $metadata->fieldMappings[$propriete]["type"] . "]" . PHP_EOL; foreach ($tableColumns as $propriete => $colonne) { $type = $this->entityClassMetadata->fieldMappings[$propriete]["type"]; $str .= " - " . $propriete . " => " . $colonne . " [" . $type . "]" . PHP_EOL; } $this->logger->debug($str); Loading
module/Import/src/Import/Service/FetcherService.php +4 −4 Changes for module/Import/src/Import/Service/FetcherService.php: 4 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -124,8 +124,8 @@ class FetcherService $_deb = microtime(true); $this->dbService->setServiceName($serviceName); $this->dbService->setEtablissement($this->etablissement); $this->dbService->deleteExistingDataForService(['id' => $sourceCode]); $this->dbService->saveEntityForService($jsonEntity, $sourceCode); $this->dbService->deleteExistingData(['id' => $sourceCode]); $this->dbService->saveEntity($jsonEntity, $sourceCode); $this->dbService->commit(); $_fin = microtime(true); $this->logger->info(sprintf("Enregistrement en base de données en %.2f secondes.", $_fin - $_deb)); Loading Loading @@ -175,8 +175,8 @@ class FetcherService $_deb = microtime(true); $this->dbService->setServiceName($serviceName); $this->dbService->setEtablissement($this->etablissement); $this->dbService->deleteExistingDataForService($filters); $this->dbService->saveEntitiesForService($jsonEntities); $this->dbService->deleteExistingData($filters); $this->dbService->saveEntities($jsonEntities); $this->dbService->commit(); $_fin = microtime(true); $this->logger->info(sprintf("Enregistrement des %d entités en %.2f secondes.", count($jsonEntities), $_fin - $_deb)); Loading
module/Import/tests/Import/Service/CallServiceTest.php +64 −1 Changes for module/Import/tests/Import/Service/CallServiceTest.php: 64 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace ImportTest\Service; use GuzzleHttp\Client; use GuzzleHttp\Exception\TransferException; use Import\Service\CallService; use stdClass; use Zend\Http\Response; use Exception; use GuzzleHttp\Exception\ClientException; Loading Loading @@ -129,6 +130,18 @@ class CallServiceTest extends \PHPUnit_Framework_TestCase $this->assertSame($this->client, $service->getClient()); } public function testCanCreateClientIfConfigIsProvided() { $service = new CallService(); $service->setConfig([ 'url' => 'url value', 'user' => 'user value', 'password' => 'password value', ]); $this->assertInstanceOf(Client::class, $service->getClient()); } public function getSendRequestPossibleError() { return [ Loading Loading @@ -160,7 +173,7 @@ class CallServiceTest extends \PHPUnit_Framework_TestCase public function testSendRequestThrowsCallExceptionWhenResponseStatusCodeIsNot200() { $response = $this->createMock(Response::class); $response->expects($this->any())->method('getStatusCode')->willReturn(123); $response->expects($this->exactly(2))->method('getStatusCode')->willReturn(123); $this->client->expects($this->once())->method('request')->willReturn($response); Loading @@ -169,4 +182,54 @@ class CallServiceTest extends \PHPUnit_Framework_TestCase $service->setLogger($this->logger); $service->get('peu/importe'); } /** * @expectedException \Import\Exception\CallException */ public function testSendRequestThrowsExceptionWhenResponseIsInvalidJSON() { $response = $this->createMock(Response::class); $response->expects($this->once())->method('getStatusCode')->willReturn(Response::STATUS_CODE_200); $response->expects($this->once())->method('getBody')->willReturn('invalid JSON'); $this->client->expects($this->once())->method('request')->willReturn($response); $service = new CallService(); $service->setClient($this->client); $service->setLogger($this->logger); $service->get('peu/importe'); } public function testCanGetVersion() { $response = $this->createMock(Response::class); $response->expects($this->once())->method('getStatusCode')->willReturn(Response::STATUS_CODE_200); $response->expects($this->once())->method('getBody')->willReturn('{"valid": "JSON"}'); $this->client->expects($this->once())->method('request')->with('GET', 'version/current')->willReturn($response); $service = new CallService(); $service->setClient($this->client); $service->setLogger($this->logger); $service->getVersion(); } public function testSendRequestReturnsJSON() { $response = $this->createMock(Response::class); $response->expects($this->once())->method('getStatusCode')->willReturn(Response::STATUS_CODE_200); $response->expects($this->once())->method('getBody')->willReturn('{"valid": "JSON"}'); $this->client->expects($this->once())->method('request')->willReturn($response); $service = new CallService(); $service->setClient($this->client); $service->setLogger($this->logger); $json = $service->get('peu/importe'); $entity = new stdClass(); $entity->valid = "JSON"; $this->assertEquals($entity, $json); } }
module/Import/tests/Import/Service/DbServiceTest.php 0 → 100644 +57 −0 Changes for module/Import/tests/Import/Service/DbServiceTest.php: 57 added lines, 0 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 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; 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); } public function testCanSaveEntity() { } }