From d5110a47fac80ab939c6ee16804b5f94530b22e9 Mon Sep 17 00:00:00 2001
From: Andy Summers <andrew.summers@wisc.edu>
Date: Tue, 9 May 2017 11:27:04 -0500
Subject: [PATCH] Define contract for creating SOAP clients with WSDLs

---
 .../edu/wisc/services/cbs/api/SoapService.php | 10 ++++-
 .../order/header/SoapOrderHeaderService.php   |  8 +++-
 .../SoapOrderInterfacingService.php           | 19 +++++---
 .../cbs/order/line/SoapOrderLineService.php   |  8 +++-
 .../order/payment/SoapOrderPaymentService.php | 19 +++++---
 .../services/cbs/price/SoapPriceService.php   | 19 +++++---
 .../cbs/product/SoapProductService.php        | 28 ++++++++++--
 .../order/header/SoapOrderHeaderServiceIT.php | 43 +++++++++++++++++++
 .../SoapOrderInterfacingServiceTest.php       |  2 +-
 9 files changed, 131 insertions(+), 25 deletions(-)
 create mode 100644 src/test/edu/wisc/services/cbs/order/header/SoapOrderHeaderServiceIT.php

diff --git a/src/main/edu/wisc/services/cbs/api/SoapService.php b/src/main/edu/wisc/services/cbs/api/SoapService.php
index a957881..026f5fc 100644
--- a/src/main/edu/wisc/services/cbs/api/SoapService.php
+++ b/src/main/edu/wisc/services/cbs/api/SoapService.php
@@ -7,13 +7,21 @@ namespace edu\wisc\services\cbs\api;
 interface SoapService extends Service
 {
 
+    /** Paths to QA CBS WSDLs */
+    const QA_HEADER = __DIR__ . '/../../../../../resources/doit_soa_order_iface_h_v2.xml';
+    const QA_LINE = __DIR__ . '/../../../../../resources/doit_soa_order_iface_l_v2.xml';
+    const QA_PAYMENT = __DIR__ . '/../../../../../resources/doit_soa_payment_v2.xml';
+    const QA_INTERFACING = __DIR__ . '/../../../../../resources/doit_soa_order_iface_i_v2.xml';
+    const QA_PRICE = __DIR__ . '/../../../../resources/doit_soa_pricing_v2.xml';
+
     /**
      * A SOAP service is constructed with a username and password. If a {@link \SoapClient} is provided, the username
      * and password are ignored.
      *
      * @param string $username  username
      * @param string $password  password
+     * @param string|null $wsdlPath path to WSDL for specifying CBS environment. Defaults to QA
      * @param \SoapClient|null $soapClient  Overriding SOAP client (for testing purposes)
      */
-    public function __construct($username, $password, \SoapClient $soapClient = null);
+    public function __construct($username, $password, $wsdlPath = null, \SoapClient $soapClient = null);
 }
diff --git a/src/main/edu/wisc/services/cbs/order/header/SoapOrderHeaderService.php b/src/main/edu/wisc/services/cbs/order/header/SoapOrderHeaderService.php
index c340902..4343084 100644
--- a/src/main/edu/wisc/services/cbs/order/header/SoapOrderHeaderService.php
+++ b/src/main/edu/wisc/services/cbs/order/header/SoapOrderHeaderService.php
@@ -20,11 +20,17 @@ class SoapOrderHeaderService implements OrderHeaderService, SoapService
     /**
      * {@inheritdoc}
      */
