Additional test cases

This commit is contained in:
Robin Appelman 2013-05-01 20:58:04 +02:00
commit 69e9082def
3 changed files with 120 additions and 5 deletions

View file

@ -29,6 +29,9 @@ class InvalidHostException extends \Exception {
class AccessDeniedException extends \Exception { class AccessDeniedException extends \Exception {
} }
class InvalidTypeException extends \Exception {
}
class ErrorCodes { class ErrorCodes {
/** /**
* connection errors * connection errors
@ -44,4 +47,6 @@ class ErrorCodes {
const NameCollision = 'NT_STATUS_OBJECT_NAME_COLLISION'; const NameCollision = 'NT_STATUS_OBJECT_NAME_COLLISION';
const AccessDenied = 'NT_STATUS_ACCESS_DENIED'; const AccessDenied = 'NT_STATUS_ACCESS_DENIED';
const DirectoryNotEmpty = 'NT_STATUS_DIRECTORY_NOT_EMPTY'; const DirectoryNotEmpty = 'NT_STATUS_DIRECTORY_NOT_EMPTY';
const FileIsADirectory = 'NT_STATUS_FILE_IS_A_DIRECTORY';
const NotADirectory = 'NT_STATUS_NOT_A_DIRECTORY';
} }

View file

@ -184,10 +184,11 @@ class Share {
/** /**
* @param $lines * @param $lines
* *
* @throws \SMB\NotFoundException * @throws NotFoundException
* @throws \SMB\AlreadyExistsException * @throws AlreadyExistsException
* @throws \SMB\AccessDeniedException * @throws AccessDeniedException
* @throws \SMB\NotEmptyException * @throws NotEmptyException
* @throws InvalidTypeException
* @throws \Exception * @throws \Exception
* @return bool * @return bool
*/ */
@ -210,6 +211,9 @@ class Share {
throw new AccessDeniedException(); throw new AccessDeniedException();
case ErrorCodes::DirectoryNotEmpty: case ErrorCodes::DirectoryNotEmpty:
throw new NotEmptyException(); throw new NotEmptyException();
case ErrorCodes::FileIsADirectory:
case ErrorCodes::NotADirectory:
throw new InvalidTypeException();
default: default:
throw new \Exception(); throw new \Exception();
} }

View file

@ -30,11 +30,30 @@ class Share extends \PHPUnit_Framework_TestCase {
public function tearDown() { public function tearDown() {
if ($this->share) { if ($this->share) {
$this->share->rmdir($this->root); $this->cleanDir($this->root);
} }
unset($this->share); unset($this->share);
} }
public function cleanDir($dir) {
$content = $this->share->dir($dir);
foreach ($content as $name => $metadata) {
if ($metadata['type'] === 'dir') {
$this->cleanDir($dir . '/' . $name);
} else {
$this->share->del($dir . '/' . $name);
}
}
$this->share->rmdir($dir);
}
private function getTextFile() {
$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua';
$file = tempnam('/tmp', 'smb_test_');
file_put_contents($file, $text);
return $file;
}
public function testListShares() { public function testListShares() {
$shares = $this->server->listShares(); $shares = $this->server->listShares();
foreach ($shares as $share) { foreach ($shares as $share) {
@ -149,4 +168,91 @@ class Share extends \PHPUnit_Framework_TestCase {
unlink($tmpFile1); unlink($tmpFile1);
} }
/**
* @expectedException \SMB\NotFoundException
*/
public function testCreateFolderInNonExistingFolder() {
$this->share->mkdir($this->root . '/foo/bar');
}
/**
* @expectedException \SMB\NotFoundException
*/
public function testRemoveFolderInNonExistingFolder() {
$this->share->rmdir($this->root . '/foo/bar');
}
/**
* @expectedException \SMB\NotFoundException
*/
public function testRemoveNonExistingFolder() {
$this->share->rmdir($this->root . '/foo');
}
/**
* @expectedException \SMB\AlreadyExistsException
*/
public function testCreateExistingFolder() {
$this->share->mkdir($this->root . '/bar');
$this->share->mkdir($this->root . '/bar');
$this->share->rmdir($this->root . '/bar');
}
/**
* @expectedException \SMB\InvalidTypeException
*/
public function testCreateFileExistingFolder() {
$this->share->mkdir($this->root . '/bar');
$this->share->put($this->getTextFile(), $this->root . '/bar');
$this->share->rmdir($this->root . '/bar');
}
/**
* @expectedException \SMB\NotFoundException
*/
public function testCreateFileInNonExistingFolder() {
$this->share->put($this->getTextFile(), $this->root . '/foo/bar');
}
/**
* @expectedException \SMB\NotFoundException
*/
public function testTestRemoveNonExistingFile() {
$this->share->del($this->root . '/foo');
}
/**
* @expectedException \SMB\NotFoundException
*/
public function testDownloadNonExistingFile() {
$this->share->get($this->root . '/foo', '/dev/null');
}
/**
* @expectedException \SMB\InvalidTypeException
*/
public function testDownloadFolder() {
$this->share->mkdir($this->root . '/foobar');
$this->share->get($this->root . '/foobar', '/dev/null');
$this->share->rmdir($this->root . '/foobar');
}
/**
* @expectedException \SMB\NotFoundException
*/
public function testDelFolder() {
$this->share->mkdir($this->root . '/foobar');
$this->share->del($this->root . '/foobar');
$this->share->rmdir($this->root . '/foobar');
}
/**
* @expectedException \SMB\InvalidTypeException
*/
public function testRmdirFile() {
$this->share->put($this->getTextFile(), $this->root . '/foobar');
$this->share->rmdir($this->root . '/foobar');
$this->share->del($this->root . '/foobar');
}
} }