Skip to content

Commit fede559

Browse files
committed
add mysql crud tests
1 parent c2d4f32 commit fede559

13 files changed

+371
-27
lines changed

.travis.yml

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,30 @@ before_install:
6363
- export REDIS_VERSION_INSTALL=3.1.1
6464

6565
install:
66-
- sudo mkdir -m 777 /var/log/php-connection-pool
67-
- chmod +x ${CP_PATH}/pool_server ${CP_PATH}/initd-php-connection-pool
68-
- sudo cp ${CP_PATH}/pool_server /usr/local/bin/pool_server
69-
- sudo cp ${CP_PATH}/config.ini.example /etc/pool.ini
70-
- ${CP_PATH}/.travis/php_install_ext.sh "${CP_PATH}" "${PHP_PATH}"
71-
- if [ ! -f /bin/env ];then sudo ln -s /usr/bin/env /bin/env; fi
72-
- echo 'extension = connect_pool.so' > ${PHP_PATH}/config/conf.d/connect_pool.ini
73-
- git clone -b ${REDIS_VERSION_INSTALL} https://github.com/phpredis/phpredis.git && ${CP_PATH}/.travis/php_install_ext.sh "${CP_PATH}/phpredis" "${PHP_PATH}" && echo 'extension = redis.so' > ${PHP_PATH}/config/conf.d/redis.ini
66+
- sudo mkdir -m 777 /var/log/php-connection-pool
67+
- chmod +x ${CP_PATH}/pool_server ${CP_PATH}/initd-php-connection-pool
68+
- sudo cp ${CP_PATH}/pool_server /usr/local/bin/pool_server
69+
- sudo cp ${CP_PATH}/config.ini.example /etc/pool.ini
70+
- ${CP_PATH}/.travis/php_install_ext.sh "${CP_PATH}" "${PHP_PATH}"
71+
- if [ ! -f /bin/env ];then sudo ln -s /usr/bin/env /bin/env; fi
72+
- echo 'extension = connect_pool.so' > ${PHP_PATH}/config/conf.d/connect_pool.ini
73+
- git clone -b ${REDIS_VERSION_INSTALL} https://github.com/phpredis/phpredis.git && ${CP_PATH}/.travis/php_install_ext.sh "${CP_PATH}/phpredis" "${PHP_PATH}" && echo 'extension = redis.so' > ${PHP_PATH}/config/conf.d/redis.ini
7474

7575
before_script:
76-
- $PHP_BIN -m
77-
- $PHP_BIN -m | grep -s connect_pool
78-
- $PHP_BIN -m | grep -s redis
79-
- sudo mkdir -m 777 /var/run/cp
80-
- sudo touch /var/run/php_connection_pool.pid && sudo chmod 777 /var/run/php_connection_pool.pid
81-
- ${CP_PATH}/pool_server start
76+
- $PHP_BIN -m
77+
- $PHP_BIN -m | grep -s connect_pool
78+
- $PHP_BIN -m | grep -s redis
79+
- sudo mkdir -m 777 /var/run/cp
80+
- sudo touch /var/run/php_connection_pool.pid && sudo chmod 777 /var/run/php_connection_pool.pid
81+
- ${CP_PATH}/pool_server start
82+
- mysql -uroot -e "CREATE DATABASE IF NOT EXISTS test"
83+
- mysql -uroot test < ${CP_PATH}/.travis/test.sql
84+
- mysql -uroot -e "SET PASSWORD = PASSWORD('password')"
8285

8386
script:
8487
- netstat -tlnp |grep 6253
8588
- ${CP_PATH}/pool_server status
86-
- env TEST_PHP_EXECUTABLE=$PHP_BIN $PHP_BIN -c "${PHP_PATH}/config/php.ini" "${PHP_PATH}/run-tests.php" --show-diff "${CP_PATH}/tests" |tee /tmp/php_cp_test.result && fail_num=$(grep -o -E "Tests\s+failed\s*:\s*[0-9]{1,}\s*\(" /tmp/php_cp_test.result |grep -o -E "[0-9]{1,}" -) && fail_num=$((fail_num+0)) && echo $fail_num && [ $fail_num -eq 0 ]
89+
- env TEST_PHP_EXECUTABLE=$PHP_BIN $PHP_BIN "${PHP_PATH}/run-tests.php" -c "${PHP_PATH}/config/php.ini" --show-diff "${CP_PATH}/tests" |tee /tmp/php_cp_test.result && fail_num=$(grep -o -E "Tests\s+failed\s*:\s*[0-9]{1,}\s*\(" /tmp/php_cp_test.result |grep -o -E "[0-9]{1,}" -) && fail_num=$((fail_num+0)) && echo $fail_num && [ $fail_num -eq 0 ]
8790

