Skip to content

Commit

Permalink
Merge pull request #88 from sungcheng/sung-add-more-testablestuff
Browse files Browse the repository at this point in the history
Add setNextResponseByString to make testing easier
  • Loading branch information
AdmTal authored May 9, 2023
2 parents fcd6791 + a9f1a20 commit 512eac8
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/TestableSoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,110 @@ public static function setNextResponse($response)
self::$nextResponseOverrideQueue->enqueue($response);
}

/**
* Set the response that should be returned by the next SOAP request by string.
* Copy of setNextResponseFromFile, the only difference is instead of reading from
* the file for the content, we can directly pass the string/xml which would simplify
* testing.
*
* @param string $soapResponse
* @throws Omnipay\Common\Exception\BadMethodCallException
* @return void
*/
public static function setNextResponseByString($soapResponse)
{
$responseDom = new DOMDocument();
$responseDom->loadXML($soapResponse);
$simpleXmlResponse = simplexml_import_dom(
$responseDom->documentElement->childNodes->item(1)->childNodes->item(1)
);
// convert SimpleXMLElement to normal object
$responseObject = json_decode(json_encode($simpleXmlResponse));

// When SOAP fields are supposed to be arrays but they only have one element in them,
// the above method of parsing the XML just returns an object instead of a one element
// array. The following code is a hack to restore the one element array structure for
// the affected fields.
$fields = array();
if (isset($responseObject->results)) {
$fields[] = &$responseObject->results;
}
if (isset($responseObject->account->paymentMethods)) {
$fields[] = &$responseObject->account->paymentMethods;
}
if (isset($responseObject->refunds)) {
$fields[] = &$responseObject->refunds;
}
if (isset($responseObject->transaction->items)) {
$fields[] = &$responseObject->transaction->items;
}
if (isset($responseObject->billingPlan->periods)) {
$fields[] = &$responseObject->billingPlan->periods;
}
if (isset($responseObject->billingPlan->nameValues)) {
$fields[] = &$responseObject->billingPlan->nameValues;
}
if (isset($responseObject->product->prices)) {
$fields[] = &$responseObject->product->prices;
}
if (isset($responseObject->product->nameValues)) {
$fields[] = &$responseObject->product->nameValues;
}
if (isset($responseObject->product->defaultBillingPlan->periods)) {
$fields[] = &$responseObject->product->defaultBillingPlan->periods;
}
if (isset($responseObject->product->defaultBillingPlan->nameValues)) {
$fields[] = &$responseObject->product->defaultBillingPlan->nameValues;
}
if (isset($responseObject->autobill->billingPlan->periods)) {
$fields[] = &$responseObject->autobill->billingPlan->periods;
}
if (isset($responseObject->autobill->items->product->defaultBillingPlan->periods)) {
$fields[] = &$responseObject->autobill->items->product->defaultBillingPlan->periods;
}
if (isset($responseObject->autobills->billingPlan->periods)) {
$fields[] = &$responseObject->autobills->billingPlan->periods;
}
if (isset($responseObject->autobills[0]->billingPlan->periods)) {
$fields[] = &$responseObject->autobills[0]->billingPlan->periods;
}
if (isset($responseObject->autobills[1]->billingPlan->periods)) {
$fields[] = &$responseObject->autobills[1]->billingPlan->periods;
}
if (isset($responseObject->autobill->items)) {
$fields[] = &$responseObject->autobill->items;
}
if (isset($responseObject->autobills->items)) {
$fields[] = &$responseObject->autobills->items;
}
if (isset($responseObject->autobills[0]->items)) {
$fields[] = &$responseObject->autobills[0]->items;
}
if (isset($responseObject->autobills[1]->items)) {
$fields[] = &$responseObject->autobills[1]->items;
}
if (isset($responseObject->autobills)) {
$fields[] = &$responseObject->autobills;
}
if (isset($responseObject->autobill->nameValues)) {
$fields[] = &$responseObject->autobill->nameValues;
}
if (isset($responseObject->session->apiReturnValues->accountUpdatePaymentMethod->account->paymentMethods)) {
$fields[] = &$responseObject->session->apiReturnValues->accountUpdatePaymentMethod->account->paymentMethods;
}

// also affects the payment methods on the accounts on the transactions in the refunds, but
// then we have to hack through all the refunds and we don't need that field anyway

foreach ($fields as &$field) {
if (is_object($field)) {
$field = array($field);
}
}

self::setNextResponse($responseObject);
}

/**
* Set the response that should be returned by the next SOAP request.
* `$filename` is the name of the XML file in the tests/Mock directory
Expand Down
33 changes: 33 additions & 0 deletions tests/TestableSoapClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Omnipay\Vindicia;

use Omnipay\Tests\TestCase;

class TestableSoapClientTest extends TestCase
{
public function testSetNextResponseByStringDoesNotFail()
{
$soap_content = '<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:vin="http://soap.vindicia.com/v18_0/Vindicia"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<authResponse xmlns="http://soap.vindicia.com/v18_0/Transaction">
<return xmlns="" xsi:type="vin:Return">
<returnCode xsi:type="vin:ReturnCode">400</returnCode>
<soapId xsi:type="xsd:string">1234567890abcdef1234567890abcdef</soapId>
<returnString xsi:type="xsd:string">Data validation error Failed to create Payment-Type-Specific Payment Record: Credit Card conversion failed: Credit Card failed Luhn check. </returnString>
</return>
</authResponse>
</soap:Body>
</soap:Envelope>
';

TestableSoapClient::setNextResponseByString($soap_content);
}
}

0 comments on commit 512eac8

Please sign in to comment.