PHP Classes
elePHPant
Icontem

File: test/WriterTest.php

Recommend this page to a friend!
  Classes of nvb  >  CSV Component for PHP  >  test/WriterTest.php  >  Download  
File: test/WriterTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: CSV Component for PHP
Reader and writer for CSV files
Author: By
Last change: replaced deprecated array syntax >>array()<< with >>[]<<
Date: 2 years ago
Size: 9,152 bytes
 

Contents

Class file image Download
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2015-05-03
 */

namespace Test\Net\Bazzline\Component\Csv;

//@todo implement call of this tests with different delimiters etc. (after the
//setters are developed
//@todo implement writeOne(!array)
class WriterTest extends AbstractTestCase
{
   
/**
     * @var array
     */
   
protected $contentAsArray = [
        [
           
'headlines foo',
           
'headlines bar'
       
],
        [
           
'foo',
           
'bar'
       
],
        [
           
'foobar',
           
'baz'
       
],
        [
           
'baz',
           
'barfoo'
       
]
    ];

    protected
$enclosures = [
       
's',
       
'"',
       
'|'
   
];

    protected
$delimiters = [
       
's',
       
'l',
       
'-',
       
',',
       
';'
   
];

    public function
testWriteContentLinePerLineUsingWriteOne()
    {
       
$delimiters = $this->delimiters;

        foreach (
$delimiters as $delimiter) {
           
$collection = $this->contentAsArray;
           
$expectedContent = $this->convertArrayToStrings($collection, $delimiter);
           
$file = $this->createFile();
           
$filesystem = $this->createFilesystem();
           
$writer = $this->createWriter();

           
$filesystem->addChild($file);
           
$writer->setDelimiter($delimiter);
           
$writer->setPath($file->url());

            foreach (
$collection as $content) {
               
$this->assertNotFalse($writer->writeOne($content));
            }

           
$this->assertEquals($expectedContent, $file->getContent());
        }
    }

    public function
testWriteContentLinePerLineUsingWriterAsAFunction()
    {
       
$delimiters = $this->delimiters;

        foreach (
$delimiters as $delimiter) {
           
$collection = $this->contentAsArray;
           
$expectedContent = $this->convertArrayToStrings($collection, $delimiter);
           
$file = $this->createFile();
           
$filesystem = $this->createFilesystem();
           
$writer = $this->createWriter();

           
$filesystem->addChild($file);
           
$writer->setDelimiter($delimiter);
           
$writer->setPath($file->url());

            foreach (
$collection as $content) {
               
$this->assertNotFalse($writer($content));
            }

           
$this->assertEquals($expectedContent, $file->getContent());
        }
    }

    public function
testWriteAllContentAtOnce()
    {
       
$delimiters = $this->delimiters;

        foreach (
$delimiters as $delimiter) {
           
$collection = $this->contentAsArray;
           
$expectedContent = $this->convertArrayToStrings($collection, $delimiter);
           
$file = $this->createFile();
           
$filesystem = $this->createFilesystem();
           
$writer = $this->createWriter();

           
$filesystem->addChild($file);
           
$writer->setDelimiter($delimiter);
           
$writer->setPath($file->url());

           
//simple write the content two times
           
$this->assertNotFalse($writer->writeMany($collection));
           
$this->assertNotFalse($writer->writeAll($collection));

           
$this->assertEquals($expectedContent, $file->getContent());
        }
    }

    public function
testWriteManyContentAtOnce()
    {
       
$delimiters = $this->delimiters;

        foreach (
$delimiters as $delimiter) {
           
$collection = $this->contentAsArray;
           
$expectedContent = $this->convertArrayToStrings($collection, $delimiter);
           
$file = $this->createFile();
           
$filesystem = $this->createFilesystem();
           
$writer = $this->createWriter();

           
$filesystem->addChild($file);
           
$writer->setDelimiter($delimiter);
           
$writer->setPath($file->url());

           
$this->assertNotFalse($writer->writeMany($collection));

           
$this->assertEquals($expectedContent, $file->getContent());
        }
    }

