Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dependency injection to MySqlDatabase, and MySqlProperties #924

Merged
merged 39 commits into from
Dec 13, 2020
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0bc1cb5
initial changes to support unit testing via phpunit, and a simple tes…
XaeroDegreaz Nov 4, 2020
98b45f7
#910 update to official phpunit version. add tests for most fields wh…
XaeroDegreaz Nov 5, 2020
fee827b
have travis install composer dependencies
XaeroDegreaz Nov 5, 2020
45b9466
fix sample config names for travis
XaeroDegreaz Nov 5, 2020
790cc34
doing more donuts on travis' lawn
XaeroDegreaz Nov 5, 2020
d7c5765
cover more conditions for getAccount
XaeroDegreaz Nov 5, 2020
2c1d5ed
#910 - Add more tests for retrieving accounts by various parameters
XaeroDegreaz Nov 6, 2020
f3273cf
add a failure test for retrieving account
XaeroDegreaz Nov 6, 2020
7a01b65
dont need class level field
XaeroDegreaz Nov 6, 2020
86ed1fe
#910 move away from global mocking strategy, and move towards integra…
XaeroDegreaz Nov 6, 2020
13e9b13
copy env for travis
XaeroDegreaz Nov 6, 2020
9d774e9
fix docker-compose command so mysql doesn't get stuck
XaeroDegreaz Nov 6, 2020
3e60fae
travis may not like localhost
XaeroDegreaz Nov 6, 2020
1e19b4e
Dont use the suffix "Test" in the base class name. PHPUnit thinks it'…
XaeroDegreaz Nov 6, 2020
dd267d1
Fix indentation
XaeroDegreaz Nov 6, 2020
50de9e5
Update README for testing
XaeroDegreaz Nov 7, 2020
cf02fce
rmove no longer needed model
XaeroDegreaz Nov 7, 2020
831d403
fix bug connecting to the database
XaeroDegreaz Nov 11, 2020
4370c30
remove mockery as a dependency, and use phpunit mocks instead. this s…
XaeroDegreaz Nov 11, 2020
6d3c9e8
initial setup to allow use of a single config file (.env) for all mys…
XaeroDegreaz Nov 11, 2020
6feecc0
Merge branch 'master' into 914-single-config-file
XaeroDegreaz Nov 12, 2020
79a20ca
Remove references to SmrMysqlSecrets.inc
XaeroDegreaz Nov 12, 2020
ce79956
playing with php-di to get the mysqldatabase class injecting properly…
XaeroDegreaz Nov 12, 2020
3a85095
Merge branch 'master' into 922-php-di-mysqldatabase
XaeroDegreaz Nov 17, 2020
76241ed
fix mysql properties removing port
XaeroDegreaz Nov 17, 2020
6a608d8
Update MySqlDatabase and MySqlProperties to use dependency injection.
XaeroDegreaz Nov 17, 2020
8250f0f
fix base integration test
XaeroDegreaz Nov 17, 2020
45e0f09
fix some lingering classes not defining db properly
XaeroDegreaz Nov 17, 2020
d49637d
fix more instances where the database did not get replaced properly
XaeroDegreaz Nov 17, 2020
b104e19
three cheers for scrutinizer
XaeroDegreaz Nov 17, 2020
385f0ee
no message
XaeroDegreaz Nov 18, 2020
c47cefd
conditionally compile the di container
XaeroDegreaz Nov 22, 2020
42b6535
fix namespacing issue in phpunit annotation which was causing tests t…
XaeroDegreaz Nov 22, 2020
8b23602
Code review changes
XaeroDegreaz Nov 25, 2020
0139aef
Move MySqlDatabase factory method. Enable reconnecting to MySQL via g…
XaeroDegreaz Dec 1, 2020
783b7d6
Merge remote-tracking branch 'github/master' into 922-php-di-mysqldat…
XaeroDegreaz Dec 1, 2020
f0d861d
no message
XaeroDegreaz Dec 1, 2020
a9db6ea
Merge remote-tracking branch 'origin/master' into 922-php-di-mysqldat…
hemberger Dec 13, 2020
bb5d38c
Update lib/Smr/Container/DiContainer.php
hemberger Dec 13, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions admin/Default/1.6/game_create_processing.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion admin/Default/changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$template->assign('ChangeMessage', $var['change_message'] ?? '');
$template->assign('AffectedDb', $var['affected_db'] ?? '');

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
XaeroDegreaz marked this conversation as resolved.
Show resolved Hide resolved
$first_entry = true;
$link_set_live = true;

