From 08ac1a25dd75254976548ecb8b7bfe77fb712028 Mon Sep 17 00:00:00 2001
From: llaffont <laurent.laffont@gmail.com>
Date: Mon, 3 Mar 2014 15:31:58 +0100
Subject: [PATCH] SIGB Orphee: get serial records & status #12327

---
 .../Class/WebService/SIGB/Orphee/Service.php  |  31 ++--
 .../Class/WebService/SIGB/OrpheeFixtures.php  |   5 +
 .../WebService/SIGB/OrpheeServiceTest.php     | 150 +++++++++++++++---
 3 files changed, 158 insertions(+), 28 deletions(-)

diff --git a/library/Class/WebService/SIGB/Orphee/Service.php b/library/Class/WebService/SIGB/Orphee/Service.php
index 389c2f8273f..080ac21100b 100644
--- a/library/Class/WebService/SIGB/Orphee/Service.php
+++ b/library/Class/WebService/SIGB/Orphee/Service.php
@@ -239,17 +239,30 @@ class Class_WebService_SIGB_Orphee_Service extends Class_WebService_SIGB_Abstrac
 	 }
 
 
-	 public function getNotice($id) {
-		 $id = $this->removeOrpheeNoticePrefix($id);
-		 $result = $this->getSearchClient()->GetLstDmt(GetLstDmt::withNtcAndFas($id, 0));
-		 $xml = $result->getXml();
-		 $notice = Class_WebService_SIGB_Orphee_GetLstDmtResponseReader::newInstance()
-			 ->getNoticeFromXML($xml);
 
-		 if ($notice)
-			 $this->cacheNotice($notice);
+	public function getExemplaire($notice_id, $code_barre){
+		if (!$item = Class_Exemplaire::findFirstBy(['code_barres' => $code_barre]))
+			return new Class_WebService_SIGB_Exemplaire(null);
+
+		if (!$notice = $item->getNotice())
+			return new Class_WebService_SIGB_Exemplaire(null);
+			
+		$tome = $notice->isPeriodique() ? (int)$notice->getTomeAlpha() : 0;
+		$id = $this->removeOrpheeNoticePrefix($notice_id);
+
+		$result = $this->getSearchClient()->GetLstDmt(GetLstDmt::withNtcAndFas($id, $tome));
+		$xml = $result->getXml();
+		$notice = Class_WebService_SIGB_Orphee_GetLstDmtResponseReader::newInstance()->getNoticeFromXML($xml);
+		if (!isset($notice) || ($exemplaire = $notice->getExemplaireByCodeBarre($code_barre)) == null){
+			$exemplaire = new Class_WebService_SIGB_Exemplaire(null);
+		}
+		return $exemplaire;
+	}
 
-		 return $notice;
+
+	 public function getNotice($id) {
+		 // disabled - see getExemplaire
+		 return new Class_WebService_SIGB_Notice($id);
 	 }
 
 
diff --git a/tests/library/Class/WebService/SIGB/OrpheeFixtures.php b/tests/library/Class/WebService/SIGB/OrpheeFixtures.php
index b8cebf9bd73..aed90d90baf 100644
--- a/tests/library/Class/WebService/SIGB/OrpheeFixtures.php
+++ b/tests/library/Class/WebService/SIGB/OrpheeFixtures.php
@@ -258,6 +258,11 @@ class OrpheeFixtures {
 	}
 
 
+	public static function xmlGetLstDmtPhosphoneNo5632() {
+		return '<?xml version="1.0" encoding="utf-8"?><datas><documents><document><no>183771</no><ntc>471991999</ntc><carte>00106001837710</carte><sit>1</sit><lib_sit>  en rayon</lib_sit><loc>12</loc><lib_loc>  Rez-de-Chaus</lib_loc><loc_ori>12</loc_ori><lib_loc_ori>  Rez-de-Chaus</lib_loc_ori><cote></cote><anx_ori>1</anx_ori><lib_anx_ori>  Médiathèque Saint Omer</lib_anx_ori><anx_cur>1</anx_cur><lib_anx_cur>  Médiathèque Saint Omer</lib_anx_cur><anx_nxt>1</anx_nxt><lib_anx_nxt>  Médiathèque Saint Omer</lib_anx_nxt><no_coll>1</no_coll><site>106</site><lib_site>  Saint Omer</lib_site><sup>112</sup><lib_sup>  Périodique</lib_sup><sec>9</sec><lib_sec>  Accueil</lib_sec><uti>2</uti><lib_uti>  prêt normal</lib_uti><sta1>101</sta1><lib_sta1>  Périodique adulte</lib_sta1><sta2>0</sta2><lib_sta2></lib_sta2><sta3>0</sta3><lib_sta3></lib_sta3><date_last_pret></date_last_pret><date_last_retour></date_last_retour><titre>Phosphore</titre><date_edi></date_edi></document></documents></datas>';
+	}
+
+
 	public static function xmlGetLstDmtVagabondEnArchivage() {
 		return '<![CDATA[<?xml version="1.0" encoding="utf-8"?><datas><documents><document><no><![CDATA[137520]]]]>><![CDATA[</no><ntc><![CDATA[1300201571]]]]>><![CDATA[</ntc><carte><![CDATA[00106001375208]]]]>><![CDATA[</carte><sit><![CDATA[14]]]]>><![CDATA[</sit><lib_sit><![CDATA[  en Archivage]]]]>><![CDATA[</lib_sit><loc><![CDATA[5]]]]>><![CDATA[</loc><lib_loc><![CDATA[  1er étage]]]]>><![CDATA[</lib_loc><loc_ori><![CDATA[5]]]]>><![CDATA[</loc_ori><lib_loc_ori><![CDATA[  1er étage]]]]>><![CDATA[</lib_loc_ori><cote><![CDATA[BD INO]]]]>><![CDATA[</cote><anx_ori><![CDATA[1]]]]>><![CDATA[</anx_ori><lib_anx_ori><![CDATA[  Médiathèque Saint Omer]]]]>><![CDATA[</lib_anx_ori><anx_cur><![CDATA[1]]]]>><![CDATA[</anx_cur><lib_anx_cur><![CDATA[  Médiathèque Saint Omer]]]]>><![CDATA[</lib_anx_cur><anx_nxt><![CDATA[1]]]]>><![CDATA[</anx_nxt><lib_anx_nxt><![CDATA[  Médiathèque Saint Omer]]]]>><![CDATA[</lib_anx_nxt><no_coll><![CDATA[0]]]]>><![CDATA[</no_coll><site><![CDATA[106]]]]>><![CDATA[</site><lib_site><![CDATA[  Saint Omer]]]]>><![CDATA[</lib_site><sup><![CDATA[108]]]]>><![CDATA[</sup><lib_sup><![CDATA[  Livre]]]]>><![CDATA[</lib_sup><sec><![CDATA[2]]]]>><![CDATA[</sec><lib_sec><![CDATA[  Adultes]]]]>><![CDATA[</lib_sec><uti><![CDATA[2]]]]>><![CDATA[</uti><lib_uti><![CDATA[  prêt normal]]]]>><![CDATA[</lib_uti><sta1><![CDATA[97]]]]>><![CDATA[</sta1><lib_sta1><![CDATA[  fiction adulte]]]]>><![CDATA[</lib_sta1><sta2><![CDATA[98]]]]>><![CDATA[</sta2><lib_sta2><![CDATA[  BD]]]]>><![CDATA[</lib_sta2><sta3><![CDATA[103]]]]>><![CDATA[</sta3><lib_sta3><![CDATA[  Manga]]]]>><![CDATA[</lib_sta3><date_last_pret><![CDATA[]]]]>><![CDATA[</date_last_pret><date_last_retour><![CDATA[]]]]>><![CDATA[</date_last_retour><titre><![CDATA[Vagabond (Inoue, Takehiko (1967-....))]]]]>><![CDATA[</titre><date_edi><![CDATA[2006]]]]>><![CDATA[</date_edi></document></documents></datas>]]>';
 	}
diff --git a/tests/library/Class/WebService/SIGB/OrpheeServiceTest.php b/tests/library/Class/WebService/SIGB/OrpheeServiceTest.php
index da1f0ed0d8f..72b973709e3 100644
--- a/tests/library/Class/WebService/SIGB/OrpheeServiceTest.php
+++ b/tests/library/Class/WebService/SIGB/OrpheeServiceTest.php
@@ -243,12 +243,28 @@ class OrpheeServiceTestGetLstDmntWithMillenium extends OrpheeServiceTestCase {
 
 	public function setUp() {
 		parent::setUp();
+
+		$this->fixture('Class_Notice',
+									 ['id' => 34,
+										'tome_alpha' => '',
+										'exemplaires' => [
+											$this->fixture('Class_Exemplaire',
+																		 ['id' => 1,
+																			'code_barres' => '00106001488142',
+																			'id_origine' => 'frOr1301700727']),
+											
+											$this->fixture('Class_Exemplaire',
+																		 ['id' => 2,
+																			'code_barres' => '00106001488155',
+																			'id_origine' => 'frOr1301700727'])
+											]
+										 ]);
+
+
 		$this->_search_client
 			->whenCalled('GetLstDmt')
 			->with(GetLstDmt::withNtcAndFas('1301700727', 0))
 			->answers(GetLstDmtResponse::withResult(OrpheeFixtures::xmlGetLstDmtMillenium()));
-		
-		$this->millenium = $this->_orphee->getNotice('frOr1301700727');
 	}
 
 
@@ -267,15 +283,9 @@ class OrpheeServiceTestGetLstDmntWithMillenium extends OrpheeServiceTestCase {
 	}
 
 
-	/** @test */
-	public function milleniumShouldBeAnInstanceOfClass_WebService_SIGB_Notice() {
-		$this->assertInstanceOf('Class_WebService_SIGB_Notice', $this->millenium);
-	}
-
-
 	/** @test */
 	public function exemplaireByCodeBarre00106001488142ShouldNotBeEmpty() {
-		$exemplaire = $this->millenium->getExemplaireByCodeBarre('00106001488142');
+		$exemplaire = $this->_orphee->getExemplaire('frOr1301700727', '00106001488142');
 		$this->assertNotEmpty($exemplaire);
 		return $exemplaire;
 	}
@@ -328,7 +338,7 @@ class OrpheeServiceTestGetLstDmntWithMillenium extends OrpheeServiceTestCase {
 	
 	/** @test */
 	public function exemplaireByCodeBarre00106001488155ShouldNotBeEmpty() {
-		$exemplaire = $this->millenium->getExemplaireByCodeBarre('00106001488155');
+		$exemplaire = $this->_orphee->getExemplaire('frOr1301700727', '00106001488155');
 		$this->assertNotEmpty($exemplaire);
 		return $exemplaire;
 	}
@@ -368,18 +378,28 @@ class OrpheeServiceTestGetLstDmntWithMillenium extends OrpheeServiceTestCase {
 class OrpheeServiceTestGetLstDmntWithLivreEspagnol extends OrpheeServiceTestCase {
 	public function setUp() {
 		parent::setUp();
+
+		$this->fixture('Class_Notice',
+									 ['id' => 34,
+										'tome_alpha' => '',
+										'exemplaires' => [
+											$this->fixture('Class_Exemplaire',
+																		 ['id' => 1,
+																			'code_barres' => 'Ancien-07086',
+																			'id_origine' => 'frOr0030008850'])
+											]
+										 ]);
+
 		$this->_search_client
 			->whenCalled('GetLstDmt')
 			->with(GetLstDmt::withNtcAndFas('0030008850', 0))
 			->answers(GetLstDmtResponse::withResult(OrpheeFixtures::xmlGetLstDmtLivreEspagnol()));
-
-		$this->notice = $this->_orphee->getNotice('frOr0030008850');
 	}
 
 
 	/** @test */
 	public function exemplaireByCodeBarreAncien_07086ShouldNotBeEmpty() {
-		$exemplaire = array_first($this->notice->getExemplaires());
+		$exemplaire = $this->_orphee->getExemplaire('frOr0030008850', 'Ancien-07086');
 		$this->assertEquals('Ancien-07086', $exemplaire->getCodeBarre());
 	}
 }
@@ -390,18 +410,65 @@ class OrpheeServiceTestGetLstDmntWithLivreEspagnol extends OrpheeServiceTestCase
 class OrpheeServiceTestGetLstDmntLAmourDansLeSangReserve extends OrpheeServiceTestCase {
 	public function setUp() {
 		parent::setUp();
+
+		$this->fixture('Class_Notice',
+									 ['id' => 34,
+										'tome_alpha' => '1',
+										'type_doc' => 1,
+										'exemplaires' => [
+											$this->fixture('Class_Exemplaire',
+																		 ['id' => 1,
+																			'code_barres' => '00106001334221',
+																			'id_origine' => 'frOr1300802087'])]]);
 		$this->_search_client
 			->whenCalled('GetLstDmt')
 			->with(GetLstDmt::withNtcAndFas('1300802087', 0))
 			->answers(GetLstDmtResponse::withResult(OrpheeFixtures::xmlGetLstDmtLAmourDansLeSangReserve()));
 
-		$this->notice = $this->_orphee->getNotice('frOr1300802087');
 	}
 
 
 	/** @test */
 	public function exemplaireShouldBeReservable() {
-		$this->assertTrue(array_first($this->notice->getExemplaires())->isReservable());
+		$exemplaire = $this->_orphee->getExemplaire('frOr1300802087', '00106001334221');
+		$this->assertTrue($exemplaire->isReservable());
+	}
+}
+
+
+
+
+class OrpheeServiceTestGetLstDmntPhosphoreNo5632 extends OrpheeServiceTestCase {
+	public function setUp() {
+		parent::setUp();
+
+		$this->fixture('Class_Notice',
+									 ['id' => 34,
+										'tome_alpha' => '5632',
+										'type_doc' => Class_TypeDoc::PERIODIQUE,
+										'exemplaires' => [
+											$this->fixture('Class_Exemplaire',
+																		 ['id' => 1,
+																			'code_barres' => '00106001837710',
+																			'id_origine' => 'frOr471991999'])]]);
+		$this->_search_client
+			->whenCalled('GetLstDmt')
+			->with(GetLstDmt::withNtcAndFas('471991999', '5632'))
+			->answers(GetLstDmtResponse::withResult(OrpheeFixtures::xmlGetLstDmtPhosphoneNo5632()));
+
+		$this->exemplaire = $this->_orphee->getExemplaire('frOr471991999', '00106001837710');
+	}
+
+
+	/** @test */
+	public function exemplaireShouldBeNotReservable() {
+		$this->assertFalse($this->exemplaire->isReservable());
+	}
+
+
+	/** @test */
+	public function disponibiliteShouldBeEnRayon() {
+		$this->assertEquals('en rayon', $this->exemplaire->getDisponibilite());
 	}
 }
 
@@ -411,6 +478,17 @@ class OrpheeServiceTestGetLstDmntLAmourDansLeSangReserve extends OrpheeServiceTe
 class OrpheeServiceTestGetLstDmntVagabondEnArchivage extends OrpheeServiceTestCase {
 	public function setUp() {
 		parent::setUp();
+
+		$this->fixture('Class_Notice',
+									 ['id' => 34,
+										'tome_alpha' => '',
+										'exemplaires' => [
+											$this->fixture('Class_Exemplaire',
+																		 ['id' => 1,
+																			'code_barres' => '00106001375208',
+																			'id_origine' => 'frOr1300201571'])]
+										 ]);
+
 		$this->_search_client
 			->whenCalled('GetLstDmt')
 			->with(GetLstDmt::withNtcAndFas('1300201571', 0))
@@ -420,8 +498,8 @@ class OrpheeServiceTestGetLstDmntVagabondEnArchivage extends OrpheeServiceTestCa
 
 	/** @test */
 	public function exemplaireShouldNotBeVisible() {
-		$notice = $this->_orphee->getNotice('frOr1300201571');
-		$this->assertFalse(array_first($notice->getExemplaires())->isVisibleOPAC());
+		$exemplaire = $this->_orphee->getExemplaire('frOr1300201571', '00106001375208');
+		$this->assertFalse($exemplaire->isVisibleOPAC());
 	}
 }
 
@@ -431,6 +509,16 @@ class OrpheeServiceTestGetLstDmntVagabondEnArchivage extends OrpheeServiceTestCa
 class OrpheeServiceTestGetLstDmntMetamausEnCatalogage extends OrpheeServiceTestCase {
 	public function setUp() {
 		parent::setUp();
+		$this->fixture('Class_Notice',
+									 ['id' => 34,
+										'tome_alpha' => '',
+										'exemplaires' => [
+											$this->fixture('Class_Exemplaire',
+																		 ['id' => 1,
+																			'code_barres' => '00106001990790',
+																			'id_origine' => 'frOr1314910159'])]
+										 ]);
+
 		$this->_search_client
 			->whenCalled('GetLstDmt')
 			->with(GetLstDmt::withNtcAndFas('1314910159', 0))
@@ -440,8 +528,32 @@ class OrpheeServiceTestGetLstDmntMetamausEnCatalogage extends OrpheeServiceTestC
 
 	/** @test */
 	public function exemplaireShouldNotBeVisible() {
-		$notice = $this->_orphee->getNotice('frOr1314910159');
-		$this->assertFalse(array_first($notice->getExemplaires())->isVisibleOPAC());
+		$exemplaire = $this->_orphee->getExemplaire('frOr1314910159', '00106001990790');
+		$this->assertFalse($exemplaire->isVisibleOPAC());
+	}
+}
+
+
+
+
+class OrpheeServiceTestGetLstDmntErrorsTest extends OrpheeServiceTestCase {
+	/** @test */
+	public function onInexistingExemplaireShouldReturnEmptyExemplaire() {
+		Class_Notice::beVolatile();
+		Class_Exemplaire::beVolatile();
+
+		$this->assertFalse($this->_orphee->getExemplaire('frOr12334', '123')->isReservable());
+	}
+
+
+	/** @test */
+	public function onInexistingNoticeShouldReturnEmptyExemplaire() {
+		Class_Notice::beVolatile();
+		$this->fixture('Class_Exemplaire',
+									 ['id' => 3,
+										'code_barres' => '123']);
+
+		$this->assertFalse($this->_orphee->getExemplaire('frOr12334', '123')->isReservable());
 	}
 }
 
-- 
GitLab