Skip to content
58 changes: 44 additions & 14 deletions tests/DropboxHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ class DropboxHelperTest extends TestCase
/** @var string $sFolderPath: Path of folder on Dropbox */
protected $sFolderPath = '/PathToTest/OnYourDropbox';

/** @var string : Cursor of the $sFodlerPath. We'll get only delta */
protected $sCursor = 'AAHeJvp9Yce1wS7YPADH7A-----';

/** @var DropboxHelper */
protected $dropboxHelper = null;

Expand All @@ -20,7 +17,6 @@ public function setUp()
$env = array_merge($_ENV, $_SERVER);

$this->sFolderPath = (isset($env['DROPBOX_FOLDER_PATH'])) ? $env['DROPBOX_FOLDER_PATH'] : null;
$this->sCursor = (isset($env['DROPBOX_FOLDER_CURSOR'])) ? $env['DROPBOX_FOLDER_CURSOR'] : null;

if (isset($env['DROPBOX_TOKEN'])) {
$this->dropboxHelper = new DropboxHelper($env['DROPBOX_TOKEN']);
Expand All @@ -34,8 +30,17 @@ public function setUp()
*/
public function testGetCurrentAccount()
{
$result = $this->dropboxHelper->getCurrentAccount();
self::assertNotNull($result, 'Cannot get account information.');
$sResult = $this->dropboxHelper->getCurrentAccount();
self::assertNotNull($sResult, 'Cannot get account information.');

// $sResult should be a json string, let's assert it below

$this->assertInternalType('string', $sResult);
$aResult = json_decode($sResult, true);
$this->assertInternalType('array', $aResult);

// You shall be informed about the account you are playing with. Maybe only when --verbose or --debug ?
echo $sResult;
}

public function testWriteReadDeleteFile()
Expand Down Expand Up @@ -79,10 +84,10 @@ public function testGetCursor()
return;
}

$oFolder = $this->dropboxHelper->loadFolderPath($this->sFolderPath);
$sCursor = $oFolder->getCursor();
$sCursor = $this->getCursorFromFolderPath($this->sFolderPath);

self::assertNotEmpty($sCursor, 'Failed to get cursor on a loaded folder');
$this->assertInternalType('string', $sCursor);
}

/**
Expand Down Expand Up @@ -186,16 +191,35 @@ public function testListFolderFromCursor()
return;
}

if (empty($this->sCursor)) {
trigger_error('WARNING: Cannot run ' . __FUNCTION__ . ', Dropbox folder cursor is empty.', E_USER_WARNING);
return;
$sCursor = $this->getCursorFromFolderPath($this->sFolderPath);
$oFolder = $this->dropboxHelper->loadFolderCursor($sCursor);

self::assertNotNull($oFolder, sprintf("Cannot load a folder from cursor: `%s`", $sCursor));

$oFolder = $this->dropboxHelper->loadFolderCursor($sCursor);

# Test Folder::next after write
$sTestFilePath = $this->sFolderPath . '/DropboxHelper-test-file-' . uniqid() . '.txt';
$sContent = "PHP Unit Test of DropboxHelper:\n" . (new \DateTime())->format(DATE_ATOM);
$bResult = $this->dropboxHelper->write($sTestFilePath, $sContent);

$oFolder = $this->dropboxHelper->loadFolderCursor($sCursor);
$this->assertInternalType('array', $oFolder->next());

# Race condition: there may be other items to test
while (($aFolder = $oFolder->next())) {
$this->assertInternalType('array', $aFolder);
}

$oFolder = $this->dropboxHelper->loadFolderCursor($this->sCursor);
# Test Folder::next after delete
$bResult = $this->dropboxHelper->delete($sTestFilePath);

self::assertNotNull($oFolder, "Cannot load a folder from cursor: {$this->sCursor}");
$oFolder = $this->dropboxHelper->loadFolderCursor($sCursor);
$this->assertInternalType('array', $oFolder->next());

# Race condition: there may be other items to test
while (($aFolder = $oFolder->next())) {
$this->assertInternalType('array', $aFolder);
}
}

Expand All @@ -207,4 +231,10 @@ public function testFolder()
'Folder is not an instance of Folder()'
);
}
}

private function getCursorFromFolderPath($sFolderPath)
{
$oFolder = $this->dropboxHelper->loadFolderPath($sFolderPath);
return $oFolder->getCursor();
}
}