CakeAccounting/tests/TestCase/Controller/DeAccountsControllerTest.php

327 lines
8.7 KiB
PHP

<?php
declare(strict_types=1);
namespace CakeAccounting\Test\TestCase\Controller;
use Cake\ORM\Table;
use CakeAccounting\Controller\DeAccountsController;
use CakeAccounting\Model\Table\DeAccountsTable;
use PHPUnit\Exception;
/**
* CakeAccounting\Controller\DeAccountsController Test Case
*
* @uses DeAccountsController
*/
class DeAccountsControllerTest extends BaseControllerTest
{
/**
* @var DeAccountsTable|Table
*/
protected DeAccountsTable|Table $DeAccounts;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeAccounting.DeAccounts',
'plugin.CakeAccounting.DeAccountTypes',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
// $this->enableCsrfToken();
// $this->enableSecurityToken();
$this->disableErrorHandlerMiddleware();
$config = $this->getTableLocator()->exists('DeAccounts') ? [] : ['className' => DeAccountsTable::class];
$this->DeAccounts = $this->getTableLocator()->get('DeAccounts', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->DeAccounts);
parent::tearDown();
}
/**
* Test index method
*
* Tests the index action with a logged in user
*
* @return void
* @throws Exception
*
* @uses DeAccountsController::index
*/
public function testIndexGetLoggedIn(): void
{
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeAccounts',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test view method
*
* Tests the view action with a logged in user
*
* @return void
* @throws Exception
*
* @uses DeAccountsController::view
*/
public function testViewGetLoggedIn(): void
{
$id = 41200;
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeAccounts',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses DeAccountsController::add
*/
public function testAddGetLoggedIn(): void
{
$cntBefore = $this->DeAccounts->find()->count();
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeAccounts',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->DeAccounts->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses DeAccountsController::add
*/
public function testAddPostLoggedInSuccess(): void
{
$cntBefore = $this->DeAccounts->find()->count();
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeAccounts',
'action' => 'add',
];
$data = [
'parent_id' => '12000',
'account_type_code' => DE_ACCOUNT_TYPE_LIABILITY, // wrong account type for the parent - parent = assets
'account_number' => '12900',
'name' => 'Crypto Investments',
'account_limit' => '',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('de-accounts');
$cntAfter = $this->DeAccounts->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$justAdded = $this->DeAccounts->find()->where(['account_number' => '12900'])->firstOrFail();
$this->assertEquals(DE_ACCOUNT_TYPE_ASSET, $justAdded->account_type_code);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses DeAccountsController::add
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->DeAccounts->find()->count();
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeAccounts',
'action' => 'add',
];
$data = [];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->DeAccounts->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test edit method
*
* Tests the edit action with a logged in user
*
* @return void
* @throws Exception
*
* @uses DeAccountsController::edit
*/
public function testEditGetLoggedIn(): void
{
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeAccounts',
'action' => 'edit',
41200,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @return void
* @throws Exception
*
* @uses DeAccountsController::edit
*/
public function testEditPutLoggedInSuccess(): void
{
//$this->loginUserByRole('admin');
$id = 41200;
$before = $this->DeAccounts->find()->where(['account_number' => $id])->firstOrFail();
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeAccounts',
'action' => 'edit',
$id,
];
$data = [
// test new data here
'parent_id' => '41000',
'account_type_code' => DE_ACCOUNT_TYPE_LIABILITY, // wrong account type for the parent - parent = revenue
'account_number' => '41300', // changed from 41200 - no journal entries or children so should be allowed
'name' => 'Sales - Services',
'account_limit' => '',
];
$this->put($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('de-accounts');
$after = $this->DeAccounts->find()->where(['account_number' => 41300])->firstOrFail();
// assert saved properly below
$this->assertEquals(DE_ACCOUNT_TYPE_REVENUE, $after->account_type_code);
$this->assertEquals('Sales - Services', $after->name);
$this->assertEquals('41300', $after->account_number);
}
/**
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @return void
* @throws Exception
*
* @uses DeAccountsController::edit
*/
public function testEditPutLoggedInFailure(): void
{
//$this->loginUserByRole('admin');
$id = 41200;
$before = $this->DeAccounts->find()->where(['account_number' => $id])->firstOrFail();
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeAccounts',
'action' => 'edit',
$id,
];
$data = [
// 'parent_id' => 9999999999999,
'account_number' => '',
'name' => 'should fail',
];
$this->put($url, $data);
$this->assertResponseCode(200);
$after = $this->DeAccounts->find()->where(['account_number' => $id])->firstOrFail();
$this->assertEquals($before->account_number, $after->account_number);
$this->assertEquals($before->name, $after->name);
$this->assertEquals($before->parent_id, $after->parent_id);
// assert save failed below
}
/**
* Test delete method
*
* Tests the delete action with a logged in user
*
* @return void
*@throws Exception
*
* @uses DeAccountsController::delete
*/
public function testDeleteLoggedIn(): void
{
$cntBefore = $this->DeAccounts->find()->count();
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeAccounts',
'action' => 'delete',
41200,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('de-accounts');
$cntAfter = $this->DeAccounts->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
}