Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
php: [7.1, 7.2, 7.3, 7.4, 8.0]
php: [7.4, 8.0]

steps:
- name: Checkout code
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
],
"minimum-stability": "RC",
"require": {
"php": "^7.1 || ^8.0",
"php": "^7.4 | ^8.0",
"ext-dom": "*",
"codeception/codeception": "^4.0",
"codeception/codeception": "^4.1",
"codeception/lib-innerbrowser": "^1.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A SOAP module for Codeception.

## Requirements

* `PHP 7.1` or higher.
* `PHP 7.4` or higher.

## Installation

Expand Down
55 changes: 24 additions & 31 deletions src/Codeception/Module/SOAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Codeception\Util\XmlBuilder;
use Codeception\Util\XmlStructure;
use DOMDocument;
use DOMNode;
use ErrorException;
use PHPUnit\Framework\Assert;
use Symfony\Component\BrowserKit\AbstractBrowser;
Expand Down Expand Up @@ -66,10 +67,7 @@ class SOAP extends Module implements DependsOnModule
*/
protected $requiredFields = ['endpoint'];

/**
* @var string
*/
protected $dependencyMessage = <<<EOF
protected string $dependencyMessage = <<<EOF
Example using PhpBrowser as backend for SOAP module.
--
modules:
Expand All @@ -80,34 +78,23 @@ class SOAP extends Module implements DependsOnModule
Framework modules can be used as well for functional testing of SOAP API.
EOF;

/**
* @var AbstractBrowser
*/
public $client;
public AbstractBrowser $client;

/**
* @var bool
*/
public $isFunctional = false;
public bool $isFunctional = false;

/**
* @var DOMDocument
*/
public $xmlRequest;
/**
* @var DOMDocument
* @var DOMNode|DOMDocument|null
*/
public $xmlResponse;
public $xmlRequest = null;

/**
* @var XmlStructure
* @var DOMNode|DOMDocument|null
*/
protected $xmlStructure;
public $xmlResponse = null;

/**
* @var InnerBrowser
*/
protected $connectionModule;
protected ?XmlStructure $xmlStructure = null;

protected ?InnerBrowser $connectionModule = null;

public function _before(TestInterface $test): void
{
Expand Down Expand Up @@ -142,6 +129,7 @@ private function getClient(): AbstractBrowser
if (!$this->client) {
throw new ModuleRequireException($this, 'Connection client is not available.');
}

return $this->client;
}

Expand All @@ -150,6 +138,7 @@ private function getXmlResponse(): DOMDocument
if (!$this->xmlResponse) {
throw new ModuleException($this, "No XML response, use `\$I->sendSoapRequest` to receive it");
}

return $this->xmlResponse;
}

Expand All @@ -158,6 +147,7 @@ private function getXmlStructure(): XmlStructure
if (!$this->xmlStructure) {
$this->xmlStructure = new XmlStructure($this->getXmlResponse());
}

return $this->xmlStructure;
}

Expand Down Expand Up @@ -440,8 +430,9 @@ public function grabAttributeFrom(string $cssOrXPath, string $attribute): string
$el = $this->getXmlStructure()->matchElement($cssOrXPath);
$elHasAttribute = $el->hasAttribute($attribute);
if (!$elHasAttribute) {
$this->fail(sprintf('Attribute not found in element matched by \'%s\'', $cssOrXPath));
$this->fail(sprintf("Attribute not found in element matched by '%s'", $cssOrXPath));
}

return $el->getAttribute($attribute);
}

Expand All @@ -467,12 +458,13 @@ protected function buildRequest(): DOMDocument
$xml->appendChild($root);
$root->setAttribute('xmlns:ns', $this->getSchema());
$root->setAttribute('xmlns:soapenv', $soap_schema_url);

$body = $xml->createElementNS($soap_schema_url, 'soapenv:Body');
$header = $xml->createElementNS($soap_schema_url, 'soapenv:Header');
$root->appendChild($header);

$root->appendChild($body);

$this->xmlRequest = $xml;
return $xml;
}
Expand All @@ -494,21 +486,22 @@ protected function processRequest(string $action, string $body): void
}

