From d243c28395a053a30501e878a126f49cf1249a1b Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Wed, 11 Feb 2015 13:32:44 +0200 Subject: [PATCH 1/2] create deductFunds() at ServiceBalance --- src/bb-modules/Client/ServiceBalance.php | 35 ++++++- .../bb-modules/Client/ServiceBalanceTest.php | 93 +++++++++++++++++++ 2 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 tests/bb-modules/Client/ServiceBalanceTest.php diff --git a/src/bb-modules/Client/ServiceBalance.php b/src/bb-modules/Client/ServiceBalance.php index edaa62158..61f97c402 100644 --- a/src/bb-modules/Client/ServiceBalance.php +++ b/src/bb-modules/Client/ServiceBalance.php @@ -20,7 +20,7 @@ class ServiceBalance implements InjectionAwareInterface protected $di = null; /** - * @param Box_Di|null $di + * @param \Box_Di|null $di */ public function setDi($di) { @@ -28,7 +28,7 @@ class ServiceBalance implements InjectionAwareInterface } /** - * @return Box_Di|null + * @return \Box_Di|null */ public function getDi() { @@ -118,4 +118,35 @@ class ServiceBalance implements InjectionAwareInterface return array($q, $params); } + + /** + * @param \Model_Client $client + * @param double $amount + * @param string $description + * @param array $data + * @return \Model_ClientBalance + * @throws \Box_Exception + */ + public function deductFunds(\Model_Client $client, $amount, $description, array $data = null) + { + if(!is_numeric($amount)) { + throw new \Box_Exception('Funds amount is not valid'); + } + + if(strlen(trim($description)) == 0) { + throw new \Box_Exception('Funds description is not valid'); + } + + $credit = $this->di['db']->dispense('ClientBalance'); + $credit->client_id = $client->id; + $credit->type = isset($data['type']) ? $data['type'] : 'default'; + $credit->rel_id = isset($data['rel_id']) ? $data['rel_id'] : null; + $credit->description = $description; + $credit->amount = -$amount; + $credit->created_at = date('Y-m-d H:i:s'); + $credit->updated_at = date('Y-m-d H:i:s'); + $this->di['db']->store($credit); + + return $credit; + } } \ No newline at end of file diff --git a/tests/bb-modules/Client/ServiceBalanceTest.php b/tests/bb-modules/Client/ServiceBalanceTest.php new file mode 100644 index 000000000..9e17ccfb0 --- /dev/null +++ b/tests/bb-modules/Client/ServiceBalanceTest.php @@ -0,0 +1,93 @@ +setDi($di); + $getDi = $service->getDi(); + $this->assertEquals($di, $getDi); + } + + public function testdeductFunds() + { + $di = new \Box_Di(); + + $clientBalance = new \Model_ClientBalance(); + $clientBalance->loadBean(new \RedBeanPHP\OODBBean()); + + $dbMock = $this->getMockBuilder('\Box_Database')->getMock(); + $dbMock->expects($this->atLeastOnce()) + ->method('dispense') + ->with('ClientBalance') + ->willReturn($clientBalance); + $dbMock->expects($this->atLeastOnce()) + ->method('store') + ->with($clientBalance); + $di['db'] = $dbMock; + + $service = new \Box\Mod\Client\ServiceBalance(); + $service->setDi($di); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $description = 'Charged for product'; + $amount = 5.55; + + $extra = array( + 'rel_id' => 1, + ); + + $result = $service->deductFunds($clientModel, $amount, $description, $extra); + + $this->assertInstanceOf('\Model_ClientBalance', $result); + $this->assertEquals(-$amount, $result->amount); + $this->assertEquals($description, $result->description); + $this->assertEquals($extra['rel_id'], $result->rel_id); + $this->assertEquals('default', $result->type); + } + + public function testdeductFunds_InvalidDescription() + { + $service = new \Box\Mod\Client\ServiceBalance(); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $description = ' '; + $amount = 5.55; + + $extra = array( + 'rel_id' => 1, + ); + + $this->setExpectedException('\Box_Exception', 'Funds description is not valid'); + $service->deductFunds($clientModel, $amount, $description, $extra); + } + + public function testdeductFunds_InvalidAmount() + { + $service = new \Box\Mod\Client\ServiceBalance(); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $description = 'Charged'; + $amount = "5.5adadzxc"; + + $extra = array( + 'rel_id' => 1, + ); + + $this->setExpectedException('\Box_Exception', 'Funds amount is not valid'); + $service->deductFunds($clientModel, $amount, $description, $extra); + } +} + \ No newline at end of file From 5119de6bdab3e5971d24a68778d545f5cf468de6 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Wed, 11 Feb 2015 17:41:16 +0200 Subject: [PATCH 2/2] create PaidSupport module --- CHANGELOG.md | 5 + src/bb-modules/Paidsupport/Service.php | 126 +++++++ .../html_admin/mod_paidsupport_settings.phtml | 46 +++ src/bb-modules/Paidsupport/icon.png | Bin 0 -> 4886 bytes src/bb-modules/Paidsupport/manifest.json | 12 + tests/bb-modules/Paidsupport/ServiceTest.php | 324 ++++++++++++++++++ .../mod_paidsupport/ServiceTest.php | 88 +++++ 7 files changed, 601 insertions(+) create mode 100644 src/bb-modules/Paidsupport/Service.php create mode 100644 src/bb-modules/Paidsupport/html_admin/mod_paidsupport_settings.phtml create mode 100644 src/bb-modules/Paidsupport/icon.png create mode 100644 src/bb-modules/Paidsupport/manifest.json create mode 100644 tests/bb-modules/Paidsupport/ServiceTest.php create mode 100644 tests/integration/bb-modules/mod_paidsupport/ServiceTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0868ee482..e8c76d9b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +Release 4.15 +------------------------------------------------------------------------------ +* New module paidsupport + + Release 4.14 (2015-02-03) ------------------------------------------------------------------------------ diff --git a/src/bb-modules/Paidsupport/Service.php b/src/bb-modules/Paidsupport/Service.php new file mode 100644 index 000000000..5c1745fb7 --- /dev/null +++ b/src/bb-modules/Paidsupport/Service.php @@ -0,0 +1,126 @@ +di = $di; + } + + /** + * @return mixed + */ + public function getDi() + { + return $this->di; + } + + public static function onBeforeClientOpenTicket(\Box_Event $event) + { + $di = $event->getDi(); + $params = $event->getParameters(); + + $client = $di['db']->load('Client', $params['client_id']); + + $paidSupportService = $di['mod_service']('Paidsupport'); + $paidSupportService->setDi($di); + $paidSupportService->enoughInBalanceToOpenTicket($client); + + return true; + } + + /** + * @param \Box_Event $event + * @return bool + */ + public static function onAfterClientOpenTicket(\Box_Event $event) + { + $di = $event->getDi(); + $params = $event->getParameters(); + + $supportTicket = $di['db']->load('SupportTicket', $params['id']); + $client = $di['db']->load('Client', $supportTicket->client_id); + + $paidSupportService = $di['mod_service']('Paidsupport'); + $paidSupportService->setDi($di); + $paidSupportService->enoughInBalanceToOpenTicket($client); + + $clientBalanceService = $di['mod_service']('Client', 'Balance'); + $clientBalanceService->setDi($di); + + $message = sprintf('Paid support ticket#%d "%s" opened', $supportTicket->id, $supportTicket->subject); + $extra = array( + 'rel_id' => $supportTicket->id, + 'type' => 'supportticket', + ); + $clientBalanceService->deductFunds($client, $paidSupportService->getTicketPrice(), $message, $extra); + + return true; + } + + /** + * @return float + */ + public function getTicketPrice() + { + $config = $this->di['mod_config']('Paidsupport'); + if (!isset($config['ticket_price'])){ + error_log('Paid Support ticket price is not set'); + return (float) 0; + } + return (float) $config['ticket_price']; + } + + /** + * @return string + */ + public function getErrorMessage() + { + $config = $this->di['mod_config']('Paidsupport'); + return isset($config['error_msg']) ? $config['error_msg'] : 'Configure paid support module!'; + } + + public function enoughInBalanceToOpenTicket(\Model_Client $client) + { + $clientBalanceService = $this->di['mod_service']('Client', 'Balance'); + $clientBalance = $clientBalanceService->getClientBalance($client); + + if ($this->getTicketPrice() > $clientBalance){ + throw new \Box_Exception($this->getErrorMessage()); + } + + return true; + } + + +} \ No newline at end of file diff --git a/src/bb-modules/Paidsupport/html_admin/mod_paidsupport_settings.phtml b/src/bb-modules/Paidsupport/html_admin/mod_paidsupport_settings.phtml new file mode 100644 index 000000000..7906b7994 --- /dev/null +++ b/src/bb-modules/Paidsupport/html_admin/mod_paidsupport_settings.phtml @@ -0,0 +1,46 @@ +{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %} +{% import "macro_functions.phtml" as mf %} +{% block meta_title %}{% trans 'Paid support settings' %}{% endblock %} +{% set active_menu = 'system' %} + +{% block breadcrumbs %} + +{% endblock %} + +{% block content %} + +
+
+
{% trans 'Paid support settings' %}
+
+ + {% set params = admin.extension_config_get({"ext":"mod_paidsupport"}) %} +
+
+
+ +
+ +
+
+
+
+ +
+ +
+
+
+ + + + +
+
+
+ +{% endblock %} \ No newline at end of file diff --git a/src/bb-modules/Paidsupport/icon.png b/src/bb-modules/Paidsupport/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..2af03dd469199f13625c3104250a0c96e5404a6f GIT binary patch literal 4886 zcmWky2{=^W8@^-Tvn5n$D3!8}LUzr_k}MfyAIn%n24gp*jO-!XkS)uMT?^SJk!_5B z%VbGRsHq`_es-q+be`v&@45Fm_dVzP&UfGQo|9&M>xLkoG#>x}f@Y>rTb9NAt2|t+ z_N_i*oMkvr#%6XrtP;oLjb%OahML|%0RU9tUj-TZ4%)F^N`=E5!*7Qm!_kN^9{`O; zEBglrqP!5HKFT3szNKrr(f}ZuX9hL0i^Babi!E~Ti6$m4%y58nMl9K1dR(#Qr#~e1 zbX>9!s5$T1gvHK1+OietkrR zf0KOXjxujIB;>D_?-CNCgIe?5-i$4eZZFneIOz2y>n!Y%cY06wB5z)WdMh(#saN*b zkt!I2jA*@&t9tOG>j}QO$AYU)Vz5ES>@e)f4l0b2jMuT!oxYy}3-CfdpB$8Lnz9S$ ze#Pvt4+HUr4o3JMfjN*1`&$Q7Q9Ln5%&G`Py zPQ}P@91F9VxJSGJIJ92#Zxhiv(P;>Gzu;8-KAbfiHr0UilzY{s_1c{cxzKj}Jp4IW zK0F14HPsB|WG>CwQ4fe0%2W-`&jHY7j>Cgx0v}uzp(i!~(Kx+i%_=EE9 zDCU*MCf&D-&>{$_cXMJ#PEnm|RT4z^F)F?@Mo&-}6;KGtnrZj#KaW6NF?BUSWai|q zw?uoLoJ=U<$v_9y*aKP;#sY0=j z;!l3?Fm{M9FN~GeY7cXy2Q}o>f@%VaKW2bwIU$uRrx_wQ+(S=_koozS}d7^@*5M=5B)3a}T}(;>tHPPz?;&;mk1#9xx=c3}iN zE{NYRrLLddwYhpbt^ejlIYIXGA69%jIvVi$z?@5kGAw6zxNw9uL$pVCjrm|rEq*5> zdj0lp-avzK8y7&bz#Fplscm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@a3# literal 0 HcmV?d00001 diff --git a/src/bb-modules/Paidsupport/manifest.json b/src/bb-modules/Paidsupport/manifest.json new file mode 100644 index 000000000..d743d3069 --- /dev/null +++ b/src/bb-modules/Paidsupport/manifest.json @@ -0,0 +1,12 @@ +{ + "id": "paidsupport", + "type": "mod", + "name": "Paid support module", + "description": "Module enables paid support service for your clients.", + "icon_url": "icon.png", + "homepage_url": "http://github.com/boxbilling/", + "author": "BoxBilling", + "author_url": "http://github.com/boxbilling/", + "license": "GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html", + "version": "1.0.0" +} \ No newline at end of file diff --git a/tests/bb-modules/Paidsupport/ServiceTest.php b/tests/bb-modules/Paidsupport/ServiceTest.php new file mode 100644 index 000000000..fa8b04930 --- /dev/null +++ b/tests/bb-modules/Paidsupport/ServiceTest.php @@ -0,0 +1,324 @@ +service= new \Box\Mod\Paidsupport\Service(); + } + + public function testgetDi() + { + $di = new \Box_Di(); + $this->service->setDi($di); + $getDi = $this->service->getDi(); + $this->assertEquals($di, $getDi); + } + + public function testenoughInBalanceToOpenTicket() + { + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $clientTotalAmount = 25; + + $clientBalanceMock = $this->getMockBuilder('\Box\Mod\Client\ServiceBalance')->getMock(); + $clientBalanceMock->expects($this->atLeastOnce()) + ->method('getClientBalance') + ->with($clientModel) + ->willReturn($clientTotalAmount); + + $paidSupportConfig = array( + 'ticket_price' => 2, + 'error_msg' => 'Insufficient funds to open ticket', + ); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function($serviceName, $subService) use ($clientBalanceMock){ + if ($serviceName == 'Client' && $subService == 'Balance') { + return $clientBalanceMock; + } + }); + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + $this->service->setDi($di); + + $result = $this->service->enoughInBalanceToOpenTicket($clientModel); + $this->assertTrue($result); + } + + public function test_NotenoughInBalanceToOpenTicket() + { + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $clientTotalAmount = 0; + + $clientBalanceMock = $this->getMockBuilder('\Box\Mod\Client\ServiceBalance')->getMock(); + $clientBalanceMock->expects($this->atLeastOnce()) + ->method('getClientBalance') + ->with($clientModel) + ->willReturn($clientTotalAmount); + + $paidSupportConfig = array( + 'ticket_price' => 2, + 'error_msg' => 'Insufficient funds to open ticket', + ); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function($serviceName, $subService) use ($clientBalanceMock){ + if ($serviceName == 'Client' && $subService == 'Balance') { + return $clientBalanceMock; + } + }); + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + $this->service->setDi($di); + + $this->setExpectedException('\Box_Exception', $paidSupportConfig['error_msg']); + $this->service->enoughInBalanceToOpenTicket($clientModel); + } + + public function test_enoughInBalanceToOpenTicket_TicketPriceEqualsTotalAmount() + { + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $clientTotalAmount = 4; + + $clientBalanceMock = $this->getMockBuilder('\Box\Mod\Client\ServiceBalance')->getMock(); + $clientBalanceMock->expects($this->atLeastOnce()) + ->method('getClientBalance') + ->with($clientModel) + ->willReturn($clientTotalAmount); + + $paidSupportConfig = array( + 'ticket_price' => $clientTotalAmount, + 'error_msg' => 'Insufficient funds to open ticket', + ); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function($serviceName, $subService) use ($clientBalanceMock){ + if ($serviceName == 'Client' && $subService == 'Balance') { + return $clientBalanceMock; + } + }); + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + $this->service->setDi($di); + + $result = $this->service->enoughInBalanceToOpenTicket($clientModel); + $this->assertTrue($result); + } + + public function test_enoughInBalanceToOpenTicket_TicketPriceIsNotSet() + { + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $clientTotalAmount = 4; + + $clientBalanceMock = $this->getMockBuilder('\Box\Mod\Client\ServiceBalance')->getMock(); + $clientBalanceMock->expects($this->atLeastOnce()) + ->method('getClientBalance') + ->with($clientModel) + ->willReturn($clientTotalAmount); + + $paidSupportConfig = array(); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function($serviceName, $subService) use ($clientBalanceMock){ + if ($serviceName == 'Client' && $subService == 'Balance') { + return $clientBalanceMock; + } + }); + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + $this->service->setDi($di); + + $result = $this->service->enoughInBalanceToOpenTicket($clientModel); + $this->assertTrue($result); + } + + public function testonBeforeClientOpenTicket() + { + $di = new \Box_Di(); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $dbMock = $this->getMockBuilder('\Box_Database')->getMock(); + $dbMock->expects($this->atLeastOnce()) + ->method('load') + ->with('Client') + ->willReturn($clientModel); + $di['db'] = $dbMock; + + $paidSupportMock = $this->getMockBuilder('\Box\Mod\Paidsupport\Service')->getMock(); + $paidSupportMock->expects($this->atLeastOnce()) + ->method('enoughInBalanceToOpenTicket') + ->with($clientModel); + $di['mod_service'] = $di->protect(function ($serviceName) use($paidSupportMock){ + if ($serviceName == 'Paidsupport'){ + return $paidSupportMock; + } + }); + + $params = array( + 'client_id' => 1, + ); + + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getParameters') + ->willReturn($params); + + $result = $this->service->onBeforeClientOpenTicket($boxEventMock); + $this->assertTrue($result); + } + + public function testgetTicketPrice() + { + $di = new \Box_Di(); + $paidSupportConfig = array( + 'ticket_price' => 1, + ); + + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + + $this->service->setDi($di); + $result = $this->service->getTicketPrice(); + $this->assertEquals($paidSupportConfig['ticket_price'], $result); + } + + public function testgetTicketPrice_NotSet() + { + $di = new \Box_Di(); + $paidSupportConfig = array(); + + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + + $this->service->setDi($di); + $result = $this->service->getTicketPrice(); + $this->assertEquals(0, $result); + } + + public function testgetErrorMessage() + { + $di = new \Box_Di(); + $errorMessage = 'Not enough funds'; + $paidSupportConfig = array( + 'error_msg' => $errorMessage, + ); + + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + + $this->service->setDi($di); + $result = $this->service->getErrorMessage(); + $this->assertEquals($errorMessage, $result); + } + + public function testgetErrorMessage_NotSet() + { + $di = new \Box_Di(); + $errorMessage = 'Configure paid support module!'; + $paidSupportConfig = array(); + + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + + $this->service->setDi($di); + $result = $this->service->getErrorMessage(); + $this->assertEquals($errorMessage, $result); + } + + public function testonAfterClientOpenTicket() + { + $di = new \Box_Di(); + + $supportTicketModel = new \Model_SupportTicket(); + $supportTicketModel->loadBean(new \RedBeanPHP\OODBBean()); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $dbMock = $this->getMockBuilder('\Box_Database')->getMock(); + $dbMock->expects($this->atLeastOnce()) + ->method('load') + ->withConsecutive(array('SupportTicket'), array('Client')) + ->willReturnOnConsecutiveCalls($supportTicketModel, $clientModel); + $di['db'] = $dbMock; + + $paidSupportMock = $this->getMockBuilder('\Box\Mod\Paidsupport\Service')->getMock(); + $paidSupportMock->expects($this->atLeastOnce()) + ->method('enoughInBalanceToOpenTicket') + ->with($clientModel); + + $clientBalanceMock = $this->getMockBuilder('\Box\Mod\Client\ServiceBalance')->getMock(); + $clientBalanceMock->expects($this->atLeastOnce()) + ->method('deductFunds'); + + $di['mod_service'] = $di->protect(function ($serviceName, $sub ='') use($paidSupportMock, $clientBalanceMock){ + if ($serviceName == 'Paidsupport'){ + return $paidSupportMock; + } + if ($serviceName == 'Client' && $sub == 'Balance'){ + return $clientBalanceMock; + } + }); + + $params = array( + 'id' => 1, + ); + + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getParameters') + ->willReturn($params); + + $result = $this->service->onAfterClientOpenTicket($boxEventMock); + $this->assertTrue($result); + } +} + \ No newline at end of file diff --git a/tests/integration/bb-modules/mod_paidsupport/ServiceTest.php b/tests/integration/bb-modules/mod_paidsupport/ServiceTest.php new file mode 100644 index 000000000..3199fa150 --- /dev/null +++ b/tests/integration/bb-modules/mod_paidsupport/ServiceTest.php @@ -0,0 +1,88 @@ + 'paidsupport', + 'type' => 'mod', + ); + $this->api_admin->extension_activate($data); + $ticketPrice = 5.5; + + $balance = $this->api_client->client_balance_get_total(); + + if ($balance < $ticketPrice){ + $this->api_admin->client_balance_add_funds(array('id' => 1, 'amount' => $ticketPrice, 'description' => 'Added from PHPUnit')); + } + + $beforeBalance = $this->api_client->client_balance_get_total(); + + $data = array( + 'ext' => 'mod_paidsupport', + 'ticket_price' => $ticketPrice, + 'error_msg' => 'Insufficient amount in balance', + ); + + $this->api_admin->extension_config_save($data); + $data = array( + 'subject' => 'Subject', + 'content' => 'content', + 'support_helpdesk_id' => '1', + + 'order_id' => '1', + 'task' => 'cancel', + ); + $id = $this->api_client->support_ticket_create($data); + + $supportTicket = $this->di['db']->load('SupportTicket', $id); + $this->assertInstanceOf('\Model_SupportTicket', $supportTicket); + + $balance = $this->api_client->client_balance_get_total(); + $this->assertEquals($beforeBalance - $ticketPrice, $balance); + + $clientBalanceModel = $this->di['db']->findOne('ClientBalance', 'ORDER BY id desc'); + $this->assertEquals(-$ticketPrice, $clientBalanceModel->amount); + } + + public function testCreatePaidSupportTicket_insufficientFunds() + { + $data = array( + 'id' => 'paidsupport', + 'type' => 'mod', + ); + $this->api_admin->extension_activate($data); + + $balance = $this->api_client->client_balance_get_total(); + + $ticketPrice = 10 + $balance; + + $errorMessage = 'Insufficient amount in balance'; + + $data = array( + 'ext' => 'mod_paidsupport', + 'ticket_price' => $ticketPrice, + 'error_msg' => $errorMessage, + ); + + $this->api_admin->extension_config_save($data); + $data = array( + 'subject' => 'Subject', + 'content' => 'content', + 'support_helpdesk_id' => '1', + + 'order_id' => '1', + 'task' => 'cancel', + ); + + $supportTickets = $this->di['db']->find('SupportTicket'); + + $this->setExpectedException('\Box_Exception', $errorMessage); + $this->api_client->support_ticket_create($data); + + $supportTicketsAfterCreate = $this->di['db']->find('SupportTicket'); + $this->assertEquals(count($supportTickets), count($supportTicketsAfterCreate)); + } + +} \ No newline at end of file