    public function
testTruncate()
    {
       
$delimiters = $this->delimiters;

        foreach (
$delimiters as $delimiter) {
           
$collection = $this->contentAsArray;
           
$content = $this->convertArrayToStrings($collection, $delimiter);
           
$file = $this->createFile();
           
$filesystem = $this->createFilesystem();
           
$writer = $this->createWriter();

           
$file->setContent($content);
           
$filesystem->addChild($file);
           
$writer->setDelimiter($delimiter);
           
$writer->setPath($file->url());

           
$writer->truncate();

           
$this->assertEquals('', $file->getContent());
        }
    }

    public function
testDelete()
    {
       
$delimiters = $this->delimiters;

        foreach (
$delimiters as $delimiter) {
           
$collection = $this->contentAsArray;
           
$content = $this->convertArrayToStrings($collection, $delimiter);
           
$file = $this->createFile();
           
$filesystem = $this->createFilesystem();
           
$writer = $this->createWriter();

           
$file->setContent($content);
           
$filesystem->addChild($file);
           
$writer->setDelimiter($delimiter);
           
$writer->setPath($file->url());

           
$this->assertTrue(file_exists($file->url()));
           
$writer->delete();
           
$this->assertFalse(file_exists($file->url()));
        }
    }

    public function
testCopy()
    {
       
$delimiters = $this->delimiters;

        foreach (
$delimiters as $delimiter) {
           
$collection = $this->contentAsArray;
           
$content = $this->convertArrayToStrings($collection, $delimiter);
           
$file = $this->createFile();
           
$filesystem = $this->createFilesystem();
           
$writer = $this->createWriter();

           
$file->setContent($content);
           
$filesystem->addChild($file);

           
$sourceFilePath = $file->url();
           
$destinationFilePath = str_replace('test.csv', 'foobar.csv', $file->url());

           
$writer->setDelimiter($delimiter);
           
$writer->setPath($sourceFilePath);

           
$this->assertFalse(file_exists($destinationFilePath));
           
$writer->copy($destinationFilePath);
           
$this->assertTrue(file_exists($destinationFilePath));
        }
    }

    public function
testCopyWithSettingNewPathAsCurrentPath()
    {
       
$delimiters = $this->delimiters;

        foreach (
$delimiters as $delimiter) {
           
$collection = $this->contentAsArray;
           
$content = $this->convertArrayToStrings($collection, $delimiter);
           
$file = $this->createFile();
           
$filesystem = $this->createFilesystem();
           
$writer = $this->createWriter();

           
$file->setContent($content);
           
$filesystem->addChild($file);

           
$sourceFilePath = $file->url();
           
$destinationFilePath = str_replace('test.csv', 'foobar.csv', $file->url());

           
$writer->setDelimiter($delimiter);
           
$writer->setPath($sourceFilePath);

           
$this->assertFalse(file_exists($destinationFilePath));
           
$writer->copy($destinationFilePath, true);
           
$this->assertTrue(file_exists($destinationFilePath));
        }
    }

    public function
testMove()
    {
       
$delimiters = $this->delimiters;

        foreach (
$delimiters as $delimiter) {
           
$collection = $this->contentAsArray;
           
$content = $this->convertArrayToStrings($collection, $delimiter);
           
$file = $this->createFile();
           
$filesystem = $this->createFilesystem();
           
$writer = $this->createWriter();

           
$file->setContent($content);
           
$filesystem->addChild($file);

           
$sourceFilePath = $file->url();
           
$destinationFilePath = str_replace('test.csv', 'foobar.csv', $file->url());

           
$writer->setDelimiter($delimiter);
           
$writer->setPath($sourceFilePath);

           
$this->assertFalse(file_exists($destinationFilePath));
           
$writer->move($destinationFilePath);
           
$this->assertTrue(file_exists($destinationFilePath));
        }
    }

   
/**
     * @param array $data
     * @param string $delimiter
     * @return string
     */
   
protected function convertArrayToStrings(array $data, $delimiter = ',')
    {
       
$string = '';

        foreach (
$data as $contents) {
            foreach (
$contents as &$part) {
               
$contains = $this->stringContains(' ');
                if (
$contains->evaluate($part, '', true)) {
                   
$part = '"' . $part . '"';
                }
            }
           
$string .= implode($delimiter, $contents) . PHP_EOL;
        }

        return
$string;
    }
}