Split out and improve tests

This commit is contained in:
Robin Appelman 2014-08-24 02:16:33 +02:00
commit 65b76d6428
3 changed files with 74 additions and 95 deletions

View file

@ -38,23 +38,14 @@ class Connection extends RawConnection {
$output = array(); $output = array();
$line = $this->readLine(); $line = $this->readLine();
$length = strlen(self::DELIMITER); $length = mb_strlen(self::DELIMITER);
while (substr($line, 0, $length) !== self::DELIMITER) { //next prompt functions as delimiter while (mb_substr($line, 0, $length) !== self::DELIMITER) { //next prompt functions as delimiter
$output[] .= $line; $output[] .= $line;
$line = parent::read(); $line = $this->readLine();
} }
return $output; return $output;
} }
/**
* read a single line of unprocessed output
*
* @return string
*/
public function readLine() {
return parent::read();
}
/** /**
* check if the first line holds a connection failure * check if the first line holds a connection failure
* *

View file

@ -35,7 +35,8 @@ class RawConnection {
setlocale(LC_ALL, Server::LOCALE); setlocale(LC_ALL, Server::LOCALE);
$env = array_merge($env, array( $env = array_merge($env, array(
'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!! 'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!!
'LC_ALL' => Server::LOCALE 'LC_ALL' => Server::LOCALE,
'LANG' => Server::LOCALE
)); ));
$this->process = proc_open($command, $descriptorSpec, $this->pipes, '/', $env); $this->process = proc_open($command, $descriptorSpec, $this->pipes, '/', $env);
if (!$this->isValid()) { if (!$this->isValid()) {
@ -72,8 +73,8 @@ class RawConnection {
* *
* @return string * @return string
*/ */
public function read() { public function readLine() {
return trim(fgets($this->getOutputStream())); return stream_get_line($this->getOutputStream(), 4086, "\n");
} }
/** /**
@ -83,7 +84,7 @@ class RawConnection {
*/ */
public function readAll() { public function readAll() {
$output = array(); $output = array();
while ($line = $this->read()) { while ($line = $this->readLine()) {
$output[] = $line; $output[] = $line;
} }
return $output; return $output;

View file

@ -37,12 +37,13 @@ abstract class AbstractShare extends \PHPUnit_Framework_TestCase {
} }
public function nameProvider() { public function nameProvider() {
// / ? < > \ : * | " are illegal characters in path on windows, no use trying to get them working // / ? < > \ : * | " are illegal characters in path on windows
return array( return array(
array('simple'), array('simple'),
array('with spaces_and-underscores'), array('with spaces_and-underscores'),
array("single'quote'"), array("single'quote'"),
array('$as#d€££Ö€ßœĚęĘĞĜΣΥΦΩΫΫ') array('日本語'),
array('$as#d€££Ö€ßœĚęĘĞĜΣΥΦΩΫ')
); );
} }
@ -96,110 +97,101 @@ abstract class AbstractShare extends \PHPUnit_Framework_TestCase {
$this->fail('Share "' . $this->config->share . '" not found'); $this->fail('Share "' . $this->config->share . '" not found');
} }
public function testDirectory() { public function testRootStartsEmpty() {
$this->assertEquals(array(), $this->share->dir($this->root)); $this->assertEquals(array(), $this->share->dir($this->root));
$this->share->mkdir($this->root . '/foo');
$dirs = $this->share->dir($this->root);
$this->assertCount(1, $dirs);
$this->assertEquals('foo', $dirs[0]->getName());
$this->share->rename($this->root . '/foo', $this->root . '/bar');
$dirs = $this->share->dir($this->root);
$this->assertEquals(1, count($dirs));
$this->assertEquals('bar', $dirs[0]->getName());
$this->share->rmdir($this->root . '/bar');
$this->assertCount(0, $this->share->dir($this->root));
}
/**
* @dataProvider fileDataProvider
*/
public function testFile($text) {
$size = strlen($text);
$tmpFile1 = tempnam('/tmp', 'smb_test_');
file_put_contents($tmpFile1, $text);
$this->share->put($tmpFile1, $this->root . '/lorem.txt');
unlink($tmpFile1);
$files = $this->share->dir($this->root);
$this->assertCount(1, $files);
$this->assertEquals('lorem.txt', $files[0]->getName());
$this->assertEquals($size, $files[0]->getSize());
$this->share->rename($this->root . '/lorem.txt', $this->root . '/foo.txt');
$files = $this->share->dir($this->root);
$this->assertEquals(1, count($files));
$this->assertEquals('foo.txt', $files[0]->getName());
$tmpFile2 = tempnam('/tmp', 'smb_test_');
$this->share->get($this->root . '/foo.txt', $tmpFile2);
$this->assertEquals($text, file_get_contents($tmpFile2));
unlink($tmpFile2);
$this->share->del($this->root . '/foo.txt');
$this->assertCount(0, $this->share->dir($this->root));
} }
/** /**
* @dataProvider nameProvider * @dataProvider nameProvider
*/ */
public function testEscaping($name) { public function testMkdir($name) {
$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua';
$tmpFile1 = tempnam('/tmp', 'smb_test_');
file_put_contents($tmpFile1, $text);
$this->share->mkdir($this->root . '/' . $name); $this->share->mkdir($this->root . '/' . $name);
$dir = $this->share->dir($this->root); $dirs = $this->share->dir($this->root);
$this->assertEquals($name, $dir[0]->getName()); $this->assertCount(1, $dirs);
$this->assertTrue($dir[0]->isDirectory()); $this->assertEquals($name, $dirs[0]->getName());
$this->assertCount(0, $this->share->dir($this->root . '/' . $name)); $this->assertTrue($dirs[0]->isDirectory());
}
$this->share->put($tmpFile1, $this->root . '/' . $name . '/foo.txt'); /**
$dir = $this->share->dir($this->root . '/' . $name); * @dataProvider nameProvider
$this->assertEquals('foo.txt', $dir[0]->getName()); */
public function testRenameDirectory($name) {
$this->share->mkdir($this->root . '/' . $name);
$this->share->rename($this->root . '/' . $name, $this->root . '/' . $name . '_rename');
$dirs = $this->share->dir($this->root);
$this->assertEquals(1, count($dirs));
$this->assertEquals($name . '_rename', $dirs[0]->getName());
}
$tmpFile2 = tempnam('/tmp', 'smb_test_'); /**
$this->share->get($this->root . '/' . $name . '/foo.txt', $tmpFile2); * @dataProvider nameProvider
$this->assertEquals($text, file_get_contents($tmpFile2)); */
unlink($tmpFile2); public function testRmdir($name) {
$this->share->mkdir($this->root . '/' . $name);
$this->share->rename($this->root . '/' . $name . '/foo.txt', $this->root . '/' . $name . '/bar.txt');
$dir = $this->share->dir($this->root . '/' . $name);
$this->assertEquals('bar.txt', $dir[0]->getName());
$this->assertFalse($dir[0]->isDirectory());
$this->share->del($this->root . '/' . $name . '/bar.txt');
$this->assertCount(0, $this->share->dir($this->root . '/' . $name));
$this->share->rmdir($this->root . '/' . $name); $this->share->rmdir($this->root . '/' . $name);
$this->assertCount(0, $this->share->dir($this->root)); $this->assertCount(0, $this->share->dir($this->root));
}
$this->share->put($tmpFile1, $this->root . '/' . $name); /**
$dir = $this->share->dir($this->root); * @dataProvider nameAndDataProvider
$this->assertEquals($name, $dir[0]->getName()); */
public function testPut($name, $text) {
$tmpFile = $this->getTextFile($text);
$size = filesize($tmpFile);
$tmpFile2 = tempnam('/tmp', 'smb_test_'); $this->share->put($tmpFile, $this->root . '/' . $name);
$this->share->get($this->root . '/' . $name, $tmpFile2); unlink($tmpFile);
$this->assertEquals($text, file_get_contents($tmpFile2));
unlink($tmpFile2); $files = $this->share->dir($this->root);
$this->assertCount(1, $files);
$this->assertEquals($name, $files[0]->getName());
$this->assertEquals($size, $files[0]->getSize());
$this->assertFalse($files[0]->isDirectory());
}
/**
* @dataProvider nameProvider
*/
public function testRenameFile($name) {
$tmpFile = $this->getTextFile();
$this->share->put($tmpFile, $this->root . '/' . $name);
unlink($tmpFile);
$this->share->rename($this->root . '/' . $name, $this->root . '/' . $name . '_renamed');
$files = $this->share->dir($this->root);
$this->assertEquals(1, count($files));
$this->assertEquals($name . '_renamed', $files[0]->getName());
}
/**
* @dataProvider nameAndDataProvider
*/
public function testGet($name, $text) {
$tmpFile = $this->getTextFile($text);
$this->share->put($tmpFile, $this->root . '/' . $name);
unlink($tmpFile);
$targetFile = tempnam('/tmp', 'smb_test_');
$this->share->get($this->root . '/' . $name, $targetFile);
$this->assertEquals($text, file_get_contents($targetFile));
unlink($targetFile);
}
/**
* @dataProvider nameProvider
*/
public function testDel($name) {
$tmpFile = $this->getTextFile();
$this->share->put($tmpFile, $this->root . '/' . $name);
unlink($tmpFile);
$this->share->del($this->root . '/' . $name); $this->share->del($this->root . '/' . $name);
$this->assertCount(0, $this->share->dir($this->root));
$tmpFile2 = tempnam('/tmp', 'smb_test_' . $name);
$this->share->put($tmpFile2, $this->root . '/' . $name);
$dir = $this->share->dir($this->root);
$this->assertEquals($name, $dir[0]->getName());
$this->share->del($this->root . '/' . $name);
unlink($tmpFile2);
$this->assertEquals(array(), $this->share->dir($this->root));
unlink($tmpFile1);
} }
/** /**
@ -314,7 +306,7 @@ abstract class AbstractShare extends \PHPUnit_Framework_TestCase {
* @expectedException \Icewind\SMB\Exception\NotFoundException * @expectedException \Icewind\SMB\Exception\NotFoundException
*/ */
public function testRenameTargetNonExisting() { public function testRenameTargetNonExisting() {
$txt= $this->getTextFile(); $txt = $this->getTextFile();
$this->share->put($txt, $this->root . '/foo.txt'); $this->share->put($txt, $this->root . '/foo.txt');
unlink($txt); unlink($txt);
$this->share->rename($this->root . '/foo.txt', $this->root . '/bar/foo.txt'); $this->share->rename($this->root . '/foo.txt', $this->root . '/bar/foo.txt');
@ -329,11 +321,6 @@ abstract class AbstractShare extends \PHPUnit_Framework_TestCase {
$this->share->del($this->root . '/foo.txt'); $this->share->del($this->root . '/foo.txt');
} }
public function testListRoot() {
$files = $this->share->dir('');
$this->assertGreaterThan(0, count($files));
}
/** /**
* @dataProvider nameAndDataProvider * @dataProvider nameAndDataProvider
*/ */