<commit_before><?php


namespace com\realexpayments\remote\sdk;

use com\realexpayments\remote\sdk\domain\payment\PaymentRequest;
use com\realexpayments\remote\sdk\domain\payment\PaymentResponse;
use com\realexpayments\remote\sdk\http\HttpClient;
use com\realexpayments\remote\sdk\http\HttpConfiguration;
use com\realexpayments\remote\sdk\http\HttpResponse;
use com\realexpayments\remote\sdk\utils\SampleXmlValidationUtils;
use Phockito;

class RealexClientTest extends \PHPUnit_Framework_TestCase {

	/**
	 * Sets up the fixture, for example, open a network connection.
	 * This method is called before a test is executed.
	 */
	protected function setUp() {
		\Phockito::include_hamcrest( false );
	}

	/**
	 * Test sending a payment request and receiving a payment response.
	 */
	public function testSend() {

		//get sample response XML
		$path            = SampleXmlValidationUtils::PAYMENT_RESPONSE_XML_PATH;
		$prefix          = __DIR__ . '/../../resources';
		$xml             = file_get_contents( $prefix . $path );
		$fromXMLResponse = ( new PaymentResponse() )->fromXml( $xml );

		//mock HttpResponse
		/** @var HttpResponse $httpResponseMock */
		$httpResponseMock = Phockito::mock( HttpResponse::class );
		\Phockito::when( $httpResponseMock->getBody() )->return( $fromXMLResponse->toXML() );
		\Phockito::when( $httpResponseMock->getResponseCode() )->return( 200 );


		// create empty request
		$request = new PaymentRequest();

		$httpConfiguration = new HttpConfiguration();
		$httpConfiguration->setOnlyAllowHttps( false );

		// mock HttpClient instance
		$httpClientMock = Phockito::mock( HttpClient::class );
		\Phockito::when( $httpClientMock->execute( \Hamcrest_Core_IsAnything::anything() ) )->return( $httpResponseMock );

		// execute and send on client
		$realexClient = new RealexClient( SampleXmlValidationUtils::SECRET, $httpClientMock, $httpConfiguration );
		$response     = $realexClient->send( $request );

		// validate response
		SampleXmlValidationUtils::checkUnmarshalledPaymentResponse( $response, $this );
	}

	/**
	 * Test sending a payment request and receiving a payment response error.
	 */
	public function  testSendWithShortErrorResponse() {

		//get sample response XML
		$path            = SampleXmlValidationUtils::PAYMENT_RESPONSE_BASIC_ERROR_XML_PATH;
		$prefix          = __DIR__ . '/../../resources';
		$xml             = file_get_contents( $prefix . $path );
		$fromXMLResponse = ( new PaymentResponse() )->fromXml( $xml );

		//mock HttpResponse
		/** @var HttpResponse $httpResponseMock */
		$httpResponseMock = Phockito::mock( HttpResponse::class );
		\Phockito::when( $httpResponseMock->getBody() )->return( $fromXMLResponse->toXML() );
		\Phockito::when( $httpResponseMock->getResponseCode() )->return( 200 );


		// create empty request
		$request = new PaymentRequest();

		$httpConfiguration = new HttpConfiguration();
		$httpConfiguration->setOnlyAllowHttps( false );

		// mock HttpClient instance
		$httpClientMock = Phockito::mock( HttpClient::class );
		\Phockito::when( $httpClientMock->execute( \Hamcrest_Core_IsAnything::anything() ) )->return( $httpResponseMock );

		// execute and send on client
		$realexClient = new RealexClient( SampleXmlValidationUtils::SECRET, $httpClientMock, $httpConfiguration );

		try {
			$realexClient->send( $request );
			$this->fail( "RealexException should have been thrown before this point." );
		} catch ( RealexException $e ) {
			//validate error
			SampleXmlValidationUtils::checkBasicResponseError( $e, $this );
		}

	}


