mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
success in testing updateBalances with: 'sudo -u www-data ./vendor/bin/phpunit --filter testAjaxGetBalance'
This commit is contained in:
parent
1a22c3dd82
commit
b5ad8ced68
@ -44,7 +44,6 @@ class StateBalancesController extends AppController
|
||||
|
||||
private function updateBalances($stateUserId)
|
||||
{
|
||||
echo "stateUserId: $stateUserId\n";
|
||||
$stateUserTransactionsTable = TableRegistry::getTableLocator()->get('StateUserTransactions');
|
||||
$transactionsTable = TableRegistry::getTableLocator()->get('Transactions');
|
||||
// info: cakephp use lazy loading, query will be executed later only if needed
|
||||
@ -128,7 +127,7 @@ class StateBalancesController extends AppController
|
||||
|
||||
$transaction_ids = [];
|
||||
if($recalculate_state_user_transactions_balance) {
|
||||
echo "recalculate state user transaction balances \n";
|
||||
|
||||
$state_user_transactions_array = $state_user_transactions->toArray();
|
||||
foreach($state_user_transactions_array as $i => $state_user_transaction) {
|
||||
$transaction_ids[$state_user_transaction->transaction_id] = $i;
|
||||
@ -149,22 +148,18 @@ class StateBalancesController extends AppController
|
||||
$amount = 0;
|
||||
|
||||
if($transaction->transaction_type_id == 1) { // creation
|
||||
echo "creation\n";
|
||||
$temp = $transaction->transaction_creations[0];
|
||||
echo "creation amount: " . $temp->amount . "\n";
|
||||
echo "target date: " . $temp->target_date . "\n";
|
||||
|
||||
$balance_temp = $this->StateBalances->newEntity();
|
||||
$balance_temp->amount = $temp->amount;
|
||||
$balance_temp->record_date = $temp->target_date;
|
||||
|
||||
$amount = $balance_temp->partDecay($transaction->received);
|
||||
$amount_date = $transaction->received;
|
||||
echo "amount in state_user_transactions: $amount\n";
|
||||
echo "date in state_user_transactions: $amount_date\n";
|
||||
echo "diff: " . ($temp->amount - $amount) . "\n";
|
||||
|
||||
//$amount_date =
|
||||
} else if($transaction->transaction_type_id == 2) { // transfer
|
||||
echo "send coins\n";
|
||||
|
||||
$temp = $transaction->transaction_send_coins[0];
|
||||
$amount = intval($temp->amount);
|
||||
// reverse if sender
|
||||
@ -172,21 +167,20 @@ class StateBalancesController extends AppController
|
||||
$amount *= -1.0;
|
||||
}
|
||||
$amount_date = $transaction->received;
|
||||
echo "amount: " . $amount . "\n";
|
||||
echo "date: " . $amount_date . "\n";
|
||||
|
||||
}
|
||||
if($i == 0) {
|
||||
$balance_cursor->amount = $amount;
|
||||
} else {
|
||||
$balance_cursor->amount = $balance_cursor->partDecay($amount_date) + $amount;
|
||||
}
|
||||
echo "balance cursor amount: " . $balance_cursor->amount . "\n";
|
||||
|
||||
$balance_cursor->record_date = $amount_date;
|
||||
$state_user_transaction_index = $transaction_ids[$transaction->id];
|
||||
$state_user_transactions_array[$state_user_transaction_index]->balance = $balance_cursor->amount;
|
||||
$state_user_transactions_array[$state_user_transaction_index]->balance_date = $balance_cursor->record_date;
|
||||
$i++;
|
||||
echo "##########################\n";
|
||||
|
||||
}
|
||||
|
||||
$results = $stateUserTransactionsTable->saveMany($state_user_transactions_array);
|
||||
@ -464,45 +458,17 @@ class StateBalancesController extends AppController
|
||||
|
||||
$this->updateBalances($user['id']);
|
||||
|
||||
|
||||
echo "try to unhex: ". $user['public_hex'] . "\n";
|
||||
$public_key_bin = hex2bin($user['public_hex']);
|
||||
//var_dump($public_key_bin);
|
||||
$stateUserQuery = $this->StateBalances->StateUsers
|
||||
->find('all')
|
||||
//->where(['public_key' => $public_key_bin])
|
||||
->where(['id' => $user['id']])
|
||||
//->contain(['StateBalances'])
|
||||
;
|
||||
$hex_from_db = bin2hex(stream_get_contents($stateUserQuery->first()->public_key));
|
||||
echo "hex from db: $hex_from_db\n";
|
||||
echo "state user query array: \n";
|
||||
var_dump($stateUserQuery->toArray());
|
||||
echo "\n";
|
||||
|
||||
$state_balance = $this->StateBalances->find()->where(['state_user_id' => $user['id']]);
|
||||
echo "state balance:\n";
|
||||
var_dump($state_balance->toArray());
|
||||
echo "\n";
|
||||
|
||||
$result_user_count = $stateUserQuery->count();
|
||||
if($result_user_count < 1) {
|
||||
return $this->returnJson(['state' => 'success', 'balance' => 0]);
|
||||
}
|
||||
else if($result_user_count > 1) {
|
||||
return $this->returnJson([
|
||||
'state' => 'error',
|
||||
'msg' => 'multiple entrys found',
|
||||
'details' => ['public_key' => $user['public_hex'], 'entry_count' => $result_count]
|
||||
]);
|
||||
}
|
||||
//$state_balance = $stateUserQuery->first()->state_balances;
|
||||
$state_balance = $this->StateBalances->find()->where(['state_user_id' => $user['id']])->first();
|
||||
|
||||
if(!$state_balance) {
|
||||
return $this->returnJson(['state' => 'success', 'balance' => 0]);
|
||||
}
|
||||
|
||||
return $this->returnJson(['state' => 'success', 'balance' => $state_balance->amount]);
|
||||
return $this->returnJson([
|
||||
'state' => 'success',
|
||||
'balance' => $state_balance->amount,
|
||||
'decay' => $state_balance->decay
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -3,6 +3,9 @@ namespace App\Test\TestCase\Controller;
|
||||
|
||||
use Cake\TestSuite\IntegrationTestTrait;
|
||||
use Cake\TestSuite\TestCase;
|
||||
use Cake\ORM\TableRegistry;
|
||||
use Cake\I18n\Time;
|
||||
|
||||
|
||||
/**
|
||||
* App\Controller\StateBalancesController Test Case
|
||||
@ -30,6 +33,12 @@ class StateBalancesControllerTest extends TestCase
|
||||
'app.TransactionTypes'
|
||||
];
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->StateBalances = TableRegistry::getTableLocator()->get('StateBalances');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test initialize method
|
||||
*
|
||||
@ -65,7 +74,7 @@ class StateBalancesControllerTest extends TestCase
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAjaxGetBalance()
|
||||
public function testAjaxGetBalance1()
|
||||
{
|
||||
$session_id = rand();
|
||||
$this->session([
|
||||
@ -77,10 +86,49 @@ class StateBalancesControllerTest extends TestCase
|
||||
'public_hex' => 'f7f4a49a4ac10379f8b9ddcb731c4d9ec495e6edd16075f52672cd25e3179f0f'
|
||||
]
|
||||
]);
|
||||
//echo "balance: $balance";
|
||||
$this->getAndParse('/state-balances/ajaxGetBalance/' . $session_id,
|
||||
['state' => 'success', 'balance' => 1200000]
|
||||
);
|
||||
|
||||
$response = $this->getAndParseWithoutCompare('/state-balances/ajaxGetBalance/' . $session_id);
|
||||
|
||||
$this->assertEquals('success', $response->state);
|
||||
$this->assertEquals(7321828, $response->balance);
|
||||
$this->assertLessThan(7321828, $response->decay);
|
||||
|
||||
}
|
||||
|
||||
public function testAjaxGetBalance2()
|
||||
{
|
||||
$session_id = rand();
|
||||
$this->session([
|
||||
'session_id' => $session_id,
|
||||
'Transaction' => ['pending' => 0, 'executing' => 0],
|
||||
'StateUser' => [
|
||||
'id' => 3,
|
||||
'email_checked' => 1,
|
||||
'public_hex' => '131c7f68dd94b2be4c913400ff7ff4cdc03ac2bda99c2d29edcacb3b065c67e6'
|
||||
]
|
||||
]);
|
||||
|
||||
$response = $this->getAndParseWithoutCompare('/state-balances/ajaxGetBalance/' . $session_id);
|
||||
$this->assertEquals('success', $response->state);
|
||||
$this->assertEquals(0, $response->balance);
|
||||
}
|
||||
public function testAjaxGetBalance3()
|
||||
{
|
||||
$session_id = rand();
|
||||
$this->session([
|
||||
'session_id' => $session_id,
|
||||
'Transaction' => ['pending' => 0, 'executing' => 0],
|
||||
'StateUser' => [
|
||||
'id' => 4,
|
||||
'email_checked' => 1,
|
||||
'public_hex' => 'e3369de3623ce8446d0424c4013e7a1d71a2671ae3d7bf1e798ebf0665d145f2'
|
||||
]
|
||||
]);
|
||||
|
||||
$response = $this->getAndParseWithoutCompare('/state-balances/ajaxGetBalance/' . $session_id);
|
||||
$this->assertEquals('success', $response->state);
|
||||
$this->assertEquals(9112595, $response->balance);
|
||||
$this->assertLessThan(9112595, $response->decay);
|
||||
}
|
||||
|
||||
public function testAjaxGetBalanceInvalidSession()
|
||||
@ -243,7 +291,24 @@ class StateBalancesControllerTest extends TestCase
|
||||
$expected = json_encode($expected);
|
||||
}
|
||||
|
||||
|
||||
$this->assertEquals($expected, $responseBodyString);
|
||||
}
|
||||
private function getAndParseWithoutCompare($path)
|
||||
{
|
||||
$this->configRequest([
|
||||
'headers' => ['Accept' => 'application/json']
|
||||
]);
|
||||
|
||||
$this->disableErrorHandlerMiddleware();
|
||||
$this->get($path);
|
||||
|
||||
// Check that the response was a 200
|
||||
$this->assertResponseOk();
|
||||
|
||||
$responseBodyString = (string)$this->_response->getBody();
|
||||
$json = json_decode($responseBodyString);
|
||||
$this->assertNotFalse($json);
|
||||
|
||||
return $json;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user