Skip to content

Commit 32daf4d

Browse files
committed
Adds test for recursive folder functionality of GithubAdapter::listContents()
- Adds keys to provided data for easy idintifiaction in case of failure - Adds JSON file as fixture
1 parent 2637017 commit 32daf4d

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

Diff for: tests/fixtures/listContents-folder-recursive.json

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"sha": "676a09fd1c7df28938e8c12dc5d9f3c3271f0249",
3+
"url": "https://api.github.com/repos/potherca-bot/test-repository/git/trees/676a09fd1c7df28938e8c12dc5d9f3c3271f0249",
4+
"tree": [
5+
{
6+
"path": "README",
7+
"mode": "100755",
8+
"type": "blob",
9+
"sha": "1ff3a296caf2d27828dd8c40673c88dbf99d4b3a",
10+
"size": 58,
11+
"url": "https://api.github.com/repos/potherca-bot/test-repository/git/blobs/1ff3a296caf2d27828dd8c40673c88dbf99d4b3a"
12+
},
13+
{
14+
"path": "a-directory",
15+
"mode": "040000",
16+
"type": "tree",
17+
"sha": "30b7e362894eecb159ce0ba2921a8363cd297213",
18+
"url": "https://api.github.com/repos/potherca-bot/test-repository/git/trees/30b7e362894eecb159ce0ba2921a8363cd297213"
19+
},
20+
{
21+
"path": "a-directory/another-file.js",
22+
"mode": "100755",
23+
"type": "blob",
24+
"sha": "f542363e1b45aa7a33e5e731678dee18f7a1e729",
25+
"size": 52,
26+
"url": "https://api.github.com/repos/potherca-bot/test-repository/git/blobs/f542363e1b45aa7a33e5e731678dee18f7a1e729"
27+
},
28+
{
29+
"path": "a-directory/readme.txt",
30+
"mode": "100755",
31+
"type": "blob",
32+
"sha": "27f8ec8435cb07992ecf18f9d5494ffc14948368",
33+
"size": 31,
34+
"url": "https://api.github.com/repos/potherca-bot/test-repository/git/blobs/27f8ec8435cb07992ecf18f9d5494ffc14948368"
35+
},
36+
{
37+
"path": "a-file.php",
38+
"mode": "100755",
39+
"type": "blob",
40+
"sha": "c6e6cd91e3ae40ab74883720a0d6cfb2af89e4b1",
41+
"size": 117,
42+
"url": "https://api.github.com/repos/potherca-bot/test-repository/git/blobs/c6e6cd91e3ae40ab74883720a0d6cfb2af89e4b1"
43+
}
44+
],
45+
"truncated": false
46+
}

Diff for: tests/unit-tests/GithubAdapterTest.php

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3-
43
namespace Potherca\Flysystem\Github;
54

5+
use PHPUnit_Framework_MockObject_Matcher_Parameters;
6+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
7+
68
/**
79
* Tests for the GithubAdapter class
810
*
@@ -15,10 +17,11 @@ class GithubAdapterTest extends \PHPUnit_Framework_TestCase
1517
{
1618
////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
1719
const MOCK_FILE_PATH = '/path/to/mock/file';
20+
const MOCK_FOLDER_PATH = 'a-directory';
1821

1922
/** @var GithubAdapter */
2023
private $adapter;
21-
/** @var ApiInterface|\PHPUnit_Framework_MockObject_MockObject */
24+
/** @var ApiInterface|MockObject */
2225
private $mockClient;
2326

2427
/**
@@ -46,13 +49,23 @@ protected function setup()
4649
* @param $method
4750
* @param $apiMethod
4851
* @param $parameters
52+
* @param mixed $returnValue
4953
*/
50-
final public function testAdapterShouldPassParameterToClient($method, $apiMethod, $parameters)
54+
final public function testAdapterShouldPassParameterToClient($method, $apiMethod, $parameters, $returnValue = null)
5155
{
52-
$mocker = $this->mockClient->expects($this->exactly(1))
53-
->method($apiMethod);
56+
if (is_string($returnValue) && is_file(sprintf('%s/../fixtures/%s.json', __DIR__, $returnValue))) {
57+
$fixturePath = sprintf('%s/../fixtures/%s.json', __DIR__, $returnValue);
58+
$fixture = json_decode(file_get_contents($fixturePath), true);
59+
$returnValue = $fixture['tree'];
60+
}
61+
62+
63+
$mocker = $this->mockClient->expects(self::exactly(1))
64+
->method($apiMethod)
65+
->willReturn($returnValue)
66+
;
5467

55-
$mocker->getMatcher()->parametersMatcher = new \PHPUnit_Framework_MockObject_Matcher_Parameters($parameters);
68+
$mocker->getMatcher()->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_Parameters($parameters);
5669

5770
call_user_func_array([$this->adapter, $method], $parameters);
5871
}
@@ -63,14 +76,17 @@ final public function testAdapterShouldPassParameterToClient($method, $apiMethod
6376
final public function provideReadMethods()
6477
{
6578
return [
66-
['has', 'exists', [self::MOCK_FILE_PATH]],
67-
['read', 'getFileContents', [self::MOCK_FILE_PATH]],
68-
['listContents', 'getTreeMetadata', [self::MOCK_FILE_PATH, true]],
69-
['getMetadata', 'getMetadata', [self::MOCK_FILE_PATH]],
70-
['getSize', 'getMetadata', [self::MOCK_FILE_PATH]],
71-
['getMimetype', 'guessMimeType', [self::MOCK_FILE_PATH]],
72-
['getTimestamp', 'getLastUpdatedTimestamp', [self::MOCK_FILE_PATH]],
73-
['getVisibility', 'getTreeMetadata', [self::MOCK_FILE_PATH, false]],
79+
'has' => ['has', 'exists', [self::MOCK_FILE_PATH]],
80+
'read' => ['read', 'getFileContents', [self::MOCK_FILE_PATH]],
81+
'listContents - File' => ['listContents', 'getTreeMetadata', [self::MOCK_FILE_PATH, false]],
82+
'listContents - File - recursive' => ['listContents', 'getTreeMetadata', [self::MOCK_FILE_PATH, true]],
83+
'listContents - Folder' => ['listContents', 'getTreeMetadata', [self::MOCK_FOLDER_PATH, false], ''],
84+
'listContents - Folder - recursive' => ['listContents', 'getTreeMetadata', [self::MOCK_FOLDER_PATH, true], 'listContents-folder-recursive'],
85+
'getMetadata' => ['getMetadata', 'getMetadata', [self::MOCK_FILE_PATH]],
86+
'getSize' => ['getSize', 'getMetadata', [self::MOCK_FILE_PATH]],
87+
'getMimetype' => ['getMimetype', 'guessMimeType', [self::MOCK_FILE_PATH]],
88+
'getTimestamp' => ['getTimestamp', 'getLastUpdatedTimestamp', [self::MOCK_FILE_PATH]],
89+
'getVisibility' => ['getVisibility', 'getTreeMetadata', [self::MOCK_FILE_PATH]],
7490
];
7591
}
7692
}

0 commit comments

Comments
 (0)