From 8861ffbdf1c1a7c8246b9c4c62a7c08fe4ad5672 Mon Sep 17 00:00:00 2001
From: Andrew Hoffmann <andrew.hoffmann@wisc.edu>
Date: Wed, 8 Jul 2015 15:11:44 -0500
Subject: [PATCH] Added the ping() method to RpcNetidClientSoap

ACCTREC-299
---
 src/main/edu/wisc/doit/RpcNetidClient.php     |  8 ++++
 src/main/edu/wisc/doit/RpcNetidClientSoap.php | 20 ++++++++-
 src/test/RpcNetidClientSoapTest.php           | 41 +++++++++++++++++++
 3 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/src/main/edu/wisc/doit/RpcNetidClient.php b/src/main/edu/wisc/doit/RpcNetidClient.php
index db9eb30..5d99fdc 100644
--- a/src/main/edu/wisc/doit/RpcNetidClient.php
+++ b/src/main/edu/wisc/doit/RpcNetidClient.php
@@ -100,6 +100,14 @@ interface RpcNetidClient {
 	 * @return RpcNetidStructValidationResponse  validation response including reasons for failure.
 	 */
 	function checkLOA( $uid, \DateTime $birthdate, $wiscard = null );
+
+	/**
+	 * Returns the version of the NetID web service.
+	 *
+	 * @return string  the version of the web service
+	 * @throws RpcNetidClientException  if web service responds with status code other than 200
+	 */
+	function ping();
 	
 }
 
diff --git a/src/main/edu/wisc/doit/RpcNetidClientSoap.php b/src/main/edu/wisc/doit/RpcNetidClientSoap.php
index c522dec..85422d8 100644
--- a/src/main/edu/wisc/doit/RpcNetidClientSoap.php
+++ b/src/main/edu/wisc/doit/RpcNetidClientSoap.php
@@ -338,6 +338,24 @@ class RpcNetidClientSoap implements RpcNetidClient {
 		throw new RpcNetidClientSoapException("Unexpected status code: {$result->result}",
 		RpcNetidClientSoapException::UNEXPECTED_STATUS_CODE );
 	}
+
+	public function ping() {
+
+		$result = $this->getSoapClient()->ping([]);
+
+		if ( $result->result === 200 ) {
+			if ( isset( $result->version ) ) {
+				return $result->version;
+			} else {
+				throw new RpcNetidClientSoapException("version not returned by web service",
+					RpcNetidClientSoapException::UNEXPECTED_RESPONSE);
+			}
+		}
+
+		throw new RpcNetidClientSoapException("Unexpected status code: {$result->result}",
+			RpcNetidClientSoapException::UNEXPECTED_STATUS_CODE );
+
+	}
 	
 	/**
 	 * Creates a client initialized with the given configuration otpions
@@ -371,7 +389,7 @@ class RpcNetidClientSoap implements RpcNetidClient {
 	}
 	
 	/**
-	 * @return SoapClient
+	 * @return \SoapClient
 	 */
 	public function getSoapClient() { return $this->soapClient; }
 	
diff --git a/src/test/RpcNetidClientSoapTest.php b/src/test/RpcNetidClientSoapTest.php
index 3f3d956..060ef83 100644
--- a/src/test/RpcNetidClientSoapTest.php
+++ b/src/test/RpcNetidClientSoapTest.php
@@ -662,4 +662,45 @@ class RpcNetidClientSoapTest extends PHPUnit_Framework_TestCase {
 		
 	}
 
+	/**
+	 * @test ping() returns the version on success
+	 */
+	function ping_200_returns_version() {
+		$result = new stdClass();
+		$result->result = 200;
+		$result->version = "1.0";
+
+		$this->mockSoapClient->expects($this->any())->method('ping')->will($this->returnValue($result));
+		$client = new RpcNetidClientSoap($this->mockSoapClient);
+		$this->assertEquals( "1.0", $client->ping() );
+	}
+
+	/**
+	 * @test ping() throws exception if unexpected response code
+	 * @expectedException \edu\wisc\doit\RpcNetidClientSoapException
+	 * @expectedExceptionCode 100
+	 */
+	function ping_500_throws() {
+		$result = new stdClass();
+		$result->result = 500;
+
+		$this->mockSoapClient->expects($this->any())->method('ping')->will($this->returnValue($result));
+		$client = new RpcNetidClientSoap($this->mockSoapClient);
+		$client->ping();
+	}
+
+	/**
+	 * @test throws exception if version is not supplied by web service
+	 * @expectedException \edu\wisc\doit\RpcNetidClientSoapException
+	 * @expectedExceptionCode 101
+	 */
+	function ping_no_version_throws() {
+		$result = new stdClass();
+		$result->result = 200;
+
+		$this->mockSoapClient->expects($this->any())->method('ping')->will($this->returnValue($result));
+		$client = new RpcNetidClientSoap($this->mockSoapClient);
+		$client->ping();
+	}
+
 }
-- 
GitLab