-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
MaterialMaterialTest.php
executable file
·247 lines (194 loc) · 7.27 KB
/
MaterialMaterialTest.php
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use EasyWeChat\Core\Http;
use EasyWeChat\Material\Material;
use EasyWeChat\Message\Article;
use GuzzleHttp\Psr7\Response;
class MaterialMaterialTest extends TestCase
{
/**
* Return mock http.
*
* @return \Mockery\MockInterface
*/
public function getMaterial()
{
$accessToken = Mockery::mock('EasyWeChat\Core\AccessToken');
$accessToken->shouldReceive('getQueryFields')->andReturn(['access_token' => 'foo']);
$material = Mockery::mock('EasyWeChat\Material\Material[parseJSON]', [$accessToken]);
$material->shouldReceive('parseJSON')->andReturnUsing(function () {
return func_get_args()[1];
});
return $material;
}
public function getMockAccessToken()
{
$token = Mockery::mock('EasyWeChat\Core\AccessToken[getQueryFields]', ['foo', 'bar']);
$token->shouldReceive('getQueryFields')->andReturn(['access_token' => 'foo']);
return $token;
}
/**
* Test for uploadImage().
*
* @expectedException EasyWeChat\Core\Exceptions\InvalidArgumentException
*/
public function testUploadImage()
{
$material = $this->getMaterial();
$response = $material->uploadImage(__DIR__.'/stubs/image.jpg');
$this->assertStringStartsWith(Material::API_UPLOAD, $response[0]);
$this->assertContains('stubs/image.jpg', $response[1]['media']);
// exception
$response = $material->uploadImage(__DIR__.'/stubs/foo.jpg');
}
/**
* Test for uploadVoice().
*/
public function testUploadVoice()
{
$material = $this->getMaterial();
$response = $material->uploadVoice(__DIR__.'/stubs/voice.wma');
$this->assertStringStartsWith(Material::API_UPLOAD, $response[0]);
$this->assertContains('stubs/voice.wma', $response[1]['media']);
}
/**
* Test for uploadThumb().
*/
public function testUploadThumb()
{
$material = $this->getMaterial();
$response = $material->uploadThumb(__DIR__.'/stubs/thumb.png');
$this->assertStringStartsWith(Material::API_UPLOAD, $response[0]);
$this->assertContains('stubs/thumb.png', $response[1]['media']);
}
/**
* Test for uploadVideo().
*/
public function testUploadVideo()
{
$material = $this->getMaterial();
$response = $material->uploadVideo(__DIR__.'/stubs/video.mp4', 'foo', 'a mp4 video.');
$this->assertStringStartsWith(Material::API_UPLOAD, $response[0]);
$this->assertContains('stubs/video.mp4', $response[1]['media']);
$this->assertEquals(json_encode(['title' => 'foo', 'introduction' => 'a mp4 video.']), $response[2]['description']);
}
/**
* Test for uploadArticle().
*/
public function testUploadArticle()
{
$material = $this->getMaterial();
$response = $material->uploadArticle(['foo' => 'bar']);
$this->assertStringStartsWith(Material::API_NEWS_UPLOAD, $response[0]);
$this->assertEquals(['articles' => ['foo' => 'bar']], $response[1]);
$response = $material->uploadArticle(new Article(['title' => 'foo']));
$this->assertEquals(['articles' => [['title' => 'foo']]], $response[1]);
$response = $material->uploadArticle([new Article(['title' => 'foo', 'show_cover' => 0]), new Article(['title' => 'bar'])]);
$this->assertEquals(['articles' => [['title' => 'foo', 'show_cover_pic' => 0], ['title' => 'bar']]], $response[1]);
}
/**
* Test for updateArticle().
*/
public function testUpdateArticle()
{
$material = $this->getMaterial();
$response = $material->updateArticle('foo', ['title' => 'bar']);
$this->assertStringStartsWith(Material::API_NEWS_UPDATE, $response[0]);
$this->assertEquals('foo', $response[1]['media_id']);
$this->assertEquals(0, $response[1]['index']);
$this->assertEquals(['title' => 'bar'], $response[1]['articles']);
// multi
$response = $material->updateArticle('foo', [['title' => 'bar']]);
$this->assertEquals(['title' => 'bar'], $response[1]['articles']);
// invalid $article
$response = $material->updateArticle('foo', ['abc' => 'bar']);
$this->assertEquals([], $response[1]['articles']);
}
/**
* Test for uploadArticleImage().
*/
public function testUploadArticleImage()
{
$material = $this->getMaterial();
$response = $material->uploadArticleImage(__DIR__.'/stubs/image.jpg');
$this->assertStringStartsWith(Material::API_NEWS_IMAGE_UPLOAD, $response[0]);
$this->assertContains('stubs/image.jpg', $response[1]['media']);
}
/**
* Test for get().
*/
public function testGet()
{
$material = $this->getMaterial();
$http = Mockery::mock(Http::class.'[json]');
$http->shouldReceive('addMiddleware')->andReturn($http);
$http->shouldReceive('json')->andReturnUsing(function ($api, $params) {
return new Response(200, ['Content-Type' => ['text/plain']], json_encode(compact('api', 'params')));
});
$material->setHttp($http);
// news
$response = $material->get('foo');
$this->assertStringStartsWith(Material::API_GET, $response['api']);
$this->assertEquals(['media_id' => 'foo'], $response['params']);
// media
$http = Mockery::mock(Http::class.'[json]');
$http->shouldReceive('addMiddleware')->andReturn($http);
$http->shouldReceive('json')->andReturnUsing(function ($api, $params) {
return new Response(200, ['Content-Type' => ['media/video']], 'media content');
});
$material->setHttp($http);
$response = $material->get('bar');
$this->assertEquals('media content', $response);
}
/**
* Test for delete().
*/
public function testDelete()
{
$material = $this->getMaterial();
$response = $material->delete('foo');
$this->assertStringStartsWith(Material::API_DELETE, $response[0]);
$this->assertEquals(['media_id' => 'foo'], $response[1]);
}
/**
* Test for lists().
*/
public function testLists()
{
$material = $this->getMaterial();
// normal
$response = $material->lists('image');
$params = [
'type' => 'image',
'offset' => 0,
'count' => 20,
];
$this->assertStringStartsWith(Material::API_LISTS, $response[0]);
$this->assertEquals($params, $response[1]);
// out of range
$response = $material->lists('image', 1, 21);
$params = [
'type' => 'image',
'offset' => 1,
'count' => 20, // 21 -> 20
];
$this->assertStringStartsWith(Material::API_LISTS, $response[0]);
$this->assertEquals($params, $response[1]);
}
/**
* Test for stats().
*/
public function testStats()
{
$material = $this->getMaterial();
$response = $material->stats();
$this->assertStringStartsWith(Material::API_STATS, $response[0]);
}
}