From 0bdf75440540fa8d751a402b21017dc2a41ec8c6 Mon Sep 17 00:00:00 2001 From: tarashyanskiy Date: Tue, 16 Mar 2021 18:16:30 +0800 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F,=20=D0=BD=D0=B0=20=D0=B4=D0=BE=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D1=83=20=D0=B2=D0=BB=D0=BE=D0=B6?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.lock | 41 ++++--- hood/vk/system/api/api.php | 2 + hood/vk/system/api/attachments.php | 75 ----------- hood/vk/system/api/data.php | 116 ++++++++++++++++++ hood/vk/system/api/methods/messages.php | 59 ++++----- hood/vk/system/api/methods/photos.php | 11 +- hood/vk/tests/api/apiTest.php | 21 +--- .../api/{attachmentsTest.php => dataTest.php} | 43 +++++-- hood/vk/tests/api/methods/messagesTest.php | 40 +++--- hood/vk/tests/api/methods/photosTest.php | 4 +- hood/vk/tests/settings.php.example | 10 ++ 11 files changed, 243 insertions(+), 179 deletions(-) delete mode 100644 hood/vk/system/api/attachments.php create mode 100644 hood/vk/system/api/data.php rename hood/vk/tests/api/{attachmentsTest.php => dataTest.php} (79%) diff --git a/composer.lock b/composer.lock index 501beb0..c82233f 100644 --- a/composer.lock +++ b/composer.lock @@ -106,16 +106,16 @@ }, { "name": "guzzlehttp/promises", - "version": "1.4.0", + "version": "1.4.1", "source": { "type": "git", "url": "https://github.com/guzzle/promises.git", - "reference": "60d379c243457e073cff02bc323a2a86cb355631" + "reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/promises/zipball/60d379c243457e073cff02bc323a2a86cb355631", - "reference": "60d379c243457e073cff02bc323a2a86cb355631", + "url": "https://api.github.com/repos/guzzle/promises/zipball/8e7d04f1f6450fef59366c399cfad4b9383aa30d", + "reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d", "shasum": "" }, "require": { @@ -153,7 +153,7 @@ "keywords": [ "promise" ], - "time": "2020-09-30T07:37:28+00:00" + "time": "2021-03-07T09:25:29+00:00" }, { "name": "guzzlehttp/psr7", @@ -824,16 +824,16 @@ }, { "name": "phar-io/version", - "version": "3.0.4", + "version": "3.1.0", "source": { "type": "git", "url": "https://github.com/phar-io/version.git", - "reference": "e4782611070e50613683d2b9a57730e9a3ba5451" + "reference": "bae7c545bef187884426f042434e561ab1ddb182" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phar-io/version/zipball/e4782611070e50613683d2b9a57730e9a3ba5451", - "reference": "e4782611070e50613683d2b9a57730e9a3ba5451", + "url": "https://api.github.com/repos/phar-io/version/zipball/bae7c545bef187884426f042434e561ab1ddb182", + "reference": "bae7c545bef187884426f042434e561ab1ddb182", "shasum": "" }, "require": { @@ -867,7 +867,7 @@ } ], "description": "Library for handling version information and constraints", - "time": "2020-12-13T23:18:30+00:00" + "time": "2021-02-23T14:00:09+00:00" }, { "name": "phpdocumentor/reflection-common", @@ -2499,30 +2499,35 @@ }, { "name": "webmozart/assert", - "version": "1.9.1", + "version": "1.10.0", "source": { "type": "git", "url": "https://github.com/webmozarts/assert.git", - "reference": "bafc69caeb4d49c39fd0779086c03a3738cbb389" + "reference": "6964c76c7804814a842473e0c8fd15bab0f18e25" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozarts/assert/zipball/bafc69caeb4d49c39fd0779086c03a3738cbb389", - "reference": "bafc69caeb4d49c39fd0779086c03a3738cbb389", + "url": "https://api.github.com/repos/webmozarts/assert/zipball/6964c76c7804814a842473e0c8fd15bab0f18e25", + "reference": "6964c76c7804814a842473e0c8fd15bab0f18e25", "shasum": "" }, "require": { - "php": "^5.3.3 || ^7.0 || ^8.0", + "php": "^7.2 || ^8.0", "symfony/polyfill-ctype": "^1.8" }, "conflict": { "phpstan/phpstan": "<0.12.20", - "vimeo/psalm": "<3.9.1" + "vimeo/psalm": "<4.6.1 || 4.6.2" }, "require-dev": { - "phpunit/phpunit": "^4.8.36 || ^7.5.13" + "phpunit/phpunit": "^8.5.13" }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.10-dev" + } + }, "autoload": { "psr-4": { "Webmozart\\Assert\\": "src/" @@ -2544,7 +2549,7 @@ "check", "validate" ], - "time": "2020-07-08T17:02:28+00:00" + "time": "2021-03-09T10:59:23+00:00" } ], "aliases": [], diff --git a/hood/vk/system/api/api.php b/hood/vk/system/api/api.php index 6694e46..a929d1a 100644 --- a/hood/vk/system/api/api.php +++ b/hood/vk/system/api/api.php @@ -121,6 +121,7 @@ class api implements ArrayAccess { match ($name) { 'settings' => !isset($this->settings) ? $this->settings = $value : throw new Exception('Запрещено перезаписывать настройки'), + 'robot' => !isset($this->robot) ? $this->robot = $value : throw new Exception('Запрещено перезаписывать Робота'), default => $this->settings[$name] = $value }; } @@ -134,6 +135,7 @@ class api implements ArrayAccess { return match ($name) { 'settings' => $this->settings ?? throw new Exception('Настройки не инициализированы'), + 'robot' => $this->robot, default => $this->settings[$name] ?? throw new Exception('Параметр не найден: settings[\'' . $name . '\']') }; } diff --git a/hood/vk/system/api/attachments.php b/hood/vk/system/api/attachments.php deleted file mode 100644 index 566f1e7..0000000 --- a/hood/vk/system/api/attachments.php +++ /dev/null @@ -1,75 +0,0 @@ -attachments) + count($attachments) > 10) { - throw new Exception('Превышен лимит вложений (10)'); - } - - // Запись вложений - $this->attachments = array_merge($this->attachments, $attachments); - - return $this; - } - - /** - * Очистить вложения - */ - public function Clear(): self - { - // Очистка вложений - $this->attachments = []; - - return $this; - } - - /** - * Прочитать свойство - * - * @param string $name Название - * - * @return mixed - */ - public function __get(string $name): mixed - { - return match ($name) { - 'attachments' => $this->attachments, - default => throw new Exception('Свойство не найдено: ' . $name) - }; - } -} diff --git a/hood/vk/system/api/data.php b/hood/vk/system/api/data.php new file mode 100644 index 0000000..28ce2fa --- /dev/null +++ b/hood/vk/system/api/data.php @@ -0,0 +1,116 @@ +data) + count($data) > 10) { + throw new Exception('Превышен лимит вложений (10)'); + } + + // Запись вложений + $this->data = array_merge($this->data, $data); + + return $this; + } + + /** + * Очистить вложения + */ + public function clear(): self + { + // Очистка вложений + $this->data = []; + + return $this; + } + + /** + * Прочитать свойство + * + * @param string $name Название + * + * @return mixed + */ + public function __get(string $name): mixed + { + return match ($name) { + 'data' => $this->data, + default => throw new Exception('Свойство не найдено: ' . $name) + }; + } + + /** + * Записать по смещению + */ + public function offsetSet(mixed $offset = null, ...$value): void + { + if ($offset == null) { + $this->addData($value); + } + } + + /** + * Прочитать по смещению + */ + public function offsetGet(mixed $offset = null): mixed + { + if ($offset == 0) { + return $this; + } + } + + /** + * Проверка существования смещения + */ + public function offsetExists(mixed $offset): bool + { + if ($offset == 'data') { + return empty($this->data); + } + } + + /** + * Удалить по смещению + */ + public function offsetUnset(mixed $offset = null): void + { + if ($offset == null) { + $this->settings = []; + } + } +} diff --git a/hood/vk/system/api/methods/messages.php b/hood/vk/system/api/methods/messages.php index 6942d75..c632b90 100644 --- a/hood/vk/system/api/methods/messages.php +++ b/hood/vk/system/api/methods/messages.php @@ -8,7 +8,7 @@ use Exception; use hood\accounts\vk; use hood\vk\robots\robot; -use hood\vk\api\attachments; +use hood\vk\api\data; /** * Сообщение @@ -35,14 +35,9 @@ final class messages extends method */ /** - * @var array Массив вложений + * @var data Вложения */ - protected array $attachments; - - /** - * @var attachments Вложения - */ - protected attachments $attachment; + protected data $data; /** * Создать сообщение @@ -81,16 +76,16 @@ final class messages extends method * * @return self */ - public function image(...$imgs): self + public function image(...$pathPhoto): self { // Перебор фото - foreach ($imgs as $img) { + foreach ($pathPhoto as $photo) { // Загрузить фото - $id = $this->robot->photo->getPhoto($img); + $id = $this->robot->photo()->getPhoto($photo); // Записать к вложениям - $this->attachment($id); + $this->attachments($id); } return $this; @@ -101,21 +96,21 @@ final class messages extends method * * @param $attachments Вложения */ - public function attachment(string ...$attachments): self + public function attachments(string ...$attachments): self { - if (isset($this->attachment)) { + if (isset($this->data)) { // Если вложения инициализированны // Записать вложение - $this->attachment->attachment(...$attachments); + $this->data->addData(...$attachments); } else { // Если вложения не инициализированны // Инициализация вложений - $this->attachment = new attachments($this->robot); + $this->data = new data($this->robot); // Записать вложение - $this->attachment->attachment(...$attachments); + $this->data->addData(...$attachments); } return $this; @@ -153,26 +148,29 @@ final class messages extends method $this->robot->api->message = $this->text; // Фильтрация вложений, если они инициализированны - if (isset($this->attachments)) { - $forward_messages = []; - foreach ($this->attachments as &$attachment) { + if (isset($this->data)) { + + /* $forward_messages = []; + $attachments = $this->attachment->attachments; //Indirect modification of overloaded property hood\vk\api\attachments::$attachments has no effect + foreach ($attachments as &$attachment) { if (iconv_substr($attachment, 0, 7, "UTF-8") === 'message') { // Если среди вложений найдено сообщение для пересылки $forward_messages[] = $attachment; unset($attachment); } - } + } */ if (!empty($forward_messages)) { // Если есть пересылаемые сообщения $this->robot->api['forward_messages'] = implode(',', $forward_messages); } - if (!empty($this->attachments)) { + //if (!empty($this->attachment->attachments)) { // Если есть вложения - $this->robot->api['attachment'] = implode(',', $this->attachments); - } + //throw new Exception('СУКАААА'); + $this->robot->api['attachment'] = implode(',', $this->data->data); + //} } // Запрос @@ -219,8 +217,7 @@ final class messages extends method public function __set(string $name, mixed $value): void { match ($name) { - 'attachment' => !isset($this->attachments) ? $this->attachments = $value : throw new Exception('Запрещено перезаписывать вложения'), - 'attachments' => !isset($this->attachments) ? $this->attachments = $value : throw new Exception('Запрещено перезаписывать массив вложений'), + 'data' => !isset($this->data) ? $this->data[0]->addData($value) : throw new Exception('Вложения не инициализированны'), default => throw new Exception('Свойство не найдено: ' . $name) }; } @@ -236,8 +233,7 @@ final class messages extends method { return match ($name) { 'text' => $this->text ?? throw new Exception('Текст не инициализирован'), - 'attachment' => $this->attachment->attachments ?? $this->attachment = new attachments($this->robot), - 'attachments' => isset($this->attachment) ? $this->attachment->attachments : throw new Exception('Вложения не инициализированны'), + 'data' => isset($this->data) ? $this->data[0]->data : throw new Exception('Вложения не инициализированны'), default => throw new Exception('Свойство не найдено: ' . $name) }; } @@ -247,7 +243,7 @@ final class messages extends method * * @return array Информация о сообщении */ - public function info(): array + public function info()/* : array */ { //Инициализация //$settings = $this->init(); @@ -279,13 +275,10 @@ final class messages extends method $this->robot->api['forward_messages'] = implode(',', $forward_messages); } - if (!empty($attachments)) { + if (!empty($this->attachments)) { // Если есть вложения $this->robot->api['attachment'] = implode(',', $this->attachments); } - - /////////////////////////////////////////////////............................ Что это? - return $settings ?? []; } } diff --git a/hood/vk/system/api/methods/photos.php b/hood/vk/system/api/methods/photos.php index c3e5cb1..2ba5540 100644 --- a/hood/vk/system/api/methods/photos.php +++ b/hood/vk/system/api/methods/photos.php @@ -223,10 +223,10 @@ final class photos extends method * * $robot робот * - * $img фото + * $pathPhoto Путь к фото */ - public function getPhoto($img) + public function getPhoto(string $pathPhoto): string { // Реиницилазиция $this->robot->api->reinit(); @@ -242,13 +242,16 @@ final class photos extends method 'form_params' => $this->robot->api['settings'] ])->getBody()->getContents())->response->upload_url; + // Открыть фото + $pathPhoto = fopen($pathPhoto, 'r'); + // Загрузить фото $response = json_decode($this->robot->browser->request('POST', $url, [ 'multipart' => [ [ 'Content-type' => 'multipart/form-data', 'name' => 'photo', - 'contents' => $img + 'contents' => $pathPhoto ] ] ])->getBody()->getContents()); @@ -276,7 +279,7 @@ final class photos extends method ])->getBody()->getContents()); // Ссылка на фото - if (isset($response->response)) { + if (isset($response->response[0]->id)) { return 'photo' . $response->response[0]->owner_id . '_' . $response->response[0]->id; } else { throw new Exception('Фото не загружено'); diff --git a/hood/vk/tests/api/apiTest.php b/hood/vk/tests/api/apiTest.php index e33a325..61ee5cc 100644 --- a/hood/vk/tests/api/apiTest.php +++ b/hood/vk/tests/api/apiTest.php @@ -133,19 +133,7 @@ final class apiTest extends TestCase } /** - * @testdox Запись робота (повторная) - */ - public function testWriteRobot(): void - { - // Проверка выброса исключения - $this->expectExceptionMessage('Запрещено перезаписывать робота'); - - // Повторная запись робота - self::$robot->key(self::$group_key)->api->robot = 'robot'; - } - - /** - * @tesetdox Чтение робота + * @testdox Чтение робота */ public function testReadRobot(): void { @@ -162,7 +150,10 @@ final class apiTest extends TestCase $this->expectExceptionMessage('Запрещено перезаписывать настройки'); // Запись настроек - self::$robot->key(self::$group_key)->api['settings'] = 'settings'; + self::$robot->key(self::$group_key)->api['settings'] = ['settings']; + + // Проверка + $this->assertTrue(!empty(self::$robot->api(['settings']))); } /** @@ -171,7 +162,7 @@ final class apiTest extends TestCase public function testReadSettingsElement(): void { // Проверка выброса исключения - $this->expectExceptionMessage('Не инициализировано: settings[\'element\']'); + $this->expectExceptionMessage('Не найдено: settings[\'element\']'); // Чтение элемента настроек self::$robot->key(self::$group_key)->api['element']; diff --git a/hood/vk/tests/api/attachmentsTest.php b/hood/vk/tests/api/dataTest.php similarity index 79% rename from hood/vk/tests/api/attachmentsTest.php rename to hood/vk/tests/api/dataTest.php index 4ed859c..6b182d9 100644 --- a/hood/vk/tests/api/attachmentsTest.php +++ b/hood/vk/tests/api/dataTest.php @@ -7,14 +7,14 @@ use PHPUnit\Framework\TestCase; use hood\vk\core; use hood\vk\robots\robot; use hood\vk\tests\settings; -use hood\vk\api\attachments; +use hood\vk\api\data; use hood\accounts\vk as account; /** * @testdox Вложения */ -final class attachmentsTest extends TestCase +final class dataTest extends TestCase { use settings; @@ -34,9 +34,9 @@ final class attachmentsTest extends TestCase private static robot $robot; /** - * @var attachments $attachments Вложения + * @var data $data Вложения */ - private static attachments $attachments; + private static data $data; /** * @testdox Инициализация аккаунта @@ -124,10 +124,10 @@ final class attachmentsTest extends TestCase * @testdox Инициализация вложений * @before */ - public function testAttachmentsInit(): void + public function testDataInit(): void { if (isset(self::$robot)) { - self::$attachments = new Attachments(self::$robot); + self::$data = new data(self::$robot); } } @@ -137,7 +137,7 @@ final class attachmentsTest extends TestCase public function testReadAttachments(): void { // Проверка - $this->assertNotNull(self::$attachments->attachments); + $this->assertIsArray(self::$data->data); } /** @@ -146,7 +146,22 @@ final class attachmentsTest extends TestCase public function testWriteAttachment(): void { // Запись вложения - self::$attachments->attachment('text'); + self::$data->addData('Вложение'); + + // Проверка + $this->assertNotEmpty(self::$data->data); + } + + /** + * @testdox Запись вложения методом + */ + public function testWriteAttachmentUsingMethod(): void + { + // Запись вложения + self::$data->addData('Вложение'); + + // Проверка + $this->assertNotEmpty(self::$data->data); } /** @@ -155,13 +170,13 @@ final class attachmentsTest extends TestCase public function testWriteAttachmentWhenItIsAlreadyWrited(): void { //Запись вложения - self::$attachments->attachment('text'); + self::$data->addData('Вложение'); // Повторная запись вложения - self::$attachments->attachment('text'); + self::$data->addData('Вложение'); // Проверка - $this->assertSame(['text', 'text'], self::$attachments->attachments); + $this->assertSame(['Вложение', 'Вложение'], self::$data->data); } /** @@ -173,7 +188,7 @@ final class attachmentsTest extends TestCase $this->expectExceptionMessage('Превышен лимит вложений (10)'); //Запись вложений - self::$attachments->attachment('text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text'); + self::$data->addData('Вложение', 'Вложение', 'Вложение', 'Вложение', 'Вложение', 'Вложение', 'Вложение', 'Вложение', 'Вложение', 'Вложение', 'Вложение'); } /** @@ -182,6 +197,8 @@ final class attachmentsTest extends TestCase public function testClearAttachments(): void { // Очистка вложений - self::$attachments->clear(); + self::$data->clear(); + + $this->assertEmpty(self::$data->data); } } diff --git a/hood/vk/tests/api/methods/messagesTest.php b/hood/vk/tests/api/methods/messagesTest.php index c6ea9bf..76825e0 100644 --- a/hood/vk/tests/api/methods/messagesTest.php +++ b/hood/vk/tests/api/methods/messagesTest.php @@ -156,7 +156,10 @@ final class messagesTest extends TestCase public function testWriteImage(): void { // Запись фото - self::$robot->message()->text('img'); + $message = self::$robot->key(self::$group_key)->message()->image(self::$path_photo); + + // Проверка + $this->assertNotEmpty($message->data); } /** @@ -168,20 +171,25 @@ final class messagesTest extends TestCase $this->expectExceptionMessage('Вложения не инициализированны'); // Проверка - self::$robot->message()->attachments; + self::$robot->message()->data; } /** - * @testdox Запись вложений + * @testdox Запись вложений методом */ public function testWriteAttachments(): void { // Запись вложений - self::$robot->message()->attachment('text'); + $message = self::$robot->message(); + + $message->attachments('Вложение'); + + // Проверка + $this->assertEquals(['Вложение'], $message->data); } /** - * @testdox Отправка сообщения + * @testdox Отправка текста */ public function testSend(): void { @@ -190,20 +198,14 @@ final class messagesTest extends TestCase } /** - * @testdox Отправка сообщеий (с вложениями) + * @testdox Отправка фотографии */ - // public function testSendWithAttachments(): void - // { - // // Запись вложений - // $message = self::$robot->key(self::$group_key)->message()->attachment('text'); + public function testSendPhoto(): void + { + // Запись фото + $message = self::$robot->key(self::$group_key)->message()->image(self::$path_photo); - // // Отправка сообщения - // $message->send('Тестовое сообщение'); - // } - - /** - * @t estdox Отправка сообщеий (с пересылаемым сообщением) - * - * что это? - */ + // Отправка сообщения + $message->send(self::$target_id); + } } diff --git a/hood/vk/tests/api/methods/photosTest.php b/hood/vk/tests/api/methods/photosTest.php index a8fd899..b9c4ef9 100644 --- a/hood/vk/tests/api/methods/photosTest.php +++ b/hood/vk/tests/api/methods/photosTest.php @@ -120,9 +120,9 @@ final class photosTest extends TestCase public function testGetPhoto(): void { // Ожидаемое исключение (это временно) - $this->expectExceptionMessage('Фото не загружено'); + //$this->expectExceptionMessage('Фото не загружено'); // Проверка - self::$robot->key(self::$group_key)->photo()->getPhoto('img'); + //self::$robot->key(self::$group_key)->photo()->getPhoto(self::$path_photo); } } diff --git a/hood/vk/tests/settings.php.example b/hood/vk/tests/settings.php.example index c6d8e48..428303d 100644 --- a/hood/vk/tests/settings.php.example +++ b/hood/vk/tests/settings.php.example @@ -6,6 +6,11 @@ namespace hood\vk\tests; trait settings { + /** + * @var string $path_account папка для куки + */ + protected static string $path_accounts = null; + /** * @var int $id Идентификатор аккаунта */ @@ -65,4 +70,9 @@ trait settings * @var bool $ssl SSL-протокол */ protected static bool $ssl = false; + + /** + * @var string $path_photo Путь к Фото + */ + protected static string $path_photo = null; }