PHPUnit数据库测试和Travis CI:无法截断表

时间:2015-07-28 00:43:49

标签: php mysql testing phpunit travis-ci

我正在编写PHPUnit测试来测试MySQL数据库填充。我的测试在本地工作正常,但在Travis CI上失败,出现以下错误:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TextSearch {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner txt;
        File file = null;
        String Default = "/eng/home/tylorkun/workspace/09.1/src/Sample.txt";

        try {
            txt = new Scanner(System.in);
            System.out.print("Please enter the text file name or type  'Default' for a default file. ");
            file = new File(txt.nextLine());

            txt = new Scanner(file);

            while (txt.hasNextLine()) {
                String line = txt.nextLine();
                System.out.println(line);
            }
            txt.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        try {
            txt = new Scanner(file);
            Scanner in = new Scanner(System.in);
            in.nextLine();
            System.out.print("Please enter a string to search for. Please do not enter a string longer than 16 characters. ");
            String wordInput = in.nextLine();

            //If too long
            if (wordInput.length() > 16) {
                System.out.println("Please do not enter a string longer than 16 characters. Try again. ");
                wordInput = in.nextLine();
            }

            //Search
            int count = 0;
            while (txt.hasNextLine()) //Should txt be in? 
            {
                String line = txt.nextLine();
                count++;
                if (line.contains(wordInput)) //count > 0
                {
                    System.out.println("'" + wordInput + "' was found " + count + " times in this document. ");
                    break;
                }
            //else
                //{
                //    System.out.println("Word was not found. ");
                //}
            }
        } catch (FileNotFoundException e) {
            System.out.println("Word was not found. ");
        }
    } //main ends
} //TextSearch ends

我有一个PHPUnit_Extensions_Database_Operation_Exception: COMPOSITE[TRUNCATE] operation failed on query: TRUNCATE `simple_table` using args: Array [SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.simple_table' doesn't exist] 父类和一个DatabaseTestCase测试类。

DatabaseTestCase

FillCommandTest

FillCommandTest

class DatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
{
    private static $pdo = null;

    private $connection = null;

    /**
     * Construct.
     */
    public function __construct()
    {
        $env = new Environment();

        $this->dsn = "mysql:dbname=". $env->get('DB_DATABASE', 'test') . ";host=" . $env->get('DB_HOST', '127.0.0.1');
        $this->username = $env->get('DB_USERNAME', 'travis');
        $this->password = $env->get('DB_PASSWORD', '');
        $this->db_name = $env->get('DB_DATABASE', 'test');
    }

    /**
     * Get database connection.
     * 
     * @return  PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
     */
    final public function getConnection()
    {
        if ($this->connection === null) {
            if (self::$pdo == null) {
                self::$pdo = new PDO($this->dsn, $this->username, $this->password);
            }

            $this->connection = $this->createDefaultDBConnection(self::$pdo, $this->db_name);
        }

        return $this->connection;
    }

    /**
     * Get XML dataset.
     * 
     * @param   string $file
     * @return  string
     */
    public function getDataSet($file = 'empty.xml')
    {
        $file = 'tests/datasets/' . $file;

        return $this->createXmlDataSet($file);
    }

    /**
     * Set up the test database table
     * 
     * @param   PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection $connection
     */
    protected function setUpTable($connection)
    {
        $pdo = $connection->getConnection();

        $sql = "CREATE TABLE IF NOT EXISTS simple_table (
            id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(50) NOT NULL,
            email VARCHAR(50) NOT NULL,
            address VARCHAR(100)
        )";

        $pdo->exec($sql);
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

从外观上看,您的代码似乎是指一个名为' test'的数据库。

但是我假设你正在部署,你的数据库名称应该是不同的。确保您的.env DB_DATABASE变量反映生产服务器上的正确变量。