Files
breadcrumb-the-shire/modules/helpdesk/tests/Module/Helpdesk/Service/DebitorDetailServiceTest.php

308 lines
11 KiB
PHP
Raw Normal View History

<?php
namespace MintyPHP\Tests\Module\Helpdesk\Service;
use MintyPHP\Module\Helpdesk\Service\BcODataGateway;
use MintyPHP\Module\Helpdesk\Service\DebitorDetailService;
use MintyPHP\Module\Helpdesk\Service\HelpdeskSettingsGateway;
use PHPUnit\Framework\TestCase;
class DebitorDetailServiceTest extends TestCase
{
private function createService(?BcODataGateway $gateway = null, bool $isConfigured = true): DebitorDetailService
{
$gateway = $gateway ?? $this->createMock(BcODataGateway::class);
$settings = $this->createMock(HelpdeskSettingsGateway::class);
$settings->method('isConfigured')->willReturn($isConfigured);
return new DebitorDetailService($gateway, $settings);
}
// --- loadDetail() (legacy) ---
public function testLoadDetailReturnsNotFoundForEmptyCustomerNo(): void
{
$service = $this->createService();
$result = $service->loadDetail('');
$this->assertSame('not_found', $result['status']);
}
public function testLoadDetailReturnsNotConfiguredWhenBcNotConfigured(): void
{
$service = $this->createService(null, false);
$result = $service->loadDetail('10254');
$this->assertSame('not_configured', $result['status']);
}
public function testLoadDetailReturnsNotFoundWhenCustomerMissing(): void
{
$gateway = $this->createMock(BcODataGateway::class);
$gateway->expects($this->once())->method('getCustomer')->with('99999')->willReturn(null);
$service = $this->createService($gateway);
$result = $service->loadDetail('99999');
$this->assertSame('not_found', $result['status']);
}
public function testLoadDetailReturnsSuccessWithAllData(): void
{
$customer = ['No' => '10254', 'Name' => 'Musterfirma GmbH'];
$contacts = [['No' => 'KT0001', 'Name' => 'Max Mustermann']];
$tickets = [['No' => 'T0001', 'Description' => 'Problem X']];
$gateway = $this->createMock(BcODataGateway::class);
$gateway->expects($this->once())->method('getCustomer')->with('10254')->willReturn($customer);
$gateway->expects($this->once())->method('getContactsForCustomer')->with('10254', 'Musterfirma GmbH')->willReturn($contacts);
$gateway->expects($this->once())->method('getTicketsForCustomer')->with('10254', 'Musterfirma GmbH')->willReturn($tickets);
$service = $this->createService($gateway);
$result = $service->loadDetail('10254');
$this->assertSame('success', $result['status']);
$this->assertSame($customer, $result['customer']);
$this->assertSame($contacts, $result['contacts']);
$this->assertSame($tickets, $result['tickets']);
}
public function testLoadDetailReturnsErrorOnBcConnectionFailure(): void
{
$gateway = $this->createMock(BcODataGateway::class);
$gateway->method('getCustomer')->willThrowException(new \RuntimeException('timeout'));
$service = $this->createService($gateway);
$result = $service->loadDetail('10254');
$this->assertSame('error', $result['status']);
}
public function testLoadDetailStillSucceedsWhenContactsFail(): void
{
$customer = ['No' => '10254', 'Name' => 'Musterfirma GmbH'];
$gateway = $this->createMock(BcODataGateway::class);
$gateway->expects($this->once())->method('getCustomer')->with('10254')->willReturn($customer);
$gateway->method('getContactsForCustomer')->willThrowException(new \RuntimeException('contacts fail'));
$gateway->method('getTicketsForCustomer')->willReturn([]);
$service = $this->createService($gateway);
$result = $service->loadDetail('10254');
$this->assertSame('success', $result['status']);
$this->assertSame([], $result['contacts']);
$this->assertSame('contacts fail', $result['contactsError']);
}
public function testLoadDetailStillSucceedsWhenTicketsFail(): void
{
$customer = ['No' => '10254', 'Name' => 'Musterfirma GmbH'];
$gateway = $this->createMock(BcODataGateway::class);
$gateway->expects($this->once())->method('getCustomer')->with('10254')->willReturn($customer);
$gateway->method('getContactsForCustomer')->willReturn([]);
$gateway->method('getTicketsForCustomer')->willThrowException(new \RuntimeException('tickets fail'));
$service = $this->createService($gateway);
$result = $service->loadDetail('10254');
$this->assertSame('success', $result['status']);
$this->assertSame([], $result['tickets']);
$this->assertSame('tickets fail', $result['ticketsError']);
}
// --- loadCustomer() ---
public function testLoadCustomerReturnsNotFoundForEmptyNo(): void
{
$service = $this->createService();
$result = $service->loadCustomer('');
$this->assertSame('not_found', $result['status']);
}
public function testLoadCustomerReturnsNotConfiguredWhenNotConfigured(): void
{
$service = $this->createService(null, false);
$result = $service->loadCustomer('10001');
$this->assertSame('not_configured', $result['status']);
}
public function testLoadCustomerReturnsNotFoundWhenCustomerNull(): void
{
$gateway = $this->createMock(BcODataGateway::class);
$gateway->method('getCustomer')->willReturn(null);
$service = $this->createService($gateway);
$result = $service->loadCustomer('99999');
$this->assertSame('not_found', $result['status']);
}
public function testLoadCustomerReturnsSuccessWithCustomerData(): void
{
$customer = ['No' => '10001', 'Name' => 'Test GmbH', 'Support_Type' => 'Premium'];
$gateway = $this->createMock(BcODataGateway::class);
$gateway->method('getCustomer')->willReturn($customer);
$service = $this->createService($gateway);
$result = $service->loadCustomer('10001');
$this->assertSame('success', $result['status']);
$this->assertSame($customer, $result['customer']);
$this->assertArrayNotHasKey('contacts', $result);
$this->assertArrayNotHasKey('tickets', $result);
}
public function testLoadCustomerReturnsErrorOnException(): void
{
$gateway = $this->createMock(BcODataGateway::class);
$gateway->method('getCustomer')->willThrowException(new \RuntimeException('Connection failed'));
$service = $this->createService($gateway);
$result = $service->loadCustomer('10001');
$this->assertSame('error', $result['status']);
$this->assertSame('BC connection failed', $result['error']);
}
// --- loadContacts() ---
public function testLoadContactsReturnsFalseForEmptyCustomerNo(): void
{
$service = $this->createService();
$result = $service->loadContacts('', 'Test GmbH');
$this->assertFalse($result['ok']);
}
public function testLoadContactsReturnsFalseForEmptyCustomerName(): void
{
$service = $this->createService();
$result = $service->loadContacts('10001', '');
$this->assertFalse($result['ok']);
}
public function testLoadContactsReturnsFalseWhenNotConfigured(): void
{
$service = $this->createService(null, false);
$result = $service->loadContacts('10001', 'Test GmbH');
$this->assertFalse($result['ok']);
}
public function testLoadContactsReturnsOkWithContacts(): void
{
$contacts = [['No' => 'KT001', 'Name' => 'Max Mustermann']];
$gateway = $this->createMock(BcODataGateway::class);
$gateway->method('getContactsForCustomer')->willReturn($contacts);
$service = $this->createService($gateway);
$result = $service->loadContacts('10001', 'Test GmbH');
$this->assertTrue($result['ok']);
$this->assertSame($contacts, $result['contacts']);
}
public function testLoadContactsReturnsErrorOnException(): void
{
$gateway = $this->createMock(BcODataGateway::class);
$gateway->method('getContactsForCustomer')->willThrowException(new \RuntimeException('API error'));
$service = $this->createService($gateway);
$result = $service->loadContacts('10001', 'Test GmbH');
$this->assertFalse($result['ok']);
$this->assertSame('API error', $result['error']);
}
// --- loadTickets() ---
public function testLoadTicketsReturnsFalseForEmptyCustomerNo(): void
{
$service = $this->createService();
$result = $service->loadTickets('', 'Test GmbH');
$this->assertFalse($result['ok']);
}
public function testLoadTicketsReturnsFalseForEmptyCustomerName(): void
{
$service = $this->createService();
$result = $service->loadTickets('10001', '');
$this->assertFalse($result['ok']);
}
public function testLoadTicketsReturnsFalseWhenNotConfigured(): void
{
$service = $this->createService(null, false);
$result = $service->loadTickets('10001', 'Test GmbH');
$this->assertFalse($result['ok']);
}
public function testLoadTicketsReturnsOkWithTickets(): void
{
$tickets = [['No' => 'T001', 'Description' => 'Test ticket', 'Ticket_State' => 'Open']];
$gateway = $this->createMock(BcODataGateway::class);
$gateway->method('getTicketsForCustomer')->willReturn($tickets);
$service = $this->createService($gateway);
$result = $service->loadTickets('10001', 'Test GmbH');
$this->assertTrue($result['ok']);
$this->assertSame($tickets, $result['tickets']);
}
public function testLoadTicketsReturnsErrorOnException(): void
{
$gateway = $this->createMock(BcODataGateway::class);
$gateway->method('getTicketsForCustomer')->willThrowException(new \RuntimeException('Timeout'));
$service = $this->createService($gateway);
$result = $service->loadTickets('10001', 'Test GmbH');
$this->assertFalse($result['ok']);
$this->assertSame('Timeout', $result['error']);
}
// --- loadTicketLog() ---
public function testLoadTicketLogReturnsFalseForEmptyTicketNo(): void
{
$service = $this->createService();
$result = $service->loadTicketLog('');
$this->assertFalse($result['ok']);
}
public function testLoadTicketLogReturnsFalseWhenNotConfigured(): void
{
$service = $this->createService(null, false);
$result = $service->loadTicketLog('T001');
$this->assertFalse($result['ok']);
}
public function testLoadTicketLogReturnsOkWithLog(): void
{
$log = [
['Entry_No' => 1, 'Type' => 'Nachricht', 'Created_By' => 'NKS', 'Support_Ticket_No' => 'T001'],
['Entry_No' => 2, 'Type' => 'Status', 'Created_By' => 'NKS', 'Support_Ticket_No' => 'T001'],
];
$gateway = $this->createMock(BcODataGateway::class);
$gateway->method('getTicketLog')->willReturn($log);
$service = $this->createService($gateway);
$result = $service->loadTicketLog('T001');
$this->assertTrue($result['ok']);
$this->assertSame($log, $result['log']);
}
public function testLoadTicketLogReturnsErrorOnException(): void
{
$gateway = $this->createMock(BcODataGateway::class);
$gateway->method('getTicketLog')->willThrowException(new \RuntimeException('API error'));
$service = $this->createService($gateway);
$result = $service->loadTicketLog('T001');
$this->assertFalse($result['ok']);
$this->assertSame('API error', $result['error']);
}
}