Skip to content

Commit b032509

Browse files
authored
Merge pull request #19 from tg-bot-api/feature/18-fix-profile-photo-normalization
feature/18 fix profile photo normalization
2 parents d1c5355 + 80c2aa4 commit b032509

7 files changed

+78
-75
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to `telegram-bot-api` will be documented in this file.
44

55
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
66

7+
<!---
78
## NEXT - YYYY-MM-DD
89
910
### Added
@@ -20,6 +21,12 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
2021
2122
### Security
2223
- Nothing
24+
--->
25+
26+
## 1.3.1 - 2020-01-30
27+
28+
### Fixed
29+
- fixed stdObject property call inUserProfilePhotosNormalizer, updated test for it.
2330

2431
## 1.3.0 - 2020-01-25
2532

src/ApiClient.php

+4-30
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ class ApiClient implements ApiClientInterface
4242

4343
/**
4444
* ApiApiClient constructor.
45-
*
46-
* @param RequestFactoryInterface $requestFactory
47-
* @param StreamFactoryInterface $streamFactory
48-
* @param ClientInterface $client
4945
*/
5046
public function __construct(
5147
RequestFactoryInterface $requestFactory,
@@ -58,9 +54,6 @@ public function __construct(
5854
}
5955

6056
/**
61-
* @param string $method
62-
* @param BotApiRequestInterface $apiRequest
63-
*
6457
* @throws ClientExceptionInterface
6558
*
6659
* @return mixed
@@ -79,30 +72,19 @@ public function send(string $method, BotApiRequestInterface $apiRequest)
7972

8073
$content = $response->getBody()->getContents();
8174

82-
return \json_decode($content);
75+
return \json_decode($content, false);
8376
}
8477

85-
/**
86-
* @param string $botKey
87-
*/
8878
public function setBotKey(string $botKey): void
8979
{
9080
$this->botKey = $botKey;
9181
}
9282

93-
/**
94-
* @param string $endPoint
95-
*/
9683
public function setEndpoint(string $endPoint): void
9784
{
9885
$this->endPoint = $endPoint;
9986
}
10087

101-
/**
102-
* @param string $method
103-
*
104-
* @return string
105-
*/
10688
protected function generateUri(string $method): string
10789
{
10890
return \sprintf(
@@ -114,10 +96,7 @@ protected function generateUri(string $method): string
11496
}
11597

11698
/**
117-
* @param mixed $boundary
118-
* @param BotApiRequestInterface $request
119-
*
120-
* @return string
99+
* @param mixed $boundary
121100
*/
122101
protected function createStreamBody($boundary, BotApiRequestInterface $request): string
123102
{
@@ -135,11 +114,8 @@ protected function createStreamBody($boundary, BotApiRequestInterface $request):
135114
}
136115

137116
/**
138-
* @param $boundary
139-
* @param $name
140-
* @param InputFileType $file
141-
*
142-
* @return string
117+
* @param $boundary
118+
* @param $name
143119
*/
144120
protected function createFileStream($boundary, $name, InputFileType $file): string
145121
{
@@ -162,8 +138,6 @@ protected function createFileStream($boundary, $name, InputFileType $file): stri
162138
* @param $boundary
163139
* @param $name
164140
* @param $value
165-
*
166-
* @return string
167141
*/
168142
protected function createDataStream(string $boundary, string $name, string $value): string
169143
{

src/Normalizer/UserProfilePhotosNormalizer.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ class UserProfilePhotosNormalizer implements DenormalizerInterface
2828

2929
/**
3030
* UserProfilePhotosNormalizer constructor.
31-
*
32-
* @param NormalizerInterface $objectNormalizer
33-
* @param ArrayDenormalizer $arrayDenormalizer
3431
*/
3532
public function __construct(NormalizerInterface $objectNormalizer, ArrayDenormalizer $arrayDenormalizer)
3633
{
@@ -42,16 +39,13 @@ public function __construct(NormalizerInterface $objectNormalizer, ArrayDenormal
4239
* @param mixed $data
4340
* @param string $class
4441
* @param null $format
45-
* @param array $context
4642
*
4743
* @throws ExceptionInterface
48-
*
49-
* @return UserProfilePhotosType
5044
*/
5145
public function denormalize($data, $class, $format = null, array $context = []): UserProfilePhotosType
5246
{
5347
$serializer = new Serializer([$this->objectNormalizer, $this->arrayDenormalizer]);
54-
$data['photos'] = $serializer->denormalize($data['photos'], PhotoSizeType::class . '[][]');
48+
$data->photos = $serializer->denormalize($data->photos, PhotoSizeType::class . '[][]');
5549

5650
return $serializer->denormalize($data, UserProfilePhotosType::class);
5751
}
@@ -60,8 +54,6 @@ public function denormalize($data, $class, $format = null, array $context = []):
6054
* @param mixed $data
6155
* @param string $type
6256
* @param null $format
63-
*
64-
* @return bool
6557
*/
6658
public function supportsDenormalization($data, $type, $format = null): bool
6759
{

tests/Method/GetUserProfilePhotosMethodTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testEncode()
1717
$botApi = $this->getBot(
1818
'getUserProfilePhotos',
1919
['user_id' => 1, 'offset' => 0, 'limit' => 100],
20-
['total_count' => 1, 'photos' => []]
20+
(object) ['total_count' => 1, 'photos' => []]
2121
);
2222

2323
$botApi->getUserProfilePhotos(GetUserProfilePhotosMethod::create(1, ['offset' => 0, 'limit' => 100]));

tests/Type/TypeTestCase.php

+20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ abstract class TypeTestCase extends \PHPUnit\Framework\TestCase
1313
{
1414
use GetNormalizerTrait;
1515

16+
/**
17+
* @return BotApi
18+
*/
19+
public function getBotFromJson(string $json)
20+
{
21+
$stub = $this->getMockBuilder(ApiClientInterface::class)
22+
->getMock();
23+
24+
$stub->expects($this->once())
25+
->method('send')
26+
->willReturn(\json_decode($json, false));
27+
28+
return new BotApi('000000000:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', $stub, $this->getNormalizer());
29+
}
30+
1631
/**
1732
* @param $result
1833
*/
@@ -33,4 +48,9 @@ protected function getMethod(): MethodInterface
3348
{
3449
return $this->getMockBuilder(MethodInterface::class)->getMock();
3550
}
51+
52+
protected function getResource($filename): string
53+
{
54+
return \file_get_contents(\sprintf('%s/resources/%s.json', __DIR__, $filename));
55+
}
3656
}

tests/Type/UserProfilePhotosTypeTest.php

+14-35
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,29 @@ class UserProfilePhotosTypeTest extends TypeTestCase
1111
{
1212
public function testEncode()
1313
{
14-
$result = [
15-
'total_count' => 1,
16-
'photos' => [
17-
[
18-
[
19-
'file_id' => 'AgADBQADyKcxG5hmrglZIi4CcakAAf4_7r0yAAQ735Gp9Icu6KADAQABAg',
20-
'file_uniqueId' => 'fileUniqueId',
21-
'file_size' => 91518,
22-
'width' => 640,
23-
'height' => 640,
24-
],
25-
[
26-
'file_id' => 'AgADBQADyKcxG5hmrglZIi4CcakAAf4_7r0yAASzMopVG_fq_qEDAQABAg',
27-
'file_uniqueId' => 'fileUniqueId',
28-
'file_size' => 219720,
29-
'width' => 1050,
30-
'height' => 1050,
31-
],
32-
],
33-
],
34-
];
14+
$result = $this->getResource('user-profile-photos');
3515

3616
$type = $this->getType($result);
3717

18+
$result = \json_decode($result, true)['result'];
19+
3820
$this->assertEquals($type->totalCount, $result['total_count']);
3921
$this->assertEquals(\count($type->photos), 1);
40-
$this->assertEquals(\count($type->photos[0]), 2);
41-
$this->assertInstanceOf(PhotoSizeType::class, $type->photos[0][0]);
42-
$this->assertInstanceOf(PhotoSizeType::class, $type->photos[0][1]);
43-
$this->assertEquals($type->photos[0][0]->fileId, $result['photos'][0][0]['file_id']);
44-
$this->assertEquals($type->photos[0][0]->fileUniqueId, $result['photos'][0][0]['file_uniqueId']);
45-
$this->assertEquals($type->photos[0][0]->fileSize, $result['photos'][0][0]['file_size']);
46-
$this->assertEquals($type->photos[0][0]->width, $result['photos'][0][0]['width']);
47-
$this->assertEquals($type->photos[0][0]->height, $result['photos'][0][0]['height']);
48-
$this->assertEquals($type->photos[0][1]->fileId, $result['photos'][0][1]['file_id']);
49-
$this->assertEquals($type->photos[0][1]->fileUniqueId, $result['photos'][0][0]['file_uniqueId']);
50-
$this->assertEquals($type->photos[0][1]->fileSize, $result['photos'][0][1]['file_size']);
51-
$this->assertEquals($type->photos[0][1]->width, $result['photos'][0][1]['width']);
52-
$this->assertEquals($type->photos[0][1]->height, $result['photos'][0][1]['height']);
22+
$this->assertEquals(\count($type->photos[0]), 3);
23+
24+
foreach ($result['photos'][0] as $index => $size) {
25+
$this->assertInstanceOf(PhotoSizeType::class, $type->photos[0][$index]);
26+
$this->assertEquals($type->photos[0][$index]->fileId, $size['file_id']);
27+
$this->assertEquals($type->photos[0][$index]->fileUniqueId, $size['file_unique_id']);
28+
$this->assertEquals($type->photos[0][$index]->fileSize, $size['file_size']);
29+
$this->assertEquals($type->photos[0][$index]->width, $size['width']);
30+
$this->assertEquals($type->photos[0][$index]->height, $size['height']);
31+
}
5332
}
5433

5534
public function getType($result): UserProfilePhotosType
5635
{
57-
$botApi = $this->getBot($result);
36+
$botApi = $this->getBotFromJson($result);
5837

5938
return $botApi->call($this->getMethod(), UserProfilePhotosType::class);
6039
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"ok": true,
3+
"result": {
4+
"total_count": 1,
5+
"photos": [
6+
[
7+
{
8+
"file_id": "secret",
9+
"file_unique_id": "secret",
10+
"file_size": 12930,
11+
"width": 160,
12+
"height": 160
13+
},
14+
{
15+
"file_id": "secret",
16+
"file_unique_id": "secret",
17+
"file_size": 40592,
18+
"width": 320,
19+
"height": 320
20+
},
21+
{
22+
"file_id": "secret",
23+
"file_unique_id": "secret",
24+
"file_size": 116195,
25+
"width": 640,
26+
"height": 640
27+
}
28+
]
29+
]
30+
}
31+
}

0 commit comments

Comments
 (0)