CakeAccounting/tests/TestCase/Controller/DeExternalAccountsControlle...

314 lines
8.4 KiB
PHP

<?php
declare(strict_types=1);
namespace CakeAccounting\Test\TestCase\Controller;
use Cake\ORM\Table;
use CakeAccounting\Controller\DeExternalAccountsController;
use CakeAccounting\Model\Table\DeExternalAccountsTable;
use PHPUnit\Exception;
/**
* CakeAccounting\Controller\DeExternalAccountsController Test Case
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController
*/
class DeExternalAccountsControllerTest extends BaseControllerTest
{
/**
* @var DeExternalAccountsTable|Table
*/
protected DeExternalAccountsTable|Table $DeExternalAccounts;
/**
* Fixtures
*
* @var array<string>
*/
protected array $fixtures = [
'plugin.CakeAccounting.DeExternalAccounts',
];
/**
* setUp method
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
// $this->enableCsrfToken();
// $this->enableSecurityToken();
$this->disableErrorHandlerMiddleware();
$config = $this->getTableLocator()->exists('DeExternalAccounts') ? [] : ['className' => DeExternalAccountsTable::class];
$this->DeExternalAccounts = $this->getTableLocator()->get('DeExternalAccounts', $config);
}
/**
* tearDown method
*
* @return void
*/
protected function tearDown(): void
{
unset($this->DeExternalAccounts);
parent::tearDown();
}
/**
* Test index method
*
* Tests the index action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::index()
* @throws Exception
*
* @return void
*/
public function testIndexGetLoggedIn(): void
{
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test view method
*
* Tests the view action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::view()
* @throws Exception
*
* @return void
*/
public function testViewGetLoggedIn(): void
{
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::add()
* @throws Exception
*
* @return void
*/
public function testAddGetLoggedIn(): void
{
$cntBefore = $this->DeExternalAccounts->find()->count();
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
$cntAfter = $this->DeExternalAccounts->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::add()
* @throws Exception
*
* @return void
*/
public function testAddPostLoggedInSuccess(): void
{
$cntBefore = $this->DeExternalAccounts->find()->count();
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'add',
];
$data = [
'entity_type_code' => DE_ENTITY_TYPE_ORGANIZATION,
'related_model' => '',
'related_model_fk' => '',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('de-external-accounts');
$cntAfter = $this->DeExternalAccounts->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
}
/**
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::add()
* @throws Exception
*
* @return void
*/
public function testAddPostLoggedInFailure(): void
{
$cntBefore = $this->DeExternalAccounts->find()->count();
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'add',
];
$data = [];
$this->post($url, $data);
$this->assertResponseCode(200);
$cntAfter = $this->DeExternalAccounts->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
/**
* Test edit method
*
* Tests the edit action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditGetLoggedIn(): void
{
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'edit',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
/**
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditPutLoggedInSuccess(): void
{
//$this->loginUserByRole('admin');
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
$before = $this->DeExternalAccounts->get($id);
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'edit',
$id,
];
$data = [
// test new data here
'entity_type_code' => DE_ENTITY_TYPE_PROPERTY,
];
$this->put($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('de-external-accounts');
$after = $this->DeExternalAccounts->get($id);
$this->assertEquals($data['entity_type_code'], $after->entity_type_code);
// assert saved properly below
}
/**
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::edit()
* @throws Exception
*
* @return void
*/
public function testEditPutLoggedInFailure(): void
{
//$this->loginUserByRole('admin');
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
$before = $this->DeExternalAccounts->get($id);
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'edit',
$id,
];
$data = [
'entity_type_code' => '',
];
$this->put($url, $data);
$this->assertResponseCode(200);
$after = $this->DeExternalAccounts->get($id);
$this->assertEquals($before->entity_type_code, $after->entity_type_code);
// assert save failed below
}
/**
* Test delete method
*
* Tests the delete action with a logged in user
*
* @uses \CakeAccounting\Controller\DeExternalAccountsController::delete()
* @throws Exception
*
* @return void
*/
public function testDeleteLoggedIn(): void
{
$cntBefore = $this->DeExternalAccounts->find()->count();
$id = '2463dc4f-6f38-4492-8c8e-918f11e13748';
//$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeAccounting',
'controller' => 'DeExternalAccounts',
'action' => 'delete',
$id,
];
$this->delete($url);
$this->assertResponseCode(302);
$this->assertRedirectContains('de-external-accounts');
$cntAfter = $this->DeExternalAccounts->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
}