Files
FOSSBilling/tests-legacy/modules/Activity/ServiceTest.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

147 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Tests\Mod\Activity;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
#[Group('Core')]
final class ServiceTest extends \BBTestCase
{
public function testDi(): void
{
$service = new \Box\Mod\Activity\Service();
$di = $this->getDi();
$db = $this->createMock('Box_Database');
$di['db'] = $db;
$service->setDi($di);
$result = $service->getDi();
$this->assertEquals($di, $result);
}
public static function searchFilters(): array
{
return [
[[], 'FROM activity_system ', true],
[['user_filter' => 'only_clients'], 'm.client_id IS NOT NULL', true],
[['user_filter' => 'only_staff'], 'm.admin_id IS NOT NULL', true],
[['priority' => '2'], 'm.priority =', true],
[['search' => 'keyword'], 'm.message LIKE ', true],
[['min_priority' => 6], 'm.priority <= :min_priority', true],
[['priority' => 6], 'm.priority = :priority', true],
// When both priority and min_priority are set, priority takes precedence
[['priority' => 5, 'min_priority' => 3], 'm.priority = :priority', true],
[['priority' => 5, 'min_priority' => 3], 'm.priority <= :min_priority', false],
];
}
#[DataProvider('searchFilters')]
public function testGetSearchQuery(array $filterKey, string $search, bool $expected): void
{
$di = $this->getDi();
$service = new \Box\Mod\Activity\Service();
$service->setDi($di);
$result = $service->getSearchQuery($filterKey);
$this->assertIsString($result[0]);
$this->assertIsArray($result[1]);
$this->assertEquals($expected, str_contains($result[0], $search));
}
public function testLogEmail(): void
{
$service = new \Box\Mod\Activity\Service();
$data = [
'client_id' => 1,
'sender' => 'sender',
'recipients' => 'recipients',
'subject' => 'subject',
'content_html' => 'html',
'content_text' => 'text',
];
$model = new \Model_ActivityClientEmail();
$model->loadBean(new \DummyBean());
$di = $this->getDi();
$db = $this->createMock('Box_Database');
$db->expects($this->atLeastOnce())
->method('dispense')
->willReturn($model);
$db->expects($this->atLeastOnce())
->method('store')
->willReturn([]);
$di['db'] = $db;
$service->setDi($di);
$result = $service->logEmail($data['subject'], $data['client_id'], $data['sender'], $data['recipients'], $data['content_html'], $data['content_text']);
$this->assertTrue($result);
}
public function testToApiArray(): void
{
$clientHistoryModel = new \Model_ActivityClientHistory();
$clientHistoryModel->loadBean(new \DummyBean());
$clientHistoryModel->client_id = 1;
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$expectionError = 'Client not found';
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->with('Client', $clientHistoryModel->client_id, $expectionError)
->willReturn($clientModel);
$di = $this->getDi();
$di['db'] = $dbMock;
$service = new \Box\Mod\Activity\Service();
$service->setDi($di);
$result = $service->toApiArray($clientHistoryModel);
$this->assertIsArray($result);
$this->assertArrayHasKey('id', $result);
$this->assertArrayHasKey('ip', $result);
$this->assertArrayHasKey('created_at', $result);
$this->assertIsArray($result['client']);
$this->assertArrayHasKey('id', $result['client']);
$this->assertArrayHasKey('first_name', $result['client']);
$this->assertArrayHasKey('last_name', $result['client']);
$this->assertArrayHasKey('email', $result['client']);
}
public function testRmByClient(): void
{
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientModel->id = 1;
$activitySystemModel = new \Model_ActivitySystem();
$activitySystemModel->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('find')
->with('ActivitySystem', 'client_id = ?', [$clientModel->id])
->willReturn([$activitySystemModel]);
$dbMock->expects($this->atLeastOnce())
->method('trash')
->with($activitySystemModel);
$di = $this->getDi();
$di['db'] = $dbMock;
$service = new \Box\Mod\Activity\Service();
$service->setDi($di);
$service->rmByClient($clientModel);
}
}