	/**
	 * Test sending a payment request and receiving a payment response error.
	 */
	public function  testSendWithLongErrorResponse() {

		//get sample response XML
		$path            = SampleXmlValidationUtils::PAYMENT_RESPONSE_FULL_ERROR_XML_PATH;
		$prefix          = __DIR__ . '/../../resources';
		$xml             = file_get_contents( $prefix . $path );
		$fromXMLResponse = ( new PaymentResponse() )->fromXml( $xml );

		//mock HttpResponse
		/** @var HttpResponse $httpResponseMock */
		$httpResponseMock = Phockito::mock( HttpResponse::class );
		\Phockito::when( $httpResponseMock->getBody() )->return( $fromXMLResponse->toXML() );
		\Phockito::when( $httpResponseMock->getResponseCode() )->return( 200 );


		// create empty request
		$request = new PaymentRequest();

		$httpConfiguration = new HttpConfiguration();
		$httpConfiguration->setOnlyAllowHttps( false );

		// mock HttpClient instance
		$httpClientMock = Phockito::mock( HttpClient::class );
		\Phockito::when( $httpClientMock->execute( \Hamcrest_Core_IsAnything::anything() ) )->return( $httpResponseMock );

		// execute and send on client
		$realexClient = new RealexClient( SampleXmlValidationUtils::SECRET, $httpClientMock, $httpConfiguration );


		$response = $realexClient->send( $request );

		//validate error
		SampleXmlValidationUtils::checkFullResponseError( $response, $this );

	}


	/**
	 * Test sending a payment request and receiving a payment response error.
	 */
	public function  testSendWithErrorResponseInvalidCode() {

		//get sample response XML
		$path   = SampleXmlValidationUtils::PAYMENT_RESPONSE_BASIC_ERROR_XML_PATH;
		$prefix = __DIR__ . '/../../resources';
		$xml    = file_get_contents( $prefix . $path );

		/** @var PaymentResponse $fromXMLResponse */
		$fromXMLResponse = ( new PaymentResponse() )->fromXml( $xml );
		$fromXMLResponse->setResult( "invalid" );

		//mock HttpResponse
		/** @var HttpResponse $httpResponseMock */
		$httpResponseMock = Phockito::mock( HttpResponse::class );
		\Phockito::when( $httpResponseMock->getBody() )->return( $fromXMLResponse->toXML() );
		\Phockito::when( $httpResponseMock->getResponseCode() )->return( 200 );


		// create empty request
		$request = new PaymentRequest();

		$httpConfiguration = new HttpConfiguration();
		$httpConfiguration->setOnlyAllowHttps( false );

		// mock HttpClient instance
		$httpClientMock = Phockito::mock( HttpClient::class );
		\Phockito::when( $httpClientMock->execute( \Hamcrest_Core_IsAnything::anything() ) )->return( $httpResponseMock );

		// execute and send on client
		$realexClient = new RealexClient( SampleXmlValidationUtils::SECRET, $httpClientMock, $httpConfiguration );


		$correctExceptionThrown = false;

		try {
			$realexClient->send( $request );
			$this->fail( "RealexException should have been thrown before this point." );

		} catch ( RealexServerException $e ) {
			$this->fail( "Incorrect exception thrown. Expected RealexException as result code is invalid." );
		} catch ( RealexException $e ) {
			$correctExceptionThrown = true;
		}

		$this->assertTrue( $correctExceptionThrown, "Incorrect exception thrown." );
	}


}
<commit_msg>testSendInvalidResponseHash pass<commit_after><?php


namespace com\realexpayments\remote\sdk;

use com\realexpayments\remote\sdk\domain\payment\PaymentRequest;
use com\realexpayments\remote\sdk\domain\payment\PaymentResponse;
use com\realexpayments\remote\sdk\http\HttpClient;
use com\realexpayments\remote\sdk\http\HttpConfiguration;
use com\realexpayments\remote\sdk\http\HttpResponse;
use com\realexpayments\remote\sdk\utils\SampleXmlValidationUtils;
use Phockito;

class RealexClientTest extends \PHPUnit_Framework_TestCase {

	/**
	 * Sets up the fixture, for example, open a network connection.
	 * This method is called before a test is executed.
	 */
	protected function setUp() {
		\Phockito::include_hamcrest( false );
	}

