From 278d482f418b666da1c65531c1b4b34a07c6cde8 Mon Sep 17 00:00:00 2001
From: lbrun <leo@sandbox.pergame.net>
Date: Thu, 26 Nov 2015 12:04:58 +0100
Subject: [PATCH] dev#25892_module_de_demande_de_consultation_sur_place: create
 request ws (getASRServicePoint / getASRItems)

---
 .../WebService/SIGB/VSmart/AvailableItem.php  | 60 ++++++++++++++++++
 ...emsReader.php => AvailableItemsReader.php} | 36 +++++------
 .../Class/WebService/SIGB/VSmart/Service.php  | 24 +++----
 .../{Consultation => VSmart}/ServicePoint.php | 10 ++-
 ...intsReader.php => ServicePointsReader.php} | 39 ++++--------
 .../Class/WebService/SIGB/VSmartFixtures.php  |  6 +-
 .../Class/WebService/SIGB/VSmartTest.php      | 62 +++++++++++++++----
 7 files changed, 152 insertions(+), 85 deletions(-)
 create mode 100644 library/Class/WebService/SIGB/VSmart/AvailableItem.php
 rename library/Class/WebService/SIGB/VSmart/{ConsultableItemsReader.php => AvailableItemsReader.php} (67%)
 rename library/Class/WebService/SIGB/{Consultation => VSmart}/ServicePoint.php (92%)
 rename library/Class/WebService/SIGB/VSmart/{ConsultableServicePointsReader.php => ServicePointsReader.php} (57%)

diff --git a/library/Class/WebService/SIGB/VSmart/AvailableItem.php b/library/Class/WebService/SIGB/VSmart/AvailableItem.php
new file mode 100644
index 00000000000..38485d6d606
--- /dev/null
+++ b/library/Class/WebService/SIGB/VSmart/AvailableItem.php
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class Class_WebService_SIGB_VSmart_AvailableItem {
+  protected
+    $_barcode,
+    $_location,
+    $_service_points;
+
+  public function setBarcode($barcode) {
+    $this->_barcode = $barcode;
+    return $this;
+  }
+
+
+  public function setLocation($location) {
+    $this->_location = $location;
+    return $this;
+  }
+
+
+  public function addServicePoint($code) {
+    $this->_service_points[] = (new Class_WebService_SIGB_VSmart_ServicePoint())->setCode($code);
+  }
+
+
+  public function getBarcode() {
+    return $this->_barcode;
+  }
+
+
+  public function getLocation() {
+    return $this->_location;
+  }
+
+
+  public function getServicePoints() {
+    return $this->_service_points;
+  }
+}
+?>
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/VSmart/ConsultableItemsReader.php b/library/Class/WebService/SIGB/VSmart/AvailableItemsReader.php
similarity index 67%
rename from library/Class/WebService/SIGB/VSmart/ConsultableItemsReader.php
rename to library/Class/WebService/SIGB/VSmart/AvailableItemsReader.php
index 2030e6a101d..ab6419ad047 100644
--- a/library/Class/WebService/SIGB/VSmart/ConsultableItemsReader.php
+++ b/library/Class/WebService/SIGB/VSmart/AvailableItemsReader.php
@@ -20,46 +20,38 @@
  */
 
 