8891
after_success: true
8992

@@ -94,7 +97,7 @@ after_script: true
9497
notifications:
9598
email:
9699
recipients:
97-
98-
100+
101+
99102
on_success: change
100103
on_failure: always

.travis/php_install.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ function install_php()
3939

4040
# prepare
4141
#param_general="--disable-all"
42+
here_dir=$(dirname "$0")
4243
conf_dir="${prefix}/${buildname}/config"
4344
conf_ext_dir="${conf_dir}/conf.d"
4445
[ ! -d "$conf_ext_dir" ] && mkdir -p -m 755 "$conf_ext_dir"
45-
cp "${prefix}/${buildname}/php.ini-production" "${conf_dir}/php.ini"
46+
cp "${prefix}/${buildname}/php.ini-production" "${conf_dir}/php.ini" && ${here_dir}/php_update_ini.sh "${conf_dir}/php.ini"
4647

4748
param_general="--with-config-file-scan-dir=${conf_dir} --sysconfdir=${conf_dir} --with-config-file-path=${conf_dir}/php.ini --with-config-file-scan-dir=${conf_ext_dir}"
4849
param_sapi="--enable-cli --disable-cgi"

.travis/php_update_ini.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
function log_update_config()
3+
{
4+
echo "[update_php_config] $@" 1>&2
5+
}
6+
7+
function update_php_config()
8+
{
9+
file_path="$1"
10+
if [ ! -f $file_path ]; then
11+
log_update_config "PHP config file ${file_path} DO NOT exist"
12+
return 1
13+
fi
14+
sed -i -e "/^;date.timezone/c\date.timezone = Asia/Chongqing" "${file_path}"
15+
ret=$?
16+
17+
if [ $ret -eq 0 ]; then
18+
log_update_config "done successfully"
19+
else
20+
log_update_config "failed"
21+
fi
22+
return $ret;
23+
}
24+
25+
# install
26+
update_php_config "$1"

.travis/test.sql

Lines changed: 119 additions & 0 deletions
Large diffs are not rendered by default.

tests/environment.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
echo $msg . PHP_EOL;
44
exit($code);
55
};
6+
67
if (!stristr(PHP_OS, "Linux")) {
78
$php_test_exit(1, "Skip this test, Linux platforms only");
89
}

tests/mysql/basic-delete.phpt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
Testing php-cp mysql delete
3+
4+
--SKIPIF--
5+
<?php
6+
require(dirname(__DIR__) . DIRECTORY_SEPARATOR . "environment.php");
7+
?>
8+
9+
--FILE--
10+
<?php
11+
try {
12+
$db = new pdoProxy('mysql:dbname=test;host=127.0.0.1;port=3306;charset=utf8', "root", "password");
13+
$sql = 'DELETE FROM `test_user` WHERE `user_id`= :user_id';
14+
$stmt = $db->prepare($sql);
15+
$user_id = 3;
16+
$stmt->bindValue(":user_id", $user_id, PDO::PARAM_INT);
17+
$exec_result = $stmt->execute();
18+
$affected_rows = null;
19+
if ($exec_result) {
20+
$affected_rows = $stmt->rowCount();
21+
}
22+
23+
$sql_select = "SELECT `user_id` FROM `test_user` WHERE `user_id`=\"${user_id}\" LIMIT 0,1";
24+
$result = $db->query($sql_select)->fetchAll(PDO::FETCH_ASSOC);
25+
$result_check = is_array($result) && count($result) === 0;
26+
$db->release();
27+
var_dump($exec_result);
28+
var_dump($affected_rows);
29+
var_dump($result_check);
30+
} catch (\Exception $e) {
31+
var_dump($e);
32+
}
33+
?>
34+
--EXPECTF--
35+
bool(true)
36+
int(1)
37+
bool(true)
38+
39+
--CLEAN--
40+
<?php
41+
?>
42+