Expand Down
2 changes: 1 addition & 1 deletion admin/Default/comp_share.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$skipExceptions = false;

//extra db object and other vars
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
$used = array();

//check the db and get the info we need
Expand Down
6 changes: 3 additions & 3 deletions admin/Default/game_delete_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
create_error('Deleting games is disabled!');

// additional db objects
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);

$smr_db_sql = array();
$history_db_sql = array();
Expand Down Expand Up @@ -291,9 +291,9 @@

// don't know why exactly we have to do that,
// but it seems that the db is used globally instead kept to each object
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();

}
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
//forward em
forward(create_container('skeleton.php', 'admin_tools.php'));
2 changes: 1 addition & 1 deletion admin/Default/ip_view_results.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$variable = SmrSession::getRequestVar('variable');
$type = SmrSession::getRequestVar('type');

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);

$container = create_container('skeleton.php', 'ip_view.php');
$template->assign('BackHREF', SmrSession::getNewHREF($container));
Expand Down
2 changes: 1 addition & 1 deletion admin/Default/log_console.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
JOIN account USING(account_id)
GROUP BY account_id');
if ($db->getNumRows()) {
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
while ($db->nextRecord()) {
$accountID = $db->getInt('account_id');
$loggedAccounts[$accountID] = array('AccountID' => $accountID,
Expand Down
2 changes: 1 addition & 1 deletion admin/Default/newsletter_send.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$processingContainer = create_container('newsletter_send_processing.php');

// Get the most recent newsletter text for preview
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
$db->query('SELECT newsletter_id, newsletter_html, newsletter_text FROM newsletter ORDER BY newsletter_id DESC LIMIT 1');
if ($db->nextRecord()) {
$id = $db->getInt('newsletter_id');
Expand Down
15 changes: 13 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
"php": "^7.4",
"phpmailer/phpmailer": "^6.1",
"vanilla/nbbc": "^2.0",
"vlucas/phpdotenv": "^v5.2.0"
"vlucas/phpdotenv": "^v5.2.0",
"php-di/php-di": "6.3.0"
},
"autoload": {
"psr-0": {
"Smr\\": "lib/"
}
},
"scripts": {
"start:dev": "docker-compose up --build -d traefik smr-dev",
"start:integration-services": [
"docker-compose --env-file test.env up -d mysql-integration-test",
"sleep 20",
Expand All @@ -24,7 +31,11 @@
"docker-compose stop",
"docker-compose rm --force"
],
"test": "docker-compose --env-file test.env run --rm smr-integration-test"
"test": "docker-compose --env-file test.env run --rm smr-integration-test",
"rebuild:dev": [
"@stop",
"@start:dev"
]
},
"autoload-dev": {
"psr-0": {
Expand Down
8 changes: 4 additions & 4 deletions engine/Default/alliance_message.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
$db->query($query);
$threads = array();
if ($db->getNumRows() > 0) {
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);

$container = create_container('alliance_message_delete_processing.php');
$container['alliance_id'] = $alliance->getAllianceID();
Expand All @@ -52,15 +52,15 @@
$thread_topics[$i] = $db->getField('topic');

$threads[$i]['Topic'] = $db->getField('topic');

$db2->query('SELECT time
FROM player_read_thread
FROM player_read_thread
WHERE ' . $player->getSQL() . '
AND alliance_id =' . $db2->escapeNumber($alliance->getAllianceID()) . '
AND thread_id=' . $db2->escapeNumber($threadID) . '
AND time>' . $db2->escapeNumber($db->getInt('sendtime')) . ' LIMIT 1');
$threads[$i]['Unread'] = $db2->getNumRows() == 0;

// Determine the thread author display name
$sender_id = $db->getInt('sender_id');
if ($sender_id == ACCOUNT_ID_PLANET) {
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/alliance_mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$template->assign('OpTime', $db->getInt('time'));

// Has player responded yet?
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
$db2->query('SELECT response FROM alliance_has_op_response WHERE alliance_id=' . $db2->escapeNumber($player->getAllianceID()) . ' AND ' . $player->getSQL() . ' LIMIT 1');

$response = $db2->nextRecord() ? $db2->getField('response') : null;
Expand Down
4 changes: 2 additions & 2 deletions engine/Default/bank_anon.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
AND game_id=' . $db->escapeNumber($player->getGameID()));

$ownedAnon = [];
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
while ($db->nextRecord()) {
$anon = [];
$anon['anon_id'] = $db->getInt('anon_id');
Expand All @@ -33,7 +33,7 @@
} else {
$anon['last_transaction'] = 'No transactions';
}

$container['account_num'] = $anon['anon_id'];
$container['password'] = $anon['password'];
$anon['href'] = SmrSession::getNewHREF($container);
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/changelog_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
$template->assign('ContinueHREF', SmrSession::getNewHREF($container));
}

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);

$db->query('SELECT *
FROM version
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/council_vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
AND race_id_1 = ' . $db->escapeNumber($player->getRaceID()));
if ($db->getNumRows() > 0) {

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);

while ($db->nextRecord()) {
$otherRaceID = $db->getInt('race_id_2');
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/current_players.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

$allRows = array();
if ($count_last_active > 0) {
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
while ($db->nextRecord()) {
$row = array();

Expand Down
2 changes: 1 addition & 1 deletion engine/Default/feature_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

$commentsContainer = $var;
$commentsContainer['body'] = 'feature_request_comments.php';
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
$featureRequests = array();
while ($db->nextRecord()) {
$featureRequestID = $db->getInt('feature_request_id');
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/galactic_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
$template->assign('PageTopic', 'Galactic Post');
Menu::galactic_post();

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);

$container = create_container('skeleton.php', 'galactic_post_view_article.php');
$template->assign('ViewArticlesHREF', SmrSession::getNewHREF($container));
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/galactic_post_delete_processing.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php declare(strict_types=1);
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
if (isset($var['article'])) {
if (Request::get('action') == 'Yes') {
$db->query('DELETE FROM galactic_post_article WHERE game_id = ' . $db->escapeNumber($player->getGameID()) . ' AND article_id = ' . $db->escapeNumber($var['id']));
Expand Down
4 changes: 2 additions & 2 deletions engine/Default/game_play.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
// generate list of game_id that this player is joined
$game_id_list[] = $game_id;

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
$db2->query('SELECT count(*) as num_playing
FROM player
WHERE last_cpl_action >= ' . $db->escapeNumber(TIME - 600) . '
Expand Down Expand Up @@ -191,7 +191,7 @@

$db->query('SELECT * FROM voting WHERE end > ' . $db->escapeNumber(TIME) . ' ORDER BY end DESC');
if ($db->getNumRows() > 0) {
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
$votedFor = array();
$db2->query('SELECT * FROM voting_results WHERE account_id = ' . $db->escapeNumber($account->getAccountID()));
while ($db2->nextRecord()) {
Expand Down
4 changes: 2 additions & 2 deletions engine/Default/game_stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
$template->assign('LowestAlign', $db->getInt('min_alignment'));
$template->assign('HighestKills', $db->getInt('max_kills'));
}

$db->query('SELECT count(*) num_alliance FROM alliance WHERE game_id = ' . $gameID);
if ($db->nextRecord()) {
$template->assign('TotalAlliances', $db->getInt('num_alliance'));
Expand Down Expand Up @@ -44,7 +44,7 @@
}

function allianceTopTen($gameID, $field) {
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance(true);
$db->query('SELECT alliance_id, SUM(' . $field . ') amount
FROM alliance
LEFT JOIN player USING (game_id, alliance_id)
Expand Down
14 changes: 7 additions & 7 deletions engine/Default/help.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

function echo_nav($topic_id) {
// database object
$db = new SmrMySqlDatabase();
$db2 = new SmrMySqlDatabase();
$db3 = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
$db2 = MySqlDatabase::getInstance(true);
$db3 = MySqlDatabase::getInstance(true);

// get current entry
$db->query('SELECT * FROM manual WHERE topic_id = '.$db->escapeNumber($topic_id));
Expand Down Expand Up @@ -100,7 +100,7 @@ function echo_nav($topic_id) {

function echo_content($topic_id) {
// database object
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();

// get current entry
$db->query('SELECT * FROM manual WHERE topic_id = '.$topic_id);
Expand All @@ -120,7 +120,7 @@ function echo_content($topic_id) {

function echo_subsection($topic_id) {
// database object
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
$return='';
// check if there are subsections
$db->query('SELECT * FROM manual WHERE parent_topic_id = '.$db->escapeNumber($topic_id).' ORDER BY order_id');
Expand All @@ -139,7 +139,7 @@ function echo_subsection($topic_id) {
function echo_menu($topic_id) {
$return='';
// database object
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();

$db->query('SELECT * FROM manual WHERE parent_topic_id = '.$db->escapeNumber($topic_id).' ORDER BY order_id');
if ($db->getNumRows()) {
Expand All @@ -158,7 +158,7 @@ function echo_menu($topic_id) {
}

function get_numbering($topic_id) {
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();

$db->query('SELECT * FROM manual WHERE topic_id = '.$db->escapeNumber($topic_id));
if ($db->nextRecord()) {
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/login_check_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

$lastLogin = $account->getLastLogin();

$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
if ($var['CheckType'] == 'Announcements') {
$db->query('SELECT 1 FROM announcement WHERE time >= ' . $db->escapeNumber($lastLogin) . ' LIMIT 1');
// do we have announcements?
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/message_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@

function displayScouts(&$messageBox, $player) {
// Generate the group messages
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
$db->query('SELECT player.*, count( message_id ) AS number, min( send_time ) as first, max( send_time) as last, sum(msg_read=\'FALSE\') as total_unread
FROM message
JOIN player ON player.account_id = message.sender_id AND message.game_id = player.game_id
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/rankings_alliance_vs_alliance.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$template->assign('PageTopic', 'Alliance VS Alliance Rankings');

Menu::rankings(1, 4);
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
$container = create_container('skeleton.php', 'rankings_alliance_vs_alliance.php');
$template->assign('SubmitHREF', SmrSession::getNewHREF($container));

Expand Down
2 changes: 1 addition & 1 deletion engine/Default/vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

$db->query('SELECT * FROM voting ORDER BY end DESC');
if ($db->getNumRows() > 0) {
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getInstance(true);
$votedFor = array();
$db2->query('SELECT * FROM voting_results WHERE account_id = ' . $db2->escapeNumber($account->getAccountID()));
while ($db2->nextRecord()) {
Expand Down
2 changes: 1 addition & 1 deletion engine/Draft/alliance_pick.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Returns an array with all relevant information about draft teams,
// including their current size and if the leader can pick teammates.
function get_draft_teams($gameId) {
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
$db->query('SELECT account_id FROM draft_leaders WHERE game_id=' . $db->escapeNumber($gameId));

// Get team leader, alliance, and alliance size
Expand Down
2 changes: 1 addition & 1 deletion htdocs/album/album_comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function create_error_offline($msg) {
exit;
}

$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();

if (!isset($_GET['comment']) || empty($_GET['comment'])) {
create_error_offline('Please enter a comment.');
Expand Down
Loading