Files
FOSSBilling/tests/modules/Servicecustom/ServiceTest.php
T
Belle Aerni 50043e6211 Run Rector against our tests (#1761)
* Update rector config to include tests

* Run Rector against the tests

* Delete test-results

* Update .gitignore

* Correctly set the PHP min to 8.1, split the configs

* Corrected PHPDoc

* Deleted tests we can't really get to pass

PHPunit is hard-coded to fail when a class has a return type of `never`, which is the correct return type of our redirect function (and rightfully so).

No way to disable the exception as far as we can tell
2023-11-11 18:41:09 +00:00

566 lines
20 KiB
PHP

<?php
namespace Box\Tests\Mod\Servicecustom;
use \RedBeanPHP\SimpleModel;
class ServiceTest extends \BBTestCase
{
/**
* @var \Box\Mod\Servicecustom\Service
*/
protected $service = null;
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 = array(
'fields' => array(
0 => array(
'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')
->will($this->returnValue($form));
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn() => $service);
$this->service->setDi($di);
$product = array(
'form_id' => random_int(1, 100)
);
$data = array(
'label' => 'label',
'field_name' => 'FieldName'
);
$result = $this->service->validateCustomForm($data, $product);
$this->assertNull($result);
}
public function testValidateCustomFormFieldNameNotSetException()
{
$form = array(
'fields' => array(
0 => array(
'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')
->will($this->returnValue($form));
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn() => $service);
$this->service->setDi($di);
$product = array(
'form_id' => random_int(1, 100)
);
$data = array();
$this->expectException(\Exception::class);
$result = $this->service->validateCustomForm($data, $product);
$this->assertNull($result);
}
public function testValidateCustomFormReadonlyFieldChangeException()
{
$form = array(
'fields' => array(
0 => array(
'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')
->will($this->returnValue($form));
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn() => $service);
$this->service->setDi($di);
$product = array(
'form_id' => random_int(1, 100)
);
$data = array(
'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')
->will($this->returnValue(random_int(1, 100)));
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->will($this->returnValue($product));
$serviceCustomModel = new \Model_ServiceCustom();
$serviceCustomModel->loadBean(new \DummyBean());
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->will($this->returnValue($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(array('getOrderService'))->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->will($this->returnValue($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(array('getOrderService'))->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->will($this->returnValue(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(array('getOrderService'))->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->will($this->returnValue($serviceCustomModel));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue(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(array('getOrderService'))->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->will($this->returnValue(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(array('getOrderService'))->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->will($this->returnValue($serviceCustomModel));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue(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(array('getOrderService'))->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->will($this->returnValue($serviceCustomModel));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue(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(array('getOrderService'))->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->will($this->returnValue($serviceCustomModel));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue(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(array('getOrderService'))->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->will($this->returnValue($serviceCustomModel));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue(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(array('getOrderService'))->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getOrderService')
->will($this->returnValue($serviceCustomModel));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('trash')
->will($this->returnValue(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 = array(
'J' => 5,
0 => 'N'
);
$toolsMock = $this->getMockBuilder('\\' . \FOSSBilling\Tools::class)->getMock();
$toolsMock->expects($this->atLeastOnce())
->method('decodeJ')
->will($this->returnValue($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')
->will($this->returnValue(array('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')
->will($this->returnValue(new \Model_ClientOrder()));
$orderService = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(array('getOrderService'))->getMock();
$orderService->expects($this->atLeastOnce())
->method('getOrderService')
->will($this->returnValue(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')
->will($this->returnValue(new \Model_ClientOrder()));
$orderService = $this->getMockBuilder('\\' . \Box\Mod\Order\Service::class)->onlyMethods(array('getOrderService'))->getMock();
$orderService->expects($this->atLeastOnce())
->method('getOrderService')
->will($this->returnValue(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(array('getServiceCustomByOrderId'))->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getServiceCustomByOrderId')
->will($this->returnValue($model));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue(random_int(1, 100)));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['logger'] = $di['logger'] = $this->getMockBuilder('Box_Log')->getMock();
$serviceMock->setDi($di);
$config = array('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(array('getServiceCustomByOrderId'))->getMock();
$serviceMock->expects($this->never())
->method('getServiceCustomByOrderId')
->will($this->returnValue($model));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->never())
->method('store')
->will($this->returnValue(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);
}
}