Beispiel #1
0
 public function testSplit()
 {
     $csvSplitter = new CSVSplitter();
     // Test for an non-existing file
     try {
         $csvSplitter->split('AnIncorrectFile.txt');
         $this->fail('An exception should be thrown if an incorrect filename was given.');
     } catch (Exception $e) {
         $this->assertEquals(CSVSplitterException::SOURCE_FILE_NOT_FOUND, $e->getCode());
     }
     // Checks for an empty file
     touch('EmptyFile.csv');
     try {
         $csvSplitter->split('EmptyFile.csv');
         $this->fail('An exception should be thrown if the give file is empty.');
     } catch (Exception $e) {
         $this->assertEquals(CSVSplitterException::SOURCE_FILE_EMPTY, $e->getCode());
     }
     unlink('EmptyFile.csv');
     // Checks for splitting files
     $csvSplitter->split($this->testFilePath);
     $actualFileNames = array();
     $dirPath = $csvSplitter->getTempDir();
     $dir = opendir($dirPath);
     while ($file = readdir($dir)) {
         if (preg_match('/^_temp*/', $file)) {
             $actualFileNames[] = $dirPath . '/' . $file;
         }
     }
     $expectedFileNames = $csvSplitter->getTempFileList();
     sort($actualFileNames);
     sort($expectedFileNames);
     $this->assertEquals($expectedFileNames, $actualFileNames);
     $header = $csvSplitter->getHeader();
     foreach ($actualFileNames as $file) {
         $this->assertTrue(filesize($file) > 0);
         if (!is_null($header)) {
             $contents = file($file);
             list($actualTempFileHeader) = $contents;
             $this->assertEquals($header, trim($actualTempFileHeader));
         }
     }
 }
Beispiel #2
0
 /**
  * Do the data import
  *
  * @return array Array containing results of import
  */
 public function handleUpload()
 {
     CSVSplitter::setRecordLimit(50);
     $csvSplitter = new CSVSplitter();
     $csvSplitter->setTempDir(ROOT_PATH . '/lib/confs/temp');
     $success = $csvSplitter->split($this->fileName);
     if ($success) {
         $noOfRecords = $csvSplitter->getNoOfRecords();
         $tempFileList = $csvSplitter->getTempFileList();
         $result = new CSVSplitStatus('success', $this->importType, $noOfRecords, $tempFileList);
     } else {
         $result = new CSVSplitStatus('failure', $this->importType);
     }
     return $result;
 }