-    public function __construct($username, $password, \SoapClient $headerSoapClient = null)
+    public function __construct($username, $password, $wsdlPath = null, \SoapClient $headerSoapClient = null)
     {
         if ($headerSoapClient !== null) {
             $this->soapClient = $headerSoapClient;
             return;
+        } else if ($wsdlPath !== null) {
+            $this->soapClient = new DOIT_SOA_ORDER_IFACE_H_V2_Service(
+                [],
+                $wsdlPath
+            );
+            $this->soapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
         } else {
             $this->soapClient = new DOIT_SOA_ORDER_IFACE_H_V2_Service(
                 [],
diff --git a/src/main/edu/wisc/services/cbs/order/interfacing/SoapOrderInterfacingService.php b/src/main/edu/wisc/services/cbs/order/interfacing/SoapOrderInterfacingService.php
index 176ad2f..45d736c 100644
--- a/src/main/edu/wisc/services/cbs/order/interfacing/SoapOrderInterfacingService.php
+++ b/src/main/edu/wisc/services/cbs/order/interfacing/SoapOrderInterfacingService.php
@@ -16,17 +16,24 @@ class SoapOrderInterfacingService implements OrderInterfacingService, SoapServic
     /** @var DOIT_SOA_ORDER_IFACE_I_V2_Service */
     private $soapClient;
 
-    public function __construct($username, $password, \SoapClient $priceSoapClient = null)
+    public function __construct($username, $password, $wsdlPath = null, \SoapClient $priceSoapClient = null)
     {
         if ($priceSoapClient !== null) {
             $this->soapClient = $priceSoapClient;
             return;
+        } else if ($wsdlPath !== null) {
+            $this->soapClient = new DOIT_SOA_ORDER_IFACE_I_V2_Service(
+                [],
+                $wsdlPath
+            );
+            $this->soapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
+        } else {
+            $this->soapClient = new DOIT_SOA_ORDER_IFACE_I_V2_Service(
+                [],
+                __DIR__ . '/../../../../../../resources/doit_soa_order_iface_i_v2.xml'
+            );
+            $this->soapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
         }
-        $this->soapClient = new DOIT_SOA_ORDER_IFACE_I_V2_Service(
-            [],
-            __DIR__ . '/../../../../../../resources/doit_soa_order_iface_i_v2.xml'
-        );
-        $this->soapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
     }
 
     public function interfaceOrder($orderNumber)
diff --git a/src/main/edu/wisc/services/cbs/order/line/SoapOrderLineService.php b/src/main/edu/wisc/services/cbs/order/line/SoapOrderLineService.php
index 528aaf4..d367895 100644
--- a/src/main/edu/wisc/services/cbs/order/line/SoapOrderLineService.php
+++ b/src/main/edu/wisc/services/cbs/order/line/SoapOrderLineService.php
@@ -16,11 +16,17 @@ class SoapOrderLineService implements SoapService, OrderLineService
     /**
      * {@inheritdoc}
      */
-    public function __construct($username, $password, \SoapClient $lineSoapClient = null)
+    public function __construct($username, $password, $wsdlPath = null, \SoapClient $lineSoapClient = null)
     {
         if ($lineSoapClient !== null) {
             $this->soapClient = $lineSoapClient;
             return;
+        } else if ($wsdlPath !== null) {
+            $this->soapClient = new DOIT_SOA_ORDER_IFACE_L_V2_Service(
+                [],
+                $wsdlPath
+            );
+            $this->soapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
         } else {
             $this->soapClient = new DOIT_SOA_ORDER_IFACE_L_V2_Service(
                 [],
diff --git a/src/main/edu/wisc/services/cbs/order/payment/SoapOrderPaymentService.php b/src/main/edu/wisc/services/cbs/order/payment/SoapOrderPaymentService.php
index dce73ae..09564d3 100644
--- a/src/main/edu/wisc/services/cbs/order/payment/SoapOrderPaymentService.php
+++ b/src/main/edu/wisc/services/cbs/order/payment/SoapOrderPaymentService.php
@@ -21,17 +21,24 @@ class SoapOrderPaymentService implements OrderPaymentService, SoapService
     /**
      * {@inheritdoc}
      */
-    public function __construct($username, $password, \SoapClient $paymentSoapClient = null)
+    public function __construct($username, $password, $wsdlPath = null, \SoapClient $paymentSoapClient = null)
     {
         if ($paymentSoapClient !== null) {
             $this->soapClient = $paymentSoapClient;
             return;
+        } else if ($wsdlPath !== null) {
+            $this->soapClient = new DOIT_SOA_PAYMENT_V2_Service(
+                [],
+                $wsdlPath
+            );
+            $this->soapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
+        } else {
+            $this->soapClient = new DOIT_SOA_PAYMENT_V2_Service(
+                [],
+                __DIR__ . '/../../../../../../resources/doit_soa_payment_v2.xml'
+            );
+            $this->soapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
         }
-        $this->soapClient = new DOIT_SOA_PAYMENT_V2_Service(
-            [],
-            __DIR__ . '/../../../../../../resources/doit_soa_payment_v2.xml'
-        );
-        $this->soapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
     }
 
     /**
diff --git a/src/main/edu/wisc/services/cbs/price/SoapPriceService.php b/src/main/edu/wisc/services/cbs/price/SoapPriceService.php
index c980072..c78e519 100644
--- a/src/main/edu/wisc/services/cbs/price/SoapPriceService.php
+++ b/src/main/edu/wisc/services/cbs/price/SoapPriceService.php
@@ -20,17 +20,24 @@ class SoapPriceService implements PriceService, SoapService
     /**
      * @inheritdoc
      */
-    public function __construct($username, $password, \SoapClient $priceSoapClient = null)
+    public function __construct($username, $password, $wsdlPath = null, \SoapClient $priceSoapClient = null)
     {
         if ($priceSoapClient !== null) {
             $this->soapClient = $priceSoapClient;
             return;
+        } else if ($wsdlPath !== null) {
+            $this->soapClient = new DOIT_SOA_PRICING_V2_Service(
+                [],
+                $wsdlPath
+            );
+            $this->soapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
+        } else {
+            $this->soapClient = new DOIT_SOA_PRICING_V2_Service(
+                [],
+                __DIR__ . '/../../../../../resources/doit_soa_pricing_v2.xml'
+            );
+            $this->soapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
         }
-        $this->soapClient = new DOIT_SOA_PRICING_V2_Service(
-            [],
-            __DIR__ . '/../../../../../resources/doit_soa_pricing_v2.xml'
-        );
-        $this->soapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
     }
 
     /**
diff --git a/src/main/edu/wisc/services/cbs/product/SoapProductService.php b/src/main/edu/wisc/services/cbs/product/SoapProductService.php
index 95cf698..6896569 100755
--- a/src/main/edu/wisc/services/cbs/product/SoapProductService.php
+++ b/src/main/edu/wisc/services/cbs/product/SoapProductService.php
@@ -11,7 +11,7 @@ use edu\wisc\services\cbs\product\update\generated\DOIT_SOA_ITEM_UPDATE_V4_Servi
 /**
  * Class SoapTechstoreClient uses SOAP to create a product in the CBS database.
  */
-class SoapProductService implements ProductService, SoapService
+class SoapProductService implements ProductService
 {
 
     /** @var DOIT_SOA_ITEM_CREATE_V4_Service */
@@ -21,16 +21,38 @@ class SoapProductService implements ProductService, SoapService
     private $productUpdateSoapClient;
 
     /**
-     * @inheritdoc
+     * A SOAP service is constructed with a username and password. If a {@link \SoapClient} is provided, the username
+     * and password are ignored.
+     *
+     * @param string $username  username
+     * @param string $password  password
+     * @param string|null $createWsdlPath path to WSDL for specifying CBS environment. Defaults to QA
+     * @param string|null $updateWsdlPath path to WSDL for specifying CBS environment. Defaults to QA
+     * @param \SoapClient|null $productCreateSoapClient  Overriding SOAP client (for testing purposes)
+     * @param \SoapClient|null $productUpdateSoapClient  Overriding SOAP client (for testing purposes)
      */
     public function __construct(
         $username,
         $password,
+        $createWsdlPath = null,
+        $updateWsdlPath = null,
         \SoapClient $productCreateSoapClient = null,
         \SoapClient $productUpdateSoapClient = null
     )
     {
-        if ($productCreateSoapClient === null || $productUpdateSoapClient === null) {
+        if ($createWsdlPath !== null && $updateWsdlPath !== null) {
+            $this->productCreateSoapClient = new DOIT_SOA_ITEM_CREATE_V4_Service(
+                [],
+                $createWsdlPath
+            );
+            $this->productCreateSoapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken( $username, $password));
+
+            $this->productUpdateSoapClient = new DOIT_SOA_ITEM_UPDATE_V4_Service(
+                [],
+                $updateWsdlPath
+            );
+            $this->productUpdateSoapClient->__setSoapHeaders(WssSoapHeaderBuilder::buildUsernameToken($username, $password));
+        } else if ($productCreateSoapClient === null || $productUpdateSoapClient === null) {
             $this->productCreateSoapClient = new DOIT_SOA_ITEM_CREATE_V4_Service(
                 [],
                 __DIR__ . '/../../../../../resources/doit_soa_item_create_v4.xml'
diff --git a/src/test/edu/wisc/services/cbs/order/header/SoapOrderHeaderServiceIT.php b/src/test/edu/wisc/services/cbs/order/header/SoapOrderHeaderServiceIT.php
new file mode 100644
index 0000000..a0b428e
--- /dev/null
+++ b/src/test/edu/wisc/services/cbs/order/header/SoapOrderHeaderServiceIT.php
@@ -0,0 +1,43 @@
+<?php
+
+use \edu\wisc\services\cbs\api\SoapService;
+use \edu\wisc\services\cbs\order\header\SoapOrderHeaderService;
+use \edu\wisc\services\cbs\IntegrationTestCase;
+
+/**
+ * Integration tests to test the various methods of instantiating a {@link SoapOrderHeaderService}.
+ * A {@link SoapOrderHeaderService} can be instantiated using:
+ *
+ * 1. Only username and password
+ * 2. Username, password and WSDL to specify a CBS environment
+ * 3. Username, password and instance of {@link MockOrderHeaderServiceClient}
+ */
+class SoapOrderHeaderServiceIT extends IntegrationTestCase
+{
+
+    /**
+     * @test
+     */
+    public function createsClientWithOnlyUsernameAndPassword()
+    {
+        $soapOrderHeaderService = new SoapOrderHeaderService(
+            static::$itData['cbs.username'],
+            static::$itData['cbs.password']
+        );
+        static::assertNotNull($soapOrderHeaderService);
+    }
+
+    /**
+     * @test
+     */
+    public function createsClientWithWsdl()
+    {
+        $soapOrderHeaderService = new SoapOrderHeaderService(
+            static::$itData['cbs.username'],
+            static::$itData['cbs.password'],
+            SoapService::QA_HEADER
+        );
+        static::assertNotNull($soapOrderHeaderService);
+    }
+
+}
\ No newline at end of file
diff --git a/src/test/edu/wisc/services/cbs/order/interfacing/SoapOrderInterfacingServiceTest.php b/src/test/edu/wisc/services/cbs/order/interfacing/SoapOrderInterfacingServiceTest.php
index 54a30ee..0747c09 100644
--- a/src/test/edu/wisc/services/cbs/order/interfacing/SoapOrderInterfacingServiceTest.php
+++ b/src/test/edu/wisc/services/cbs/order/interfacing/SoapOrderInterfacingServiceTest.php
@@ -23,7 +23,7 @@ class SoapOrderInterfacingServiceTest extends \PHPUnit_Framework_TestCase
     protected function setUp()
     {
         $this->mockSoapClient = \Mockery::mock(DOIT_SOA_ORDER_IFACE_I_V2_Service::class);
-        $this->orderInterfacingService = new SoapOrderInterfacingService('', '', $this->mockSoapClient);
+        $this->orderInterfacingService = new SoapOrderInterfacingService('', '', null, $this->mockSoapClient);
     }
 
     /** Close/verify mock after each test */
-- 
GitLab