Skip to content

Commit

Permalink
added StarredFile resource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene Schmidt committed Jan 16, 2016
1 parent a2b8248 commit 714e158
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,48 @@ or
print_r($avatarResource->getUserAvatarByEmail('[email protected]')->toArray());
```

### Create and remove shared link

```php
$libraryResource = new Library($client);
$directoryResource = new Directory($client);
$fileResource = new File($client);
$sharedLinkResource = new SharedLink($client);

// create share link for a file
$expire = 5;
$shareType = SharedLinkType::SHARE_TYPE_DOWNLOAD;
$p = "/" . basename($newFilename);
$password = 'qwertz123';

$shareLinkType = $sharedLinkResource->create($lib, $p, $expire, $shareType, $password);

// remove shared link
$success = $sharedLinkResource->remove($shareLinkType);
```

### Get all starred files, star and unstar file

```php
$libraryResource = new Library($client);
$starredFileResource = new StarredFile($client);

// get all starred files
$dirItems = $starredFileResource->getAll();

// unstar all starred files
foreach ($dirItems as $dirItem) {
$lib = $libraryResource->getById($dirItem->repo);
$starredFileResource->unstar($lib, $dirItem);
}

// re-star all files
foreach ($dirItems as $dirItem) {
$lib = $libraryResource->getById($dirItem->repo);
$starredFileResource->star($lib, $dirItem);
}
```

### Debugging and how to enable logging of requests and responses

This example requires monolog. Log entries and Guzzle debug info will be written to stdout.
Expand Down
140 changes: 140 additions & 0 deletions test/unit/Resource/StarredFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,144 @@ function ($method, $uri, $params) use ($starResponse, $lib, $dirItem) {

$this->assertSame($responseUrl, $result);
}

/**
* Test star() with error response
*
* @throws \Exception
* @return void
*/
public function testStarErrorStatusCode()
{
$lib = new LibraryType();
$lib->id = 123;

$dirItem = new DirectoryItem();
$dirItem->type = 'file';
$dirItem->path = '/some/path';

$responseUrl = 'https://example.com/test/';

$starResponse = new Response(
500,
[
'Accept' => 'application/json',
'Location' => $responseUrl
]
);

$mockedClient = $this->getMockBuilder('\Seafile\Client\Http\Client')->getMock();

$mockedClient->expects($this->any())
->method('getConfig')
->with('base_uri')
->willReturn($responseUrl);

$mockedClient->expects($this->any())
->method('request')
->with('POST')
->willReturn($starResponse);

/** @var Client $mockedClient */
$starredFileResource = new StarredFile($mockedClient);

$this->setExpectedException('Exception', 'Could not star file');

$starredFileResource->star($lib, $dirItem);
}

/**
* Test star() with missing location
*
* @throws \Exception
* @return void
*/
public function testStarErrorMissingLocation()
{
$lib = new LibraryType();
$lib->id = 123;

$dirItem = new DirectoryItem();
$dirItem->type = 'file';
$dirItem->path = '/some/path';

$mockedClient = $this->getMockBuilder('\Seafile\Client\Http\Client')->getMock();

$mockedClient->expects($this->any())
->method('getConfig')
->with('base_uri')
->willReturn('https://example.com/test/');

$mockedClient->expects($this->any())
->method('request')
->with('POST')
->willReturn(new Response(500));

/** @var Client $mockedClient */
$starredFileResource = new StarredFile($mockedClient);

$this->setExpectedException('Exception', 'Could not star file');

$starredFileResource->star($lib, $dirItem);
}

/**
* DataProvider for unstar()
*
* @return array
*/
public function dataProviderUnstar()
{
return [
[[
'responseCode' => 200,
'result' => true
]],
[[
'responseCode' => 500,
'result' => false
]]
];
}

/**
* Test unstar()
*
* @param array $data Data provider array
*
* @return void
* @throws \Exception
* @dataProvider dataProviderUnstar
*/
public function testUnstar(array $data)
{
$lib = new LibraryType();
$lib->id = 123;

$dirItem = new DirectoryItem();
$dirItem->type = 'file';
$dirItem->path = '/some/path';

$mockedClient = $this->getMockBuilder('\Seafile\Client\Http\Client')->getMock();

$mockedClient->expects($this->any())
->method('getConfig')
->with('base_uri')
->willReturn('https://example.com/test/');

$mockedClient->expects($this->any())
->method('request')
->with('DELETE')
->willReturn(
new Response($data['responseCode'])
);

/** @var Client $mockedClient */
$starredFileResource = new StarredFile($mockedClient);

$this->assertSame(
$data['result'],
$starredFileResource->unstar($lib, $dirItem)
);
}
}

0 comments on commit 714e158

Please sign in to comment.