Blame tests/p_php/20-php-mysql-test.sh

Christoph Galuschka bad8eb
#!/bin/sh
Christoph Galuschka bad8eb
# Author: Athmane Madjoudj <athmanem@gmail.com>
Christoph Galuschka 376560
# Author: Christoph Galuschka <tigalch@tigalch.org>
Christoph Galuschka bad8eb
# reusing the script from LAMP-Tests
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
t_Log "Running $0 - php-cli basic interaction with mysql test."
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
# Install php-mysql-module
Christoph Galuschka bad8eb
t_InstallPackage php-mysql
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
# we need a working and running mysql server
Christoph Galuschka 376560
#starting with 5.10 we need to reflect mysql55
Christoph Galuschka 376560
if [ $centos_ver = 5 ]
Christoph Galuschka 376560
then
Christoph Galuschka 975f89
  t_InstallPackage mysql-server mysql55-mysql-server nc
Christoph Galuschka b5edf2
  t_ServiceControl mysql55-mysqld stop
Rene Diepstraten acb41a
  t_ServiceControl mysqld start >/dev/null 2>&1
Rene Diepstraten bbeceb
elif [ $centos_ver = 6 ]
Rene Diepstraten bbeceb
then
Christoph Galuschka 376560
  t_InstallPackage mysql-server nc
Rene Diepstraten acb41a
  t_ServiceControl mysqld start >/dev/null 2>&1
Rene Diepstraten bbeceb
else
Rene Diepstraten bbeceb
  t_InstallPackage mariadb-server nc
Rene Diepstraten acb41a
  t_ServiceControl mariadb start >/dev/null 2>&1
Christoph Galuschka 376560
fi
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
#create a little DB to use
Christoph Galuschka bad8eb
CREATE='/var/tmp/mysql-php-QA.sql'
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
cat >$CREATE <
Christoph Galuschka d4b6ff
drop database if exists phptests;
Christoph Galuschka bad8eb
create database phptests;
Christoph Galuschka bad8eb
use phptests;
Christoph Galuschka bad8eb
create table tests (name varchar(20)) ;
Christoph Galuschka bad8eb
grant all on phptests.* to 'centos'@'localhost' identified by 'qa';
Christoph Galuschka bad8eb
flush privileges;
Christoph Galuschka bad8eb
EOF
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
mysql <$CREATE
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
# create PHP Script and write something into DB
Christoph Galuschka bad8eb
INSERT='/var/tmp/test.php'
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
cat >$INSERT <
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
\$dbconnect = mysql_connect("localhost","centos","qa");
Christoph Galuschka bad8eb
if (!\$dbconnect)
Christoph Galuschka bad8eb
  {
Christoph Galuschka bad8eb
  die('Could not connect: ' . mysql_error());
Christoph Galuschka bad8eb
  }
Christoph Galuschka bad8eb
mysql_select_db("phptests", \$dbconnect);
Christoph Galuschka bad8eb
mysql_query("INSERT INTO tests (name)
Christoph Galuschka bad8eb
VALUES ('phpsqltest')");
Christoph Galuschka bad8eb
mysql_close(\$dbconnect);
Christoph Galuschka bad8eb
?> 
Christoph Galuschka bad8eb
EOF
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
php $INSERT
Christoph Galuschka bad8eb
if [ $? -ne 0 ]
Christoph Galuschka bad8eb
  then
Christoph Galuschka 518959
  t_Log "Inserting into DB failed"
Christoph Galuschka bad8eb
  exit 1
Christoph Galuschka bad8eb
fi
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
# create PHP script to read from DB
Christoph Galuschka bad8eb
READ='/var/tmp/read.php'
Christoph Galuschka bad8eb
cat >$READ <
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
\$dbconnect = mysql_connect("localhost","centos","qa");
Christoph Galuschka bad8eb
if (!\$dbconnect)
Christoph Galuschka bad8eb
  {
Christoph Galuschka bad8eb
  die('Could not connect: ' . mysql_error());
Christoph Galuschka bad8eb
  }
Christoph Galuschka bad8eb
mysql_select_db("phptests", \$dbconnect);
Christoph Galuschka bad8eb
\$array = mysql_query("SELECT count(*) as success FROM tests WHERE name = 'phpsqltest'");
Christoph Galuschka bad8eb
mysql_close(\$dbconnect);
Christoph Galuschka bad8eb
\$line = mysql_fetch_array(\$array, MYSQL_ASSOC);
Christoph Galuschka bad8eb
print \$line['success'];
Christoph Galuschka bad8eb
?>
Christoph Galuschka bad8eb
EOF
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
# If we execute the script and get '1' it works (1 entry should be in the DB)
Christoph Galuschka bad8eb
php $READ | grep -q '1'
Christoph Galuschka bad8eb
Christoph Galuschka bad8eb
t_CheckExitStatus $?
Christoph Galuschka bad8eb
Christoph Galuschka 518959
#cleaning up
Christoph Galuschka 518959
/bin/rm $READ $CREATE $INSERT
Christoph Galuschka 975f89
mysql -u root -e 'drop database phptests' >/dev/null 2>&1