-class Class_WebService_SIGB_VSmart_ConsultableItemsReader {
-  protected $_barcode = '';
-  protected $_location = '';
-  protected $_servicepoint = '';
+class Class_WebService_SIGB_VSmart_AvailableItemsReader {
+  protected $_items = [];
+  protected $_current_item;
 
 
-  public static function newInstanceParse($xml) {
-    $instance = new self();
-    return $instance->parse($xml);
-  }
-
-
-  public function parse($xml) {
+  public function getItemsFromXML($xml) {
     $this->_xml_parser = Class_WebService_XMLParser::newInstance()
                           ->setElementHandler($this)
                           ->parse($xml);
 
-    return $this;
+    return $this->_items;
   }
 
 
-  public function endBarcode($data) {
-    $this->_barcode = $data;
+  public function startItemResults() {
+    $this->_current_item = new Class_WebService_SIGB_VSmart_AvailableItem();
+    $this->_items[] = $this->_current_item;
   }
 
 
-  public function endLocation($data) {
-    $this->_location = $data;
+  public function endBarcode($data) {
+    $this->_current_item->setBarcode($data);
   }
 
 
-  public function endServicePoint($data) {
-    $this->_servicepoint = $data;
+  public function endLocation($data) {
+    $this->_current_item->setLocation($data);
   }
 
 
-  public function getItems() {
-    return ['barcode' => $this->_barcode,
-            'location' => $this->_location,
-            'servicepoint' => $this->_servicepoint];
+  public function endServicePoint($data) {
+    $this->_current_item->addServicePoint($data);
   }
 }
 
diff --git a/library/Class/WebService/SIGB/VSmart/Service.php b/library/Class/WebService/SIGB/VSmart/Service.php
index fd192b29a55..e2f30b8258e 100644
--- a/library/Class/WebService/SIGB/VSmart/Service.php
+++ b/library/Class/WebService/SIGB/VSmart/Service.php
@@ -27,6 +27,7 @@ class Class_WebService_SIGB_VSmart_Service extends Class_WebService_SIGB_Abstrac
   protected $_soap_client;
   protected $_authenticate_wsdl;
   protected $_popup_root;
+  protected $_language = 'fre';
 
   public static function newInstance() {
     return new self();
@@ -227,28 +228,23 @@ class Class_WebService_SIGB_VSmart_Service extends Class_WebService_SIGB_Abstrac
   }
 
 
-  public function getConsultItems($id) {
+  public function getAvailableItems($id) {
     $ids = explode('/', $id);
     $xml = $this->httpGet(['fu' => 'GetASRItems',
                            'database' => $ids[0],
-                           'bibrecord' => $ids[1]]);
+                           'bibrecord' => $ids[1],
+                           'language' => $this->_language]);
 
-    $items = Class_WebService_SIGB_VSmart_ConsultableItemsReader
-      ::newInstanceParse($xml)
-      ->getItems();
-
-    return $items;
+    return (new Class_WebService_SIGB_VSmart_AvailableItemsReader())->getItemsFromXML($xml);
   }
 
 
-  public function getConsultServicePoints() {
-    $xml = $this->httpGet(['fu' => 'GetASRServicePoints']);
-
-    $service_points = Class_WebService_SIGB_VSmart_ConsultableServicePointsReader
-      ::newInstanceParse($xml)
-      ->getServicePoints();
+  public function getServicePoints() {
+    $xml = $this->httpGet(['fu' => 'GetASRServicePoints',
+                           'language' => $this->_language]);
 
-    return $service_points;
+    return (new Class_WebService_SIGB_VSmart_ServicePointsReader())
+      ->getServicePointsFromXML($xml);
   }
 }
 
diff --git a/library/Class/WebService/SIGB/Consultation/ServicePoint.php b/library/Class/WebService/SIGB/VSmart/ServicePoint.php
similarity index 92%
rename from library/Class/WebService/SIGB/Consultation/ServicePoint.php
rename to library/Class/WebService/SIGB/VSmart/ServicePoint.php
index 9cd3c342e0e..feef5d38927 100644
--- a/library/Class/WebService/SIGB/Consultation/ServicePoint.php
+++ b/library/Class/WebService/SIGB/VSmart/ServicePoint.php
@@ -20,11 +20,10 @@
  */
 
 
-class Class_WebService_SIGB_Consultation_ServicePoint {
-
-  protected $_code;
-  protected $_label;
-
+class Class_WebService_SIGB_VSmart_ServicePoint {
+  protected
+    $_code,
+    $_label;
 
   public function setCode($code) {
     $this->_code = $code;
@@ -47,5 +46,4 @@ class Class_WebService_SIGB_Consultation_ServicePoint {
     return $this->_label;
   }
 }
-
 ?>
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/VSmart/ConsultableServicePointsReader.php b/library/Class/WebService/SIGB/VSmart/ServicePointsReader.php
similarity index 57%
rename from library/Class/WebService/SIGB/VSmart/ConsultableServicePointsReader.php
rename to library/Class/WebService/SIGB/VSmart/ServicePointsReader.php
index f759a5ea225..4f0efd5e065 100644
--- a/library/Class/WebService/SIGB/VSmart/ConsultableServicePointsReader.php
+++ b/library/Class/WebService/SIGB/VSmart/ServicePointsReader.php
@@ -20,49 +20,34 @@
  */
 
 
-class Class_WebService_SIGB_VSmart_ConsultableServicePointsReader {
+class Class_WebService_SIGB_VSmart_ServicePointsReader {
   protected
-    $_xml_parser,
-    $_service_point,
-    $_current_item;
+    $_service_points,
+    $_current_service;
 
 
-  public static function newInstance() {
-    return new self();
-  }
-
-
-  public function getServicePointFromXML($xml) {
-    $this->_xml_parser = Class_WebService_XMLParser::newInstance()
-                          ->setElementHandler($this)
-                          ->parse($xml);
+  public function getServicePointsFromXML($xml) {
+    Class_WebService_XMLParser::newInstance()
+      ->setElementHandler($this)
+      ->parse($xml);
 
-    return $this->_service_point;
+    return $this->_service_points;
   }
 
 
   public function startServicePoint() {
-    $_this->_current_item = new Class_WebService_SIGB_Consultation_ServicePoint();
-  }
-
-
-  public function endServicePoint($data) {
-    $this->_servicepoint = $data;
+    $this->_current_service = new Class_WebService_SIGB_VSmart_ServicePoint();
+    $this->_service_points[] = $this->_current_service;
   }
 
 
   public function endServicePointCode($data) {
-    $this->_servicepoints['code'] = $data;
+    $this->_current_service->setCode($data);
   }
 
 
   public function endWording($data) {
-    $this->_servicepoints['label'] = $data;
-  }
-
-
-  public function getServicePoints() {
-    return var_dump($this->_servicepoints);
+    $this->_current_service->setLabel($data);
   }
 }
 
diff --git a/tests/library/Class/WebService/SIGB/VSmartFixtures.php b/tests/library/Class/WebService/SIGB/VSmartFixtures.php
index 24a5a5ee0f7..67e6ccc2952 100644
--- a/tests/library/Class/WebService/SIGB/VSmartFixtures.php
+++ b/tests/library/Class/WebService/SIGB/VSmartFixtures.php
@@ -328,7 +328,7 @@ class VSmartFixtures {
   }
 
 
-  public static function xmlConsultableItems() {
+  public static function xmlAvailableItems() {
       return
          '<VubisSmart>
               <Header>
@@ -345,7 +345,7 @@ class VSmartFixtures {
   }
 
 
-  public static function xmlConsultableServicePoints() {
+  public static function xmlServicePoints() {
       return
          '<VubisSmart>
               <Header>
@@ -354,7 +354,7 @@ class VSmartFixtures {
               </Header>
               <ServicePoint>
                   <ServicePointCode>RES_Liv</ServicePointCode>
-                  <Wording>Livraison</Wording>
+                  <Wording>Salle patrimoine</Wording>
               </ServicePoint>
               <ServicePoint>
                   <ServicePointCode>RES_Mag</ServicePointCode>
diff --git a/tests/library/Class/WebService/SIGB/VSmartTest.php b/tests/library/Class/WebService/SIGB/VSmartTest.php
index f1015ebf1f3..cd7c36a300d 100644
--- a/tests/library/Class/WebService/SIGB/VSmartTest.php
+++ b/tests/library/Class/WebService/SIGB/VSmartTest.php
@@ -693,60 +693,96 @@ class VSmartServiceFunctionsTest extends Storm_Test_ModelTestCase {
 
 
 
-class VSmartServiceConsultableItemsTest extends  VSmartServiceTestCase {
+class VSmartServiceAvailableItemsTest extends  VSmartServiceTestCase {
   public function setUp() {
     parent::setUp();
     $mock_web_client = $this->mock()
       ->whenCalled('open_url')
       ->with('http://46.20.169.8/production/VubisSmartHttpApi.csp?fu=GetASRItems&database=1&bibrecord=179920')
-      ->answers(VSmartFixtures::xmlConsultableItems());
+      ->answers(VSmartFixtures::xmlAvailableItems());
 
     $this->items = Class_WebService_SIGB_VSmart_Service::newInstance()
       ->setServerRoot('http://46.20.169.8/production')
       ->setWebClient($mock_web_client)
-      ->getConsultItems('1/179920');
+      ->getAvailableItems('1/179920');
   }
 
 
   /** @test */
   public function getBarcodeShouldReturnMCP271095() {
-    $this->assertEquals('MCP271095', $this->items['barcode']);
+    $this->assertEquals('MCP271095', $this->items[0]->getBarcode());
   }
 
 
   /** @test */
-  public function getServicePointShouldBeRES_Liv() {
-    $this->assertEquals('RES_Liv', $this->items['servicepoint']);
+  public function getLocationShouldBeRESMCFP() {
+    $this->assertEquals('RES/MCFP [MPBOU] BP-2430', $this->items[0]->getLocation());
   }
 }
 
 
 
 
-class VSmartServiceConsultableServicePointsTest extends  VSmartServiceTestCase {
+class VSmartServicePlaceRequestTest extends  VSmartServiceTestCase {
   public function setUp() {
     parent::setUp();
     $mock_web_client = $this->mock()
       ->whenCalled('open_url')
-      ->with('http://46.20.169.8/production/VubisSmartHttpApi.csp?fu=GetASRServicePoints')
-      ->answers(VSmartFixtures::xmlConsultableServicePoints());
+      ->with('http://46.20.169.8/production/VubisSmartHttpApi.csp?fu=GetASRItems&database=1&bibrecord=179920&language=fre')
+      ->answers(VSmartFixtures::xmlAvailableItems());
 
-    $this->servicepoints = Class_WebService_SIGB_VSmart_Service::newInstance()
+    $this->items = Class_WebService_SIGB_VSmart_Service::newInstance()
+      ->setServerRoot('http://46.20.169.8/production')
+      ->setWebClient($mock_web_client)
+      ->getAvailableItems('1/179920');
+  }
+
+
+  /** @test */
+  public function getBarcodeShouldReturnMCP271095() {
+    $this->assertEquals('MCP271095', $this->items[0]->getBarcode());
+  }
+
+
+  /** @test */
+  public function getLocationShouldReturnRESMCFP() {
+    $this->assertEquals('RES/MCFP [MPBOU] BP-2430', $this->items[0]->getLocation());
+  }
+
+
+  /** @test */
+  public function getFirstItemServicePointCodeShouldReturnRES_Liv() {
+    $this->assertEquals('RES_Liv', $this->items[0]->getServicePoints()[0]->getCode());
+  }
+}
+
+
+
+
+class VSmartServiceAvailableServicePointsTest extends  VSmartServiceTestCase {
+  public function setUp() {
+    parent::setUp();
+    $mock_web_client = $this->mock()
+      ->whenCalled('open_url')
+      ->with('http://46.20.169.8/production/VubisSmartHttpApi.csp?fu=GetASRServicePoints&language=fre')
+      ->answers(VSmartFixtures::xmlServicePoints());
+
+    $this->service_points = Class_WebService_SIGB_VSmart_Service::newInstance()
       ->setServerRoot('http://46.20.169.8/production')
       ->setWebClient($mock_web_client)
-      ->getConsultServicePoints();
+      ->getServicePoints();
   }
 
 
   /** @test */
   public function getFirstServicePointCodeShouldReturnRES_Liv() {
-    $this->assertEquals('RES_Liv', $this->servicepoints[0]['code']);
+    $this->assertEquals('RES_Liv', $this->service_points[0]->getCode());
   }
 
 
   /** @test */
   public function getSecondServicePointLabelShouldBeMagasin() {
-    $this->assertEquals('Magasin', $this->servicepoints[1]['label']);
+    $this->assertEquals('Magasin', $this->service_points[1]->getLabel());
   }
 }
 ?>
\ No newline at end of file
-- 
GitLab