forked from Potherca/flysystem-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompareToLocalCalls.php
More file actions
181 lines (149 loc) · 5.97 KB
/
Copy pathcompareToLocalCalls.php
File metadata and controls
181 lines (149 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
use Github\Client;
use League\Flysystem\Adapter\Local as LocalAdapter;
use League\Flysystem\Filesystem;
use Potherca\Flysystem\Github\Api;
use Potherca\Flysystem\Github\GithubAdapter;
use Potherca\Flysystem\Github\Settings;
class CompareToLocalAdapterIntegrationTest extends PHPUnit_Framework_TestCase {
////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
/** @var string */
private $gitRemote = 'https://github.com/potherca-bot/test-repository.git';
/** @var Filesystem */
private $filesystem;
final public static function setUpBeforeClass()
{
$envFilePath = __DIR__ . '/.env';
self::loadEnvironmentalVariables($envFilePath);
}
final public function setUp()
{
$project = 'potherca-bot/test-repository';
$credentials = [Settings::AUTHENTICATE_USING_TOKEN, getenv('GITHUB_API_KEY')];
$this->filesystem = new Filesystem(new GithubAdapter(new Api(new Client(), new Settings($project, $credentials))));
}
/////////////////////////////////// TESTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
/**
* @param string $function
* @param array $parameters
* @param array $file
*
* @dataProvider provideFiles
*/
final public function testOutputMatchesLocalAdapterOutputWhenCalledOnTheSameSource($function, array $parameters, array $file)
{
$path = $file['path'];
$localFileSystem = $this->getLocalFileSystem();
$localResult = $localFileSystem->{$function}($path);
$result = $this->filesystem->{$function}($path);
if (array_key_exists('callback', $parameters)) {
$parameters['callback']($localResult, $result);
} else {
self::assertEquals($localResult, $result);
}
}
/////////////////////////////// DATAPROVIDERS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
final public function provideFiles()
{
$this->createFixture();
$files = [];
$localFileSystem = $this->getLocalFileSystem();
// @TODO: Test for FileNotFoundException
//$files[] = ['path'=>'NON-EXISTENT-FILE'];
$functions = [
'assertPresent' => [],
'get' => ['callback' => function ($localResult, $result) {
if ($localResult instanceof \League\Flysystem\Directory) {
/** @var $localResult \League\Flysystem\Directory */
/** @var $result \League\Flysystem\Directory */
$localContents = $localResult->getContents();
/** @var array $contents */
$contents = $result->getContents();
$this->compare($localContents, $contents);
} elseif ($localResult instanceof \League\Flysystem\File) {
/** @var $localResult \League\Flysystem\File */
/** @var $result \League\Flysystem\File */
self::assertEquals($localResult->read(), $result->read());
} else {
self::assertEquals($localResult, $result);
}
}],
'getMimetype' => [],
'getSize' => [],
//@FIXME: Synchronize local timestamp with remote git repo timestamp so "getTimestamp" can be tested
// 'getTimestamp' => [],
'getVisibility' => [],
'has' => [],
'listContents' => ['type' => 'dir', 'callback' => [$this, 'compare']],
'read' => ['type' => 'file'],
'readStream' => ['type' => 'file', 'callback' => function ($localStream, $githubStream){
self::assertEquals(stream_get_contents($localStream), stream_get_contents($githubStream));
}],
];
$localFiles = $localFileSystem->listContents('', true);
foreach ($localFiles as $index => $file) {
$path = $file['path'];
if (strpos($path, '.') !== 0) {
foreach ($functions as $function => $parameters) {
if (array_key_exists('type', $parameters) === false || $file['type'] === $parameters['type']) {
$key = sprintf('%s - %s', $function, $path);
$files[$key] = [$function, $parameters, $file];
}
}
}
}
ksort($files);
return $files;
}
////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
/**
* @param $envFilePath
*/
private static function loadEnvironmentalVariables($envFilePath)
{
if (is_file($envFilePath)) {
$loader = (new josegonzalez\Dotenv\Loader($envFilePath));
$loader->parse()->putenv();
}
}
private function createFixture()
{
$gitRemote = $this->gitRemote;
$fixturesPath = $this->getFixturePath();
$glob = glob($fixturesPath.'**');
if (is_dir($fixturesPath) === false || count($glob) === 0) {
fwrite(STDERR, sprintf('Creating fixture directory from %s.%s', $gitRemote, "\n"));
exec(sprintf('git clone %s %s', $gitRemote, $fixturesPath));
}
}
/**
* @param array $localContents
* @param array $contents
*/
private function compare(array $localContents, array $contents)
{
array_walk($contents, 'ksort');
array_walk($localContents, 'ksort');
$localContents = array_map(function ($value) {
unset($value['timestamp']);
return $value;
}, $localContents);
foreach ($localContents as $index => $localContent) {
foreach ($localContent as $key => $value) {
self::assertEquals($value, $contents[$index][$key]);
}
}
}
private function getLocalFileSystem()
{
return new Filesystem(new LocalAdapter($this->getFixturePath()));
}
/**
* @return string
*/
private function getFixturePath()
{
return dirname(__DIR__) . '/fixtures/integration-test-repository/';
}
}
/*EOF*/