	/**
	 * Test sending a payment request and receiving a payment response.
	 */
	public function testSend() {

		//get sample response XML
		$path            = SampleXmlValidationUtils::PAYMENT_RESPONSE_XML_PATH;
		$prefix          = __DIR__ . '/../../resources';
		$xml             = file_get_contents( $prefix . $path );
		$fromXMLResponse = ( new PaymentResponse() )->fromXml( $xml );

		//mock HttpResponse
		/** @var HttpResponse $httpResponseMock */
		$httpResponseMock = Phockito::mock( HttpResponse::class );
		\Phockito::when( $httpResponseMock->getBody() )->return( $fromXMLResponse->toXML() );
		\Phockito::when( $httpResponseMock->getResponseCode() )->return( 200 );


		// create empty request
		$request = new PaymentRequest();

		$httpConfiguration = new HttpConfiguration();
		$httpConfiguration->setOnlyAllowHttps( false );

		// mock HttpClient instance
		$httpClientMock = Phockito::mock( HttpClient::class );
		\Phockito::when( $httpClientMock->execute( \Hamcrest_Core_IsAnything::anything() ) )->return( $httpResponseMock );

		// execute and send on client
		$realexClient = new RealexClient( SampleXmlValidationUtils::SECRET, $httpClientMock, $httpConfiguration );
		$response     = $realexClient->send( $request );

		// validate response
		SampleXmlValidationUtils::checkUnmarshalledPaymentResponse( $response, $this );
	}

	/**
	 * Test sending a payment request and receiving a payment response error.
	 */
	public function  testSendWithShortErrorResponse() {

		//get sample response XML
		$path            = SampleXmlValidationUtils::PAYMENT_RESPONSE_BASIC_ERROR_XML_PATH;
		$prefix          = __DIR__ . '/../../resources';
		$xml             = file_get_contents( $prefix . $path );
		$fromXMLResponse = ( new PaymentResponse() )->fromXml( $xml );

		//mock HttpResponse
		/** @var HttpResponse $httpResponseMock */
		$httpResponseMock = Phockito::mock( HttpResponse::class );
		\Phockito::when( $httpResponseMock->getBody() )->return( $fromXMLResponse->toXML() );
		\Phockito::when( $httpResponseMock->getResponseCode() )->return( 200 );


		// create empty request
		$request = new PaymentRequest();

		$httpConfiguration = new HttpConfiguration();
		$httpConfiguration->setOnlyAllowHttps( false );

		// mock HttpClient instance
		$httpClientMock = Phockito::mock( HttpClient::class );
		\Phockito::when( $httpClientMock->execute( \Hamcrest_Core_IsAnything::anything() ) )->return( $httpResponseMock );

		// execute and send on client
		$realexClient = new RealexClient( SampleXmlValidationUtils::SECRET, $httpClientMock, $httpConfiguration );

		try {
			$realexClient->send( $request );
			$this->fail( "RealexException should have been thrown before this point." );
		} catch ( RealexException $e ) {
			//validate error
			SampleXmlValidationUtils::checkBasicResponseError( $e, $this );
		}

	}


	/**
	 * Test sending a payment request and receiving a payment response error.
	 */
	public function  testSendWithLongErrorResponse() {

		//get sample response XML
		$path            = SampleXmlValidationUtils::PAYMENT_RESPONSE_FULL_ERROR_XML_PATH;
		$prefix          = __DIR__ . '/../../resources';
		$xml             = file_get_contents( $prefix . $path );
		$fromXMLResponse = ( new PaymentResponse() )->fromXml( $xml );

		//mock HttpResponse
		/** @var HttpResponse $httpResponseMock */
		$httpResponseMock = Phockito::mock( HttpResponse::class );
		\Phockito::when( $httpResponseMock->getBody() )->return( $fromXMLResponse->toXML() );
		\Phockito::when( $httpResponseMock->getResponseCode() )->return( 200 );


		// create empty request
		$request = new PaymentRequest();

		$httpConfiguration = new HttpConfiguration();
		$httpConfiguration->setOnlyAllowHttps( false );

		// mock HttpClient instance
		$httpClientMock = Phockito::mock( HttpClient::class );
		\Phockito::when( $httpClientMock->execute( \Hamcrest_Core_IsAnything::anything() ) )->return( $httpResponseMock );

		// execute and send on client
		$realexClient = new RealexClient( SampleXmlValidationUtils::SECRET, $httpClientMock, $httpConfiguration );


		$response = $realexClient->send( $request );

		//validate error
		SampleXmlValidationUtils::checkFullResponseError( $response, $this );

	}


