143 lines
5.0 KiB
PHP
143 lines
5.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace MintyPHP\Tests\Module\Helpdesk\Service;
|
||
|
|
|
||
|
|
use MintyPHP\Http\SessionStoreInterface;
|
||
|
|
use MintyPHP\Module\Helpdesk\Service\BcSoapGateway;
|
||
|
|
use MintyPHP\Module\Helpdesk\Service\HelpdeskOAuthTokenService;
|
||
|
|
use MintyPHP\Module\Helpdesk\Service\HelpdeskSettingsGateway;
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
|
||
|
|
class BcSoapGatewayTest extends TestCase
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* @param array{http_code:int,body:string,error:string} $response
|
||
|
|
*/
|
||
|
|
private function createGateway(array $response): BcSoapGateway
|
||
|
|
{
|
||
|
|
$settings = $this->createMock(HelpdeskSettingsGateway::class);
|
||
|
|
$settings->method('isConfigured')->willReturn(true);
|
||
|
|
$settings->method('getODataBaseUrl')->willReturn('https://bc.example.com:7048/BusinessCentral-NUP/ODataV4');
|
||
|
|
$settings->method('getCompanyName')->willReturn('Test Company');
|
||
|
|
$settings->method('getAuthMode')->willReturn(HelpdeskSettingsGateway::AUTH_MODE_BASIC);
|
||
|
|
$settings->method('getBasicUser')->willReturn('tester');
|
||
|
|
$settings->method('getBasicPassword')->willReturn('secret');
|
||
|
|
|
||
|
|
$oauthTokenService = $this->createMock(HelpdeskOAuthTokenService::class);
|
||
|
|
$sessionStore = $this->createMock(SessionStoreInterface::class);
|
||
|
|
$sessionStore->method('all')->willReturn([
|
||
|
|
'current_tenant' => ['id' => 1],
|
||
|
|
]);
|
||
|
|
|
||
|
|
return new class($settings, $oauthTokenService, $sessionStore, $response) extends BcSoapGateway {
|
||
|
|
/**
|
||
|
|
* @param array{http_code:int,body:string,error:string} $fakeResponse
|
||
|
|
*/
|
||
|
|
public function __construct(
|
||
|
|
HelpdeskSettingsGateway $settings,
|
||
|
|
HelpdeskOAuthTokenService $oauthTokenService,
|
||
|
|
SessionStoreInterface $sessionStore,
|
||
|
|
private readonly array $fakeResponse
|
||
|
|
) {
|
||
|
|
parent::__construct($settings, $oauthTokenService, $sessionStore);
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function sendSoapRequest(
|
||
|
|
string $endpoint,
|
||
|
|
string $soapAction,
|
||
|
|
string $requestBody,
|
||
|
|
array $headers,
|
||
|
|
string $basicUser,
|
||
|
|
string $basicPassword
|
||
|
|
): array {
|
||
|
|
return $this->fakeResponse;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testGetTicketCommunicationTextReturnsErrorForEmptyTicketNo(): void
|
||
|
|
{
|
||
|
|
$gateway = $this->createGateway([
|
||
|
|
'http_code' => 200,
|
||
|
|
'body' => '',
|
||
|
|
'error' => '',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$result = $gateway->getTicketCommunicationText('');
|
||
|
|
|
||
|
|
$this->assertFalse($result['ok']);
|
||
|
|
$this->assertSame('Missing ticket number', $result['error']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testGetTicketCommunicationTextParsesSuccessfulResponse(): void
|
||
|
|
{
|
||
|
|
$soapBody = <<<'XML'
|
||
|
|
<?xml version="1.0" encoding="utf-8"?>
|
||
|
|
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
||
|
|
<soap:Body>
|
||
|
|
<GetTicketCommunicationText_Result xmlns="urn:microsoft-dynamics-schemas/codeunit/ICISupportWebserviceAPI">
|
||
|
|
<return_value>0</return_value>
|
||
|
|
<communicationText>Hallo aus SOAP</communicationText>
|
||
|
|
<messageEntries>line 1</messageEntries>
|
||
|
|
</GetTicketCommunicationText_Result>
|
||
|
|
</soap:Body>
|
||
|
|
</soap:Envelope>
|
||
|
|
XML;
|
||
|
|
|
||
|
|
$gateway = $this->createGateway([
|
||
|
|
'http_code' => 200,
|
||
|
|
'body' => $soapBody,
|
||
|
|
'error' => '',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$result = $gateway->getTicketCommunicationText('T26-1216');
|
||
|
|
|
||
|
|
$this->assertTrue($result['ok']);
|
||
|
|
$this->assertTrue($result['soap_used']);
|
||
|
|
$this->assertSame(0, $result['return_value']);
|
||
|
|
$this->assertSame('Hallo aus SOAP', $result['communication_text']);
|
||
|
|
$this->assertSame('line 1', $result['message_entries']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testGetTicketCommunicationTextReturnsErrorWhenReturnCodeIndicatesFailure(): void
|
||
|
|
{
|
||
|
|
$soapBody = <<<'XML'
|
||
|
|
<?xml version="1.0" encoding="utf-8"?>
|
||
|
|
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
||
|
|
<soap:Body>
|
||
|
|
<GetTicketCommunicationText_Result xmlns="urn:microsoft-dynamics-schemas/codeunit/ICISupportWebserviceAPI">
|
||
|
|
<return_value>400</return_value>
|
||
|
|
<communicationText></communicationText>
|
||
|
|
</GetTicketCommunicationText_Result>
|
||
|
|
</soap:Body>
|
||
|
|
</soap:Envelope>
|
||
|
|
XML;
|
||
|
|
|
||
|
|
$gateway = $this->createGateway([
|
||
|
|
'http_code' => 200,
|
||
|
|
'body' => $soapBody,
|
||
|
|
'error' => '',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$result = $gateway->getTicketCommunicationText('T26-1216');
|
||
|
|
|
||
|
|
$this->assertFalse($result['ok']);
|
||
|
|
$this->assertSame(400, $result['return_value']);
|
||
|
|
$this->assertSame('BC SOAP returned error code 400', $result['error']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testGetTicketCommunicationTextReturnsErrorForInvalidSoapXml(): void
|
||
|
|
{
|
||
|
|
$gateway = $this->createGateway([
|
||
|
|
'http_code' => 200,
|
||
|
|
'body' => 'invalid-xml',
|
||
|
|
'error' => '',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$result = $gateway->getTicketCommunicationText('T26-1216');
|
||
|
|
|
||
|
|
$this->assertFalse($result['ok']);
|
||
|
|
$this->assertSame('Invalid SOAP XML response', $result['error']);
|
||
|
|
}
|
||
|
|
}
|