Files
FOSSBilling/tests/modules/System/ServiceTest.php
T
Belle Aerni 32d011225b Rector & PHP-CS-Fixer Improvements (#2047)
* Added the rector cache to the action

* Enabled PHP-CS fixer for the tests & library folder

* Use the PHP-CS-Fixer Cache

* Fix the tests

* Ensure the Sentry placeholder isn't messed up from CS changes

* Typo..
2024-01-08 00:10:00 +00:00

406 lines
12 KiB
PHP

<?php
namespace Box\Mod\System;
use Twig\Environment;
class ServiceTest extends \BBTestCase
{
/**
* @var Service
*/
protected $service;
public function setup(): void
{
$this->service = new Service();
}
public function testgetParamValueMissingKeyParam()
{
$param = [];
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Parameter key is missing');
$this->service->getParamValue($param);
}
public function testgetCompany()
{
$config = ['url' => 'www.fossbilling.org'];
$expected = [
'www' => 'http://localhost/',
'name' => 'Inc. Test',
'email' => 'work@example.eu',
'tel' => null,
'signature' => null,
'logo_url' => null,
'logo_url_dark' => null,
'favicon_url' => null,
'address_1' => null,
'address_2' => null,
'address_3' => null,
'account_number' => null,
'bank_name' => null,
'bic' => null,
'display_bank_info' => null,
'bank_info_pagebottom' => null,
'number' => null,
'note' => null,
'privacy_policy' => null,
'tos' => null,
'vat_number' => null,
];
$multParamsResults = [
[
'param' => 'company_name',
'value' => 'Inc. Test',
],
[
'param' => 'company_email',
'value' => 'work@example.eu',
],
];
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->willReturn($multParamsResults);
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['config'] = $config;
$this->service->setDi($di);
$result = $this->service->getCompany();
$this->assertIsArray($result);
$this->assertEquals($expected, $result);
}
public function testgetLanguages()
{
$result = $this->service->getLanguages(true);
$this->assertIsArray($result);
}
public function testgetParams()
{
$expected = [
'company_name' => 'Inc. Test',
'company_email' => 'work@example.eu',
];
$multParamsResults = [
[
'param' => 'company_name',
'value' => 'Inc. Test',
],
[
'param' => 'company_email',
'value' => 'work@example.eu',
],
];
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->willReturn($multParamsResults);
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->getParams([]);
$this->assertIsArray($result);
$this->assertEquals($expected, $result);
}
public function testupdateParams()
{
$data = [
'company_name' => 'newValue',
];
$eventMock = $this->getMockBuilder('\Box_EventManager')->getMock();
$eventMock->expects($this->atLeastOnce())
->method('fire');
$logMock = $this->getMockBuilder('\Box_Log')->getMock();
$systemServiceMock = $this->getMockBuilder('\\' . Service::class)->onlyMethods(['setParamValue'])->getMock();
$systemServiceMock->expects($this->atLeastOnce())
->method('setParamValue')
->willReturn(true);
$di = new \Pimple\Container();
$di['events_manager'] = $eventMock;
$di['logger'] = $logMock;
$systemServiceMock->setDi($di);
$result = $systemServiceMock->updateParams($data);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testgetMessages()
{
$latestVersion = '1.0.0';
$type = 'info';
$systemServiceMock = $this->getMockBuilder('\\' . Service::class)->onlyMethods(['getParamValue'])->getMock();
$systemServiceMock->expects($this->atLeastOnce())
->method('getParamValue')
->willReturn(false);
$updaterMock = $this->getMockBuilder('\\' . \FOSSBilling\Update::class)->getMock();
$updaterMock->expects($this->atLeastOnce())
->method('isUpdateAvailable')
->willReturn(true);
$updaterMock->expects($this->atLeastOnce())
->method('getLatestVersion')
->willReturn($latestVersion);
$di = new \Pimple\Container();
$di['updater'] = $updaterMock;
$di['mod_service'] = $di->protect(fn () => $systemServiceMock);
$systemServiceMock->setDi($di);
$result = $systemServiceMock->getMessages($type);
$this->assertIsArray($result);
}
public function testtemplateExistsEmptyPaths()
{
$getThemeResults = ['paths' => []];
$systemServiceMock = $this->getMockBuilder('\\' . Service::class)->addMethods(['getThemeConfig'])->getMock();
$systemServiceMock->expects($this->atLeastOnce())
->method('getThemeConfig')
->willReturn($getThemeResults);
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn () => $systemServiceMock);
$this->service->setDi($di);
$result = $this->service->templateExists('defaultFile.cp');
$this->assertIsBool($result);
$this->assertFalse($result);
}
public function testrenderStringTemplateException()
{
$vars = [
'_client_id' => 1,
];
$this
->getMockBuilder('Drupal\Core\Template\TwigEnvironment')
->disableOriginalConstructor()
->getMock();
$twigMock = $this->getMockBuilder('\\' . Environment::class)->disableOriginalConstructor()->getMock();
$twigMock->expects($this->atLeastOnce())
->method('addGlobal');
$twigMock->method('createTemplate')
->will($this->throwException(new \Error('Error')));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('load')
->willReturn(new \Model_Client());
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['twig'] = $twigMock;
$di['api_client'] = new \Model_Client();
$this->service->setDi($di);
$this->expectException(\Error::class);
$this->service->renderString('test', false, $vars);
}
public function testrenderStringTemplate()
{
$vars = [
'_client_id' => 1,
];
$twigMock = $this->getMockBuilder(Environment::class)
->disableOriginalConstructor()
->getMock();
$twigMock->expects($this->atLeastOnce())
->method('addGlobal');
// $twigMock->method('createTemplate')
// ->willReturn(new \FakeTemplateWrapper('test'));
// $twigMock->method('load')
// ->willReturn(new \FakeTemplateWrapper('test'));
$twigMock->method('render')
->willReturn('');
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('load')
->willReturn(new \Model_Client());
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['twig'] = $twigMock;
$di['api_client'] = new \Model_Client();
$this->service->setDi($di);
$string = $this->service->renderString('test', true, $vars);
$this->assertEquals($string, 'test');
}
public function testclearCache()
{
$toolsMock = $this->getMockBuilder('\\' . \FOSSBilling\Tools::class)->getMock();
$toolsMock->expects($this->atLeastOnce())
->method('emptyFolder')
->willReturn(true);
$di = new \Pimple\Container();
$di['tools'] = $toolsMock;
$this->service->setDi($di);
$result = $this->service->clearCache();
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testgetPeriod()
{
$code = '1W';
$expexted = 'Every week';
$result = $this->service->getPeriod($code);
$this->assertIsString($result);
$this->assertEquals($expexted, $result);
}
public function testgetCountries()
{
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getConfig')
->willReturn(['countries' => 'US']);
$di = new \Pimple\Container();
$di['mod'] = $di->protect(fn () => $modMock);
$this->service->setDi($di);
$result = $this->service->getCountries();
$this->assertIsArray($result);
}
public function testgetEuCountries()
{
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getConfig')
->willReturn(['countries' => 'US']);
$di = new \Pimple\Container();
$di['mod'] = $di->protect(fn () => $modMock);
$this->service->setDi($di);
$result = $this->service->getEuCountries();
$this->assertIsArray($result);
}
public function testgetStates()
{
$result = $this->service->getStates();
$this->assertIsArray($result);
}
public function testgetPhoneCodes()
{
$data = [];
$result = $this->service->getPhoneCodes($data);
$this->assertIsArray($result);
}
public function testgetVersion()
{
$result = $this->service->getVersion();
$this->assertIsString($result);
$this->assertEquals(\FOSSBilling\Version::VERSION, $result);
}
public function testgetPendingMessages()
{
$di = new \Pimple\Container();
$sessionMock = $this->getMockBuilder('\\' . \FOSSBilling\Session::class)->disableOriginalConstructor()->getMock();
$sessionMock->expects($this->atLeastOnce())
->method('get')
->with('pending_messages')
->willReturn([]);
$di['session'] = $sessionMock;
$this->service->setDi($di);
$result = $this->service->getPendingMessages();
$this->assertIsArray($result);
}
public function testgetPendingMessagesGetReturnsNotArray()
{
$di = new \Pimple\Container();
$sessionMock = $this->getMockBuilder('\\' . \FOSSBilling\Session::class)->disableOriginalConstructor()->getMock();
$sessionMock->expects($this->atLeastOnce())
->method('get')
->with('pending_messages')
->willReturn(null);
$di['session'] = $sessionMock;
$this->service->setDi($di);
$result = $this->service->getPendingMessages();
$this->assertIsArray($result);
}
public function testsetPendingMessage()
{
$serviceMock = $this->getMockBuilder('\\' . Service::class)
->onlyMethods(['getPendingMessages'])
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getPendingMessages')
->willReturn([]);
$di = new \Pimple\Container();
$sessionMock = $this->getMockBuilder('\\' . \FOSSBilling\Session::class)->disableOriginalConstructor()->getMock();
$sessionMock->expects($this->atLeastOnce())
->method('set')
->with('pending_messages');
$di['session'] = $sessionMock;
$serviceMock->setDi($di);
$message = 'Important Message';
$result = $serviceMock->setPendingMessage($message);
$this->assertTrue($result);
}
public function testclearPendingMessages()
{
$di = new \Pimple\Container();
$sessionMock = $this->getMockBuilder('\\' . \FOSSBilling\Session::class)->disableOriginalConstructor()->getMock();
$sessionMock->expects($this->atLeastOnce())
->method('delete')
->with('pending_messages');
$di['session'] = $sessionMock;
$this->service->setDi($di);
$result = $this->service->clearPendingMessages();
$this->assertTrue($result);
}
}