1
|
|
|
<?php |
2
|
|
|
use Writing_On_GitHub_Base_Client as Base; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @group api |
6
|
|
|
*/ |
7
|
|
|
class Writing_On_GitHub_Fetch_Client_Test extends Writing_On_GitHub_Base_Client_Test { |
8
|
|
|
|
9
|
|
|
public function setUp() { |
10
|
|
|
parent::setUp(); |
11
|
|
|
|
12
|
|
|
$this->fetch = new Writing_On_GitHub_Fetch_Client( $this->app ); |
|
|
|
|
13
|
|
|
$this->api_cache |
|
|
|
|
14
|
|
|
->shouldReceive( 'fetch_blob' ) |
15
|
|
|
->andReturn( false ) |
16
|
|
|
->byDefault(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function test_should_fail_if_missing_token() { |
20
|
|
|
delete_option( Base::TOKEN_OPTION_KEY ); |
21
|
|
|
|
22
|
|
|
$this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
|
|
|
23
|
|
|
$this->assertSame( 'missing_token', $error->get_error_code() ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function test_should_fail_if_missing_repo() { |
27
|
|
|
delete_option( Base::REPO_OPTION_KEY ); |
28
|
|
|
|
29
|
|
|
$this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
|
|
|
30
|
|
|
$this->assertSame( 'missing_repository', $error->get_error_code() ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function test_should_fail_if_malformed_repo() { |
34
|
|
|
// If you find a particular name passing that shouldn't, |
35
|
|
|
// add it to the list here and make it pass. |
36
|
|
|
$this->malformed_repo( 'repositoryname' ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function test_should_return_files_with_tree() { |
40
|
|
|
// $this->set_get_refs_heads_master( true ); |
|
|
|
|
41
|
|
|
// $this->set_get_commits( true ); |
|
|
|
|
42
|
|
|
$this->set_get_trees( true, 'master' ); |
43
|
|
|
|
44
|
|
|
$this->assertCount( 3, $this->fetch->tree_recursive() ); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function test_should_fail_if_cant_fetch_tree() { |
48
|
|
|
$this->set_get_trees( false, 'master' ); |
49
|
|
|
|
50
|
|
|
$this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
|
|
|
51
|
|
|
$this->assertSame( '422_unprocessable_entity', $error->get_error_code() ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// public function test_should_return_commit_with_blobs_from_cache() { |
|
|
|
|
55
|
|
|
// $this->set_get_trees( true, 'master' ); |
|
|
|
|
56
|
|
|
// $this->set_get_blobs( true ); |
|
|
|
|
57
|
|
|
// $this->api_cache |
58
|
|
|
// ->shouldReceive( 'fetch_blob' ) |
|
|
|
|
59
|
|
|
// ->times( 3 ) |
|
|
|
|
60
|
|
|
// ->with( Mockery::anyOf( |
|
|
|
|
61
|
|
|
// '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
62
|
|
|
// '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
63
|
|
|
// '2d73165945b0ccbe4932f1363457986b0ed49f19' |
64
|
|
|
// ) ) |
65
|
|
|
// ->andReturn( $this->blob ); |
|
|
|
|
66
|
|
|
|
67
|
|
|
// $this->assertCount( 3, $files = $this->fetch->tree_recursive() ); |
|
|
|
|
68
|
|
|
|
69
|
|
|
// foreach ( $files as $file ) { |
|
|
|
|
70
|
|
|
// $this->assertSame( $this->blob, $this->fetch->blob( $file ) ); |
|
|
|
|
71
|
|
|
// } |
72
|
|
|
// } |
73
|
|
|
|
74
|
|
|
public function test_should_return_commit_with_no_blobs_if_api_fails() { |
75
|
|
|
$this->set_get_trees( true, 'master' ); |
76
|
|
|
$this->set_get_blobs( false ); |
77
|
|
|
|
78
|
|
|
$this->assertCount( 3, $files = $this->fetch->tree_recursive() ); |
|
|
|
|
79
|
|
|
|
80
|
|
|
foreach ( $files as $file ) { |
81
|
|
|
$this->assertInstanceOf( 'WP_Error', $error = $this->fetch->blob( $file ) ); |
|
|
|
|
82
|
|
|
$this->assertSame( '404_not_found', $error->get_error_code() ); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// public function test_should_return_and_validate_full_commit() { |
|
|
|
|
87
|
|
|
// $this->set_get_refs_heads_master( true ); |
|
|
|
|
88
|
|
|
// $this->set_get_commits( true ); |
|
|
|
|
89
|
|
|
// $this->set_get_trees( true ); |
|
|
|
|
90
|
|
|
// $this->set_get_blobs( true ); |
|
|
|
|
91
|
|
|
// $this->api_cache |
92
|
|
|
// ->shouldReceive( 'set_blob' ) |
|
|
|
|
93
|
|
|
// ->times( 3 ) |
|
|
|
|
94
|
|
|
// ->with( |
95
|
|
|
// Mockery::anyOf( |
96
|
|
|
// '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
97
|
|
|
// '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
98
|
|
|
// '2d73165945b0ccbe4932f1363457986b0ed49f19' |
99
|
|
|
// ), |
100
|
|
|
// Mockery::type( 'Writing_On_GitHub_Blob' ) |
|
|
|
|
101
|
|
|
// ) |
102
|
|
|
// ->andReturnUsing( function ( $sha, $blob ) { |
|
|
|
|
103
|
|
|
// return $blob; |
104
|
|
|
// } ); |
105
|
|
|
// $this->api_cache |
106
|
|
|
// ->shouldReceive( 'set_tree' ) |
|
|
|
|
107
|
|
|
// ->once() |
108
|
|
|
// ->with( '9108868e3800bec6763e51beb0d33e15036c3626', Mockery::type( 'Writing_On_GitHub_Tree' ) ) |
|
|
|
|
109
|
|
|
// ->andReturnUsing( function ( $sha, $tree ) { |
|
|
|
|
110
|
|
|
// return $tree; |
111
|
|
|
// } ); |
112
|
|
|
// $this->api_cache |
113
|
|
|
// ->shouldReceive( 'set_commit' ) |
|
|
|
|
114
|
|
|
// ->once() |
115
|
|
|
// ->with( 'db2510854e6aeab68ead26b48328b19f4bdf926e', Mockery::type( 'Writing_On_GitHub_Commit' ) ) |
|
|
|
|
116
|
|
|
// ->andReturnUsing( function ( $sha, $commit ) { |
|
|
|
|
117
|
|
|
// return $commit; |
118
|
|
|
// } ); |
119
|
|
|
|
120
|
|
|
// $this->assertInstanceOf( 'Writing_On_GitHub_Commit', $master = $this->fetch->master() ); |
|
|
|
|
121
|
|
|
|
122
|
|
|
// /** |
123
|
|
|
// * Validate the commit's api data mapped correctly. |
124
|
|
|
// */ |
125
|
|
|
// $this->assertSame( '7497c0574b9430ff5e5521b4572b7452ea36a056', $master->sha() ); |
|
|
|
|
126
|
|
|
// $this->assertSame( '[email protected]', $master->author()->email ); |
|
|
|
|
127
|
|
|
// $this->assertSame( '2015-11-02T00:36:54Z', $master->author()->date ); |
|
|
|
|
128
|
|
|
// $this->assertSame( '[email protected]', $master->committer()->email ); |
|
|
|
|
129
|
|
|
// $this->assertSame( '2015-11-02T00:36:54Z', $master->committer()->date ); |
|
|
|
|
130
|
|
|
// $this->assertSame( 'Initial full site export - wogh', $master->message() ); |
|
|
|
|
131
|
|
|
// $this->assertCount( 1, $parents = $master->parents() ); |
|
|
|
|
132
|
|
|
// $this->assertSame( 'db2510854e6aeab68ead26b48328b19f4bdf926e', $parents[0]->sha ); |
|
|
|
|
133
|
|
|
|
134
|
|
|
// $this->assertInstanceOf( 'Writing_On_GitHub_Tree', $tree = $master->tree() ); |
|
|
|
|
135
|
|
|
|
136
|
|
|
// /** |
137
|
|
|
// * Validate the tree's api data mapped correctly. |
138
|
|
|
// */ |
139
|
|
|
// $this->assertSame( '9108868e3800bec6763e51beb0d33e15036c3626', $tree->sha() ); |
|
|
|
|
140
|
|
|
|
141
|
|
|
// $this->assertCount( 3, $blobs = $tree->blobs() ); |
|
|
|
|
142
|
|
|
|
143
|
|
|
// /** |
144
|
|
|
// * Validate the blobs' api data mapped correctly. |
145
|
|
|
// */ |
146
|
|
|
// $blobs = $tree->blobs(); |
|
|
|
|
147
|
|
|
// $this->assertCount( 3, $blobs ); |
|
|
|
|
148
|
|
|
// foreach ( $blobs as $blob ) { |
|
|
|
|
149
|
|
|
// $this->assertTrue( in_array( $blob->sha(), array( |
|
|
|
|
150
|
|
|
// '2d73165945b0ccbe4932f1363457986b0ed49f19', |
151
|
|
|
// '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
152
|
|
|
// '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
153
|
|
|
// ) ) ); |
|
|
|
|
154
|
|
|
// $this->assertTrue( in_array( $blob->path(), array( |
|
|
|
|
155
|
|
|
// '_pages/sample-page.md', |
156
|
|
|
// '_posts/2015-11-02-hello-world.md', |
157
|
|
|
// 'README.md', |
158
|
|
|
// ) ) ); |
|
|
|
|
159
|
|
|
// } |
160
|
|
|
// } |
161
|
|
|
|
162
|
|
|
protected function malformed_repo( $repo ) { |
163
|
|
|
update_option( Base::REPO_OPTION_KEY, $repo ); |
164
|
|
|
|
165
|
|
|
$this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
|
|
|
166
|
|
|
$this->assertSame( 'malformed_repository', $error->get_error_code() ); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.