api = new Client(); } public function testGetDi(): void { $di = $this->getDi(); $this->api->setDi($di); $getDi = $this->api->getDi(); $this->assertEquals($di, $getDi); } public function testGet(): void { $serviceMock = $this->createMock(\Box\Mod\Invoice\Service::class); $serviceMock->expects($this->atLeastOnce()) ->method('toApiArray') ->willReturn([]); $dbMock = $this->createMock('\Box_Database'); $model = new \Model_Invoice(); $model->loadBean(new \DummyBean()); $dbMock->expects($this->atLeastOnce()) ->method('findOne') ->willReturn($model); $di = $this->getDi(); $di['db'] = $dbMock; $this->api->setDi($di); $this->api->setService($serviceMock); $this->api->setIdentity(new \Model_Admin()); $data['hash'] = md5('1'); $result = $this->api->get($data); $this->assertIsArray($result); } public function testGetInvoiceNotFound(): void { $dbMock = $this->createMock('\Box_Database'); $model = new \Model_Invoice(); $model->loadBean(new \DummyBean()); $dbMock->expects($this->atLeastOnce()) ->method('findOne') ->willReturn(null); $di = $this->getDi(); $di['db'] = $dbMock; $this->api->setDi($di); $this->api->setIdentity(new \Model_Admin()); $data['hash'] = md5('1'); $this->expectException(\FOSSBilling\Exception::class); $this->expectExceptionMessage('Invoice was not found'); $this->api->get($data); } public function testUpdate(): void { $serviceMock = $this->createMock(\Box\Mod\Invoice\Service::class); $serviceMock->expects($this->atLeastOnce()) ->method('updateInvoice') ->willReturn(true); $dbMock = $this->createMock('\Box_Database'); $model = new \Model_Invoice(); $model->loadBean(new \DummyBean()); $dbMock->expects($this->atLeastOnce()) ->method('findOne') ->willReturn($model); $di = $this->getDi(); $di['db'] = $dbMock; $this->api->setDi($di); $this->api->setService($serviceMock); $this->api->setIdentity(new \Model_Admin()); $data['hash'] = md5('1'); $result = $this->api->update($data); $this->assertIsBool($result); $this->assertTrue(true); } public function testUpdateInvoiceNotFound(): void { $dbMock = $this->createMock('\Box_Database'); $model = new \Model_Invoice(); $model->loadBean(new \DummyBean()); $dbMock->expects($this->atLeastOnce()) ->method('findOne') ->willReturn(null); $di = $this->getDi(); $di['db'] = $dbMock; $this->api->setDi($di); $this->api->setIdentity(new \Model_Admin()); $data['hash'] = md5('1'); $this->expectException(\FOSSBilling\Exception::class); $this->expectExceptionMessage('Invoice was not found'); $this->api->update($data); } public function testUpdateInvoiceIsPaid(): void { $dbMock = $this->createMock('\Box_Database'); $model = new \Model_Invoice(); $model->loadBean(new \DummyBean()); $model->status = 'paid'; $dbMock->expects($this->atLeastOnce()) ->method('findOne') ->willReturn($model); $di = $this->getDi(); $di['db'] = $dbMock; $this->api->setDi($di); $this->api->setIdentity(new \Model_Admin()); $data['hash'] = md5('1'); $this->expectException(\FOSSBilling\Exception::class); $this->expectExceptionMessage('Paid Invoice cannot be modified'); $this->api->update($data); } public function testRenewalInvoice(): void { $generatedHash = 'generatedHashString'; $serviceMock = $this->createMock(\Box\Mod\Invoice\Service::class); $model = new \Model_Invoice(); $model->loadBean(new \DummyBean()); $model->hash = $generatedHash; $serviceMock->expects($this->atLeastOnce()) ->method('generateForOrder') ->willReturn($model); $serviceMock->expects($this->atLeastOnce()) ->method('approveInvoice'); $dbMock = $this->createMock('\Box_Database'); $clientOrder = new \Model_ClientOrder(); $clientOrder->loadBean(new \DummyBean()); $clientOrder->price = 10; $dbMock->expects($this->atLeastOnce()) ->method('findOne') ->willReturn($clientOrder); $di = $this->getDi(); $di['db'] = $dbMock; $di['logger'] = new \Box_Log(); $this->api->setDi($di); $this->api->setService($serviceMock); $identity = new \Model_Admin(); $identity->loadBean(new \DummyBean()); $this->api->setIdentity($identity); $data['order_id'] = 1; $result = $this->api->renewal_invoice($data); $this->assertIsString($result); $this->assertEquals($generatedHash, $result); } public function testRenewalInvoiceOrderIsFree(): void { $dbMock = $this->createMock('\Box_Database'); $clientOrder = new \Model_ClientOrder(); $clientOrder->loadBean(new \DummyBean()); $clientOrder->id = 1; $clientOrder->price = 0; $dbMock->expects($this->atLeastOnce()) ->method('findOne') ->willReturn($clientOrder); $di = $this->getDi(); $di['db'] = $dbMock; $di['logger'] = new \Box_Log(); $this->api->setDi($di); $identity = new \Model_Admin(); $identity->loadBean(new \DummyBean()); $this->api->setIdentity($identity); $data['order_id'] = 1; $this->expectException(\FOSSBilling\Exception::class); $this->expectExceptionMessage(sprintf('Order %d is free. No need to generate invoice.', $clientOrder->id)); $this->api->renewal_invoice($data); } public function testRenewalInvoiceOrderNotFound(): void { $dbMock = $this->createMock('\Box_Database'); $clientOrder = new \Model_ClientOrder(); $clientOrder->loadBean(new \DummyBean()); $clientOrder->price = 10; $dbMock->expects($this->atLeastOnce()) ->method('findOne') ->willReturn(null); $di = $this->getDi(); $di['db'] = $dbMock; $this->api->setDi($di); $identity = new \Model_Admin(); $identity->loadBean(new \DummyBean()); $this->api->setIdentity($identity); $data['order_id'] = 1; $this->expectException(\FOSSBilling\Exception::class); $this->expectExceptionMessage('Order not found'); $this->api->renewal_invoice($data); } public function testFundsInvoice(): void { $generatedHash = 'generatedHashString'; $serviceMock = $this->createMock(\Box\Mod\Invoice\Service::class); $model = new \Model_Invoice(); $model->loadBean(new \DummyBean()); $model->hash = $generatedHash; $serviceMock->expects($this->atLeastOnce()) ->method('generateFundsInvoice') ->willReturn($model); $serviceMock->expects($this->atLeastOnce()) ->method('approveInvoice'); $di = $this->getDi(); $di['logger'] = new \Box_Log(); $this->api->setDi($di); $this->api->setService($serviceMock); $identity = new \Model_Client(); $identity->loadBean(new \DummyBean()); $this->api->setIdentity($identity); $data['amount'] = 10; $result = $this->api->funds_invoice($data); $this->assertIsString($result); $this->assertEquals($generatedHash, $result); } public function testDelete(): void { $serviceMock = $this->createMock(\Box\Mod\Invoice\Service::class); $serviceMock->expects($this->atLeastOnce()) ->method('deleteInvoiceByClient') ->willReturn(true); $dbMock = $this->createMock('\Box_Database'); $model = new \Model_Invoice(); $model->loadBean(new \DummyBean()); $dbMock->expects($this->atLeastOnce()) ->method('findOne') ->willReturn($model); $di = $this->getDi(); $di['db'] = $dbMock; $di['logger'] = new \Box_Log(); $this->api->setDi($di); $this->api->setService($serviceMock); $identity = new \Model_Client(); $identity->loadBean(new \DummyBean()); $this->api->setIdentity($identity); $data['hash'] = md5('1'); $result = $this->api->delete($data); $this->assertIsBool($result); $this->assertTrue($result); } public function testTransactionGetList(): void { $transactionService = $this->createMock(\Box\Mod\Invoice\ServiceTransaction::class); $transactionService->expects($this->atLeastOnce()) ->method('getSearchQuery') ->willReturn(['SqlString', []]); $paginatorMock = $this->getMockBuilder(\FOSSBilling\Pagination::class) ->onlyMethods(['getPaginatedResultSet']) ->disableOriginalConstructor() ->getMock(); $paginatorMock->expects($this->atLeastOnce()) ->method('getPaginatedResultSet') ->willReturn(['list' => []]); $di = $this->getDi(); $di['pager'] = $paginatorMock; $di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $transactionService); $this->api->setDi($di); $identity = new \Model_Client(); $identity->loadBean(new \DummyBean()); $this->api->setIdentity($identity); $result = $this->api->transaction_get_list([]); $this->assertIsArray($result); } public function testGetTaxRate(): void { $client = new \Model_Client(); $client->loadBean(new \DummyBean()); $taxRate = 20; $invoiceTaxService = $this->getMockBuilder(\Box\Mod\Invoice\ServiceTax::class) ->getMock(); $invoiceTaxService->expects($this->atLeastOnce()) ->method('getTaxRateForClient') ->willReturn($taxRate); $di = $this->getDi(); $di['mod_service'] = $di->protect(function ($service, $sub) use ($invoiceTaxService) { if ($service == 'Invoice' && $sub == 'Tax') { return $invoiceTaxService; } }); $this->api->setDi($di); $this->api->setIdentity($client); $result = $this->api->get_tax_rate(); $this->assertEquals($taxRate, $result); } }