Files
FOSSBilling/tests/modules/Invoice/ServiceTaxTest.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

363 lines
12 KiB
PHP

<?php
namespace Box\Mod\Invoice;
class ServiceTaxTest extends \BBTestCase
{
/**
* @var \Box\Mod\Invoice\ServiceTax
*/
protected $service = null;
public function setup(): void
{
$this->service = new \Box\Mod\Invoice\ServiceTax();
}
public function testgetDi()
{
$di = new \Pimple\Container();
$this->service->setDi($di);
$getDi = $this->service->getDi();
$this->assertEquals($di, $getDi);
}
public function testgetTaxRateForClientByCountryAndState()
{
$taxRateExpected = 0.21;
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientServiceMock = $this->getMockBuilder('\\' . \Box\Mod\Client\Service::class)
->getMock();
$clientServiceMock->expects($this->atLeastOnce())
->method('isClientTaxable')
->will($this->returnValue(true));
$taxModel = new \Model_Tax();
$taxModel->loadBean(new \DummyBean());
$taxModel->taxrate = $taxRateExpected;
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->will($this->returnValue($taxModel));
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn() => $clientServiceMock);
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->getTaxRateForClient($clientModel);
$this->assertIsFloat($result);
$this->assertEquals($taxRateExpected, $result);
}
public function testgetTaxRateForClientByCountry()
{
$taxRateExpected = 0.21;
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientServiceMock = $this->getMockBuilder('\\' . \Box\Mod\Client\Service::class)
->getMock();
$clientServiceMock->expects($this->atLeastOnce())
->method('isClientTaxable')
->will($this->returnValue(true));
$taxModel = new \Model_Tax();
$taxModel->loadBean(new \DummyBean());
$taxModel->taxrate = $taxRateExpected;
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->will($this->onConsecutiveCalls(null, $taxModel));
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn() => $clientServiceMock);
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->getTaxRateForClient($clientModel);
$this->assertIsFloat($result);
$this->assertEquals($taxRateExpected, $result);
}
public function testgetTaxRateForClient()
{
$taxRateExpected = 0.21;
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientServiceMock = $this->getMockBuilder('\\' . \Box\Mod\Client\Service::class)
->getMock();
$clientServiceMock->expects($this->atLeastOnce())
->method('isClientTaxable')
->will($this->returnValue(true));
$taxModel = new \Model_Tax();
$taxModel->loadBean(new \DummyBean());
$taxModel->taxrate = $taxRateExpected;
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->will($this->onConsecutiveCalls(null, null, $taxModel));
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn() => $clientServiceMock);
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->getTaxRateForClient($clientModel);
$this->assertIsFloat($result);
$this->assertEquals($taxRateExpected, $result);
}
public function testgetTaxRateForClient_TaxWasNotFound()
{
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientServiceMock = $this->getMockBuilder('\\' . \Box\Mod\Client\Service::class)
->getMock();
$clientServiceMock->expects($this->atLeastOnce())
->method('isClientTaxable')
->will($this->returnValue(true));
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->will($this->onConsecutiveCalls(null, null, null));
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn() => $clientServiceMock);
$di['db'] = $dbMock;
$this->service->setDi($di);
$taxRateExpected = 0;
$result = $this->service->getTaxRateForClient($clientModel);
$this->assertIsInt($result);
$this->assertEquals($taxRateExpected, $result);
}
public function testgetTaxRateForClient_ClientIsNotTaxable()
{
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientServiceMock = $this->getMockBuilder('\\' . \Box\Mod\Client\Service::class)
->getMock();
$clientServiceMock->expects($this->atLeastOnce())
->method('isClientTaxable')
->will($this->returnValue(false));
$taxModel = new \Model_Tax();
$taxModel->loadBean(new \DummyBean());
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn() => $clientServiceMock);
$this->service->setDi($di);
$taxRateExpected = 0;
$result = $this->service->getTaxRateForClient($clientModel);
$this->assertIsInt($result);
$this->assertEquals($taxRateExpected, $result);
}
public function testgetTaxWhenTaxRateIsZero()
{
$invoiceModel = new \Model_Invoice();
$invoiceModel->loadBean(new \DummyBean());
$invoiceModel->taxrate = 0;
$result = $this->service->getTax($invoiceModel);
$this->assertIsInt($result);
$this->assertEquals(0, $result);
}
public function testgetTax()
{
$invoiceModel = new \Model_Invoice();
$invoiceModel->loadBean(new \DummyBean());
$invoiceModel->taxrate = 15;
$invoiceItemModel = new \Model_InvoiceItem();
$invoiceItemModel->loadBean(new \DummyBean());
$invoiceItemModel->quantity = 1;
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('find')
->willReturn(array($invoiceItemModel));
$invoiceItemService = $this->getMockBuilder('\\' . \Box\Mod\Invoice\ServiceInvoiceItem::class)->getMock();
$invoiceItemService->expects($this->atLeastOnce())
->method('getTax')
->willReturn(21);
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn() => $invoiceItemService);
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->getTax($invoiceModel);
$this->assertIsInt($result);
}
public function testdelete()
{
$taxModel = new \Model_Tax();
$taxModel->loadBean(new \DummyBean());
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('trash');
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$this->service->setDi($di);
$result = $this->service->delete($taxModel);
$this->assertTrue($result);
}
public function testcreate()
{
$systemService = $this->getMockBuilder('\\' . \Box\Mod\System\Service::class)->getMock();
$systemService->expects($this->atLeastOnce())
->method('checkLimits');
$taxModel = new \Model_Tax();
$taxModel->loadBean(new \DummyBean());
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->will($this->returnValue($taxModel));
$newId = 2;
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue($newId));
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(fn() => $systemService);
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$this->service->setDi($di);
$data = array(
'name' => 'tax',
'taxrate' => '0.18',
);
$result = $this->service->create($data);
$this->assertIsInt($result);
$this->assertEquals($newId, $result);
}
public function testUpdate()
{
$taxModel = new \Model_Tax();
$taxModel->loadBean(new \DummyBean());
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue(2));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$this->service->setDi($di);
$data = array(
'name' => 'tax',
'taxrate' => '0.18',
);
$result = $this->service->update($taxModel, $data);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testgetSearchQuery()
{
$result = $this->service->getSearchQuery(array());
$this->assertIsString($result[0]);
$this->assertIsArray($result[1]);
$this->assertEquals(array(), $result[1]);
}
public function testsetupEUTaxes()
{
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('exec');
$systemService = $this->getMockBuilder('\\' . \Box\Mod\System\Service::class)
->getMock();
$euCountriesData = array(
'AT' => 'Austria',
);
$euVatData = array(
'AT' => 20,
);
$systemService->expects($this->atLeastOnce())
->method('getEuCountries')
->will($this->returnValue($euCountriesData));
$systemService->expects($this->atLeastOnce())
->method('getEuVat')
->will($this->returnValue($euVatData));
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Invoice\ServiceTax::class)
->onlyMethods(array('create'))
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('create');
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn() => $systemService);
$serviceMock->setDi($di);
$result = $serviceMock->setupEUTaxes(array());
$this->assertTrue($result);
}
public function testtoApiArray()
{
$taxModel = new \Model_Tax();
$taxModel->loadBean(new \DummyBean());
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('toArray')
->with($taxModel)
->willReturn(array());
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->toApiArray($taxModel);
$this->assertIsArray($result);
}
}