	/**
	 * Test sending a payment request and receiving a payment response error.
	 */
	public function  testSendWithErrorResponseInvalidCode() {

		//get sample response XML
		$path   = SampleXmlValidationUtils::PAYMENT_RESPONSE_BASIC_ERROR_XML_PATH;
		$prefix = __DIR__ . '/../../resources';
		$xml    = file_get_contents( $prefix . $path );

		/** @var PaymentResponse $fromXMLResponse */
		$fromXMLResponse = ( new PaymentResponse() )->fromXml( $xml );
		$fromXMLResponse->setResult( "invalid" );

		//mock HttpResponse
		/** @var HttpResponse $httpResponseMock */
		$httpResponseMock = Phockito::mock( HttpResponse::class );
		\Phockito::when( $httpResponseMock->getBody() )->return( $fromXMLResponse->toXML() );
		\Phockito::when( $httpResponseMock->getResponseCode() )->return( 200 );


		// create empty request
		$request = new PaymentRequest();

		$httpConfiguration = new HttpConfiguration();
		$httpConfiguration->setOnlyAllowHttps( false );

		// mock HttpClient instance
		$httpClientMock = Phockito::mock( HttpClient::class );
		\Phockito::when( $httpClientMock->execute( \Hamcrest_Core_IsAnything::anything() ) )->return( $httpResponseMock );

		// execute and send on client
		$realexClient = new RealexClient( SampleXmlValidationUtils::SECRET, $httpClientMock, $httpConfiguration );


		$correctExceptionThrown = false;

		try {
			$realexClient->send( $request );
			$this->fail( "RealexException should have been thrown before this point." );

		} catch ( RealexServerException $e ) {
			$this->fail( "Incorrect exception thrown. Expected RealexException as result code is invalid." );
		} catch ( RealexException $e ) {
			$correctExceptionThrown = true;
		}

		$this->assertTrue( $correctExceptionThrown, "Incorrect exception thrown." );
	}


	/**
	 * Test receiving a response which has an invalid hash.
	 *
	 * @expectedException com\realexpayments\remote\sdk\RealexException
	 */
	public function  testSendInvalidResponseHash() {

		//get sample response XML
		$path            = SampleXmlValidationUtils::PAYMENT_RESPONSE_XML_PATH;
		$prefix          = __DIR__ . '/../../resources';
		$xml             = file_get_contents( $prefix . $path );

		/** @var PaymentResponse $fromXMLResponse */
		$fromXMLResponse = ( new PaymentResponse() )->fromXml( $xml );

		//add invalid hash
		$fromXMLResponse->setHash("invalid hash");

		//mock HttpResponse
		/** @var HttpResponse $httpResponseMock */
		$httpResponseMock = Phockito::mock( HttpResponse::class );
		\Phockito::when( $httpResponseMock->getBody() )->return( $fromXMLResponse->toXML() );
		\Phockito::when( $httpResponseMock->getResponseCode() )->return( 200 );


		// create empty request
		$request = new PaymentRequest();

		$httpConfiguration = new HttpConfiguration();
		$httpConfiguration->setOnlyAllowHttps( false );

		// mock HttpClient instance
		$httpClientMock = Phockito::mock( HttpClient::class );
		\Phockito::when( $httpClientMock->execute( \Hamcrest_Core_IsAnything::anything() ) )->return( $httpResponseMock );

		// execute and send on client
		$realexClient = new RealexClient( SampleXmlValidationUtils::SECRET, $httpClientMock, $httpConfiguration );
		$realexClient->send( $request );

		//shouldn't get this far
		$this->fail("RealexException should have been thrown before this point.");

	}


}

{'fraction_non_alphanumeric': 0.132171581769437, 'fraction_numerical': 0.001809651474530831, 'mean_word_length': 4.363407620416966, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3139721', 'n_tokens_mistral': 4754, 'n_tokens_neox': 4329, 'n_words': 998}