Skip to content

Commit f6549eb

Browse files
committed
Adds test to cover Api::getMetaData() when given directory path.
1 parent e0b3654 commit f6549eb

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

src/Api.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,20 @@ final public function getMetaData($path)
208208
$project = sprintf('%s/%s', $this->settings->getVendor(), $this->settings->getPackage());
209209
$reference = $this->settings->getReference();
210210

211-
$url = sprintf('%s/repos/%s/contents/%s?ref=%s', self::GITHUB_API_URL, $project, $path, $reference);
212-
$htmlUrl = sprintf('%s/%s/blob/%s/%s', self::GITHUB_URL, $project, $reference, $path);
211+
$url = sprintf(
212+
'%s/repos/%s/contents/%s?ref=%s',
213+
self::GITHUB_API_URL,
214+
$project,
215+
trim($path, '/'),
216+
$reference
217+
);
218+
$htmlUrl = sprintf(
219+
'%s/%s/blob/%s/%s',
220+
self::GITHUB_URL,
221+
$project,
222+
$reference,
223+
trim($path, '/')
224+
);
213225

214226
$metadata = [
215227
self::KEY_TYPE => self::KEY_DIRECTORY,

tests/unit-tests/ApiTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ApiTest extends \PHPUnit_Framework_TestCase
2222
////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
2323
const MOCK_FILE_PATH = '/path/to/mock/file';
2424
const MOCK_FILE_CONTENTS = 'Mock file contents';
25+
const MOCK_FOLDER_PATH = 'a-directory';
2526

2627
/** @var Api */
2728
private $api;
@@ -221,6 +222,65 @@ final public function testApiShouldAccountForFileNotExistingWhenAskingInfoForFil
221222
self::assertEquals($expected, $actual);
222223
}
223224

225+
/**
226+
* @covers ::getMetaData
227+
*/
228+
final public function testApiShouldReturnMetadataForDirectoryWhenGivenPathIsDirectory()
229+
{
230+
$api = $this->api;
231+
232+
$mockPackage = 'mockPackage';
233+
$mockPath = self::MOCK_FOLDER_PATH;
234+
$mockReference = 'mockReference';
235+
$mockVendor = 'mockVendor';
236+
237+
$expectedUrl = sprintf(
238+
'%s/repos/%s/%s/contents/%s?ref=%s',
239+
$api::GITHUB_API_URL,
240+
$mockVendor,
241+
$mockPackage,
242+
$mockPath,
243+
$mockReference
244+
);
245+
246+
$expectedHtmlUrl = sprintf(
247+
'%s/%s/%s/blob/%s/%s',
248+
$api::GITHUB_URL,
249+
$mockVendor,
250+
$mockPackage,
251+
$mockReference,
252+
$mockPath
253+
);
254+
255+
$expected = [
256+
'type' => $api::KEY_DIRECTORY,
257+
'url' => $expectedUrl,
258+
'html_url' => $expectedHtmlUrl,
259+
'_links' => Array (
260+
'self' => $expectedUrl,
261+
'html' => $expectedHtmlUrl,
262+
),
263+
];
264+
265+
266+
$this->prepareMockSettings([
267+
'getVendor' => $mockVendor,
268+
'getPackage' => $mockPackage,
269+
'getReference' => $mockReference,
270+
]);
271+
272+
$this->prepareMockApi(
273+
'show',
274+
$api::API_REPO,
275+
[$mockVendor, $mockPackage, $mockPath, $mockReference],
276+
[0 => null]
277+
);
278+
279+
$actual = $api->getMetaData($mockPath);
280+
281+
self::assertEquals($expected, $actual);
282+
}
283+
224284
/**
225285
* @covers ::getMetaData
226286
*/

0 commit comments

Comments
 (0)