diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f59dfe..6b234ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. + + +## 1.3.1 - 2020-01-30 + +### Fixed +- fixed stdObject property call inUserProfilePhotosNormalizer, updated test for it. ## 1.3.0 - 2020-01-25 diff --git a/src/ApiClient.php b/src/ApiClient.php index 8f214ae..83b7d26 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -42,10 +42,6 @@ class ApiClient implements ApiClientInterface /** * ApiApiClient constructor. - * - * @param RequestFactoryInterface $requestFactory - * @param StreamFactoryInterface $streamFactory - * @param ClientInterface $client */ public function __construct( RequestFactoryInterface $requestFactory, @@ -58,9 +54,6 @@ public function __construct( } /** - * @param string $method - * @param BotApiRequestInterface $apiRequest - * * @throws ClientExceptionInterface * * @return mixed @@ -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( @@ -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 { @@ -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 { @@ -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 { diff --git a/src/Normalizer/UserProfilePhotosNormalizer.php b/src/Normalizer/UserProfilePhotosNormalizer.php index 9ac276e..3a82305 100644 --- a/src/Normalizer/UserProfilePhotosNormalizer.php +++ b/src/Normalizer/UserProfilePhotosNormalizer.php @@ -28,9 +28,6 @@ class UserProfilePhotosNormalizer implements DenormalizerInterface /** * UserProfilePhotosNormalizer constructor. - * - * @param NormalizerInterface $objectNormalizer - * @param ArrayDenormalizer $arrayDenormalizer */ public function __construct(NormalizerInterface $objectNormalizer, ArrayDenormalizer $arrayDenormalizer) { @@ -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); } @@ -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 { diff --git a/tests/Method/GetUserProfilePhotosMethodTest.php b/tests/Method/GetUserProfilePhotosMethodTest.php index 024a097..67bc713 100644 --- a/tests/Method/GetUserProfilePhotosMethodTest.php +++ b/tests/Method/GetUserProfilePhotosMethodTest.php @@ -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])); diff --git a/tests/Type/TypeTestCase.php b/tests/Type/TypeTestCase.php index 95234cb..dcaba09 100644 --- a/tests/Type/TypeTestCase.php +++ b/tests/Type/TypeTestCase.php @@ -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 */ @@ -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)); + } } diff --git a/tests/Type/UserProfilePhotosTypeTest.php b/tests/Type/UserProfilePhotosTypeTest.php index 41bc7d3..e8232fb 100644 --- a/tests/Type/UserProfilePhotosTypeTest.php +++ b/tests/Type/UserProfilePhotosTypeTest.php @@ -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); } diff --git a/tests/Type/resources/user-profile-photos.json b/tests/Type/resources/user-profile-photos.json new file mode 100644 index 0000000..c0a93ae --- /dev/null +++ b/tests/Type/resources/user-profile-photos.json @@ -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 + } + ] + ] + } +}