diff --git a/composer.json b/composer.json index 20f442a..46763eb 100755 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "homepage": "https://github.com/VKCOM/vk-php-sdk", "license": "MIT", "require": { - "php": ">=7.1" + "php": ">=7.1", + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": "^6", diff --git a/src/VK/Tool/Messages/KeyboardConverter/Contracts/Convertible/ArrayInterface.php b/src/VK/Tool/Messages/KeyboardConverter/Contracts/Convertible/ArrayInterface.php new file mode 100644 index 0000000..5cb1c71 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Contracts/Convertible/ArrayInterface.php @@ -0,0 +1,7 @@ +factory = $factory; + } + + public function keyboard(): Object\Keyboard\Converter + { + if ($this->keyboardConverter === null) { + $this->keyboardConverter = new Object\Keyboard\Converter($this->factory->getKeyboardFactory()->getTypeFactory()); + } + + return $this->keyboardConverter; + } + + public function carousel(): Object\Template\Carousel\Converter + { + if ($this->carouselConverter === null) { + $this->carouselConverter = new Object\Template\Carousel\Converter( + $this->factory->getTemplateFactory()->getCarouselFactory()->getTypeFactory() + ); + } + + return $this->carouselConverter; + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Facade.php b/src/VK/Tool/Messages/KeyboardConverter/Facade.php new file mode 100644 index 0000000..454b536 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Facade.php @@ -0,0 +1,39 @@ +keyboard() + ->basic($callback, $oneTime); + } + + public static function createKeyboardInline(callable $callback): string + { + return self::getConverter() + ->keyboard() + ->inline($callback); + } + + public static function createCarousel(callable $callback): string + { + return self::getConverter() + ->carousel() + ->basic($callback); + } + + private static function getConverter(): Converter + { + return new Converter(self::getFactory()); + } + + private static function getFactory(): FactoryInterface + { + return new Factory(); + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Factory.php b/src/VK/Tool/Messages/KeyboardConverter/Factory.php new file mode 100644 index 0000000..66678d7 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Factory.php @@ -0,0 +1,37 @@ +templateFactory === null) { + $this->templateFactory = new Factory\Template($this->getKeyboardFactory()->getButtonFactory()); + } + + return $this->templateFactory; + } + + public function getKeyboardFactory(): Contracts\Factory\KeyboardInterface + { + if ($this->keyboardFactory === null) { + $this->keyboardFactory = new Factory\Keyboard(); + } + + return $this->keyboardFactory; + } + +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Factory/Keyboard.php b/src/VK/Tool/Messages/KeyboardConverter/Factory/Keyboard.php new file mode 100644 index 0000000..76feac3 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Factory/Keyboard.php @@ -0,0 +1,37 @@ +typeFactory === null) { + $this->typeFactory = new VkObject\Keyboard\Type\Factory($this->getButtonFactory()); + } + + return $this->typeFactory; + } + + public function getButtonFactory(): Contracts\Keyboard\Button\FactoryInterface + { + if ($this->buttonFactory === null) { + $this->buttonFactory = new VkObject\Keyboard\Button\Factory(); + } + + return $this->buttonFactory; + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Factory/Template.php b/src/VK/Tool/Messages/KeyboardConverter/Factory/Template.php new file mode 100644 index 0000000..0fa8e45 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Factory/Template.php @@ -0,0 +1,32 @@ +buttonFactory = $buttonFactory; + } + + public function getCarouselFactory(): Contracts\Factory\Template\CarouselInterface + { + if ($this->carouselFactory === null) { + $this->carouselFactory = new Template\Carousel($this->buttonFactory); + } + + return $this->carouselFactory; + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Factory/Template/Carousel.php b/src/VK/Tool/Messages/KeyboardConverter/Factory/Template/Carousel.php new file mode 100644 index 0000000..0ee71de --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Factory/Template/Carousel.php @@ -0,0 +1,50 @@ +buttonFactory = $buttonFactory; + } + + public function getTypeFactory(): Contracts\Template\Carousel\Type\FactoryInterface + { + if ($this->typeFactory === null) { + $this->typeFactory = new VkObject\Template\Carousel\Type\Factory( + $this->getElementFactory(), + $this->buttonFactory + ); + } + + return $this->typeFactory; + } + + public function getElementFactory(): Contracts\Template\Carousel\Element\FactoryInterface + { + if ($this->elementFactory === null) { + $this->elementFactory = new VkObject\Template\Carousel\Element\Factory(); + } + + return $this->elementFactory; + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/AbstractButton.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/AbstractButton.php new file mode 100644 index 0000000..12e8e1e --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/AbstractButton.php @@ -0,0 +1,30 @@ +button[self::KEY_ACTION] = $this->convertAction(); + return $this->button; + } + + abstract protected function action(): array; + abstract protected function type(): string; + + private function convertAction(): array { + $action = $this->action(); + $action[self::KEY_ACTION_TYPE] = $this->type(); + return $action; + } +} diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/App.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/App.php new file mode 100644 index 0000000..0f8f746 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/App.php @@ -0,0 +1,46 @@ +label = $label; + $this->appId = $appId; + $this->ownerId = $ownerId; + $this->hash = $hash; + } + + protected function action(): array { + return [ + 'app_id' => $this->appId, + 'owner_id' => $this->ownerId, + 'hash' => $this->hash, + 'label' => $this->label + ]; + } + + protected function type(): string { + return 'open_app'; + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Factory.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Factory.php new file mode 100644 index 0000000..a97a07f --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Factory.php @@ -0,0 +1,29 @@ +label = $label; + $this->link = $link; + $this->payload = $payload; + } + + protected function action(): array { + return [ + self::KEY_LINK => $this->link, + self::KEY_LABEL => $this->label, + self::KEY_PAYLOAD => json_encode($this->payload, JSON_UNESCAPED_UNICODE) + ]; + } + + protected function type(): string { + return 'open_link'; + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Location.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Location.php new file mode 100644 index 0000000..f3a46f1 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Location.php @@ -0,0 +1,26 @@ +payload = $payload; + } + + protected function action(): array { + return [ + 'payload' => json_encode($this->payload, JSON_UNESCAPED_UNICODE) + ]; + } + + protected function type(): string { + return 'location'; + + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Pay.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Pay.php new file mode 100644 index 0000000..8c81a60 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Pay.php @@ -0,0 +1,27 @@ +hash = $hash; + } + + protected function action(): array { + return [ + self::KEY_HASH => $this->hash + ]; + } + + protected function type(): string { + return 'vkpay'; + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Text.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Text.php new file mode 100644 index 0000000..5324a19 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Button/Text.php @@ -0,0 +1,40 @@ +label = $label; + $this->payload = $payload; + $this->button[self::KEY_COLOR] = $color; + } + + protected function action(): array { + return [ + 'payload' => json_encode($this->payload, JSON_UNESCAPED_UNICODE), + 'label' => $this->label + ]; + } + + protected function type(): string { + return 'text'; + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Converter.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Converter.php new file mode 100644 index 0000000..eba7deb --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Converter.php @@ -0,0 +1,28 @@ +typeFactory = $typeFactory; + } + + public function basic(callable $callback, bool $oneTime = true): string + { + return $this->typeFactory->basic($callback, $oneTime)->convert(); + } + + public function inline(callable $callback): string + { + return $this->typeFactory->inline($callback)->convert(); + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/AbstractType.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/AbstractType.php new file mode 100644 index 0000000..8fae4bd --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/AbstractType.php @@ -0,0 +1,39 @@ +keyboard[self::KEY_BUTTONS] = $this->convertButtonsRecursive($this->buttons); + return json_encode($this->keyboard, JSON_UNESCAPED_UNICODE); + } + + private function convertButtonsRecursive(array $buttons): array { + return array_map(function ($button): array { + if ($button instanceof AbstractButton) { + return $button->convert(); + } elseif (is_array($button)) { + return $this->convertButtonsRecursive($button); + } + + return []; + }, $buttons); + } + +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/Basic.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/Basic.php new file mode 100644 index 0000000..31c6087 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/Basic.php @@ -0,0 +1,20 @@ +buttons = $buttons; + $this->keyboard[self::KEY_ONE_TIME] = $oneTime; + } + +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/Factory.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/Factory.php new file mode 100644 index 0000000..0a7fb60 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/Factory.php @@ -0,0 +1,39 @@ +buttonFactory = $buttonFactory; + } + + public function basic(callable $callback, bool $oneTime = true): Basic + { + return new Basic($this->callUserFunctionWithButtonsFactory($callback), $oneTime); + } + + public function inline(callable $callback): Inline + { + return new Inline($this->callUserFunctionWithButtonsFactory($callback)); + } + + private function callUserFunctionWithButtonsFactory(callable $function): array + { + return call_user_func($function, $this->buttonFactory); + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/Inline.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/Inline.php new file mode 100644 index 0000000..ab40832 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Keyboard/Type/Inline.php @@ -0,0 +1,14 @@ +buttons = $buttons; + $this->keyboard[self::KEY_INLINE] = true; + } + +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Converter.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Converter.php new file mode 100644 index 0000000..7de3c1c --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Converter.php @@ -0,0 +1,23 @@ +carouselTypeFactory = $carouselTypeFactory; + } + + public function basic(callable $callback): string + { + return $this->carouselTypeFactory->basic($callback)->convert(); + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Element/AbstractElement.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Element/AbstractElement.php new file mode 100644 index 0000000..2906533 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Element/AbstractElement.php @@ -0,0 +1,64 @@ +element[self::KEY_BUTTONS] = array_map(function ($button): array { + if ($button instanceof AbstractButton) { + return $button->convert(); + } + + return $button; + }, $buttons); + + if ($title !== null) { + $this->element[self::KEY_TITLE] = $title; + } + + if ($description !== null) { + $this->element[self::KEY_DESCRIPTION] = $description; + } + + if ($photoId !== null) { + $this->element[self::KEY_PHOTO_ID] = $photoId; + } + + } + + public function convert(): array { + $this->element[self::KEY_ACTION] = $this->convertAction(); + return $this->element; + } + + abstract protected function action(): array; + abstract protected function type(): string; + + private function convertAction(): array { + $action = $this->action(); + $action[self::KEY_ACTION_TYPE] = $this->type(); + return $action; + } +} diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Element/Factory.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Element/Factory.php new file mode 100644 index 0000000..ff1b0d2 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Element/Factory.php @@ -0,0 +1,33 @@ +link = $link; + parent::__construct($buttons, $title, $description, $photoId); + } + + protected function action(): array { + return [ + self::KEY_LINK => $this->link + ]; + } + + protected function type(): string { + return 'open_link'; + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Element/OpenPhoto.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Element/OpenPhoto.php new file mode 100644 index 0000000..c114e35 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Element/OpenPhoto.php @@ -0,0 +1,21 @@ +template[self::KEY_ELEMENTS] = array_map(function (AbstractElement $element): array { + return $element->convert(); + }, $this->elements); + $this->template[self::KEY_TYPE] = self::TYPE; + return json_encode($this->template, JSON_UNESCAPED_UNICODE); + } + +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Type/Basic.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Type/Basic.php new file mode 100644 index 0000000..6cc0c39 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Type/Basic.php @@ -0,0 +1,16 @@ +elements = $elements; + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Type/Factory.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Type/Factory.php new file mode 100644 index 0000000..3e840e1 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Carousel/Type/Factory.php @@ -0,0 +1,41 @@ +elementFactory = $elementFactory; + $this->buttonFactory = $buttonFactory; + } + + public function basic(callable $callback): Basic + { + return new Basic($this->callUserFunctionWithElementsFactory($callback)); + } + + private function callUserFunctionWithElementsFactory(callable $function): array + { + return call_user_func($function, $this->elementFactory, $this->buttonFactory); + } +} \ No newline at end of file diff --git a/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Converter.php b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Converter.php new file mode 100644 index 0000000..00f3118 --- /dev/null +++ b/src/VK/Tool/Messages/KeyboardConverter/Object/Template/Converter.php @@ -0,0 +1,13 @@ +factory = $this->createMock(Contracts\FactoryInterface::class); + $this->converter = new Converter($this->factory); + } + + public function testKeyboard(): void + { + $keyboardFactory = $this->createMock(Contracts\Factory\KeyboardInterface::class); + $this->factory + ->method('getKeyboardFactory') + ->willReturn($keyboardFactory); + + $this->factory + ->expects($this->once()) + ->method('getKeyboardFactory'); + $keyboardFactory + ->expects($this->once()) + ->method('getTypeFactory'); + + $keyboard = $this->converter->keyboard(); + $this->assertSame($keyboard, $this->converter->keyboard()); + } + + public function testCarousel(): void + { + $templateFactory = $this->createMock(Contracts\Factory\TemplateInterface::class); + $this->factory + ->method('getTemplateFactory') + ->willReturn($templateFactory); + + $carouselFactory = $this->createMock(Contracts\Factory\Template\CarouselInterface::class); + $templateFactory + ->expects($this->once()) + ->method('getCarouselFactory') + ->willReturn($carouselFactory); + + $typeFactory = $this->createMock(Contracts\Template\Carousel\Type\FactoryInterface::class); + $carouselFactory + ->expects($this->once()) + ->method('getTypeFactory') + ->willReturn($typeFactory); + + $carousel = $this->converter->carousel(); + $this->assertSame($carousel, $this->converter->carousel()); + } +} diff --git a/tests/KeyboardConverter/Factory/KeyboardTest.php b/tests/KeyboardConverter/Factory/KeyboardTest.php new file mode 100644 index 0000000..f78de5f --- /dev/null +++ b/tests/KeyboardConverter/Factory/KeyboardTest.php @@ -0,0 +1,34 @@ +getMockBuilder(Factory\Keyboard::class) + ->setMethodsExcept(['getTypeFactory']) + ->getMock(); + $keyboard + ->expects($this->once()) + ->method('getButtonFactory'); + + $typeFactory = $keyboard->getTypeFactory(); + $this->assertInstanceOf(Keyboard\Type\Factory::class, $typeFactory); + $this->assertSame($typeFactory, $keyboard->getTypeFactory()); + } + + public function testGetButtonFactory(): void + { + $keyboard = new Factory\Keyboard(); + $buttonFactory = $keyboard->getButtonFactory(); + $this->assertInstanceOf(Keyboard\Button\Factory::class, $buttonFactory); + $this->assertSame($buttonFactory, $keyboard->getButtonFactory()); + } +} diff --git a/tests/KeyboardConverter/Factory/Template/CarouselTest.php b/tests/KeyboardConverter/Factory/Template/CarouselTest.php new file mode 100644 index 0000000..93a8601 --- /dev/null +++ b/tests/KeyboardConverter/Factory/Template/CarouselTest.php @@ -0,0 +1,46 @@ +buttonFactory = $this->createMock(FactoryInterface::class); + } + + public function testGetTypeFactory(): void + { + /** @var Carousel|MockObject $carousel */ + $carousel = $this->getMockBuilder(Carousel::class) + ->setConstructorArgs([$this->buttonFactory]) + ->setMethodsExcept(['getTypeFactory']) + ->getMock(); + $carousel + ->expects($this->once()) + ->method('getElementFactory'); + + $typeFactory = $carousel->getTypeFactory(); + $this->assertSame($typeFactory, $carousel->getTypeFactory()); + $this->assertInstanceOf(VkObject\Template\Carousel\Type\Factory::class, $typeFactory); + } + + public function testGetElementFactory(): void + { + $carousel = new Carousel($this->buttonFactory); + $elementFactory = $carousel->getElementFactory(); + $this->assertInstanceOf(VkObject\Template\Carousel\Element\Factory::class, $elementFactory); + $this->assertSame($elementFactory, $carousel->getElementFactory()); + } +} diff --git a/tests/KeyboardConverter/Factory/TemplateTest.php b/tests/KeyboardConverter/Factory/TemplateTest.php new file mode 100644 index 0000000..8b7c137 --- /dev/null +++ b/tests/KeyboardConverter/Factory/TemplateTest.php @@ -0,0 +1,36 @@ +buttonFactory = $this->createMock(FactoryInterface::class); + $this->template = new Template($this->buttonFactory); + } + + public function testGetCarouselFactory() + { + $carouselFactory = $this->template->getCarouselFactory(); + $this->assertSame($carouselFactory, $this->template->getCarouselFactory()); + $this->assertInstanceOf(Carousel::class, $carouselFactory); + } +} diff --git a/tests/KeyboardConverter/FactoryTest.php b/tests/KeyboardConverter/FactoryTest.php new file mode 100644 index 0000000..bb88a94 --- /dev/null +++ b/tests/KeyboardConverter/FactoryTest.php @@ -0,0 +1,33 @@ +factory = new Factory(); + } + + public function testGetTemplateFactory(): void + { + $templateFactory = $this->factory->getTemplateFactory(); + $this->assertInstanceOf(Factory\Template::class, $templateFactory); + $this->assertSame($templateFactory, $this->factory->getTemplateFactory()); + } + + public function testGetKeyboardFactory(): void + { + $keyboardFactory = $this->factory->getKeyboardFactory(); + $this->assertInstanceOf(Factory\Keyboard::class, $keyboardFactory); + $this->assertSame($keyboardFactory, $this->factory->getKeyboardFactory()); + } +} diff --git a/tests/KeyboardConverter/Object/AbstractJsonConvertibleTest.php b/tests/KeyboardConverter/Object/AbstractJsonConvertibleTest.php new file mode 100644 index 0000000..2cbb6c4 --- /dev/null +++ b/tests/KeyboardConverter/Object/AbstractJsonConvertibleTest.php @@ -0,0 +1,20 @@ +convertFullPathToJsonByFileName($name)); + } + + abstract protected function getJsonFolderPath(): string; + + private function convertFullPathToJsonByFileName(string $filename): string + { + return sprintf('%s/%s', $this->getJsonFolderPath(), $filename); + } +} \ No newline at end of file diff --git a/tests/KeyboardConverter/Object/Keyboard/Button/AbstractButtonTest.php b/tests/KeyboardConverter/Object/Keyboard/Button/AbstractButtonTest.php new file mode 100644 index 0000000..703f7c9 --- /dev/null +++ b/tests/KeyboardConverter/Object/Keyboard/Button/AbstractButtonTest.php @@ -0,0 +1,27 @@ +getButtonWithNullableFields(); + $convertedToJsonButtonFileName = $this->getConvertedToJsonButtonFileName(); + $this->assertSame( + json_decode($this->getExpectedJsonByFileName($convertedToJsonButtonFileName), true), + $button->convert() + ); + } + + protected function getJsonFolderPath(): string + { + return __DIR__.'\\converted_to_json_buttons'; + } + + abstract protected function getButtonWithNullableFields(): AbstractButton; + abstract protected function getConvertedToJsonButtonFileName(): string; +} \ No newline at end of file diff --git a/tests/KeyboardConverter/Object/Keyboard/Button/AppTest.php b/tests/KeyboardConverter/Object/Keyboard/Button/AppTest.php new file mode 100644 index 0000000..24ed227 --- /dev/null +++ b/tests/KeyboardConverter/Object/Keyboard/Button/AppTest.php @@ -0,0 +1,19 @@ +factory = new Button\Factory(); + } + + public function testApp(): void + { + $app = new Button\App('::test::',0, '::test::', '::test::'); + $this->assertEquals($app, $this->factory->app('::test::', 0, '::test::', '::test::')); + } + + public function testPay() + { + $pay = new Button\Pay('::test::'); + $this->assertEquals($pay, $this->factory->pay('::test::')); + } + + public function testLocation() + { + $location = new Button\Location([]); + $this->assertEquals($location, $this->factory->location([])); + } + + public function testLink() + { + $link = new Button\Link('::test::','::test::', []); + $this->assertEquals($link, $this->factory->link('::test::','::test::', [])); + } + + public function testText() + { + $text = new Button\Text('::test::',[]); + $this->assertEquals($text, $this->factory->text('::test::',[])); + } +} diff --git a/tests/KeyboardConverter/Object/Keyboard/Button/LinkTest.php b/tests/KeyboardConverter/Object/Keyboard/Button/LinkTest.php new file mode 100644 index 0000000..43b12ef --- /dev/null +++ b/tests/KeyboardConverter/Object/Keyboard/Button/LinkTest.php @@ -0,0 +1,20 @@ +factory = $this->createMock(Contracts\Keyboard\Type\FactoryInterface::class); + $this->converter = new VkObject\Keyboard\Converter($this->factory); + } + + public function testInline() + { + $inline = $this->createMock(VkObject\Keyboard\Type\Inline::class); + $this->factory + ->expects($this->once()) + ->method('inline') + ->willReturn($inline); + $inline + ->expects($this->once()) + ->method('convert'); + + $this->converter->inline(function (Contracts\Keyboard\Button\FactoryInterface $factory) { + return [ + [ + $factory->text('::test::', []) + ] + ]; + }); + } + + public function testBasic() + { + $basic = $this->createMock(VkObject\Keyboard\Type\Basic::class); + $this->factory + ->expects($this->once()) + ->method('basic') + ->willReturn($basic); + $basic + ->expects($this->once()) + ->method('convert'); + + $this->converter->basic(function () { + return [[]]; + }); + } +} diff --git a/tests/KeyboardConverter/Object/Keyboard/Type/AbstractTypeTest.php b/tests/KeyboardConverter/Object/Keyboard/Type/AbstractTypeTest.php new file mode 100644 index 0000000..7de0161 --- /dev/null +++ b/tests/KeyboardConverter/Object/Keyboard/Type/AbstractTypeTest.php @@ -0,0 +1,27 @@ +getTypeWithNullableFields(); + $convertedToJsonTypeFileName = $this->getConvertedToJsonTypeFileName(); + $this->assertSame( + json_decode($this->getExpectedJsonByFileName($convertedToJsonTypeFileName), true), + json_decode($type->convert(), true) + ); + } + + protected function getJsonFolderPath(): string + { + return __DIR__ . '/converted_to_json_types'; + } + + abstract protected function getTypeWithNullableFields(): AbstractType; + abstract protected function getConvertedToJsonTypeFileName(): string; +} \ No newline at end of file diff --git a/tests/KeyboardConverter/Object/Keyboard/Type/BasicTest.php b/tests/KeyboardConverter/Object/Keyboard/Type/BasicTest.php new file mode 100644 index 0000000..adf7c1f --- /dev/null +++ b/tests/KeyboardConverter/Object/Keyboard/Type/BasicTest.php @@ -0,0 +1,19 @@ +buttonFactory = $this->createMock(Button\FactoryInterface::class); + $this->typeFactory = new Type\Factory($this->buttonFactory); + } + + + public function testInline() + { + $mockWithCallback = $this->getPreparedMockWithCallbackByCallbackName('testInlineCallback'); + $this->typeFactory->inline([$mockWithCallback, 'testInlineCallback']); + } + + public function testBasic() + { + $mockWithCallback = $this->getPreparedMockWithCallbackByCallbackName('testBasicCallback'); + $this->typeFactory->basic([$mockWithCallback, 'testBasicCallback']); + } + + private function getPreparedMockWithCallbackByCallbackName(string $name): MockObject + { + $mockWithCallback = $this->getMockBuilder(stdClass::class) + ->setMethods([$name]) + ->getMock(); + $mockWithCallback->expects($this->once()) + ->method($name) + ->willReturn([]); + + return $mockWithCallback; + } +} diff --git a/tests/KeyboardConverter/Object/Keyboard/Type/InlineTest.php b/tests/KeyboardConverter/Object/Keyboard/Type/InlineTest.php new file mode 100644 index 0000000..9062cf8 --- /dev/null +++ b/tests/KeyboardConverter/Object/Keyboard/Type/InlineTest.php @@ -0,0 +1,19 @@ +factory = $this->createMock(Contracts\Template\Carousel\Type\FactoryInterface::class); + $this->converter = new Converter($this->factory); + } + + public function testBasic() + { + $basic = $this->createMock(Carousel\Type\Basic::class); + $basic->expects($this->once()) + ->method('convert'); + + $this->factory->expects($this->once()) + ->method('basic') + ->willReturn($basic); + + $this->converter->basic(function () { + return []; + }); + } +} diff --git a/tests/KeyboardConverter/Object/Template/Carousel/Element/AbstractElementTest.php b/tests/KeyboardConverter/Object/Template/Carousel/Element/AbstractElementTest.php new file mode 100644 index 0000000..3759e39 --- /dev/null +++ b/tests/KeyboardConverter/Object/Template/Carousel/Element/AbstractElementTest.php @@ -0,0 +1,27 @@ +getElementWithNullableFields(); + $convertedToJsonButtonFileName = $this->getConvertedToJsonElementFileName(); + $this->assertSame( + json_decode($this->getExpectedJsonByFileName($convertedToJsonButtonFileName), true), + $element->convert() + ); + } + + protected function getJsonFolderPath(): string + { + return __DIR__.'\\converted_to_json_elements'; + } + + abstract protected function getElementWithNullableFields(): AbstractElement; + abstract protected function getConvertedToJsonElementFileName(): string; +} \ No newline at end of file diff --git a/tests/KeyboardConverter/Object/Template/Carousel/Element/OpenLinkTest.php b/tests/KeyboardConverter/Object/Template/Carousel/Element/OpenLinkTest.php new file mode 100644 index 0000000..c6455ee --- /dev/null +++ b/tests/KeyboardConverter/Object/Template/Carousel/Element/OpenLinkTest.php @@ -0,0 +1,18 @@ +getTypeWithNullableFields(); + $convertedToJsonTypeFileName = $this->getConvertedToJsonTypeFileName(); + $this->assertSame( + json_decode($this->getExpectedJsonByFileName($convertedToJsonTypeFileName), true), + json_decode($type->convert(), true) + ); + } + + protected function getJsonFolderPath(): string + { + return __DIR__ . '/converted_to_json_types'; + } + + abstract protected function getTypeWithNullableFields(): AbstractType; + abstract protected function getConvertedToJsonTypeFileName(): string; +} \ No newline at end of file diff --git a/tests/KeyboardConverter/Object/Template/Carousel/Type/BasicTest.php b/tests/KeyboardConverter/Object/Template/Carousel/Type/BasicTest.php new file mode 100644 index 0000000..3a5c961 --- /dev/null +++ b/tests/KeyboardConverter/Object/Template/Carousel/Type/BasicTest.php @@ -0,0 +1,20 @@ +typeFactory = new VkObject\Template\Carousel\Type\Factory( + new VkObject\Template\Carousel\Element\Factory(), + new VkObject\Keyboard\Button\Factory() + ); + } + + public function testBasic(): void + { + $mockWithCallback = $this->getPreparedMockWithCallbackByCallbackName('testBasicCallback'); + $this->typeFactory->basic([$mockWithCallback, 'testBasicCallback']); + } + + private function getPreparedMockWithCallbackByCallbackName(string $name): MockObject + { + $mockWithCallback = $this->getMockBuilder(stdClass::class) + ->setMethods([$name]) + ->getMock(); + $mockWithCallback->expects($this->once()) + ->method($name) + ->willReturn([]); + + return $mockWithCallback; + } +} diff --git a/tests/KeyboardConverter/Object/Template/Carousel/Type/converted_to_json_types/basic.json b/tests/KeyboardConverter/Object/Template/Carousel/Type/converted_to_json_types/basic.json new file mode 100644 index 0000000..6c0e561 --- /dev/null +++ b/tests/KeyboardConverter/Object/Template/Carousel/Type/converted_to_json_types/basic.json @@ -0,0 +1,4 @@ +{ + "elements": [], + "type": "carousel" +} \ No newline at end of file diff --git a/tests/KeyboardConverter/Object/Template/ConverterTest.php b/tests/KeyboardConverter/Object/Template/ConverterTest.php new file mode 100644 index 0000000..7ff0f02 --- /dev/null +++ b/tests/KeyboardConverter/Object/Template/ConverterTest.php @@ -0,0 +1,29 @@ +converter = new Converter(); + } + + public function testCarousel() + { + $elementFactory = new Carousel\Element\Factory(); + $buttonFactory = new Keyboard\Button\Factory(); + $typeFactory = new Carousel\Type\Factory($elementFactory, $buttonFactory); + $this->assertEquals(new Carousel\Converter($typeFactory), $this->converter->carousel($typeFactory)); + } +}