/**
* @return string|bool
* @return string|false
*/
protected function processInternalRequest(string $action, string $body)
{
ob_start();
try {
$this->getClient()->setServerParameter('HTTP_HOST', 'localhost');
$this->processRequest($action, $body);
} catch (ErrorException $e) {
} catch (ErrorException $exception) {
// Zend_Soap outputs warning as an exception
if (strpos($e->getMessage(), 'Warning: Cannot modify header information') === false) {
if (strpos($exception->getMessage(), 'Warning: Cannot modify header information') === false) {
ob_end_clean();
throw $e;
throw $exception;
}
}

$response = ob_get_contents();
ob_end_clean();
return $response;
Expand Down
51 changes: 29 additions & 22 deletions tests/unit/Codeception/Module/SoapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,55 @@

declare(strict_types=1);

use Codeception\Configuration;
use Codeception\Lib\Connector\Universal;
use Codeception\Lib\ModuleContainer;
use Codeception\Module\SOAP;
use Codeception\Module\UniversalFramework;
use Codeception\PHPUnit\TestCase;
use Codeception\Util\Stub;
use Codeception\Util\Soap as SoapUtil;

/**
* Class SoapTest
* @group appveyor
*/
final class SoapTest extends \Codeception\PHPUnit\TestCase
final class SoapTest extends TestCase
{
protected ?SOAP $module = null;

/**
* @var \Codeception\Module\Soap
*/
protected $module = null;

protected $layout;
protected ?string $layout = null;

public function _setUp()
{
$container = \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer');
$frameworkModule = new \Codeception\Module\UniversalFramework($container);
$frameworkModule->client = Stub::makeEmpty('\Codeception\Lib\Connector\Universal');
$this->module = new \Codeception\Module\SOAP($container);
$container = Stub::make(ModuleContainer::class);
$frameworkModule = new UniversalFramework($container);
$frameworkModule->client = Stub::makeEmpty(Universal::class);
$this->module = new SOAP($container);
$this->module->_setConfig(array(
'schema' => 'http://www.w3.org/2001/xml.xsd',
'endpoint' => 'http://codeception.com/api/wsdl'
));
$this->module->_inject($frameworkModule);
$this->layout = \Codeception\Configuration::dataDir().'/layout.xml';

$this->layout = Configuration::dataDir().'/layout.xml';
$this->module->isFunctional = true;
$this->module->_before(Stub::makeEmpty('\Codeception\Test\Test'));
$this->module->_before(Stub::makeEmpty(\Codeception\Test\Test::class));
}

public function testXmlIsBuilt()
{
$dom = new \DOMDocument();
$dom = new DOMDocument();
$dom->load($this->layout);
$this->assertXmlStringEqualsXmlString($dom->saveXML(), $this->module->xmlRequest->saveXML());
}

public function testBuildHeaders()
{
$this->module->haveSoapHeader('AuthHeader', ['username' => 'davert', 'password' => '123456']);
$dom = new \DOMDocument();
$dom = new DOMDocument();
$dom->load($this->layout);

$header = $dom->createElement('AuthHeader');
$header->appendChild($dom->createElement('username', 'davert'));
$header->appendChild($dom->createElement('password', '123456'));
Expand All @@ -58,11 +62,13 @@ public function testBuildRequest()
{
$this->module->sendSoapRequest('KillHumans', "<item><id>1</id><subitem>2</subitem></item>");
$this->assertNotNull($this->module->xmlRequest);
$dom = new \DOMDocument();
$dom = new DOMDocument();
$dom->load($this->layout);

$body = $dom->createElement('item');
$body->appendChild($dom->createElement('id', '1'));
$body->appendChild($dom->createElement('subitem', '2'));

$request = $dom->createElement('ns:KillHumans');
$request->appendChild($body);
$dom->documentElement->getElementsByTagName('Body')->item(0)->appendChild($request);
Expand All @@ -71,19 +77,21 @@ public function testBuildRequest()

public function testBuildRequestWithDomNode()
{
$dom = new \DOMDocument();
$dom = new DOMDocument();
$dom->load($this->layout);

$body = $dom->createElement('item');
$body->appendChild($dom->createElement('id', '1'));
$body->appendChild($dom->createElement('subitem', '2'));

$request = $dom->createElement('ns:KillHumans');
$request->appendChild($body);
$dom->documentElement->getElementsByTagName('Body')->item(0)->appendChild($request);

$this->module->sendSoapRequest('KillHumans', $body);
$this->assertXmlStringEqualsXmlString($dom->saveXML(), $this->module->xmlRequest->saveXML());
}

public function testSeeXmlIncludes()
{
$dom = new DOMDocument();
Expand Down Expand Up @@ -111,7 +119,6 @@ public function testSeeXmlNotContainsXPath()
$this->module->dontSeeSoapResponseContainsXPath('//doc/a[@a2=2 and @a31]');
}


public function testSeeXmlEquals()
{
$dom = new DOMDocument();
Expand All @@ -133,7 +140,7 @@ public function testSeeXmlIncludesWithBuilder()
->val('123');
$this->module->seeSoapResponseIncludes($xml);
}

public function testGrabTextFrom()
{
$dom = new DOMDocument();
Expand Down