Skip to content

Feature/18 fix profile photo normalization #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to `telegram-bot-api` will be documented in this file.

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

<!---
## NEXT - YYYY-MM-DD

### Added
Expand All @@ -20,6 +21,12 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

### Security
- Nothing
--->

## 1.3.1 - 2020-01-30

### Fixed
- fixed stdObject property call inUserProfilePhotosNormalizer, updated test for it.

## 1.3.0 - 2020-01-25

Expand Down
34 changes: 4 additions & 30 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ class ApiClient implements ApiClientInterface

/**
* ApiApiClient constructor.
*
* @param RequestFactoryInterface $requestFactory
* @param StreamFactoryInterface $streamFactory
* @param ClientInterface $client
*/
public function __construct(
RequestFactoryInterface $requestFactory,
Expand All @@ -58,9 +54,6 @@ public function __construct(
}

/**
* @param string $method
* @param BotApiRequestInterface $apiRequest
*
* @throws ClientExceptionInterface
*
* @return mixed
Expand All @@ -79,30 +72,19 @@ public function send(string $method, BotApiRequestInterface $apiRequest)

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

return \json_decode($content);
return \json_decode($content, false);
}

/**
* @param string $botKey
*/
public function setBotKey(string $botKey): void
{
$this->botKey = $botKey;
}

/**
* @param string $endPoint
*/
public function setEndpoint(string $endPoint): void
{
$this->endPoint = $endPoint;
}

/**
* @param string $method
*
* @return string
*/
protected function generateUri(string $method): string
{
return \sprintf(
Expand All @@ -114,10 +96,7 @@ protected function generateUri(string $method): string
}

/**
* @param mixed $boundary
* @param BotApiRequestInterface $request
*
* @return string
* @param mixed $boundary
*/
protected function createStreamBody($boundary, BotApiRequestInterface $request): string
{
Expand All @@ -135,11 +114,8 @@ protected function createStreamBody($boundary, BotApiRequestInterface $request):
}

/**
* @param $boundary
* @param $name
* @param InputFileType $file
*
* @return string
* @param $boundary
* @param $name
*/
protected function createFileStream($boundary, $name, InputFileType $file): string
{
Expand All @@ -162,8 +138,6 @@ protected function createFileStream($boundary, $name, InputFileType $file): stri
* @param $boundary
* @param $name
* @param $value
*
* @return string
*/
protected function createDataStream(string $boundary, string $name, string $value): string
{
Expand Down
10 changes: 1 addition & 9 deletions src/Normalizer/UserProfilePhotosNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ class UserProfilePhotosNormalizer implements DenormalizerInterface

/**
* UserProfilePhotosNormalizer constructor.
*
* @param NormalizerInterface $objectNormalizer
* @param ArrayDenormalizer $arrayDenormalizer
*/
public function __construct(NormalizerInterface $objectNormalizer, ArrayDenormalizer $arrayDenormalizer)
{
Expand All @@ -42,16 +39,13 @@ public function __construct(NormalizerInterface $objectNormalizer, ArrayDenormal
* @param mixed $data
* @param string $class
* @param null $format
* @param array $context
*
* @throws ExceptionInterface
*
* @return UserProfilePhotosType
*/
public function denormalize($data, $class, $format = null, array $context = []): UserProfilePhotosType
{
$serializer = new Serializer([$this->objectNormalizer, $this->arrayDenormalizer]);
$data['photos'] = $serializer->denormalize($data['photos'], PhotoSizeType::class . '[][]');
$data->photos = $serializer->denormalize($data->photos, PhotoSizeType::class . '[][]');

return $serializer->denormalize($data, UserProfilePhotosType::class);
}
Expand All @@ -60,8 +54,6 @@ public function denormalize($data, $class, $format = null, array $context = []):
* @param mixed $data
* @param string $type
* @param null $format
*
* @return bool
*/
public function supportsDenormalization($data, $type, $format = null): bool
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Method/GetUserProfilePhotosMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testEncode()
$botApi = $this->getBot(
'getUserProfilePhotos',
['user_id' => 1, 'offset' => 0, 'limit' => 100],
['total_count' => 1, 'photos' => []]
(object) ['total_count' => 1, 'photos' => []]
);

$botApi->getUserProfilePhotos(GetUserProfilePhotosMethod::create(1, ['offset' => 0, 'limit' => 100]));
Expand Down
20 changes: 20 additions & 0 deletions tests/Type/TypeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ abstract class TypeTestCase extends \PHPUnit\Framework\TestCase
{
use GetNormalizerTrait;

/**
* @return BotApi
*/
public function getBotFromJson(string $json)
{
$stub = $this->getMockBuilder(ApiClientInterface::class)
->getMock();

$stub->expects($this->once())
->method('send')
->willReturn(\json_decode($json, false));

return new BotApi('000000000:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', $stub, $this->getNormalizer());
}

/**
* @param $result
*/
Expand All @@ -33,4 +48,9 @@ protected function getMethod(): MethodInterface
{
return $this->getMockBuilder(MethodInterface::class)->getMock();
}

protected function getResource($filename): string
{
return \file_get_contents(\sprintf('%s/resources/%s.json', __DIR__, $filename));
}
}
49 changes: 14 additions & 35 deletions tests/Type/UserProfilePhotosTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,29 @@ class UserProfilePhotosTypeTest extends TypeTestCase
{
public function testEncode()
{
$result = [
'total_count' => 1,
'photos' => [
[
[
'file_id' => 'AgADBQADyKcxG5hmrglZIi4CcakAAf4_7r0yAAQ735Gp9Icu6KADAQABAg',
'file_uniqueId' => 'fileUniqueId',
'file_size' => 91518,
'width' => 640,
'height' => 640,
],
[
'file_id' => 'AgADBQADyKcxG5hmrglZIi4CcakAAf4_7r0yAASzMopVG_fq_qEDAQABAg',
'file_uniqueId' => 'fileUniqueId',
'file_size' => 219720,
'width' => 1050,
'height' => 1050,
],
],
],
];
$result = $this->getResource('user-profile-photos');

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

$result = \json_decode($result, true)['result'];

$this->assertEquals($type->totalCount, $result['total_count']);
$this->assertEquals(\count($type->photos), 1);
$this->assertEquals(\count($type->photos[0]), 2);
$this->assertInstanceOf(PhotoSizeType::class, $type->photos[0][0]);
$this->assertInstanceOf(PhotoSizeType::class, $type->photos[0][1]);
$this->assertEquals($type->photos[0][0]->fileId, $result['photos'][0][0]['file_id']);
$this->assertEquals($type->photos[0][0]->fileUniqueId, $result['photos'][0][0]['file_uniqueId']);
$this->assertEquals($type->photos[0][0]->fileSize, $result['photos'][0][0]['file_size']);
$this->assertEquals($type->photos[0][0]->width, $result['photos'][0][0]['width']);
$this->assertEquals($type->photos[0][0]->height, $result['photos'][0][0]['height']);
$this->assertEquals($type->photos[0][1]->fileId, $result['photos'][0][1]['file_id']);
$this->assertEquals($type->photos[0][1]->fileUniqueId, $result['photos'][0][0]['file_uniqueId']);
$this->assertEquals($type->photos[0][1]->fileSize, $result['photos'][0][1]['file_size']);
$this->assertEquals($type->photos[0][1]->width, $result['photos'][0][1]['width']);
$this->assertEquals($type->photos[0][1]->height, $result['photos'][0][1]['height']);
$this->assertEquals(\count($type->photos[0]), 3);

foreach ($result['photos'][0] as $index => $size) {
$this->assertInstanceOf(PhotoSizeType::class, $type->photos[0][$index]);
$this->assertEquals($type->photos[0][$index]->fileId, $size['file_id']);
$this->assertEquals($type->photos[0][$index]->fileUniqueId, $size['file_unique_id']);
$this->assertEquals($type->photos[0][$index]->fileSize, $size['file_size']);
$this->assertEquals($type->photos[0][$index]->width, $size['width']);
$this->assertEquals($type->photos[0][$index]->height, $size['height']);
}
}

public function getType($result): UserProfilePhotosType
{
$botApi = $this->getBot($result);
$botApi = $this->getBotFromJson($result);

return $botApi->call($this->getMethod(), UserProfilePhotosType::class);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Type/resources/user-profile-photos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"ok": true,
"result": {
"total_count": 1,
"photos": [
[
{
"file_id": "secret",
"file_unique_id": "secret",
"file_size": 12930,
"width": 160,
"height": 160
},
{
"file_id": "secret",
"file_unique_id": "secret",
"file_size": 40592,
"width": 320,
"height": 320
},
{
"file_id": "secret",
"file_unique_id": "secret",
"file_size": 116195,
"width": 640,
"height": 640
}
]
]
}
}