Files
FOSSBilling/tests-legacy/modules/Servicehosting/Api/ClientTest.php
T
Copilotandadmdly 7fbd89f840 Refactor test quality: extract pagination mock helper, fix currency codes, clean up deprecated annotations (#3439)
* Initial plan

* refactor(tests): extract pagination mock helper, fix currency codes, rename api property, remove deprecated annotations

Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/e7163564-4994-454a-b30c-179118effbf5

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
2026-04-25 02:23:52 +01:00

264 lines
8.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Mod\Servicehosting\Api;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
#[Group('Core')]
final class ClientTest extends \BBTestCase
{
protected ?Client $clientApi;
public function setUp(): void
{
$this->clientApi = new Client();
}
public function testGetDi(): void
{
$di = $this->getDi();
$this->clientApi->setDi($di);
$getDi = $this->clientApi->getDi();
$this->assertEquals($di, $getDi);
}
public function testChangeUsername(): void
{
$getServiceReturnValue = [new \Model_ClientOrder(), new \Model_ServiceHosting()];
$apiMock = $this->getMockBuilder(Client::class)
->onlyMethods(['_getService'])
->getMock();
$apiMock->expects($this->atLeastOnce())
->method('_getService')
->willReturn($getServiceReturnValue);
$serviceMock = $this->createMock(\Box\Mod\Servicehosting\Service::class);
$serviceMock->expects($this->atLeastOnce())
->method('changeAccountUsername')
->willReturn(true);
$apiMock->setService($serviceMock);
$result = $apiMock->change_username([]);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testChangeDomain(): void
{
$getServiceReturnValue = [new \Model_ClientOrder(), new \Model_ServiceHosting()];
$apiMock = $this->getMockBuilder(Client::class)
->onlyMethods(['_getService'])
->getMock();
$apiMock->expects($this->atLeastOnce())
->method('_getService')
->willReturn($getServiceReturnValue);
$serviceMock = $this->createMock(\Box\Mod\Servicehosting\Service::class);
$serviceMock->expects($this->atLeastOnce())
->method('changeAccountDomain')
->willReturn(true);
$apiMock->setService($serviceMock);
$result = $apiMock->change_domain([]);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testChangePassword(): void
{
$getServiceReturnValue = [new \Model_ClientOrder(), new \Model_ServiceHosting()];
$apiMock = $this->getMockBuilder(Client::class)
->onlyMethods(['_getService'])
->getMock();
$apiMock->expects($this->atLeastOnce())
->method('_getService')
->willReturn($getServiceReturnValue);
$serviceMock = $this->createMock(\Box\Mod\Servicehosting\Service::class);
$serviceMock->expects($this->atLeastOnce())
->method('changeAccountPassword')
->willReturn(true);
$apiMock->setService($serviceMock);
$result = $apiMock->change_password([]);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testHpGetPairs(): void
{
$serviceMock = $this->createMock(\Box\Mod\Servicehosting\Service::class);
$serviceMock->expects($this->atLeastOnce())
->method('getHpPairs')
->willReturn([]);
$this->clientApi->setService($serviceMock);
$result = $this->clientApi->hp_get_pairs([]);
$this->assertIsArray($result);
}
public function testGetService(): void
{
$data = [
'order_id' => 1,
];
$clientOrderModel = new \Model_ClientOrder();
$clientOrderModel->loadBean(new \DummyBean());
$clientOrderModel->status = \Model_ClientOrder::STATUS_ACTIVE;
$dbMock = $this->createMock(\Box_Database::class);
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn($clientOrderModel);
$model = new \Model_ServiceHosting();
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($model);
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$di['db'] = $dbMock;
$this->clientApi->setDi($di);
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientModel->id = 1;
$this->clientApi->setIdentity($clientModel);
$result = $this->clientApi->_getService($data);
$this->assertIsArray($result);
$this->assertInstanceOf('\Model_ClientOrder', $result[0]);
$this->assertInstanceOf('\Model_ServiceHosting', $result[1]);
}
public function testGetServiceOrderNotActivated(): void
{
$data = [
'order_id' => 1,
];
$clientOrderModel = new \Model_ClientOrder();
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn($clientOrderModel);
$model = null;
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($model);
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$di['db'] = $dbMock;
$this->clientApi->setDi($di);
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientModel->id = 1;
$this->clientApi->setIdentity($clientModel);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Order is not activated');
$this->clientApi->_getService($data);
}
public static function inactiveOrderStatusProvider(): array
{
return [
[\Model_ClientOrder::STATUS_SUSPENDED],
[\Model_ClientOrder::STATUS_CANCELED],
[\Model_ClientOrder::STATUS_PENDING_SETUP],
[\Model_ClientOrder::STATUS_FAILED_SETUP],
[\Model_ClientOrder::STATUS_FAILED_RENEW],
];
}
#[DataProvider('inactiveOrderStatusProvider')]
public function testGetServiceInactiveOrderStatusException(string $status): void
{
$data = [
'order_id' => 1,
];
$clientOrderModel = new \Model_ClientOrder();
$clientOrderModel->loadBean(new \DummyBean());
$clientOrderModel->status = $status;
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn($clientOrderModel);
$model = new \Model_ServiceHosting();
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($model);
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$di['db'] = $dbMock;
$this->clientApi->setDi($di);
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientModel->id = 1;
$this->clientApi->setIdentity($clientModel);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Order is not activated');
$this->clientApi->_getService($data);
}
public function testGetServiceOrderNotFound(): void
{
$data = [
'order_id' => 1,
];
$clientOrderModel = null;
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn($clientOrderModel);
$di = $this->getDi();
$di['db'] = $dbMock;
$this->clientApi->setDi($di);
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientModel->id = 1;
$this->clientApi->setIdentity($clientModel);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Order not found');
$this->clientApi->_getService($data);
}
public function testGetServiceMissingOrderId(): void
{
$data = [];
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Order ID is required');
$this->clientApi->_getService($data);
}
}