Files
FOSSBilling/tests-legacy/modules/Support/Api/Api_ClientTest.php
T
Adam DaleyandCopilot a0b3158b30 PHPUnit test-legacy refactor and cleanup (#3144)
* PHPUnit test-legacy cleanup

Refactor and cleanup the legacy PHPUnit tests.

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-01-13 17:32:09 +00:00

241 lines
7.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Tests\Mod\Support\Api;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
#[Group('Core')]
final class Api_ClientTest extends \BBTestCase
{
protected ?\Box\Mod\Support\Api\Client $clientApi;
public function setUp(): void
{
$this->clientApi = new \Box\Mod\Support\Api\Client();
}
public function testTicketGetList(): void
{
$simpleResultArr = [
'list' => [
['id' => 1],
],
];
$paginatorMock = $this->getMockBuilder(\FOSSBilling\Pagination::class)
->onlyMethods(['getPaginatedResultSet'])
->disableOriginalConstructor()
->getMock();
$paginatorMock->expects($this->atLeastOnce())
->method('getPaginatedResultSet')
->willReturn($simpleResultArr);
$serviceMock = $this->getMockBuilder(\Box\Mod\Support\Service::class)
->onlyMethods(['getSearchQuery', 'toApiArray'])->getMock();
$serviceMock->expects($this->atLeastOnce())->method('getSearchQuery')
->willReturn(['query', []]);
$serviceMock->expects($this->atLeastOnce())
->method('toApiArray')
->willReturn([]);
$model = new \Model_SupportTicket();
$model->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->willReturn($model);
$di = $this->getDi();
$di['pager'] = $paginatorMock;
$di['db'] = $dbMock;
$this->clientApi->setDi($di);
$client = new \Model_Client();
$client->loadBean(new \DummyBean());
$client->id = 1;
$this->clientApi->setService($serviceMock);
$this->clientApi->setIdentity($client);
$data = [];
$result = $this->clientApi->ticket_get_list($data);
$this->assertIsArray($result);
}
public function testTicketGet(): void
{
$serviceMock = $this->getMockBuilder(\Box\Mod\Support\Service::class)
->onlyMethods(['findOneByClient', 'toApiArray'])->getMock();
$serviceMock->expects($this->atLeastOnce())->method('findOneByClient')
->willReturn(new \Model_SupportTicket());
$serviceMock->expects($this->atLeastOnce())->method('toApiArray')
->willReturn([]);
$di = $this->getDi();
$this->clientApi->setDi($di);
$client = new \Model_Client();
$client->loadBean(new \DummyBean());
$client->id = 1;
$this->clientApi->setService($serviceMock);
$this->clientApi->setIdentity($client);
$data = [
'id' => 1,
];
$result = $this->clientApi->ticket_get($data);
$this->assertIsArray($result);
}
public function testHelpdeskGetPairs(): void
{
$serviceMock = $this->getMockBuilder(\Box\Mod\Support\Service::class)
->onlyMethods(['helpdeskGetPairs'])->getMock();
$serviceMock->expects($this->atLeastOnce())->method('helpdeskGetPairs')
->willReturn([0 => 'General']);
$this->clientApi->setService($serviceMock);
$result = $this->clientApi->helpdesk_get_pairs();
$this->assertIsArray($result);
}
public function testTicketCreate(): void
{
$serviceMock = $this->getMockBuilder(\Box\Mod\Support\Service::class)
->onlyMethods(['ticketCreateForClient'])->getMock();
$serviceMock->expects($this->atLeastOnce())->method('ticketCreateForClient')
->willReturn(1);
$dbMock = $this->getMockBuilder('\Box_Database')->disableOriginalConstructor()->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->willReturn(new \Model_SupportHelpdesk());
$di = $this->getDi();
$di['db'] = $dbMock;
$this->clientApi->setDi($di);
$client = new \Model_Client();
$client->loadBean(new \DummyBean());
$client->id = 1;
$this->clientApi->setService($serviceMock);
$this->clientApi->setIdentity($client);
$data = [
'content' => 'Content',
'subject' => 'Subject',
'support_helpdesk_id' => 1,
];
$result = $this->clientApi->ticket_create($data);
$this->assertIsInt($result);
}
public function testTicketReply(): void
{
$serviceMock = $this->getMockBuilder(\Box\Mod\Support\Service::class)
->onlyMethods(['canBeReopened', 'ticketReply'])->getMock();
$serviceMock->expects($this->atLeastOnce())->method('canBeReopened')
->willReturn(true);
$serviceMock->expects($this->atLeastOnce())->method('ticketReply')
->willReturn(1);
$dbMock = $this->getMockBuilder('\Box_Database')->disableOriginalConstructor()->getMock();
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn(new \Model_SupportTicket());
$di = $this->getDi();
$di['db'] = $dbMock;
$this->clientApi->setDi($di);
$client = new \Model_Client();
$client->loadBean(new \DummyBean());
$client->id = 1;
$this->clientApi->setService($serviceMock);
$this->clientApi->setIdentity($client);
$data = [
'content' => 'Content',
'id' => 1,
];
$result = $this->clientApi->ticket_reply($data);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testTicketReplyCanNotBeReopenedException(): void
{
$serviceMock = $this->getMockBuilder(\Box\Mod\Support\Service::class)
->onlyMethods(['canBeReopened', 'ticketReply'])->getMock();
$serviceMock->expects($this->atLeastOnce())->method('canBeReopened')
->willReturn(false);
$serviceMock->expects($this->never())->method('ticketReply')
->willReturn(1);
$dbMock = $this->getMockBuilder('\Box_Database')->disableOriginalConstructor()->getMock();
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn(new \Model_SupportTicket());
$di = $this->getDi();
$di['db'] = $dbMock;
$this->clientApi->setDi($di);
$client = new \Model_Client();
$client->loadBean(new \DummyBean());
$client->id = 1;
$this->clientApi->setService($serviceMock);
$this->clientApi->setIdentity($client);
$data = [
'content' => 'Content',
'id' => 1,
];
$this->expectException(\FOSSBilling\Exception::class);
$result = $this->clientApi->ticket_reply($data);
$this->assertIsInt($result);
}
public function testTicketClose(): void
{
$serviceMock = $this->getMockBuilder(\Box\Mod\Support\Service::class)
->onlyMethods(['findOneByClient', 'closeTicket'])->getMock();
$serviceMock->expects($this->atLeastOnce())->method('findOneByClient')
->willReturn(new \Model_SupportTicket());
$serviceMock->expects($this->atLeastOnce())->method('closeTicket')
->willReturn(true);
$di = $this->getDi();
$this->clientApi->setDi($di);
$client = new \Model_Client();
$client->loadBean(new \DummyBean());
$client->id = 1;
$this->clientApi->setService($serviceMock);
$this->clientApi->setIdentity($client);
$data = [
'content' => 'Content',
'id' => 1,
];
$result = $this->clientApi->ticket_close($data);
$this->assertTrue($result);
}
}