<?php

require_once 'PHPUnit/Framework/TestCase.php';
require_once 'FilePatternSplitter.php';

/**
 * test case.
 */
class FilePatternSplitterTest extends PHPUnit_Framework_TestCase {
	
	public $obj;
	/**
	 * Prepares the environment before running a test.
	 */
	protected function setUp() {
		parent::setUp ();
		$this->obj = new FilePatternSplitter;
	}
	
	/**
	 * Cleans up the environment after running a test.
	 */
	protected function tearDown() {
		parent::tearDown ();
		$this->obj->cleanup(getcwd());
	}
	
	public function testGetPatternPositions() {
		$s = 'xxabcxxbbcxxxx';
		$p = array('/[^x]+/');
		$expected = array(array('abc', 2, $p[0]), array('bbc', 7, $p[0]));
		$method = $this->getMethod('getPatternPositions');
		$actual = $method->invokeArgs($this->obj, array($s, $p));
		$this->assertEquals($expected, $actual);
	}
	
	public function testGetFirstPattern(){
		$s = 'xxabcxxbbcxxxx';
		$p = array('/[^x]+/');
		$expected = array('abc', 2, $p[0]);
		$method = $this->getMethod('getPatternPositions');
		$patternpositions = $method->invokeArgs($this->obj, array($s, $p));
		$method = $this->getMethod('getFirstPattern');
		$actual = $method->invokeArgs($this->obj, array($patternpositions));
		$this->assertEquals($expected, $actual);
	}
	public function testGetPrefix(){
		$expected = 'fps00006_';
		$method = $this->getMethod('getPrefix');
		$actual = $method->invokeArgs($this->obj, array(6));
		$this->assertEquals($expected, $actual);
	}
	public function testGetFPSFiles(){
		$cwd = getcwd();
		$expected = array(
					$cwd.DIRECTORY_SEPARATOR.'fps00001_test.txt', 
					$cwd.DIRECTORY_SEPARATOR.'fps00002_test.txt'
				);
		touch($expected[0]);
		touch($expected[1]);
		$method = $this->getMethod('getFPSFiles');
		$actual = $method->invokeArgs($this->obj,array($cwd));
		$this->assertEquals($expected, $actual);
		unlink($expected[0]);
		unlink($expected[1]);
	}
	/**
	* @expectedException InvalidArgumentException
	*/ 
	public function testGetFPSFilesException(){
		$method = $this->getMethod('getFPSFiles');
		$actual = $method->invokeArgs($this->obj,array(''));
	}
	
	public function testGetOutFH(){
		$filenum = 9;
		$file = 'test__.txt';
		$newfile = 'fps00009_'.$file;
		//print "\n".$newfile."\n"; 
		$this->assertFalse(file_exists($newfile), 'newfile does not exist');
		$method = $this->getMethod('getOutFH');
		$fh = $method->invokeArgs($this->obj,array($filenum, $file));
		$this->assertTrue(file_exists($newfile), 'newfile exists');
		$this->assertTrue(get_resource_type($fh) == 'stream', 'fh is a filehandle' );
		fclose($fh);
		unlink($newfile);
	}
	
	public function testMergeFile(){
		$fcontents1 = "a\nb\nc\n";
		$file1 = 'fps00001_test1__.txt';
		file_put_contents($file1, $fcontents1);
		$mergefile = $file1.'.merged';
		$fh = fopen($mergefile, 'w');
		$method = $this->getMethod('mergeFile');
		$method->invokeArgs($this->obj,array($file1, $fh));
		fclose($fh);
		$fcontents2 = file_get_contents($mergefile);
		unlink($file1);
		unlink($mergefile);
		$this->assertEquals($fcontents1, $fcontents2);
	}
	public function testMergeFiles(){
		$fcontents1 = "a\nb\nc\n";
		$file1 = 'fps00001_test1__.txt';
		file_put_contents($file1, $fcontents1);
		$fcontents2 = "d\ne\nf\n";
		$file2 = 'fps00002_test1__.txt';
		file_put_contents($file2, $fcontents2);
		$mergefile = 'test1__.txt.merged';
		$method = $this->getMethod('mergeFiles');
		$method->invokeArgs($this->obj,array(array($file1, $file2)));
		$mcontents = file_get_contents($mergefile);
		unlink($file1);
		unlink($file2);
		unlink($mergefile);
		$this->assertEquals($fcontents1.$fcontents2, $mcontents);
	}
	public function testMerge(){
		$fcontents1 = "a\nb\nc\n";
		$file1 = 'fps00001_test1__.txt';
		file_put_contents($file1, $fcontents1);
		$fcontents2 = "d\ne\nf\n";
		$file2 = 'fps00002_test1__.txt';
		file_put_contents($file2, $fcontents2);
		$mergefile = 'test1__.txt.merged';
		$this->obj->merge(getcwd());
		$mcontents = file_get_contents($mergefile);
		unlink($file1);
		unlink($file2);
		unlink($mergefile);
		$this->assertEquals($fcontents1.$fcontents2, $mcontents);
	}
	/**
	* @expectedException InvalidArgumentException
	*/ 
	public function testMergeException1(){
		$this->obj->merge('whatever');
		$this->assertTrue(false);
	}
	/**
	* @expectedException RuntimeException
	*/ 
	public function testMergeException2(){
		$this->obj->merge(getcwd());
		$this->assertTrue(false);
	}
	