tests/mysql/basic-insert.phpt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
Testing php-cp mysql insert
3+
4+
--SKIPIF--
5+
<?php
6+
require(dirname(__DIR__) . DIRECTORY_SEPARATOR . "environment.php");
7+
?>
8+
9+
--FILE--
10+
<?php
11+
try {
12+
$db = new pdoProxy('mysql:dbname=test;host=127.0.0.1;port=3306;charset=utf8', "root", "password");
13+
$sql = 'INSERT INTO `test_user` SET `nickname`= :nickname,`email`= :email,`register_datetime`= :reg_datetime';
14+
$stmt = $db->prepare($sql);
15+
$seq = time();
16+
$nickname = "user_reg_time_${seq}";
17+
$stmt->bindValue(":nickname", $nickname, PDO::PARAM_STR);
18+
$stmt->bindValue(":email", "user_reg_time_${seq}@example.com", PDO::PARAM_STR);
19+
$stmt->bindValue(":reg_datetime", date("Y-m-d H:i:s"), PDO::PARAM_STR);
20+
$exec_result = $stmt->execute();
21+
$insert_id = $affected_rows = null;
22+
if ($exec_result) {
23+
$insert_id = $db->lastInsertId();
24+
$affected_rows = $stmt->rowCount();
25+
}
26+
27+
$sql_select = "SELECT `user_id` FROM `test_user` WHERE `nickname`=\"${nickname}\" LIMIT 0,1";
28+
$result = $db->query($sql_select)->fetchAll(PDO::FETCH_ASSOC);
29+
$result_check = isset($result[0]) && is_array($result[0]) && $result[0]["user_id"] == $insert_id;
30+
$db->release();
31+
var_dump($exec_result);
32+
var_dump($result_check);
33+
var_dump($insert_id);
34+
var_dump($affected_rows);
35+
} catch (\Exception $e) {
36+
var_dump($e);
37+
}
38+
?>
39+
--EXPECTF--
40+
bool(true)
41+
bool(true)
42+
string(%d) "%d"
43+
int(1)
44+
45+
--CLEAN--
46+
<?php
47+
?>
48+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Testing php-cp mysql query select max
3+
4+
--SKIPIF--
5+
<?php
6+
require(dirname(__DIR__) . DIRECTORY_SEPARATOR . "environment.php");
7+
?>
8+
9+
--FILE--
10+
<?php
11+
try {
12+
$db = new pdoProxy('mysql:dbname=test;host=127.0.0.1;port=3306;charset=utf8', "root", "password");
13+
$sql = 'SELECT MAX(`street`) as max_street FROM `test_post_address` WHERE `user_id`=1 ORDER BY `pa_id` ASC LIMIT 0,1';
14+
$result = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
15+
$result_check = isset($result[0]) && is_array($result[0]) && $result[0]["max_street"] == "30120";
16+
$db->release();
17+
var_dump($result_check);
18+
} catch (\Exception $e) {
19+
var_dump($e);
20+
}
21+
?>
22+
--EXPECT--
23+
bool(true)
24+
25+
--CLEAN--
26+
<?php
27+
?>
28+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Testing php-cp mysql select
3+
4+
--SKIPIF--
5+
<?php
6+
require(dirname(__DIR__) . DIRECTORY_SEPARATOR . "environment.php");
7+
?>
8+
9+
--FILE--
10+
<?php
11+
try {
12+
$db = new pdoProxy('mysql:dbname=test;host=127.0.0.1;port=3306;charset=utf8', "root", "password");
13+
$sql = 'SELECT `user_id`,`nickname` FROM `test_user` WHERE `email`="[email protected]"';
14+
$result = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
15+
$result_check = isset($result[0]) && is_array($result[0]) && $result[0]["user_id"] == 2
16+
&& $result[0]["nickname"] == "user2";
17+
$db->release();
18+
var_dump($result_check);
19+
} catch (\Exception $e) {
20+
var_dump($e);
21+
}
22+
?>
23+
--EXPECT--
24+
bool(true)
25+
26+
--CLEAN--
27+
<?php
28+
?>
29+

tests/mysql/basic-select1.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Testing php-cp platform
2+
Testing php-cp mysql select 1
33

44
--SKIPIF--
55
<?php
@@ -9,8 +9,8 @@ require(dirname(__DIR__) . DIRECTORY_SEPARATOR . "environment.php");
99
--FILE--
1010
<?php
1111
try{
12-
$db = new pdoProxy('mysql:dbname=mysql;host=127.0.0.1;port=3306;charset=utf8', "travis", "");
13-
$result = $db->query("SELECT 1")->fetchAll();
12+
$db = new pdoProxy('mysql:dbname=test;host=127.0.0.1;port=3306;charset=utf8', "root", "password");
13+
$result = $db->query("SELECT 1")->fetchAll(PDO::FETCH_ASSOC);
1414
$db->release();
1515
var_dump((bool) $result);
1616
} catch (\Exception $e) {

0 commit comments

Comments
 (0)