Integration Tests Using PHPUnit to Ensure Your Website is Workin

  • 时间:2020-09-12 10:17:13
  • 分类:网络文摘
  • 阅读:75 次

You must want to make sure your website is configured properly to work with the static contents (e.g. html, js, css) and the dynamic contents (PHP, MySQL) – either you are using Apache2 or Ngnix servers.

We can write some integration tests using PHPUnit that write contents to the website public folder, and test the contents via HTTP or HTTPS url. That way, we make sure the website is configured correctly to serve static files, dynamic contents (even with MySQL databases).

You could put this in a BASH script, then schedule the script in the Crontab that runs e.g. hourly. If the tests fail, send an email notification so you are notified in time.

1
2
3
4
5
6
7
#!/bin/bash
 
phpunit tests.php
if [[ $? -ne 0 ]]; then
   echo "Tests Failed"
   echo "Sending email..."
fi
#!/bin/bash

phpunit tests.php
if [[ $? -ne 0 ]]; then
   echo "Tests Failed"
   echo "Sending email..."
fi

Test the Static Contents

You could repeat this test by replacing the file extension with HTML, and it should still work.

1
2
3
4
5
6
7
8
9
  public function test_a_text_file() {
    define('API_ROOT_FOLDER', '/var/www/your_website');
    $domain = "helloacm.com";
    $file = API_ROOT_FOLDER . '/test.txt';
    $data = "This is a text file " . date("Y-m-d h:i:s");
    file_put_contents($file, $data);
    $data = trim(shell_exec("curl -s \"https://$domain/test.txt\""));
    $this->assertEquals($data, $data);
  }
  public function test_a_text_file() {
    define('API_ROOT_FOLDER', '/var/www/your_website');
    $domain = "helloacm.com";
    $file = API_ROOT_FOLDER . '/test.txt';
    $data = "This is a text file " . date("Y-m-d h:i:s");
    file_put_contents($file, $data);
    $data = trim(shell_exec("curl -s \"https://$domain/test.txt\""));
    $this->assertEquals($data, $data);
  }

Test the Dynamic Contents

The PHP or other dynamic contents should be interpreted at the server side before sending the content to the client. In the integration tests, we can write a PHP script, and curl it to expect the content.

1
2
3
4
5
6
7
8
9
  public function test_a_php_file() {
    define('API_ROOT_FOLDER', '/var/www/your_website');
    $domain = "helloacm.com";
    $file = API_ROOT_FOLDER . '/test.php';
    $data = "<?php \n echo 'This is a PHP file " . date("Y-m-d h:i:s"). "';";
    file_put_contents($file, $data);
    $data = trim(shell_exec("curl -s \"https://$domain/test.php\""));
    $this->assertEquals($data, 'This is a PHP file '. date("Y-m-d h:i:s"));
  }
  public function test_a_php_file() {
    define('API_ROOT_FOLDER', '/var/www/your_website');
    $domain = "helloacm.com";
    $file = API_ROOT_FOLDER . '/test.php';
    $data = "<?php \n echo 'This is a PHP file " . date("Y-m-d h:i:s"). "';";
    file_put_contents($file, $data);
    $data = trim(shell_exec("curl -s \"https://$domain/test.php\""));
    $this->assertEquals($data, 'This is a PHP file '. date("Y-m-d h:i:s"));
  }

Test the Dynamic Contents with MySQL database access

Let’s push this a step further by integration the test the connection to Database (MySQL).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  public function test_a_php_file_with_mysql_access() {
    define('API_ROOT_FOLDER', '/var/www/your_website');
    $domain = "helloacm.com";
    $file = API_ROOT_FOLDER . '/test.php';
    $DB_HOST = "localhost";
    $DB_USER = "root";
    $DB_PASSWORD = "password";
    $data = "<?php
    \$conn = mysqli_connect('$DB_HOST', '$DB_USER', '$DB_PASSWORD', \"mysql\");
    \$query = \"select * from user where User='root'\";
    \$result = mysqli_query(\$conn, \$query) or die(mysqli_error(\$conn));
    \$array = mysqli_fetch_assoc(\$result);
    echo \$array['User'] . date(\"Y-m-d h:i:s\");        
    ";
    file_put_contents($file, $data);
    $data = trim(shell_exec("curl -s \"https://$domain/test.php\""));
    $this->assertEquals($data, 'root'. date("Y-m-d h:i:s"));
  }
  public function test_a_php_file_with_mysql_access() {
    define('API_ROOT_FOLDER', '/var/www/your_website');
    $domain = "helloacm.com";
    $file = API_ROOT_FOLDER . '/test.php';
    $DB_HOST = "localhost";
    $DB_USER = "root";
    $DB_PASSWORD = "password";
    $data = "<?php
    \$conn = mysqli_connect('$DB_HOST', '$DB_USER', '$DB_PASSWORD', \"mysql\");
    \$query = \"select * from user where User='root'\";
    \$result = mysqli_query(\$conn, \$query) or die(mysqli_error(\$conn));
    \$array = mysqli_fetch_assoc(\$result);
    echo \$array['User'] . date(\"Y-m-d h:i:s\");        
    ";
    file_put_contents($file, $data);
    $data = trim(shell_exec("curl -s \"https://$domain/test.php\""));
    $this->assertEquals($data, 'root'. date("Y-m-d h:i:s"));
  }

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
龟兔赛跑数学问题  相遇后再追及的数学问题  小花在此次比赛中答错了几道题  原来四个数的平均数是多少  四面小旗子可组成几种不同的信号  玩具店的玩具每当卖出一半时  到底谁在说假话  46个人吃了100个馒头成人和小孩各有多少人  蜘蛛蜻蜓蝉各有多少只  鸡兔同笼共306只脚问鸡兔各几只 
评论列表
添加评论