sebastianbergmann / dbunit

DbUnit port for PHP/PHPUnit.
https://phpunit.de/
Other
225 stars 186 forks source link

CSV dataset - check that parameters passing to function array_combine have equal number of elements #77

Closed howkey666 closed 6 years ago

howkey666 commented 11 years ago

I think that it would be great if in method "addTable" in class PHPUnit_Extensions_Database_DataSet_CsvDataSet will be check that parameters passing to function array_combine have equal number of elements. I want a know which dataset is wrong and do not see a PHP error. The method "addTable" can looks like this (I'm using PHPUnit 3.5.15):

/**
     * Adds a table to the dataset
     *
     * The table will be given the passed name. $csvFile should be a path to
     * a valid csv file (based on the arguments passed to the constructor.)
     *
     * @param string $tableName
     * @param string $csvFile
     */
    public function addTable($tableName, $csvFile)
    {
        if (!is_file($csvFile)) {
            throw new InvalidArgumentException("Could not find csv file: {$csvFile}");
        }

        if (!is_readable($csvFile)) {
            throw new InvalidArgumentException("Could not read csv file: {$csvFile}");
        }

        $fh      = fopen($csvFile, 'r');
        $columns = $this->getCsvRow($fh);
        $columnsCount = count($columns);

        if ($columns === FALSE)
        {
            throw new InvalidArgumentException("Could not determine the headers from the given file {$csvFile}");
        }

        $metaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData($tableName, $columns);
        $table    = new PHPUnit_Extensions_Database_DataSet_DefaultTable($metaData);

        while (($row = $this->getCsvRow($fh)) !== FALSE)
        {
            if ($columnsCount !== count($row)) {
                throw new Exception("Csv file: {$csvFile} and table: {$tableName} should have an equal number of elements");
            } 
            $table->addRow(array_combine($columns, $row));
        }

        $this->tables[$tableName] = $table;
    }
stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had activity within the last 60 days. It will be closed after 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] commented 6 years ago

This issue has been automatically closed because it has not had activity since it was marked as stale. Thank you for your contributions.