Files
FOSSBilling/tests/modules/Servicecustom/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

560 lines
19 KiB
PHP

<?php
namespace Box\Tests\Mod\Servicecustom;
class ServiceTest extends \BBTestCase
{
/**
* @var \Box\Mod\Servicecustom\Service
*/
protected $service;
public function setup(): void
{
$this->service = new \Box\Mod\Servicecustom\Service();
}
public function testDi()
{
$di = new \Pimple\Container();
$this->service->setDi($di);
$getDi = $this->service->getDi();
$this->assertEquals($di, $getDi);
}
public function testValidateCustomForm()
{
$form = [
'fields' => [
0 => [
'required' => 1,
'readonly' => 1,
'name' => 'field_name',
'default_value' => 'FieldName',
'label' => 'label',
],
],
];
$service = $this->getMockBuilder('\\' . \Box\Mod\Formbuilder\Service::class)->getMock();
$service->expects($this->atLeastOnce())
->method('getForm')
->willReturn($form);
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn () => $service);
$this->service->setDi($di);
$product = [
'form_id' => random_int(1, 100),
];
$data = [
'label' => 'label',
'field_name' => 'FieldName',
];
$result = $this->service->validateCustomForm($data, $product);
$this->assertNull($result);
}
public function testValidateCustomFormFieldNameNotSetException()
{
$form = [
'fields' => [
0 => [
'required' => 1,
'readonly' => 1,
'name' => 'field_name',
'default_value' => 'default',
'label' => 'label',
],
],
];
$service = $this->getMockBuilder('\\' . \Box\Mod\Formbuilder\Service::class)->getMock();
$service->expects($this->atLeastOnce())
->method('getForm')
->willReturn($form);
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn () => $service);
$this->service->setDi($di);
$product = [
'form_id' => random_int(1, 100),
];
$data = [];
$this->expectException(\Exception::class);
$result = $this->service->validateCustomForm($data, $product);
$this->assertNull($result);
}
public function testValidateCustomFormReadonlyFieldChangeException()
{
$form = [
'fields' => [
0 => [
'required' => 1,
'readonly' => 1,
'name' => 'field_name',
'default_value' => 'default',
'label' => 'label',
],
],
];
$service = $this->getMockBuilder('\\' . \Box\Mod\Formbuilder\Service::class)->getMock();
$service->expects($this->atLeastOnce())
->method('getForm')
->willReturn($form);
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn () => $service);
$this->service->setDi($di);
$product = [
'form_id' => random_int(1, 100),
];
$data = [
'field_name' => 'field_name',
];
$this->expectException(\Exception::class);
$result = $this->service->validateCustomForm($data, $product);
$this->assertNull($result);
}
public function testActionCreate()
{
$order = new \Model_ClientOrder();
$order->loadBean(new \DummyBean());
$order->product_id = random_int(1, 100);
$order->client_id = random_int(1, 100);
$order->config = 'config';
$product = new \Model_Product();
$product->loadBean(new \DummyBean());
$product->plugin = 'plugin';
$product->plugin_config = 'plugin_config';
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn(random_int(1, 100));
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->willReturn($product);
$serviceCustomModel = new \Model_ServiceCustom();
$serviceCustomModel->loadBean(new \DummyBean());
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->willReturn($serviceCustomModel);
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->action_create($order);
$this->assertInstanceOf('Model_ServiceCustom', $result);
}
public function testActionActivate()
{
$order = new \Model_ClientOrder();
$order->loadBean(new \DummyBean());
$order->client_id = random_int(1, 100);
$order->config = 'config';
$serviceCustomModel = new \Model_ServiceCustom();
$serviceCustomModel->loadBean(new \DummyBean());
$serviceCustomModel->plugin = '';
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(['getOrderService'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($serviceCustomModel);
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn () => $serviceMock);
$this->service->setDi($di);
$result = $this->service->action_activate($order);
$this->assertTrue($result);
}
public function testActionActivateOrderServiceNotCreatedException()
{
$order = new \Model_ClientOrder();
$order->loadBean(new \DummyBean());
$order->client_id = random_int(1, 100);
$order->config = 'config';
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(['getOrderService'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn(null);
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn () => $serviceMock);
$this->service->setDi($di);
$this->expectException(\Exception::class);
$this->service->action_activate($order);
}
public function testActionRenew()
{
$order = new \Model_ClientOrder();
$order->loadBean(new \DummyBean());
$order->client_id = random_int(1, 100);
$order->config = 'config';
$serviceCustomModel = new \Model_ServiceCustom();
$serviceCustomModel->loadBean(new \DummyBean());
$serviceCustomModel->plugin = '';
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(['getOrderService'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($serviceCustomModel);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn(random_int(1, 100));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn () => $serviceMock);
$this->service->setDi($di);
$result = $this->service->action_renew($order);
$this->assertTrue($result);
}
public function testActiveServiceNotFoundException()
{
$order = new \Model_ClientOrder();
$order->loadBean(new \DummyBean());
$order->id = random_int(1, 100);
$order->client_id = random_int(1, 100);
$order->config = 'config';
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(['getOrderService'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn(null);
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn () => $serviceMock);
$this->service->setDi($di);
$this->expectException(\Exception::class);
$result = $this->service->action_renew($order);
$this->assertTrue($result);
}
public function testActionSuspend()
{
$order = new \Model_ClientOrder();
$order->loadBean(new \DummyBean());
$order->client_id = random_int(1, 100);
$order->config = 'config';
$serviceCustomModel = new \Model_ServiceCustom();
$serviceCustomModel->loadBean(new \DummyBean());
$serviceCustomModel->plugin = '';
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(['getOrderService'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($serviceCustomModel);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn(random_int(1, 100));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn () => $serviceMock);
$this->service->setDi($di);
$result = $this->service->action_suspend($order);
$this->assertTrue($result);
}
public function testActionUnsuspend()
{
$order = new \Model_ClientOrder();
$order->loadBean(new \DummyBean());
$order->client_id = random_int(1, 100);
$order->config = 'config';
$serviceCustomModel = new \Model_ServiceCustom();
$serviceCustomModel->loadBean(new \DummyBean());
$serviceCustomModel->plugin = '';
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(['getOrderService'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($serviceCustomModel);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn(random_int(1, 100));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn () => $serviceMock);
$this->service->setDi($di);
$result = $this->service->action_unsuspend($order);
$this->assertTrue($result);
}
public function testActionCancel()
{
$order = new \Model_ClientOrder();
$order->loadBean(new \DummyBean());
$order->client_id = random_int(1, 100);
$order->config = 'config';
$serviceCustomModel = new \Model_ServiceCustom();
$serviceCustomModel->loadBean(new \DummyBean());
$serviceCustomModel->plugin = '';
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(['getOrderService'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($serviceCustomModel);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn(random_int(1, 100));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn () => $serviceMock);
$this->service->setDi($di);
$result = $this->service->action_cancel($order);
$this->assertTrue($result);
}
public function testActionUncancel()
{
$order = new \Model_ClientOrder();
$order->loadBean(new \DummyBean());
$order->client_id = random_int(1, 100);
$order->config = 'config';
$serviceCustomModel = new \Model_ServiceCustom();
$serviceCustomModel->loadBean(new \DummyBean());
$serviceCustomModel->plugin = '';
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(['getOrderService'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($serviceCustomModel);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn(random_int(1, 100));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn () => $serviceMock);
$this->service->setDi($di);
$result = $this->service->action_uncancel($order);
$this->assertTrue($result);
}
public function testActionDelete()
{
$order = new \Model_ClientOrder();
$order->loadBean(new \DummyBean());
$order->client_id = random_int(1, 100);
$order->config = 'config';
$serviceCustomModel = new \Model_ServiceCustom();
$serviceCustomModel->loadBean(new \DummyBean());
$serviceCustomModel->plugin = '';
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(['getOrderService'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($serviceCustomModel);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('trash')
->willReturn(null);
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn () => $serviceMock);
$this->service->setDi($di);
$result = $this->service->action_delete($order);
$this->assertTrue($result);
}
public function testGetConfig()
{
$decoded = [
'J' => 5,
0 => 'N',
];
$toolsMock = $this->getMockBuilder('\\' . \FOSSBilling\Tools::class)->getMock();
$toolsMock->expects($this->atLeastOnce())
->method('decodeJ')
->willReturn($decoded);
$di = new \Pimple\Container();
$di['tools'] = $toolsMock;
$this->service->setDi($di);
$model = new \Model_ServiceCustom();
$model->loadBean(new \DummyBean());
$model->config = json_encode($decoded);
$result = $this->service->getConfig($model);
$this->assertEquals($result, $decoded);
}
public function testToApiArray()
{
$toolsMock = $this->getMockBuilder('\\' . \FOSSBilling\Tools::class)->getMock();
$toolsMock->expects($this->atLeastOnce())
->method('decodeJ')
->willReturn(['config_param' => 'config_value']);
$di = new \Pimple\Container();
$di['tools'] = $toolsMock;
$this->service->setDi($di);
$model = new \Model_ServiceCustom();
$model->loadBean(new \DummyBean());
$model->id = random_int(1, 100);
$model->client_id = random_int(1, 100);
$model->plugin = 'plugin';
$model->config = 'config_json';
$model->updated_at = date('Y-m-d H:i:s');
$model->created_at = date('Y-m-d H:i:s');
$result = $this->service->toApiArray($model);
$this->assertEquals($result['client_id'], $model->client_id);
$this->assertEquals($result['plugin'], $model->plugin);
$this->assertEquals($result['config_param'], 'config_value');
$this->assertEquals($result['updated_at'], $model->updated_at);
$this->assertEquals($result['created_at'], $model->created_at);
}
public function testCustomCallForbiddenMethodException()
{
$this->expectException(\Exception::class);
$this->service->customCall(new \Model_ServiceCustom(), 'delete');
}
public function testGetServiceCustomByOrderId()
{
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->willReturn(new \Model_ClientOrder());
$orderService = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(['getOrderService'])->getMock();
$orderService->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn(new \Model_ServiceCustom());
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn () => $orderService);
$this->service->setDi($di);
$result = $this->service->getServiceCustomByOrderId(random_int(1, 100));
$this->assertInstanceOf('Model_ServiceCustom', $result);
}
public function testGetServiceCustomByOrderIdOrderServiceNotFoundException()
{
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->willReturn(new \Model_ClientOrder());
$orderService = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(['getOrderService'])->getMock();
$orderService->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn(null);
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn () => $orderService);
$this->service->setDi($di);
$this->expectException(\Exception::class);
$this->service->getServiceCustomByOrderId(random_int(1, 100));
}
public function testUpdateConfig()
{
$model = new \Model_ServiceCustom();
$model->loadBean(new \DummyBean());
$model->id = random_int(1, 100);
$serviceMock = $this->getMockBuilder(\Box\Mod\Servicecustom\Service::class)->onlyMethods(['getServiceCustomByOrderId'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getServiceCustomByOrderId')
->willReturn($model);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn(random_int(1, 100));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['logger'] = $di['logger'] = $this->getMockBuilder('Box_Log')->getMock();
$serviceMock->setDi($di);
$config = ['param1' => 'value1'];
$result = $serviceMock->updateConfig(random_int(1, 100), $config);
$this->assertNull($result);
}
public function testUpdateConfigNotArrayException()
{
$model = new \Model_ServiceCustom();
$model->loadBean(new \DummyBean());
$model->id = random_int(1, 100);
$serviceMock = $this->getMockBuilder(\Box\Mod\Servicecustom\Service::class)->onlyMethods(['getServiceCustomByOrderId'])->getMock();
$serviceMock->expects($this->never())
->method('getServiceCustomByOrderId')
->willReturn($model);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->never())
->method('store')
->willReturn(random_int(1, 100));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['logger'] = $di['logger'] = $this->getMockBuilder('Box_Log')->getMock();
$serviceMock->setDi($di);
$config = '';
$this->expectException(\Exception::class);
$serviceMock->updateConfig(random_int(1, 100), $config);
}
}