@@ -5,216 +5,216 @@ |
||
5 | 5 | */ |
6 | 6 | class Writing_On_GitHub_Persist_Client_Test extends Writing_On_GitHub_Base_Client_Test { |
7 | 7 | |
8 | - public function setUp() { |
|
9 | - parent::setUp(); |
|
10 | - |
|
11 | - $this->persist = new Writing_On_GitHub_Persist_Client( $this->app ); |
|
12 | - // $this->commit |
|
13 | - // ->shouldReceive( 'tree' ) |
|
14 | - // ->andReturn( $this->tree ); |
|
15 | - } |
|
16 | - |
|
17 | - public function test_should_delete_file() { |
|
18 | - // $this->tree->shouldReceive(); |
|
19 | - |
|
20 | - $this->set_delete_contents( true, '_posts_test.md' ); |
|
21 | - $this->persist->delete_file( '_posts_test.md', 'mysha', 'this is message for delete file' ); |
|
22 | - } |
|
23 | - |
|
24 | - // public function test_should_not_add_commit_if_no_change() { |
|
25 | - // $this->tree |
|
26 | - // ->shouldReceive( 'is_changed' ) |
|
27 | - // ->once() |
|
28 | - // ->andReturn( false ); |
|
29 | - |
|
30 | - // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
31 | - // $this->assertSame( 'no_commit', $error->get_error_code() ); |
|
32 | - // } |
|
33 | - |
|
34 | - // public function test_should_fail_if_create_tree_fails() { |
|
35 | - // $this->tree |
|
36 | - // ->shouldReceive( 'is_changed' ) |
|
37 | - // ->once() |
|
38 | - // ->andReturn( true ); |
|
39 | - // $this->tree |
|
40 | - // ->shouldReceive( 'to_body' ) |
|
41 | - // ->once() |
|
42 | - // ->andReturn( |
|
43 | - // array( |
|
44 | - // 'tree' => array( |
|
45 | - // array( |
|
46 | - // 'path' => '_posts/2015-10-23-new-post.md', |
|
47 | - // 'type' => 'blob', |
|
48 | - // 'content' => 'Post content 1', |
|
49 | - // 'mode' => '100644', |
|
50 | - // ) |
|
51 | - // ) |
|
52 | - // ) |
|
53 | - // ); |
|
54 | - // $this->set_post_trees( false ); |
|
55 | - |
|
56 | - // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
57 | - // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
58 | - // } |
|
59 | - |
|
60 | - // public function test_should_fail_if_create_commit_fails() { |
|
61 | - // $this->tree |
|
62 | - // ->shouldReceive( 'is_changed' ) |
|
63 | - // ->once() |
|
64 | - // ->andReturn( true ); |
|
65 | - // $this->tree |
|
66 | - // ->shouldReceive( 'to_body' ) |
|
67 | - // ->once() |
|
68 | - // ->andReturn( |
|
69 | - // array( |
|
70 | - // 'tree' => array( |
|
71 | - // array( |
|
72 | - // 'path' => '_posts/2015-10-23-new-post.md', |
|
73 | - // 'type' => 'blob', |
|
74 | - // 'content' => 'Post content 1', |
|
75 | - // 'mode' => '100644', |
|
76 | - // ) |
|
77 | - // ) |
|
78 | - // ) |
|
79 | - // ); |
|
80 | - // $this->tree |
|
81 | - // ->shouldReceive( 'set_sha' ) |
|
82 | - // ->once() |
|
83 | - // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
84 | - // $this->commit |
|
85 | - // ->shouldReceive( 'to_body' ) |
|
86 | - // ->once() |
|
87 | - // ->andReturn( array( |
|
88 | - // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
89 | - // 'message' => 'Commit message', |
|
90 | - // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
91 | - // ) ); |
|
92 | - // $this->set_post_trees( true ); |
|
93 | - // $this->set_post_commits( false ); |
|
94 | - |
|
95 | - // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
96 | - // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
97 | - // } |
|
98 | - |
|
99 | - // public function test_should_fail_if_update_master_fails() { |
|
100 | - // $this->tree |
|
101 | - // ->shouldReceive( 'is_changed' ) |
|
102 | - // ->once() |
|
103 | - // ->andReturn( true ); |
|
104 | - // $this->tree |
|
105 | - // ->shouldReceive( 'to_body' ) |
|
106 | - // ->once() |
|
107 | - // ->andReturn( |
|
108 | - // array( |
|
109 | - // 'tree' => array( |
|
110 | - // array( |
|
111 | - // 'path' => '_posts/2015-10-23-new-post.md', |
|
112 | - // 'type' => 'blob', |
|
113 | - // 'content' => 'Post content 1', |
|
114 | - // 'mode' => '100644', |
|
115 | - // ) |
|
116 | - // ) |
|
117 | - // ) |
|
118 | - // ); |
|
119 | - // $this->tree |
|
120 | - // ->shouldReceive( 'set_sha' ) |
|
121 | - // ->once() |
|
122 | - // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
123 | - // $this->commit |
|
124 | - // ->shouldReceive( 'to_body' ) |
|
125 | - // ->once() |
|
126 | - // ->andReturn( array( |
|
127 | - // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
128 | - // 'message' => 'Commit message', |
|
129 | - // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
130 | - // ) ); |
|
131 | - // $this->set_post_trees( true ); |
|
132 | - // $this->set_post_commits( true ); |
|
133 | - // $this->set_patch_refs_heads_master( false ); |
|
134 | - |
|
135 | - // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
136 | - // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
137 | - // } |
|
138 | - |
|
139 | - // public function test_should_create_anonymous_commit() { |
|
140 | - // $this->tree |
|
141 | - // ->shouldReceive( 'is_changed' ) |
|
142 | - // ->once() |
|
143 | - // ->andReturn( true ); |
|
144 | - // $this->tree |
|
145 | - // ->shouldReceive( 'to_body' ) |
|
146 | - // ->once() |
|
147 | - // ->andReturn( |
|
148 | - // array( |
|
149 | - // 'tree' => array( |
|
150 | - // array( |
|
151 | - // 'path' => '_posts/2015-10-23-new-post.md', |
|
152 | - // 'type' => 'blob', |
|
153 | - // 'content' => 'Post content 1', |
|
154 | - // 'mode' => '100644', |
|
155 | - // ) |
|
156 | - // ) |
|
157 | - // ) |
|
158 | - // ); |
|
159 | - // $this->tree |
|
160 | - // ->shouldReceive( 'set_sha' ) |
|
161 | - // ->once() |
|
162 | - // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
163 | - // $this->commit |
|
164 | - // ->shouldReceive( 'to_body' ) |
|
165 | - // ->once() |
|
166 | - // ->andReturn( array( |
|
167 | - // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
168 | - // 'message' => 'Commit message', |
|
169 | - // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
170 | - // ) ); |
|
171 | - // $this->set_post_trees( true ); |
|
172 | - // $this->set_post_commits( true ); |
|
173 | - // $this->set_patch_refs_heads_master( true ); |
|
174 | - |
|
175 | - // $this->assertTrue( $this->persist->commit( $this->commit ) ); |
|
176 | - // } |
|
177 | - |
|
178 | - // public function test_should_create_authored_commit() { |
|
179 | - // $this->tree |
|
180 | - // ->shouldReceive( 'is_changed' ) |
|
181 | - // ->once() |
|
182 | - // ->andReturn( true ); |
|
183 | - // $this->tree |
|
184 | - // ->shouldReceive( 'to_body' ) |
|
185 | - // ->once() |
|
186 | - // ->andReturn( |
|
187 | - // array( |
|
188 | - // 'tree' => array( |
|
189 | - // array( |
|
190 | - // 'path' => '_posts/2015-10-23-new-post.md', |
|
191 | - // 'type' => 'blob', |
|
192 | - // 'content' => 'Post content 1', |
|
193 | - // 'mode' => '100644', |
|
194 | - // ) |
|
195 | - // ) |
|
196 | - // ) |
|
197 | - // ); |
|
198 | - // $this->tree |
|
199 | - // ->shouldReceive( 'set_sha' ) |
|
200 | - // ->once() |
|
201 | - // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
202 | - // $this->commit |
|
203 | - // ->shouldReceive( 'to_body' ) |
|
204 | - // ->once() |
|
205 | - // ->andReturn( array( |
|
206 | - // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
207 | - // 'message' => 'Commit message', |
|
208 | - // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
209 | - // ) ); |
|
210 | - // $this->set_post_trees( true ); |
|
211 | - // update_option( '_wogh_export_user_id', $this->factory->user->create( array( |
|
212 | - // 'display_name' => 'James DiGioia', |
|
213 | - // 'user_email' => '[email protected]', |
|
214 | - // ) ) ); |
|
215 | - // $this->set_post_commits( true, false ); |
|
216 | - // $this->set_patch_refs_heads_master( true ); |
|
217 | - |
|
218 | - // $this->assertTrue( $this->persist->commit( $this->commit ) ); |
|
219 | - // } |
|
8 | + public function setUp() { |
|
9 | + parent::setUp(); |
|
10 | + |
|
11 | + $this->persist = new Writing_On_GitHub_Persist_Client( $this->app ); |
|
12 | + // $this->commit |
|
13 | + // ->shouldReceive( 'tree' ) |
|
14 | + // ->andReturn( $this->tree ); |
|
15 | + } |
|
16 | + |
|
17 | + public function test_should_delete_file() { |
|
18 | + // $this->tree->shouldReceive(); |
|
19 | + |
|
20 | + $this->set_delete_contents( true, '_posts_test.md' ); |
|
21 | + $this->persist->delete_file( '_posts_test.md', 'mysha', 'this is message for delete file' ); |
|
22 | + } |
|
23 | + |
|
24 | + // public function test_should_not_add_commit_if_no_change() { |
|
25 | + // $this->tree |
|
26 | + // ->shouldReceive( 'is_changed' ) |
|
27 | + // ->once() |
|
28 | + // ->andReturn( false ); |
|
29 | + |
|
30 | + // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
31 | + // $this->assertSame( 'no_commit', $error->get_error_code() ); |
|
32 | + // } |
|
33 | + |
|
34 | + // public function test_should_fail_if_create_tree_fails() { |
|
35 | + // $this->tree |
|
36 | + // ->shouldReceive( 'is_changed' ) |
|
37 | + // ->once() |
|
38 | + // ->andReturn( true ); |
|
39 | + // $this->tree |
|
40 | + // ->shouldReceive( 'to_body' ) |
|
41 | + // ->once() |
|
42 | + // ->andReturn( |
|
43 | + // array( |
|
44 | + // 'tree' => array( |
|
45 | + // array( |
|
46 | + // 'path' => '_posts/2015-10-23-new-post.md', |
|
47 | + // 'type' => 'blob', |
|
48 | + // 'content' => 'Post content 1', |
|
49 | + // 'mode' => '100644', |
|
50 | + // ) |
|
51 | + // ) |
|
52 | + // ) |
|
53 | + // ); |
|
54 | + // $this->set_post_trees( false ); |
|
55 | + |
|
56 | + // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
57 | + // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
58 | + // } |
|
59 | + |
|
60 | + // public function test_should_fail_if_create_commit_fails() { |
|
61 | + // $this->tree |
|
62 | + // ->shouldReceive( 'is_changed' ) |
|
63 | + // ->once() |
|
64 | + // ->andReturn( true ); |
|
65 | + // $this->tree |
|
66 | + // ->shouldReceive( 'to_body' ) |
|
67 | + // ->once() |
|
68 | + // ->andReturn( |
|
69 | + // array( |
|
70 | + // 'tree' => array( |
|
71 | + // array( |
|
72 | + // 'path' => '_posts/2015-10-23-new-post.md', |
|
73 | + // 'type' => 'blob', |
|
74 | + // 'content' => 'Post content 1', |
|
75 | + // 'mode' => '100644', |
|
76 | + // ) |
|
77 | + // ) |
|
78 | + // ) |
|
79 | + // ); |
|
80 | + // $this->tree |
|
81 | + // ->shouldReceive( 'set_sha' ) |
|
82 | + // ->once() |
|
83 | + // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
84 | + // $this->commit |
|
85 | + // ->shouldReceive( 'to_body' ) |
|
86 | + // ->once() |
|
87 | + // ->andReturn( array( |
|
88 | + // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
89 | + // 'message' => 'Commit message', |
|
90 | + // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
91 | + // ) ); |
|
92 | + // $this->set_post_trees( true ); |
|
93 | + // $this->set_post_commits( false ); |
|
94 | + |
|
95 | + // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
96 | + // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
97 | + // } |
|
98 | + |
|
99 | + // public function test_should_fail_if_update_master_fails() { |
|
100 | + // $this->tree |
|
101 | + // ->shouldReceive( 'is_changed' ) |
|
102 | + // ->once() |
|
103 | + // ->andReturn( true ); |
|
104 | + // $this->tree |
|
105 | + // ->shouldReceive( 'to_body' ) |
|
106 | + // ->once() |
|
107 | + // ->andReturn( |
|
108 | + // array( |
|
109 | + // 'tree' => array( |
|
110 | + // array( |
|
111 | + // 'path' => '_posts/2015-10-23-new-post.md', |
|
112 | + // 'type' => 'blob', |
|
113 | + // 'content' => 'Post content 1', |
|
114 | + // 'mode' => '100644', |
|
115 | + // ) |
|
116 | + // ) |
|
117 | + // ) |
|
118 | + // ); |
|
119 | + // $this->tree |
|
120 | + // ->shouldReceive( 'set_sha' ) |
|
121 | + // ->once() |
|
122 | + // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
123 | + // $this->commit |
|
124 | + // ->shouldReceive( 'to_body' ) |
|
125 | + // ->once() |
|
126 | + // ->andReturn( array( |
|
127 | + // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
128 | + // 'message' => 'Commit message', |
|
129 | + // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
130 | + // ) ); |
|
131 | + // $this->set_post_trees( true ); |
|
132 | + // $this->set_post_commits( true ); |
|
133 | + // $this->set_patch_refs_heads_master( false ); |
|
134 | + |
|
135 | + // $this->assertWPError( $error = $this->persist->commit( $this->commit ) ); |
|
136 | + // $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
137 | + // } |
|
138 | + |
|
139 | + // public function test_should_create_anonymous_commit() { |
|
140 | + // $this->tree |
|
141 | + // ->shouldReceive( 'is_changed' ) |
|
142 | + // ->once() |
|
143 | + // ->andReturn( true ); |
|
144 | + // $this->tree |
|
145 | + // ->shouldReceive( 'to_body' ) |
|
146 | + // ->once() |
|
147 | + // ->andReturn( |
|
148 | + // array( |
|
149 | + // 'tree' => array( |
|
150 | + // array( |
|
151 | + // 'path' => '_posts/2015-10-23-new-post.md', |
|
152 | + // 'type' => 'blob', |
|
153 | + // 'content' => 'Post content 1', |
|
154 | + // 'mode' => '100644', |
|
155 | + // ) |
|
156 | + // ) |
|
157 | + // ) |
|
158 | + // ); |
|
159 | + // $this->tree |
|
160 | + // ->shouldReceive( 'set_sha' ) |
|
161 | + // ->once() |
|
162 | + // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
163 | + // $this->commit |
|
164 | + // ->shouldReceive( 'to_body' ) |
|
165 | + // ->once() |
|
166 | + // ->andReturn( array( |
|
167 | + // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
168 | + // 'message' => 'Commit message', |
|
169 | + // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
170 | + // ) ); |
|
171 | + // $this->set_post_trees( true ); |
|
172 | + // $this->set_post_commits( true ); |
|
173 | + // $this->set_patch_refs_heads_master( true ); |
|
174 | + |
|
175 | + // $this->assertTrue( $this->persist->commit( $this->commit ) ); |
|
176 | + // } |
|
177 | + |
|
178 | + // public function test_should_create_authored_commit() { |
|
179 | + // $this->tree |
|
180 | + // ->shouldReceive( 'is_changed' ) |
|
181 | + // ->once() |
|
182 | + // ->andReturn( true ); |
|
183 | + // $this->tree |
|
184 | + // ->shouldReceive( 'to_body' ) |
|
185 | + // ->once() |
|
186 | + // ->andReturn( |
|
187 | + // array( |
|
188 | + // 'tree' => array( |
|
189 | + // array( |
|
190 | + // 'path' => '_posts/2015-10-23-new-post.md', |
|
191 | + // 'type' => 'blob', |
|
192 | + // 'content' => 'Post content 1', |
|
193 | + // 'mode' => '100644', |
|
194 | + // ) |
|
195 | + // ) |
|
196 | + // ) |
|
197 | + // ); |
|
198 | + // $this->tree |
|
199 | + // ->shouldReceive( 'set_sha' ) |
|
200 | + // ->once() |
|
201 | + // ->with( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ); |
|
202 | + // $this->commit |
|
203 | + // ->shouldReceive( 'to_body' ) |
|
204 | + // ->once() |
|
205 | + // ->andReturn( array( |
|
206 | + // 'tree' => 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8', |
|
207 | + // 'message' => 'Commit message', |
|
208 | + // 'parents' => array( 'c0d2cb90b51826096c826b61a0e74d2c973d7ad8' ) |
|
209 | + // ) ); |
|
210 | + // $this->set_post_trees( true ); |
|
211 | + // update_option( '_wogh_export_user_id', $this->factory->user->create( array( |
|
212 | + // 'display_name' => 'James DiGioia', |
|
213 | + // 'user_email' => '[email protected]', |
|
214 | + // ) ) ); |
|
215 | + // $this->set_post_commits( true, false ); |
|
216 | + // $this->set_patch_refs_heads_master( true ); |
|
217 | + |
|
218 | + // $this->assertTrue( $this->persist->commit( $this->commit ) ); |
|
219 | + // } |
|
220 | 220 | } |
@@ -6,147 +6,147 @@ |
||
6 | 6 | */ |
7 | 7 | class Writing_On_GitHub_Fetch_Client_Test extends Writing_On_GitHub_Base_Client_Test { |
8 | 8 | |
9 | - public function setUp() { |
|
10 | - parent::setUp(); |
|
11 | - |
|
12 | - $this->fetch = new Writing_On_GitHub_Fetch_Client( $this->app ); |
|
13 | - } |
|
14 | - |
|
15 | - public function test_should_fail_if_missing_token() { |
|
16 | - delete_option( Base::TOKEN_OPTION_KEY ); |
|
17 | - |
|
18 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
19 | - $this->assertSame( 'missing_token', $error->get_error_code() ); |
|
20 | - } |
|
21 | - |
|
22 | - public function test_should_fail_if_missing_repo() { |
|
23 | - delete_option( Base::REPO_OPTION_KEY ); |
|
24 | - |
|
25 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
26 | - $this->assertSame( 'missing_repository', $error->get_error_code() ); |
|
27 | - } |
|
28 | - |
|
29 | - public function test_should_fail_if_malformed_repo() { |
|
30 | - // If you find a particular name passing that shouldn't, |
|
31 | - // add it to the list here and make it pass. |
|
32 | - $this->malformed_repo( 'repositoryname' ); |
|
33 | - } |
|
34 | - |
|
35 | - public function test_compare() { |
|
36 | - $this->set_get_trees( true, 'master' ); |
|
37 | - $this->set_get_compare( true ); |
|
38 | - $infos = $this->fetch->compare( '861f87e8851b8debb78db548269d29f8da4d94ac' ); |
|
39 | - $this->assertCount( 1, $infos ); |
|
40 | - $this->assertInstanceOf( 'Writing_On_GitHub_File_Info', $infos[0] ); |
|
41 | - } |
|
42 | - |
|
43 | - public function test_should_return_files_with_tree() { |
|
44 | - // $this->set_get_refs_heads_master( true ); |
|
45 | - // $this->set_get_commits( true ); |
|
46 | - $this->set_get_trees( true, 'master' ); |
|
47 | - |
|
48 | - $this->assertCount( 3, $this->fetch->tree_recursive() ); |
|
49 | - } |
|
50 | - |
|
51 | - public function test_should_fail_if_cant_fetch_tree() { |
|
52 | - $this->set_get_trees( false, 'master' ); |
|
53 | - |
|
54 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
55 | - $this->assertSame( '422_unprocessable_entity', $error->get_error_code() ); |
|
56 | - } |
|
57 | - |
|
58 | - public function test_should_return_commit_with_no_blobs_if_api_fails() { |
|
59 | - $this->set_get_trees( true, 'master' ); |
|
60 | - $this->set_get_blobs( false ); |
|
61 | - |
|
62 | - $this->assertCount( 3, $files = $this->fetch->tree_recursive() ); |
|
63 | - |
|
64 | - foreach ( $files as $file ) { |
|
65 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->blob( $file ) ); |
|
66 | - $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - // public function test_should_return_and_validate_full_commit() { |
|
71 | - // $this->set_get_refs_heads_master( true ); |
|
72 | - // $this->set_get_commits( true ); |
|
73 | - // $this->set_get_trees( true ); |
|
74 | - // $this->set_get_blobs( true ); |
|
75 | - // $this->api_cache |
|
76 | - // ->shouldReceive( 'set_blob' ) |
|
77 | - // ->times( 3 ) |
|
78 | - // ->with( |
|
79 | - // Mockery::anyOf( |
|
80 | - // '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
81 | - // '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
82 | - // '2d73165945b0ccbe4932f1363457986b0ed49f19' |
|
83 | - // ), |
|
84 | - // Mockery::type( 'Writing_On_GitHub_Blob' ) |
|
85 | - // ) |
|
86 | - // ->andReturnUsing( function ( $sha, $blob ) { |
|
87 | - // return $blob; |
|
88 | - // } ); |
|
89 | - // $this->api_cache |
|
90 | - // ->shouldReceive( 'set_tree' ) |
|
91 | - // ->once() |
|
92 | - // ->with( '9108868e3800bec6763e51beb0d33e15036c3626', Mockery::type( 'Writing_On_GitHub_Tree' ) ) |
|
93 | - // ->andReturnUsing( function ( $sha, $tree ) { |
|
94 | - // return $tree; |
|
95 | - // } ); |
|
96 | - // $this->api_cache |
|
97 | - // ->shouldReceive( 'set_commit' ) |
|
98 | - // ->once() |
|
99 | - // ->with( 'db2510854e6aeab68ead26b48328b19f4bdf926e', Mockery::type( 'Writing_On_GitHub_Commit' ) ) |
|
100 | - // ->andReturnUsing( function ( $sha, $commit ) { |
|
101 | - // return $commit; |
|
102 | - // } ); |
|
103 | - |
|
104 | - // $this->assertInstanceOf( 'Writing_On_GitHub_Commit', $master = $this->fetch->master() ); |
|
105 | - |
|
106 | - // /** |
|
107 | - // * Validate the commit's api data mapped correctly. |
|
108 | - // */ |
|
109 | - // $this->assertSame( '7497c0574b9430ff5e5521b4572b7452ea36a056', $master->sha() ); |
|
110 | - // $this->assertSame( '[email protected]', $master->author()->email ); |
|
111 | - // $this->assertSame( '2015-11-02T00:36:54Z', $master->author()->date ); |
|
112 | - // $this->assertSame( '[email protected]', $master->committer()->email ); |
|
113 | - // $this->assertSame( '2015-11-02T00:36:54Z', $master->committer()->date ); |
|
114 | - // $this->assertSame( 'Initial full site export - wogh', $master->message() ); |
|
115 | - // $this->assertCount( 1, $parents = $master->parents() ); |
|
116 | - // $this->assertSame( 'db2510854e6aeab68ead26b48328b19f4bdf926e', $parents[0]->sha ); |
|
117 | - |
|
118 | - // $this->assertInstanceOf( 'Writing_On_GitHub_Tree', $tree = $master->tree() ); |
|
119 | - |
|
120 | - // /** |
|
121 | - // * Validate the tree's api data mapped correctly. |
|
122 | - // */ |
|
123 | - // $this->assertSame( '9108868e3800bec6763e51beb0d33e15036c3626', $tree->sha() ); |
|
124 | - |
|
125 | - // $this->assertCount( 3, $blobs = $tree->blobs() ); |
|
126 | - |
|
127 | - // /** |
|
128 | - // * Validate the blobs' api data mapped correctly. |
|
129 | - // */ |
|
130 | - // $blobs = $tree->blobs(); |
|
131 | - // $this->assertCount( 3, $blobs ); |
|
132 | - // foreach ( $blobs as $blob ) { |
|
133 | - // $this->assertTrue( in_array( $blob->sha(), array( |
|
134 | - // '2d73165945b0ccbe4932f1363457986b0ed49f19', |
|
135 | - // '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
136 | - // '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
137 | - // ) ) ); |
|
138 | - // $this->assertTrue( in_array( $blob->path(), array( |
|
139 | - // '_pages/sample-page.md', |
|
140 | - // '_posts/2015-11-02-hello-world.md', |
|
141 | - // 'README.md', |
|
142 | - // ) ) ); |
|
143 | - // } |
|
144 | - // } |
|
145 | - |
|
146 | - protected function malformed_repo( $repo ) { |
|
147 | - update_option( Base::REPO_OPTION_KEY, $repo ); |
|
148 | - |
|
149 | - $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
150 | - $this->assertSame( 'malformed_repository', $error->get_error_code() ); |
|
151 | - } |
|
9 | + public function setUp() { |
|
10 | + parent::setUp(); |
|
11 | + |
|
12 | + $this->fetch = new Writing_On_GitHub_Fetch_Client( $this->app ); |
|
13 | + } |
|
14 | + |
|
15 | + public function test_should_fail_if_missing_token() { |
|
16 | + delete_option( Base::TOKEN_OPTION_KEY ); |
|
17 | + |
|
18 | + $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
19 | + $this->assertSame( 'missing_token', $error->get_error_code() ); |
|
20 | + } |
|
21 | + |
|
22 | + public function test_should_fail_if_missing_repo() { |
|
23 | + delete_option( Base::REPO_OPTION_KEY ); |
|
24 | + |
|
25 | + $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
26 | + $this->assertSame( 'missing_repository', $error->get_error_code() ); |
|
27 | + } |
|
28 | + |
|
29 | + public function test_should_fail_if_malformed_repo() { |
|
30 | + // If you find a particular name passing that shouldn't, |
|
31 | + // add it to the list here and make it pass. |
|
32 | + $this->malformed_repo( 'repositoryname' ); |
|
33 | + } |
|
34 | + |
|
35 | + public function test_compare() { |
|
36 | + $this->set_get_trees( true, 'master' ); |
|
37 | + $this->set_get_compare( true ); |
|
38 | + $infos = $this->fetch->compare( '861f87e8851b8debb78db548269d29f8da4d94ac' ); |
|
39 | + $this->assertCount( 1, $infos ); |
|
40 | + $this->assertInstanceOf( 'Writing_On_GitHub_File_Info', $infos[0] ); |
|
41 | + } |
|
42 | + |
|
43 | + public function test_should_return_files_with_tree() { |
|
44 | + // $this->set_get_refs_heads_master( true ); |
|
45 | + // $this->set_get_commits( true ); |
|
46 | + $this->set_get_trees( true, 'master' ); |
|
47 | + |
|
48 | + $this->assertCount( 3, $this->fetch->tree_recursive() ); |
|
49 | + } |
|
50 | + |
|
51 | + public function test_should_fail_if_cant_fetch_tree() { |
|
52 | + $this->set_get_trees( false, 'master' ); |
|
53 | + |
|
54 | + $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
55 | + $this->assertSame( '422_unprocessable_entity', $error->get_error_code() ); |
|
56 | + } |
|
57 | + |
|
58 | + public function test_should_return_commit_with_no_blobs_if_api_fails() { |
|
59 | + $this->set_get_trees( true, 'master' ); |
|
60 | + $this->set_get_blobs( false ); |
|
61 | + |
|
62 | + $this->assertCount( 3, $files = $this->fetch->tree_recursive() ); |
|
63 | + |
|
64 | + foreach ( $files as $file ) { |
|
65 | + $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->blob( $file ) ); |
|
66 | + $this->assertSame( '404_not_found', $error->get_error_code() ); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + // public function test_should_return_and_validate_full_commit() { |
|
71 | + // $this->set_get_refs_heads_master( true ); |
|
72 | + // $this->set_get_commits( true ); |
|
73 | + // $this->set_get_trees( true ); |
|
74 | + // $this->set_get_blobs( true ); |
|
75 | + // $this->api_cache |
|
76 | + // ->shouldReceive( 'set_blob' ) |
|
77 | + // ->times( 3 ) |
|
78 | + // ->with( |
|
79 | + // Mockery::anyOf( |
|
80 | + // '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
81 | + // '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
82 | + // '2d73165945b0ccbe4932f1363457986b0ed49f19' |
|
83 | + // ), |
|
84 | + // Mockery::type( 'Writing_On_GitHub_Blob' ) |
|
85 | + // ) |
|
86 | + // ->andReturnUsing( function ( $sha, $blob ) { |
|
87 | + // return $blob; |
|
88 | + // } ); |
|
89 | + // $this->api_cache |
|
90 | + // ->shouldReceive( 'set_tree' ) |
|
91 | + // ->once() |
|
92 | + // ->with( '9108868e3800bec6763e51beb0d33e15036c3626', Mockery::type( 'Writing_On_GitHub_Tree' ) ) |
|
93 | + // ->andReturnUsing( function ( $sha, $tree ) { |
|
94 | + // return $tree; |
|
95 | + // } ); |
|
96 | + // $this->api_cache |
|
97 | + // ->shouldReceive( 'set_commit' ) |
|
98 | + // ->once() |
|
99 | + // ->with( 'db2510854e6aeab68ead26b48328b19f4bdf926e', Mockery::type( 'Writing_On_GitHub_Commit' ) ) |
|
100 | + // ->andReturnUsing( function ( $sha, $commit ) { |
|
101 | + // return $commit; |
|
102 | + // } ); |
|
103 | + |
|
104 | + // $this->assertInstanceOf( 'Writing_On_GitHub_Commit', $master = $this->fetch->master() ); |
|
105 | + |
|
106 | + // /** |
|
107 | + // * Validate the commit's api data mapped correctly. |
|
108 | + // */ |
|
109 | + // $this->assertSame( '7497c0574b9430ff5e5521b4572b7452ea36a056', $master->sha() ); |
|
110 | + // $this->assertSame( '[email protected]', $master->author()->email ); |
|
111 | + // $this->assertSame( '2015-11-02T00:36:54Z', $master->author()->date ); |
|
112 | + // $this->assertSame( '[email protected]', $master->committer()->email ); |
|
113 | + // $this->assertSame( '2015-11-02T00:36:54Z', $master->committer()->date ); |
|
114 | + // $this->assertSame( 'Initial full site export - wogh', $master->message() ); |
|
115 | + // $this->assertCount( 1, $parents = $master->parents() ); |
|
116 | + // $this->assertSame( 'db2510854e6aeab68ead26b48328b19f4bdf926e', $parents[0]->sha ); |
|
117 | + |
|
118 | + // $this->assertInstanceOf( 'Writing_On_GitHub_Tree', $tree = $master->tree() ); |
|
119 | + |
|
120 | + // /** |
|
121 | + // * Validate the tree's api data mapped correctly. |
|
122 | + // */ |
|
123 | + // $this->assertSame( '9108868e3800bec6763e51beb0d33e15036c3626', $tree->sha() ); |
|
124 | + |
|
125 | + // $this->assertCount( 3, $blobs = $tree->blobs() ); |
|
126 | + |
|
127 | + // /** |
|
128 | + // * Validate the blobs' api data mapped correctly. |
|
129 | + // */ |
|
130 | + // $blobs = $tree->blobs(); |
|
131 | + // $this->assertCount( 3, $blobs ); |
|
132 | + // foreach ( $blobs as $blob ) { |
|
133 | + // $this->assertTrue( in_array( $blob->sha(), array( |
|
134 | + // '2d73165945b0ccbe4932f1363457986b0ed49f19', |
|
135 | + // '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
136 | + // '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
137 | + // ) ) ); |
|
138 | + // $this->assertTrue( in_array( $blob->path(), array( |
|
139 | + // '_pages/sample-page.md', |
|
140 | + // '_posts/2015-11-02-hello-world.md', |
|
141 | + // 'README.md', |
|
142 | + // ) ) ); |
|
143 | + // } |
|
144 | + // } |
|
145 | + |
|
146 | + protected function malformed_repo( $repo ) { |
|
147 | + update_option( Base::REPO_OPTION_KEY, $repo ); |
|
148 | + |
|
149 | + $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() ); |
|
150 | + $this->assertSame( 'malformed_repository', $error->get_error_code() ); |
|
151 | + } |
|
152 | 152 | } |
@@ -2,275 +2,275 @@ |
||
2 | 2 | |
3 | 3 | abstract class Writing_On_GitHub_Base_Client_Test extends Writing_On_GitHub_TestCase { |
4 | 4 | |
5 | - /** |
|
6 | - * @var string |
|
7 | - */ |
|
8 | - const HOST_OPTION_VALUE = 'https://api.github.com'; |
|
9 | - |
|
10 | - /** |
|
11 | - * @var string |
|
12 | - */ |
|
13 | - const REPO_OPTION_VALUE = 'woghtest/wogh-test'; |
|
14 | - |
|
15 | - /** |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - const TOKEN_OPTION_VALUE = 'the-token'; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - const BRANCH_OPTION_VALUE = 'master'; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected static $responses = array(); |
|
29 | - |
|
30 | - /** |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - protected static $validations = array(); |
|
34 | - |
|
35 | - public function setUp() { |
|
36 | - parent::setUp(); |
|
37 | - |
|
38 | - WP_HTTP_TestCase::init(); |
|
39 | - update_option( 'wogh_repository', self::REPO_OPTION_VALUE ); |
|
40 | - update_option( 'wogh_oauth_token', self::TOKEN_OPTION_VALUE ); |
|
41 | - update_option( 'wogh_host', self::HOST_OPTION_VALUE ); |
|
42 | - update_option( 'wogh_branch', self::BRANCH_OPTION_VALUE ); |
|
43 | - $this->http_responder = array( $this, 'mock_github_api' ); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * This does some checks and fails the test if something is wrong |
|
48 | - * or returns intended mock data for the given endpoint + method. |
|
49 | - * |
|
50 | - * @return void|string |
|
51 | - */ |
|
52 | - public function mock_github_api( $request, $url ) { |
|
53 | - $host_length = strlen( self::HOST_OPTION_VALUE ); |
|
54 | - |
|
55 | - if ( self::HOST_OPTION_VALUE !== substr( $url, 0, $host_length ) ) { |
|
56 | - $this->assertTrue( false, 'Called wrong host.' ); |
|
57 | - } |
|
58 | - |
|
59 | - if ( |
|
60 | - ! isset( $request['headers']['Authorization'] ) || |
|
61 | - 'token ' . self::TOKEN_OPTION_VALUE !== $request['headers']['Authorization'] |
|
62 | - ) { |
|
63 | - $this->assertTrue( false, 'Missing authorization key.' ); |
|
64 | - } |
|
65 | - |
|
66 | - $url = explode( '/', substr( $url, $host_length + 1 ) ); |
|
67 | - |
|
68 | - if ( 'repos' !== $url[0] ) { |
|
69 | - $this->assertTrue( false, 'Called wrong endpoint.' ); |
|
70 | - } |
|
71 | - |
|
72 | - $repo = $url[1] . '/' . $url[2]; |
|
73 | - |
|
74 | - if ( self::REPO_OPTION_VALUE !== $repo ) { |
|
75 | - $this->assertTrue( false, 'Called wrong repo.' ); |
|
76 | - } |
|
77 | - |
|
78 | - // git api |
|
79 | - $parts = array_slice( $url, $url[3] === 'git' ? 4 : 3 ); |
|
80 | - array_unshift( $parts, strtolower( $request['method'] ) ); |
|
81 | - $endpoint = implode( '_', $parts ); |
|
82 | - $endpoint = str_replace( '?recursive=1', '', $endpoint ); |
|
83 | - $this->assertTrue( call_user_func( static::$validations[ $endpoint ], $request ), 'Request did not validate.' ); |
|
84 | - |
|
85 | - return static::$responses[ $endpoint ]; |
|
86 | - } |
|
87 | - |
|
88 | - protected function set_get_refs_heads_master( $succeed ) { |
|
89 | - $this->set_endpoint( |
|
90 | - function ( $request ) { |
|
91 | - if ( '[]' === $request['body'] ) { |
|
92 | - return false; |
|
93 | - } |
|
94 | - |
|
95 | - return true; |
|
96 | - }, $succeed ? '200 OK' : '404 Not Found', $succeed |
|
97 | - ); |
|
98 | - } |
|
99 | - |
|
100 | - protected function set_get_commits( $succeed ) { |
|
101 | - $this->set_endpoint( |
|
102 | - function ( $request ) { |
|
103 | - if ( '[]' === $request['body'] ) { |
|
104 | - return false; |
|
105 | - } |
|
106 | - |
|
107 | - return true; |
|
108 | - }, $succeed ? '200 OK' : '404 Not Found', $succeed, 'db2510854e6aeab68ead26b48328b19f4bdf926e' |
|
109 | - ); |
|
110 | - } |
|
111 | - |
|
112 | - protected function set_get_trees( $succeed, $sha = '' ) { |
|
113 | - $this->set_endpoint( |
|
114 | - function ( $request ) { |
|
115 | - if ( '[]' === $request['body'] ) { |
|
116 | - return false; |
|
117 | - } |
|
118 | - |
|
119 | - return true; |
|
120 | - }, $succeed ? '200 OK' : '422 Unprocessable Entity', $succeed, |
|
121 | - $sha ? $sha : '9108868e3800bec6763e51beb0d33e15036c3626' |
|
122 | - ); |
|
123 | - } |
|
124 | - |
|
125 | - protected function set_get_blobs( $succeed ) { |
|
126 | - $shas = array( |
|
127 | - '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
128 | - '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
129 | - '2d73165945b0ccbe4932f1363457986b0ed49f19', |
|
130 | - ); |
|
131 | - |
|
132 | - foreach ( $shas as $sha ) { |
|
133 | - $this->set_endpoint( |
|
134 | - function ( $request ) { |
|
135 | - if ( '[]' === $request['body'] ) { |
|
136 | - return false; |
|
137 | - } |
|
138 | - |
|
139 | - return true; |
|
140 | - }, $succeed ? '200 OK' : '404 Not Found', $succeed, $sha |
|
141 | - ); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - protected function set_post_trees( $succeed ) { |
|
146 | - $this->set_endpoint( |
|
147 | - function ( $request ) { |
|
148 | - $body = json_decode( $request['body'], true ); |
|
149 | - |
|
150 | - if ( ! isset( $body['tree'] ) ) { |
|
151 | - return false; |
|
152 | - } |
|
153 | - |
|
154 | - if ( 1 !== count( $body['tree'] ) ) { |
|
155 | - return false; |
|
156 | - } |
|
157 | - |
|
158 | - $blob = reset( $body['tree'] ); |
|
159 | - |
|
160 | - if ( |
|
161 | - ! isset( $blob['path'] ) || |
|
162 | - ! isset( $blob['type'] ) || |
|
163 | - ! isset( $blob['content'] ) || |
|
164 | - ! isset( $blob['mode'] ) |
|
165 | - ) { |
|
166 | - return false; |
|
167 | - } |
|
168 | - |
|
169 | - return true; |
|
170 | - }, |
|
171 | - $succeed ? '201 Created' : '404 Not Found', |
|
172 | - $succeed |
|
173 | - ); |
|
174 | - } |
|
175 | - |
|
176 | - protected function set_post_commits( $succeed, $anonymous = true ) { |
|
177 | - $this->set_endpoint( |
|
178 | - function ( $request ) use ( $anonymous ) { |
|
179 | - $body = json_decode( $request['body'], true ); |
|
180 | - |
|
181 | - if ( |
|
182 | - ! isset( $body['tree'] ) || |
|
183 | - ! isset( $body['message'] ) || |
|
184 | - ! isset( $body['parents'] ) || |
|
185 | - ! isset( $body['author'] ) |
|
186 | - ) { |
|
187 | - return false; |
|
188 | - } |
|
189 | - |
|
190 | - if ( 1 !== count( $body['parents'] ) ) { |
|
191 | - return false; |
|
192 | - } |
|
193 | - |
|
194 | - if ( ! $anonymous ) { |
|
195 | - if ( |
|
196 | - 'James DiGioia' !== $body['author']['name'] || |
|
197 | - '[email protected]' !== $body['author']['email'] |
|
198 | - ) { |
|
199 | - return false; |
|
200 | - } |
|
201 | - } else { |
|
202 | - if ( |
|
203 | - 'Anonymous' !== $body['author']['name'] || |
|
204 | - '[email protected]' !== $body['author']['email'] |
|
205 | - ) { |
|
206 | - return false; |
|
207 | - } |
|
208 | - } |
|
209 | - |
|
210 | - return true; |
|
211 | - }, |
|
212 | - $succeed ? '201 Created' : '404 Not Found', |
|
213 | - $succeed |
|
214 | - ); |
|
215 | - } |
|
216 | - |
|
217 | - protected function set_get_compare( $succeed ) { |
|
218 | - $this->set_endpoint( |
|
219 | - function ( $request ) { |
|
220 | - if ( '[]' === $request['body'] ) { |
|
221 | - return false; |
|
222 | - } |
|
223 | - return true; |
|
224 | - }, |
|
225 | - $succeed ? '200 OK' : '404 Not Found', |
|
226 | - $succeed, |
|
227 | - '861f87e8851b8debb78db548269d29f8da4d94ac...master' |
|
228 | - ); |
|
229 | - } |
|
230 | - |
|
231 | - protected function set_delete_contents( $succeed, $filepath ) { |
|
232 | - $this->set_endpoint( |
|
233 | - function ( $request ) { |
|
234 | - if ( '[]' === $request['body'] ) { |
|
235 | - return false; |
|
236 | - } |
|
237 | - return true; |
|
238 | - }, |
|
239 | - $succeed ? '200 OK' : '404 Not Found', |
|
240 | - $succeed, |
|
241 | - $filepath |
|
242 | - ); |
|
243 | - } |
|
244 | - |
|
245 | - protected function set_patch_refs_heads_master( $succeed ) { |
|
246 | - $this->set_endpoint( |
|
247 | - function ( $request ) { |
|
248 | - $body = json_decode( $request['body'], true ); |
|
249 | - |
|
250 | - if ( ! isset( $body['sha'] ) ) { |
|
251 | - return false; |
|
252 | - } |
|
253 | - |
|
254 | - return true; |
|
255 | - }, |
|
256 | - $succeed ? '201 Created' : '404 Not Found', |
|
257 | - $succeed |
|
258 | - ); |
|
259 | - } |
|
260 | - |
|
261 | - private function set_endpoint( $validation, $status, $succeed, $sha = '' ) { |
|
262 | - list( , $caller ) = debug_backtrace( false ); |
|
263 | - $endpoint = substr( $caller['function'], 4 ) . ( $sha ? "_$sha" : '' ); |
|
264 | - |
|
265 | - static::$validations[ $endpoint ] = $validation; |
|
266 | - |
|
267 | - static::$responses[ $endpoint ] = array( |
|
268 | - 'headers' => array( |
|
269 | - 'status' => $status, |
|
270 | - ), |
|
271 | - 'body' => file_get_contents( |
|
272 | - $this->data_dir . $endpoint . '_' . ( $succeed ? 'succeed' : 'fail' ) . '.json' |
|
273 | - ), |
|
274 | - ); |
|
275 | - } |
|
5 | + /** |
|
6 | + * @var string |
|
7 | + */ |
|
8 | + const HOST_OPTION_VALUE = 'https://api.github.com'; |
|
9 | + |
|
10 | + /** |
|
11 | + * @var string |
|
12 | + */ |
|
13 | + const REPO_OPTION_VALUE = 'woghtest/wogh-test'; |
|
14 | + |
|
15 | + /** |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + const TOKEN_OPTION_VALUE = 'the-token'; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + const BRANCH_OPTION_VALUE = 'master'; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected static $responses = array(); |
|
29 | + |
|
30 | + /** |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + protected static $validations = array(); |
|
34 | + |
|
35 | + public function setUp() { |
|
36 | + parent::setUp(); |
|
37 | + |
|
38 | + WP_HTTP_TestCase::init(); |
|
39 | + update_option( 'wogh_repository', self::REPO_OPTION_VALUE ); |
|
40 | + update_option( 'wogh_oauth_token', self::TOKEN_OPTION_VALUE ); |
|
41 | + update_option( 'wogh_host', self::HOST_OPTION_VALUE ); |
|
42 | + update_option( 'wogh_branch', self::BRANCH_OPTION_VALUE ); |
|
43 | + $this->http_responder = array( $this, 'mock_github_api' ); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * This does some checks and fails the test if something is wrong |
|
48 | + * or returns intended mock data for the given endpoint + method. |
|
49 | + * |
|
50 | + * @return void|string |
|
51 | + */ |
|
52 | + public function mock_github_api( $request, $url ) { |
|
53 | + $host_length = strlen( self::HOST_OPTION_VALUE ); |
|
54 | + |
|
55 | + if ( self::HOST_OPTION_VALUE !== substr( $url, 0, $host_length ) ) { |
|
56 | + $this->assertTrue( false, 'Called wrong host.' ); |
|
57 | + } |
|
58 | + |
|
59 | + if ( |
|
60 | + ! isset( $request['headers']['Authorization'] ) || |
|
61 | + 'token ' . self::TOKEN_OPTION_VALUE !== $request['headers']['Authorization'] |
|
62 | + ) { |
|
63 | + $this->assertTrue( false, 'Missing authorization key.' ); |
|
64 | + } |
|
65 | + |
|
66 | + $url = explode( '/', substr( $url, $host_length + 1 ) ); |
|
67 | + |
|
68 | + if ( 'repos' !== $url[0] ) { |
|
69 | + $this->assertTrue( false, 'Called wrong endpoint.' ); |
|
70 | + } |
|
71 | + |
|
72 | + $repo = $url[1] . '/' . $url[2]; |
|
73 | + |
|
74 | + if ( self::REPO_OPTION_VALUE !== $repo ) { |
|
75 | + $this->assertTrue( false, 'Called wrong repo.' ); |
|
76 | + } |
|
77 | + |
|
78 | + // git api |
|
79 | + $parts = array_slice( $url, $url[3] === 'git' ? 4 : 3 ); |
|
80 | + array_unshift( $parts, strtolower( $request['method'] ) ); |
|
81 | + $endpoint = implode( '_', $parts ); |
|
82 | + $endpoint = str_replace( '?recursive=1', '', $endpoint ); |
|
83 | + $this->assertTrue( call_user_func( static::$validations[ $endpoint ], $request ), 'Request did not validate.' ); |
|
84 | + |
|
85 | + return static::$responses[ $endpoint ]; |
|
86 | + } |
|
87 | + |
|
88 | + protected function set_get_refs_heads_master( $succeed ) { |
|
89 | + $this->set_endpoint( |
|
90 | + function ( $request ) { |
|
91 | + if ( '[]' === $request['body'] ) { |
|
92 | + return false; |
|
93 | + } |
|
94 | + |
|
95 | + return true; |
|
96 | + }, $succeed ? '200 OK' : '404 Not Found', $succeed |
|
97 | + ); |
|
98 | + } |
|
99 | + |
|
100 | + protected function set_get_commits( $succeed ) { |
|
101 | + $this->set_endpoint( |
|
102 | + function ( $request ) { |
|
103 | + if ( '[]' === $request['body'] ) { |
|
104 | + return false; |
|
105 | + } |
|
106 | + |
|
107 | + return true; |
|
108 | + }, $succeed ? '200 OK' : '404 Not Found', $succeed, 'db2510854e6aeab68ead26b48328b19f4bdf926e' |
|
109 | + ); |
|
110 | + } |
|
111 | + |
|
112 | + protected function set_get_trees( $succeed, $sha = '' ) { |
|
113 | + $this->set_endpoint( |
|
114 | + function ( $request ) { |
|
115 | + if ( '[]' === $request['body'] ) { |
|
116 | + return false; |
|
117 | + } |
|
118 | + |
|
119 | + return true; |
|
120 | + }, $succeed ? '200 OK' : '422 Unprocessable Entity', $succeed, |
|
121 | + $sha ? $sha : '9108868e3800bec6763e51beb0d33e15036c3626' |
|
122 | + ); |
|
123 | + } |
|
124 | + |
|
125 | + protected function set_get_blobs( $succeed ) { |
|
126 | + $shas = array( |
|
127 | + '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25', |
|
128 | + '8d9b2e6fd93761211dc03abd71f4a9189d680fd0', |
|
129 | + '2d73165945b0ccbe4932f1363457986b0ed49f19', |
|
130 | + ); |
|
131 | + |
|
132 | + foreach ( $shas as $sha ) { |
|
133 | + $this->set_endpoint( |
|
134 | + function ( $request ) { |
|
135 | + if ( '[]' === $request['body'] ) { |
|
136 | + return false; |
|
137 | + } |
|
138 | + |
|
139 | + return true; |
|
140 | + }, $succeed ? '200 OK' : '404 Not Found', $succeed, $sha |
|
141 | + ); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + protected function set_post_trees( $succeed ) { |
|
146 | + $this->set_endpoint( |
|
147 | + function ( $request ) { |
|
148 | + $body = json_decode( $request['body'], true ); |
|
149 | + |
|
150 | + if ( ! isset( $body['tree'] ) ) { |
|
151 | + return false; |
|
152 | + } |
|
153 | + |
|
154 | + if ( 1 !== count( $body['tree'] ) ) { |
|
155 | + return false; |
|
156 | + } |
|
157 | + |
|
158 | + $blob = reset( $body['tree'] ); |
|
159 | + |
|
160 | + if ( |
|
161 | + ! isset( $blob['path'] ) || |
|
162 | + ! isset( $blob['type'] ) || |
|
163 | + ! isset( $blob['content'] ) || |
|
164 | + ! isset( $blob['mode'] ) |
|
165 | + ) { |
|
166 | + return false; |
|
167 | + } |
|
168 | + |
|
169 | + return true; |
|
170 | + }, |
|
171 | + $succeed ? '201 Created' : '404 Not Found', |
|
172 | + $succeed |
|
173 | + ); |
|
174 | + } |
|
175 | + |
|
176 | + protected function set_post_commits( $succeed, $anonymous = true ) { |
|
177 | + $this->set_endpoint( |
|
178 | + function ( $request ) use ( $anonymous ) { |
|
179 | + $body = json_decode( $request['body'], true ); |
|
180 | + |
|
181 | + if ( |
|
182 | + ! isset( $body['tree'] ) || |
|
183 | + ! isset( $body['message'] ) || |
|
184 | + ! isset( $body['parents'] ) || |
|
185 | + ! isset( $body['author'] ) |
|
186 | + ) { |
|
187 | + return false; |
|
188 | + } |
|
189 | + |
|
190 | + if ( 1 !== count( $body['parents'] ) ) { |
|
191 | + return false; |
|
192 | + } |
|
193 | + |
|
194 | + if ( ! $anonymous ) { |
|
195 | + if ( |
|
196 | + 'James DiGioia' !== $body['author']['name'] || |
|
197 | + '[email protected]' !== $body['author']['email'] |
|
198 | + ) { |
|
199 | + return false; |
|
200 | + } |
|
201 | + } else { |
|
202 | + if ( |
|
203 | + 'Anonymous' !== $body['author']['name'] || |
|
204 | + '[email protected]' !== $body['author']['email'] |
|
205 | + ) { |
|
206 | + return false; |
|
207 | + } |
|
208 | + } |
|
209 | + |
|
210 | + return true; |
|
211 | + }, |
|
212 | + $succeed ? '201 Created' : '404 Not Found', |
|
213 | + $succeed |
|
214 | + ); |
|
215 | + } |
|
216 | + |
|
217 | + protected function set_get_compare( $succeed ) { |
|
218 | + $this->set_endpoint( |
|
219 | + function ( $request ) { |
|
220 | + if ( '[]' === $request['body'] ) { |
|
221 | + return false; |
|
222 | + } |
|
223 | + return true; |
|
224 | + }, |
|
225 | + $succeed ? '200 OK' : '404 Not Found', |
|
226 | + $succeed, |
|
227 | + '861f87e8851b8debb78db548269d29f8da4d94ac...master' |
|
228 | + ); |
|
229 | + } |
|
230 | + |
|
231 | + protected function set_delete_contents( $succeed, $filepath ) { |
|
232 | + $this->set_endpoint( |
|
233 | + function ( $request ) { |
|
234 | + if ( '[]' === $request['body'] ) { |
|
235 | + return false; |
|
236 | + } |
|
237 | + return true; |
|
238 | + }, |
|
239 | + $succeed ? '200 OK' : '404 Not Found', |
|
240 | + $succeed, |
|
241 | + $filepath |
|
242 | + ); |
|
243 | + } |
|
244 | + |
|
245 | + protected function set_patch_refs_heads_master( $succeed ) { |
|
246 | + $this->set_endpoint( |
|
247 | + function ( $request ) { |
|
248 | + $body = json_decode( $request['body'], true ); |
|
249 | + |
|
250 | + if ( ! isset( $body['sha'] ) ) { |
|
251 | + return false; |
|
252 | + } |
|
253 | + |
|
254 | + return true; |
|
255 | + }, |
|
256 | + $succeed ? '201 Created' : '404 Not Found', |
|
257 | + $succeed |
|
258 | + ); |
|
259 | + } |
|
260 | + |
|
261 | + private function set_endpoint( $validation, $status, $succeed, $sha = '' ) { |
|
262 | + list( , $caller ) = debug_backtrace( false ); |
|
263 | + $endpoint = substr( $caller['function'], 4 ) . ( $sha ? "_$sha" : '' ); |
|
264 | + |
|
265 | + static::$validations[ $endpoint ] = $validation; |
|
266 | + |
|
267 | + static::$responses[ $endpoint ] = array( |
|
268 | + 'headers' => array( |
|
269 | + 'status' => $status, |
|
270 | + ), |
|
271 | + 'body' => file_get_contents( |
|
272 | + $this->data_dir . $endpoint . '_' . ( $succeed ? 'succeed' : 'fail' ) . '.json' |
|
273 | + ), |
|
274 | + ); |
|
275 | + } |
|
276 | 276 | } |
@@ -2,146 +2,146 @@ |
||
2 | 2 | |
3 | 3 | abstract class Writing_On_GitHub_TestCase extends WP_HTTP_TestCase { |
4 | 4 | |
5 | - /** |
|
6 | - * @var string |
|
7 | - */ |
|
8 | - protected $data_dir; |
|
9 | - |
|
10 | - /** |
|
11 | - * @var Writing_On_GitHub|Mockery\Mock |
|
12 | - */ |
|
13 | - protected $app; |
|
14 | - |
|
15 | - /** |
|
16 | - * @var Writing_On_GitHub_Controller|Mockery\Mock |
|
17 | - */ |
|
18 | - protected $controller; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var Writing_On_GitHub_Request|Mockery\Mock |
|
22 | - */ |
|
23 | - protected $request; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var Writing_On_GitHub_Import|Mockery\Mock |
|
27 | - */ |
|
28 | - protected $import; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var Writing_On_GitHub_Export|Mockery\Mock |
|
32 | - */ |
|
33 | - protected $export; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var Writing_On_GitHub_Response|Mockery\Mock |
|
37 | - */ |
|
38 | - protected $response; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var Writing_On_GitHub_Payload|Mockery\Mock |
|
42 | - */ |
|
43 | - protected $payload; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var Writing_On_GitHub_Api|Mockery\Mock |
|
47 | - */ |
|
48 | - protected $api; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var Writing_On_GitHub_Semaphore|Mockery\Mock |
|
52 | - */ |
|
53 | - protected $semaphore; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var Writing_On_GitHub_Database|Mockery\Mock |
|
57 | - */ |
|
58 | - protected $database; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var Writing_On_GitHub_Post|Mockery\Mock |
|
62 | - */ |
|
63 | - protected $post; |
|
64 | - |
|
65 | - /** |
|
66 | - * @var Writing_On_GitHub_Blob|Mockery\Mock |
|
67 | - */ |
|
68 | - protected $blob; |
|
69 | - |
|
70 | - /** |
|
71 | - * @var Writing_On_GitHub_Fetch_Client|Mockery\Mock |
|
72 | - */ |
|
73 | - protected $fetch; |
|
74 | - |
|
75 | - /** |
|
76 | - * @var Writing_On_GitHub_Persist_Client|Mockery\Mock |
|
77 | - */ |
|
78 | - protected $persist; |
|
79 | - |
|
80 | - public function setUp() { |
|
81 | - parent::setUp(); |
|
82 | - |
|
83 | - $this->data_dir = dirname( __DIR__ ) . '/data/'; |
|
84 | - |
|
85 | - $this->app = Mockery::mock( 'Writing_On_GitHub' ); |
|
86 | - $this->controller = Mockery::mock( 'Writing_On_GitHub_Controller' ); |
|
87 | - $this->request = Mockery::mock( 'Writing_On_GitHub_Request' ); |
|
88 | - $this->import = Mockery::mock( 'Writing_On_GitHub_Import' ); |
|
89 | - $this->export = Mockery::mock( 'Writing_On_GitHub_Export' ); |
|
90 | - $this->response = Mockery::mock( 'Writing_On_GitHub_Response' ); |
|
91 | - $this->payload = Mockery::mock( 'Writing_On_GitHub_Payload' ); |
|
92 | - $this->api = Mockery::mock( 'Writing_On_GitHub_Api' ); |
|
93 | - $this->semaphore = Mockery::mock( 'Writing_On_GitHub_Semaphore' ); |
|
94 | - $this->database = Mockery::mock( 'Writing_On_GitHub_Database' ); |
|
95 | - $this->post = Mockery::mock( 'Writing_On_GitHub_Post' ); |
|
96 | - $this->blob = Mockery::mock( 'Writing_On_GitHub_Blob' ); |
|
97 | - $this->fetch = Mockery::mock( 'Writing_On_GitHub_Fetch_Client' ); |
|
98 | - $this->persist = Mockery::mock( 'Writing_On_GitHub_Persist_Client' ); |
|
99 | - |
|
100 | - Writing_On_GitHub::$instance = $this->app; |
|
101 | - |
|
102 | - $this->app |
|
103 | - ->shouldReceive( 'request' ) |
|
104 | - ->andReturn( $this->request ) |
|
105 | - ->byDefault(); |
|
106 | - $this->app |
|
107 | - ->shouldReceive( 'import' ) |
|
108 | - ->andReturn( $this->import ) |
|
109 | - ->byDefault(); |
|
110 | - $this->app |
|
111 | - ->shouldReceive( 'export' ) |
|
112 | - ->andReturn( $this->export ) |
|
113 | - ->byDefault(); |
|
114 | - $this->app |
|
115 | - ->shouldReceive( 'response' ) |
|
116 | - ->andReturn( $this->response ) |
|
117 | - ->byDefault(); |
|
118 | - $this->app |
|
119 | - ->shouldReceive( 'api' ) |
|
120 | - ->andReturn( $this->api ) |
|
121 | - ->byDefault(); |
|
122 | - $this->app |
|
123 | - ->shouldReceive( 'semaphore' ) |
|
124 | - ->andReturn( $this->semaphore ) |
|
125 | - ->byDefault(); |
|
126 | - $this->app |
|
127 | - ->shouldReceive( 'database' ) |
|
128 | - ->andReturn( $this->database ) |
|
129 | - ->byDefault(); |
|
130 | - $this->app |
|
131 | - ->shouldReceive( 'blob' ) |
|
132 | - ->andReturn( $this->blob ) |
|
133 | - ->byDefault(); |
|
134 | - $this->api |
|
135 | - ->shouldReceive( 'fetch' ) |
|
136 | - ->andReturn( $this->fetch ) |
|
137 | - ->byDefault(); |
|
138 | - $this->api |
|
139 | - ->shouldReceive( 'persist' ) |
|
140 | - ->andReturn( $this->persist ) |
|
141 | - ->byDefault(); |
|
142 | - } |
|
143 | - |
|
144 | - public function tearDown() { |
|
145 | - Mockery::close(); |
|
146 | - } |
|
5 | + /** |
|
6 | + * @var string |
|
7 | + */ |
|
8 | + protected $data_dir; |
|
9 | + |
|
10 | + /** |
|
11 | + * @var Writing_On_GitHub|Mockery\Mock |
|
12 | + */ |
|
13 | + protected $app; |
|
14 | + |
|
15 | + /** |
|
16 | + * @var Writing_On_GitHub_Controller|Mockery\Mock |
|
17 | + */ |
|
18 | + protected $controller; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var Writing_On_GitHub_Request|Mockery\Mock |
|
22 | + */ |
|
23 | + protected $request; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var Writing_On_GitHub_Import|Mockery\Mock |
|
27 | + */ |
|
28 | + protected $import; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var Writing_On_GitHub_Export|Mockery\Mock |
|
32 | + */ |
|
33 | + protected $export; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var Writing_On_GitHub_Response|Mockery\Mock |
|
37 | + */ |
|
38 | + protected $response; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var Writing_On_GitHub_Payload|Mockery\Mock |
|
42 | + */ |
|
43 | + protected $payload; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var Writing_On_GitHub_Api|Mockery\Mock |
|
47 | + */ |
|
48 | + protected $api; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var Writing_On_GitHub_Semaphore|Mockery\Mock |
|
52 | + */ |
|
53 | + protected $semaphore; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var Writing_On_GitHub_Database|Mockery\Mock |
|
57 | + */ |
|
58 | + protected $database; |
|
59 | + |
|
60 | + /** |
|
61 | + * @var Writing_On_GitHub_Post|Mockery\Mock |
|
62 | + */ |
|
63 | + protected $post; |
|
64 | + |
|
65 | + /** |
|
66 | + * @var Writing_On_GitHub_Blob|Mockery\Mock |
|
67 | + */ |
|
68 | + protected $blob; |
|
69 | + |
|
70 | + /** |
|
71 | + * @var Writing_On_GitHub_Fetch_Client|Mockery\Mock |
|
72 | + */ |
|
73 | + protected $fetch; |
|
74 | + |
|
75 | + /** |
|
76 | + * @var Writing_On_GitHub_Persist_Client|Mockery\Mock |
|
77 | + */ |
|
78 | + protected $persist; |
|
79 | + |
|
80 | + public function setUp() { |
|
81 | + parent::setUp(); |
|
82 | + |
|
83 | + $this->data_dir = dirname( __DIR__ ) . '/data/'; |
|
84 | + |
|
85 | + $this->app = Mockery::mock( 'Writing_On_GitHub' ); |
|
86 | + $this->controller = Mockery::mock( 'Writing_On_GitHub_Controller' ); |
|
87 | + $this->request = Mockery::mock( 'Writing_On_GitHub_Request' ); |
|
88 | + $this->import = Mockery::mock( 'Writing_On_GitHub_Import' ); |
|
89 | + $this->export = Mockery::mock( 'Writing_On_GitHub_Export' ); |
|
90 | + $this->response = Mockery::mock( 'Writing_On_GitHub_Response' ); |
|
91 | + $this->payload = Mockery::mock( 'Writing_On_GitHub_Payload' ); |
|
92 | + $this->api = Mockery::mock( 'Writing_On_GitHub_Api' ); |
|
93 | + $this->semaphore = Mockery::mock( 'Writing_On_GitHub_Semaphore' ); |
|
94 | + $this->database = Mockery::mock( 'Writing_On_GitHub_Database' ); |
|
95 | + $this->post = Mockery::mock( 'Writing_On_GitHub_Post' ); |
|
96 | + $this->blob = Mockery::mock( 'Writing_On_GitHub_Blob' ); |
|
97 | + $this->fetch = Mockery::mock( 'Writing_On_GitHub_Fetch_Client' ); |
|
98 | + $this->persist = Mockery::mock( 'Writing_On_GitHub_Persist_Client' ); |
|
99 | + |
|
100 | + Writing_On_GitHub::$instance = $this->app; |
|
101 | + |
|
102 | + $this->app |
|
103 | + ->shouldReceive( 'request' ) |
|
104 | + ->andReturn( $this->request ) |
|
105 | + ->byDefault(); |
|
106 | + $this->app |
|
107 | + ->shouldReceive( 'import' ) |
|
108 | + ->andReturn( $this->import ) |
|
109 | + ->byDefault(); |
|
110 | + $this->app |
|
111 | + ->shouldReceive( 'export' ) |
|
112 | + ->andReturn( $this->export ) |
|
113 | + ->byDefault(); |
|
114 | + $this->app |
|
115 | + ->shouldReceive( 'response' ) |
|
116 | + ->andReturn( $this->response ) |
|
117 | + ->byDefault(); |
|
118 | + $this->app |
|
119 | + ->shouldReceive( 'api' ) |
|
120 | + ->andReturn( $this->api ) |
|
121 | + ->byDefault(); |
|
122 | + $this->app |
|
123 | + ->shouldReceive( 'semaphore' ) |
|
124 | + ->andReturn( $this->semaphore ) |
|
125 | + ->byDefault(); |
|
126 | + $this->app |
|
127 | + ->shouldReceive( 'database' ) |
|
128 | + ->andReturn( $this->database ) |
|
129 | + ->byDefault(); |
|
130 | + $this->app |
|
131 | + ->shouldReceive( 'blob' ) |
|
132 | + ->andReturn( $this->blob ) |
|
133 | + ->byDefault(); |
|
134 | + $this->api |
|
135 | + ->shouldReceive( 'fetch' ) |
|
136 | + ->andReturn( $this->fetch ) |
|
137 | + ->byDefault(); |
|
138 | + $this->api |
|
139 | + ->shouldReceive( 'persist' ) |
|
140 | + ->andReturn( $this->persist ) |
|
141 | + ->byDefault(); |
|
142 | + } |
|
143 | + |
|
144 | + public function tearDown() { |
|
145 | + Mockery::close(); |
|
146 | + } |
|
147 | 147 | } |