PHP Classes
elePHPant
Icontem

File: test/ReaderTest.php

Recommend this page to a friend!
  Classes of nvb  >  CSV Component for PHP  >  test/ReaderTest.php  >  Download  
File: test/ReaderTest.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: 1 year ago
Size: 7,322 bytes
 

Contents

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

namespace Test\Net\Bazzline\Component\Csv;

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

    public function
testHasHeadline()
    {
       
$content = $this->contentAsArray;
       
$file = $this->createFile();
       
$filesystem = $this->createFilesystem();
       
$reader = $this->createReader();

       
$expectedContent = array_slice($content, 1);
       
$expectedHeadline = $content[0];
       
$expectedContent = $this->addHeadlineAsKeysToContent($expectedHeadline, $expectedContent);

       
$file->setContent($this->getContentAsString());
       
$filesystem->addChild($file);
       
$reader->setPath($file->url());
       
$reader->enableHasHeadline();

       
$this->assertTrue($reader->hasHeadline(), 'has headline');
       
$this->assertEquals($expectedContent, $reader->readAll(), 'read all');
       
$this->assertEquals($expectedHeadline, $reader->readHeadline(), 'read headline');
    }

    public function
testReadWholeContentAtOnce()
    {
       
$file = $this->createFile();
       
$filesystem = $this->createFilesystem();
       
$reader = $this->createReader();

       
$file->setContent($this->getContentAsString());
       
$filesystem->addChild($file);
       
$reader->setPath($file->url());

       
$this->assertFalse($reader->hasHeadline());
       
$this->assertEquals($this->contentAsArray, $reader->readAll());
    }

    public function
testReadWholeContentByUsingTheIteratorInterface()
    {
       
$file = $this->createFile();
       
$filesystem = $this->createFilesystem();
       
$reader = $this->createReader();

       
$file->setContent($this->getContentAsString());
       
$filesystem->addChild($file);
       
$reader->setPath($file->url());

       
$index = 0;
        foreach (
$reader as $line) {
           
$this->assertEquals($this->contentAsArray[$index], $line);
            ++
$index;
        }
    }

    public function
testReadWholeContentByUsingReaderAsAFunction()
    {
       
$file = $this->createFile();
       
$filesystem = $this->createFilesystem();
       
$reader = $this->createReader();

       
$file->setContent($this->getContentAsString());
       
$filesystem->addChild($file);
       
$reader->setPath($file->url());

       
$index = 0;

        while (
$line = $reader()) {
           
$this->assertEquals($this->contentAsArray[$index], $line);
            ++
$index;
        }
    }

    public function
testReadWholeContentLinePerLine()
    {
       
$file = $this->createFile();
       
$filesystem = $this->createFilesystem();
       
$reader = $this->createReader();

       
$file->setContent($this->getContentAsString());
       
$filesystem->addChild($file);
       
$reader->setPath($file->url());

       
$index = 0;

        while (
$line = $reader->readOne()) {
           
$this->assertEquals($this->contentAsArray[$index], $line);
            ++
$index;
        }
    }

   
/**
     * @return array
     */
   
public function readChunkOfTheContentDataProvider()
    {
       
$content = $this->contentAsArray;
       
$indices = array_keys($content);
       
$length = count($indices);

        return [
           
'read only the first line' => [
               
'content' => $content,
               
'end' => $indices[1],
               
'start' => $indices[0]
            ],
           
'read one line the middle' => [
               
'content' => $content,
               
'end' => $indices[2],
               
'start' => $indices[1]
            ],
           
'read whole content' => [
               
'content' => $content,
               
'end' => $indices[($length - 1)],
               
'start' => $indices[0]
            ]
        ];
    }

   
/**
     * @dataProvider readChunkOfTheContentDataProvider
     * @param array $content
     * @param int $end
     * @param int $start
     */
   
public function testReadChunkOfTheContentByProvidingStartLineNumberAndAmountOfLines(array $content, $end, $start)
    {
       
$file = $this->createFile();
       
$filesystem = $this->createFilesystem();
       
$length = ($end - $start);
       
$reader = $this->createReader();

       
$file->setContent($this->convertArrayToStrings($content));
       
$filesystem->addChild($file);
       
$reader->setPath($file->url());

       
$expectedContent = [];

       
$counter = $start;

        while (
$counter < $end) {
           
$expectedContent[] = $content[$counter];
            ++
$counter;
        }

       
$this->assertEquals($expectedContent, $reader->readMany($length, $start));
    }

    public function
testReadContentByProvidingTheCurrentLineNumber()
    {
       
$data = $this->contentAsArray;
       
$file = $this->createFile();
       
$filesystem = $this->createFilesystem();
       
$reader = $this->createReader();

       
$file->setContent($this->getContentAsString());
       
$filesystem->addChild($file);
       
$reader->setPath($file->url());

        foreach (
$data as $lineNumber => $line) {
           
$this->assertEquals($line, $reader->readOne($lineNumber));
        }
    }

    public function
testReadContentByProvidingTheCurrentLineNumberByUsingReaderAsAFunction()
    {
       
$data = $this->contentAsArray;
       
$file = $this->createFile();
       
$filesystem = $this->createFilesystem();
       
$reader = $this->createReader();
       
$file->setContent($this->getContentAsString());
       
$filesystem->addChild($file);
       
$reader->setPath($file->url());
        foreach(
$data as $lineNumber => $line) {
           
$this->assertEquals($line, $reader($lineNumber));
        }
    }

   
/**
     * @return string
     */
   
protected function getContentAsString()
    {
        return
$this->convertArrayToStrings($this->contentAsArray);
    }

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

        foreach (
$data as $contents) {
           
$string .= implode($delimiter, $contents) . PHP_EOL;
        }

        return
$string;
    }

   
/**
     * @param array $headline
     * @param array $content
     * @return array
     */
   
private function addHeadlineAsKeysToContent(array $headline, array $content)
    {
       
$adaptedContent = [];

        foreach (
$content as $key => $columns) {
           
$adaptedContent[$key] = [];
            foreach (
$columns as $columnKey => $columnContent) {
               
$adaptedContent[$key][$headline[$columnKey]] = $columnContent;
            }
        }

        return
$adaptedContent;
    }
}