Skip to content

Commit

Permalink
Rework unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Oct 4, 2016
1 parent 53810b1 commit 5d798d8
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 14 deletions.
4 changes: 2 additions & 2 deletions phpunit.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Pop Data Component PHPUnit Test">
<testsuite name="Pop CSV Component PHPUnit Test">
<directory>tests</directory>
</testsuite>
</testsuites>
Expand All @@ -11,7 +11,7 @@
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="/tmp/pop-data-cc" charset="UTF-8"
<log type="coverage-html" target="/tmp/pop-csv-cc" charset="UTF-8"
yui="true" highlight="false" showUncoveredFiles="true"
lowUpperBound="35" highLowerBound="70" />
</logging>
Expand Down
12 changes: 7 additions & 5 deletions src/Csv.php
Expand Up @@ -96,8 +96,9 @@ public function unserialize(array $options = [])
*/
public function outputToHttp($filename = 'pop-data.csv', $forceDownload = true)
{
if (null === $this->string) {
throw new Exception('Error: The data has not been properly serialized.');
// Attempt to serialize data if it hasn't been done yet
if ((null === $this->string) && (null !== $this->data)) {
$this->serialize();
}

$headers = [
Expand Down Expand Up @@ -141,14 +142,15 @@ public function __toString()
* Output CSV data to a file
*
* @param string $to
* @throws Exception
* @return void
*/
public function writeToFile($to)
{
if (null === $this->string) {
throw new Exception('Error: The data has not been properly serialized.');
// Attempt to serialize data if it hasn't been done yet
if ((null === $this->string) && (null !== $this->data)) {
$this->serialize();
}

file_put_contents($to, $this->string);
}

Expand Down
99 changes: 92 additions & 7 deletions tests/CsvTest.php
Expand Up @@ -2,22 +2,73 @@

namespace Pop\Data\Test;

use Pop\Data\Type\Csv;
use Pop\Csv\Csv;

class CsvTest extends \PHPUnit_Framework_TestCase
{

public function testUnserializeAndSerialize()
{
$data = Csv::unserialize(file_get_contents(__DIR__ . '/tmp/data.csv'));
$this->assertEquals('testuser1', $data[0]['username']);
$string = Csv::serialize($data);
$this->assertContains('testuser1,testuser1@test.com', $string);
$data1 = new Csv(__DIR__ . '/tmp/data.csv');
$data2 = new Csv(file_get_contents(__DIR__ . '/tmp/data.csv'));
$data1Ary = $data1->unserialize();
$data2Ary = $data2->unserialize();
$this->assertEquals('testuser1', $data1Ary[0]['username']);
$this->assertEquals('testuser1', $data2Ary[0]['username']);
$string = new Csv($data1Ary);
$this->assertContains('testuser1,testuser1@test.com', $string->serialize());
}

public function testSerializeWithAssociativeArray()
{
$data = [
'my_table' => [
[
'first_name' => 'Bob',
'last_name' => 'Smith, III'
],
[
'first_name' => 'Jane',
'last_name' => 'Smith "Janey"'
],
[
'first_name' => 'Jim',
'last_name' => 'Smith, Jr. "Junior"'
]
]
];
$string = new Csv($data);
$this->assertContains('Bob,"Smith, III"', (string)$string);
}

public function testOmit()
{
$data = [
[
'first_name' => 'Bob',
'last_name' => 'Smith, III'
],
[
'first_name' => 'Jane',
'last_name' => 'Smith "Janey"'
],
[
'first_name' => 'Jim',
'last_name' => 'Smith, Jr. "Junior"'
]
];
$string = new Csv($data);
$csvString = $string->serialize(['omit' => 'first_name']);
$this->assertNotContains('Bob', $csvString);
}

public function testIsValid()
{
$this->assertTrue(Csv::isValid(file_get_contents(__DIR__ . '/tmp/data.csv')));
}

public function testWriteToFile()
{
$data = [
'my_table' => [
[
Expand All @@ -30,8 +81,42 @@ public function testSerializeWithAssociativeArray()
]
]
];
$string = Csv::serialize($data);
$this->assertContains('Bob,Smith', $string);
$string = new Csv($data);
$string->writeToFile(__DIR__ . '/tmp/test.csv');
$this->assertFileExists(__DIR__ . '/tmp/test.csv');

if (file_exists(__DIR__ . '/tmp/test.csv')) {
unlink(__DIR__ . '/tmp/test.csv');
}
}

/**
* @runInSeparateProcess
*/
public function testOutputToHttp()
{
$data = new Csv([
['foo' => 'bar']
]);
ob_start();
$data->outputToHttp('test.csv', false);
$result = ob_get_clean();
$this->assertContains('foo', $result);
}

/**
* @runInSeparateProcess
*/
public function testOutputToHttps()
{
$_SERVER['SERVER_PORT'] = 443;
$data = new Csv([
['foo' => 'bar']
]);
ob_start();
$data->outputToHttp('test.csv', false);
$result = ob_get_clean();
$this->assertContains('foo', $result);
}

}

0 comments on commit 5d798d8

Please sign in to comment.