Skip to content

Commit

Permalink
Update csv and temp libs
Browse files Browse the repository at this point in the history
  • Loading branch information
michaljurecko committed Oct 26, 2020
1 parent 099d234 commit 29f5a6c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 93 deletions.
8 changes: 2 additions & 6 deletions .travis.yml
@@ -1,12 +1,8 @@
language: php
before_script: composer install
php:
- 5.6
- 7.0
- hhvm
matrix:
allow_failures:
- php: hhvm
- 7.1
- 7.4
install:
- composer install --dev --no-scripts
script:
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Expand Up @@ -10,11 +10,12 @@
}
],
"require": {
"keboola/php-temp": ">=0.1.0",
"keboola/csv": "~1.1.0"
"php": "^7.1",
"keboola/php-temp": "^2.0",
"keboola/csv": "^2.2"
},
"require-dev": {
"phpunit/phpunit": "^5.2",
"phpunit/phpunit": ">=7.5",
"codeclimate/php-test-reporter": "dev-master"
},
"autoload": {
Expand Down
5 changes: 1 addition & 4 deletions phpunit.xml
@@ -1,13 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
<phpunit colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php">

<testsuite name="API Test Suite">
Expand Down
107 changes: 39 additions & 68 deletions src/Keboola/CsvTable/Table.php
Expand Up @@ -2,13 +2,14 @@

namespace Keboola\CsvTable;

use Keboola\Csv\CsvFile;
use Keboola\Csv\CsvOptions;
use Keboola\Csv\CsvWriter;
use Keboola\Temp\Temp;

/**
* CsvFile class with attribute, primaryKey, incremental and name properties
*/
class Table extends CsvFile {
class Table extends CsvWriter {
/** @var array */
protected $attributes = array();

Expand All @@ -24,76 +25,59 @@ class Table extends CsvFile {
/** @var bool */
protected $incremental = null;

/**
* @brief Create a CSV file, and optionally set its header
*
* @param string $name File name Suffix
* @param array $header A header line to write into created file
* @param \Keboola\Temp\Temp $temp
* @return \Keboola\CsvTable\Table
*/
public static function create($name = '', array $header = array(), Temp $temp = null)
{
/** @var array */
protected $header = [];

if ($temp == null) {
$temp = new Temp('csv-table');
}
public function __construct(string $name, array $header = [], Temp $temp = null, $delimiter = CsvOptions::DEFAULT_DELIMITER, $enclosure = CsvOptions::DEFAULT_ENCLOSURE, $lineBreak = "\n")
{
$this->temp = $temp ? $temp : new Temp('csv-table');
$this->header = $header;
$tmpFile = $this->temp ->createTmpFile($name);
parent::__construct($tmpFile->getPathname(), $delimiter, $enclosure, $lineBreak);

$tmpFile = $temp->createTmpFile($name);
$csvFile = new self($tmpFile->getPathname());
// Write header
if (!empty($header)) {
$csvFile->writeRow($header);
}
if (!empty($this->header)) {
$this->writeRow($this->header);
}

// Preserve Temp to prevent deletion!
$csvFile->setTemp($temp);
}

$csvFile->name = $name;
public function __destruct()
{
parent::__destruct();
$this->temp->remove();
}

return $csvFile;
}
public function getPathName(): string
{
return $this->fileName;
}

/**
* @brief Resets all attributes to key:value pairs from $attributes
* @param array $attributes
*/
public function setAttributes(array $attributes)
public function getHeader(): array
{
return $this->header;
}

public function setAttributes(array $attributes): void
{
$this->attributes = $attributes;
}

/**
* @brief Adds attributes as key:value pairs from $attributes
* Existing attributes will be replaced with new values
* @param array $attributes
*/
public function addAttributes(array $attributes)
public function addAttributes(array $attributes): void
{
$this->attributes = array_replace($this->attributes, $attributes);
}

/**
* @return array
*/
public function getAttributes()
public function getAttributes(): array
{
return $this->attributes;
}

/**
* @brief Set incremental property
* @param bool $incremental
*/
public function setIncremental($incremental)
public function setIncremental(bool $incremental): void
{
$this->incremental = (bool) $incremental;
}

/**
* @return bool
*/
public function getIncremental()
public function getIncremental(): bool
{
return $this->incremental;
}
Expand All @@ -102,7 +86,7 @@ public function getIncremental()
* @brief Set a primaryKey (to combine multiple columns, use array or comma separated col names)
* @param string|array $primaryKey
*/
public function setPrimaryKey($primaryKey)
public function setPrimaryKey($primaryKey): void
{
if (!is_array($primaryKey)) {
$primaryKey = explode(',', $primaryKey);
Expand All @@ -113,9 +97,9 @@ public function setPrimaryKey($primaryKey)

/**
* @param bool $asArray
* @return string
* @return string|array
*/
public function getPrimaryKey($asArray = false)
public function getPrimaryKey(bool $asArray = false)
{
return empty($this->primaryKey)
? null
Expand All @@ -126,25 +110,12 @@ public function getPrimaryKey($asArray = false)
);
}

/**
* @param string
*/
public function setName($name) {
public function setName(string $name): void {
$this->name = $name;
}

/**
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}

/**
* @param Keboola\Temp\Temp
*/
public function setTemp(Temp $temp) {
$this->temp = $temp;
}
}
18 changes: 6 additions & 12 deletions tests/Keboola/CsvTable/TableTest.php
@@ -1,20 +1,14 @@
<?php
/**
* User: kachnitel
* Date: 7/12/13
* Time: 3:32 PM
*/

namespace Keboola\Temp\Tests;

use Keboola\CsvTable\Table;
use PHPUnit\Framework\TestCase;

class TempTest extends \PHPUnit_Framework_TestCase
class TempTest extends TestCase
{
public function testCreate()
{
/** @var \SplFileInfo $file */
$table = Table::create('filename_suffix', ['first_col', 'second_col']);
$table = new Table('filename_suffix', ['first_col', 'second_col']);
$this->assertFileExists($table->getPathName());
$this->assertEquals(
'"first_col","second_col"
Expand All @@ -32,23 +26,23 @@ public function testCreate()

public function testPrimaryKey()
{
$table = Table::create('pk', ['id', 'user', 'data']);
$table = new Table('pk', ['id', 'user', 'data']);
$table->setPrimaryKey('id,user');
$this->assertEquals('id,user', $table->getPrimaryKey());
$this->assertEquals(['id', 'user'], $table->getPrimaryKey(true));
}

public function testPrimaryKeyArray()
{
$table = Table::create('pk', ['id', 'user', 'data']);
$table = new Table('pk', ['id', 'user', 'data']);
$table->setPrimaryKey(['id', 'user']);
$this->assertEquals('id,user', $table->getPrimaryKey());
$this->assertEquals(['id', 'user'], $table->getPrimaryKey(true));
}

public function testPrimaryKeyEmpty()
{
$table = Table::create('pk', ['id', 'user', 'data']);
$table = new Table('pk', ['id', 'user', 'data']);
$this->assertNull($table->getPrimaryKey());
}
}

0 comments on commit 29f5a6c

Please sign in to comment.