From 5d798d890199aa831c627dea7ff74d6ef7f002a8 Mon Sep 17 00:00:00 2001 From: Nick Sagona Date: Mon, 3 Oct 2016 21:54:49 -0500 Subject: [PATCH] Rework unit tests --- phpunit.xml | 4 +- src/Csv.php | 12 +++--- tests/CsvTest.php | 99 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 101 insertions(+), 14 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 4177dd8..d3c9d5b 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,7 @@ - + tests @@ -11,7 +11,7 @@ - diff --git a/src/Csv.php b/src/Csv.php index 802e1ab..5c20903 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -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 = [ @@ -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); } diff --git a/tests/CsvTest.php b/tests/CsvTest.php index 8c7200b..7a539cc 100644 --- a/tests/CsvTest.php +++ b/tests/CsvTest.php @@ -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' => [ [ @@ -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); } } \ No newline at end of file