	public function testCheckLine(){
		$line = 'aaaaa';
		$method = $this->getMethod('checkLine');
		$method->invokeArgs($this->obj,array($line, 1, ini_get('pcre.backtrack_limit')));
	}
	/**
	* @expectedException RuntimeException
	*/ 
	public function testCheckLine2(){
		$line='a';
		for ($i=0; $i<100001; $i++){
			$line .= 'a';
		}
		$method = $this->getMethod('checkLine');
		$method->invokeArgs($this->obj,array($line, 1, ini_get('pcre.backtrack_limit')));
	}
	public function testSplit(){
		$fcontents1 = "a\nb\nc\nd\ne\nc";
		$file1 = 'test1__.txt';
		file_put_contents($file1, $fcontents1);
		$fps1 = 'fps00001_'.$file1;
		$fps2 = 'fps00002_'.$file1;
		$fps3 = 'fps00003_'.$file1;
		$this->assertFalse(file_exists($fps1), 'fps1 does not exist');
		$this->assertFalse(file_exists($fps2), 'fps2 does not exist');
		$this->assertFalse(file_exists($fps3), 'fps3 does not exist');
		$this->obj->split($file1, array('/b/'));
		$this->assertTrue(file_exists($fps1), 'fps2 does exist');
		$this->assertTrue(file_exists($fps2), 'fps2 does exist');
		$this->assertFalse(file_exists($fps3), 'fps3 does not exist');
		unlink($file1);
		unlink($fps1);
		unlink($fps2);
	}
	
	/**
	* @expectedException InvalidArgumentException
	*/ 
	public function testSplitException1(){
		$this->obj->split('nonexistent file', array());
		$this->assertTrue(false);
	}
	/**
	* @expectedException InvalidArgumentException
	*/ 
	public function testSplitException2(){
		$this->obj->split('FilePatternSplitterTest.php', array());
		$this->assertTrue(false);
	}
	
	
	/**
	* @expectedException InvalidArgumentException
	*/ 
	public function testHandleArgvException(){
		$this->obj->handleArgv(array());
	}
	/**
	 * @expectedException InvalidArgumentException
	 */
	public function testHandleArgvException1(){
		$this->obj->handleArgv(array('', 'something'));
	}
	
	/**
	* @expectedException InvalidArgumentException
	*/ 
	public function testHandleArgvSplit(){
		$fcontents1 = "a\nb\nc\nd\ne\nc";
		$file1 = 'test1__.txt';
		file_put_contents($file1, $fcontents1);
		$this->obj->handleArgv(array('', 'split', $file1));
		unlink($file1);
	}
	public function testHandleArgvSplit1(){
		$fcontents1 = "a\nb\nc\nd\ne\nc";
		$file1 = 'test1__.txt';
		file_put_contents($file1, $fcontents1);
		$this->obj->handleArgv(array('', 'split', $file1, '/b/'));
		unlink($file1);
	}
	public function testHandleArgvMerge(){
		$fcontents1 = "a\nb\nc\n";
		$file1 = 'fps00001_test1__.txt';
		file_put_contents($file1, $fcontents1);
		$this->obj->handleArgv(array('', 'merge', '.'));
		unlink($file1);
		unlink('test1__.txt.merged');
	}
	/**
	* @expectedException InvalidArgumentException
	*/ 
	public function testHandleArgvMerge2(){
		$this->obj->handleArgv(array('', 'merge'));
	}
	
	
	protected static function getMethod($name) {
		$class = new ReflectionClass('FilePatternSplitter');
		$method = $class->getMethod($name);
		$method->setAccessible(true);
		return $method;
	}	
	
	public function testSplitAndMerge(){
		$fcontents = "a\nb\nc\nd\ne\nc";
		$file = 'test1__.txt';
		file_put_contents($file, $fcontents);
		$this->obj->handleArgv(array('', 'split', $file, '/b/'));
		$this->obj->handleArgv(array('', 'merge', '.'));
		$mergedfile = 'test1__.txt';
		$mergedcontents = file_get_contents($mergedfile);
		$this->assertEquals($fcontents, $mergedcontents);
		unlink($file);
	}
	public function testSplitAndMergeWindowsLinebreaks(){
		$fcontents = "a\r\nb\r\nc\r\nd\r\ne\r\nc";
		$file = 'test1__.txt';
		file_put_contents($file, $fcontents);
		$this->obj->handleArgv(array('', 'split', $file, '/b/'));
		$this->obj->handleArgv(array('', 'merge', '.'));
		$mergedfile = 'test1__.txt';
		$mergedcontents = file_get_contents($mergedfile);
		$this->assertEquals($fcontents, $mergedcontents);
		unlink($file);
	}
	public function testCleanup(){
		$file1 = 'fps00001_test1__.txt';
		$file2 = 'test1__.txt.merged';
		touch($file1);
		touch($file2);
		$this->assertTrue(file_exists($file1), 'file1 does exist');
		$this->assertTrue(file_exists($file2), 'file2 does exist');
		$this->obj->handleArgv(array('', 'cleanup', '.'));
		$this->assertFalse(file_exists($file1), 'file1 does not exist');
		$this->assertFalse(file_exists($file2), 'file2 does not exist');
	}
	/**
	* @expectedException InvalidArgumentException
	*/ 
	public function testCleanupException(){
		$this->obj->cleanup('nonexistingdir');
	}
}

