-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
196 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.idea | ||
/vendor/ | ||
/vendor/ | ||
composer.lock | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,26 @@ | ||
{ | ||
"name": "madmagestelegram/types", | ||
"description": "Representation of telegram bot types in php classes", | ||
"keywords": [ | ||
"telegram", | ||
"types", | ||
"rpc", | ||
"php" | ||
], | ||
"type": "library", | ||
"license": "MIT", | ||
"require": { | ||
"php": "^7.2|^8.0", | ||
"php": ">=7.4", | ||
"ext-json": "*", | ||
"jms/serializer": "^3.1" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"MadmagesTelegram\\Types\\": "src/" | ||
"MadmagesTelegram\\Types\\": "src/", | ||
"Tests\\": "tests/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./app</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Main"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
class BaseTestCase extends \PHPUnit\Framework\TestCase | ||
{ | ||
public static function clearNullFields(array $data): array | ||
{ | ||
$data = array_filter($data); | ||
array_walk( | ||
$data, | ||
static function (&$data) { | ||
if (is_array($data)) { | ||
$data = self::clearNullFields($data); | ||
} | ||
} | ||
); | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Tests\Clients; | ||
|
||
use Tests\BaseTestCase; | ||
|
||
class ClientTest extends BaseTestCase | ||
{ | ||
/** | ||
* @dataProvider clientDataProvider | ||
*/ | ||
public function testClient(array $sampleData): void | ||
{ | ||
$client = new TestClient(); | ||
$data = $client->sendMessage(1, 'test'); | ||
self::assertSame($sampleData, self::clearNullFields($data)); | ||
} | ||
|
||
public function testTypedClient(): void | ||
{ | ||
$message = (new TestTypesClient())->sendMessage(1, 'test'); | ||
self::assertSame(TestTypesClient::SAMPLE_DATA, self::clearNullFields($message->_getData())); | ||
} | ||
|
||
public function clientDataProvider(): array | ||
{ | ||
return [ | ||
[ | ||
[ | ||
'sendMessage', | ||
[ | ||
'chat_id' => 1, | ||
'text' => 'test', | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Tests\Clients; | ||
|
||
use MadmagesTelegram\Types\Client; | ||
|
||
class TestClient extends Client | ||
{ | ||
public function _apiCall(string $method, array $parameters) | ||
{ | ||
return [$method, $parameters]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Tests\Clients; | ||
|
||
use MadmagesTelegram\Types\TypedClient; | ||
|
||
class TestTypesClient extends TypedClient | ||
{ | ||
|
||
public const SAMPLE_DATA = [ | ||
'message_id' => 1, | ||
'from' => | ||
[ | ||
'id' => 1, | ||
'is_bot' => true, | ||
'first_name' => 'test', | ||
'username' => 'test', | ||
], | ||
'date' => 1, | ||
'chat' => | ||
[ | ||
'id' => 1, | ||
'type' => 'private', | ||
'username' => 'test', | ||
'first_name' => 'test', | ||
], | ||
'text' => 'test', | ||
]; | ||
|
||
public function _apiCall(string $method, array $parameters): string | ||
{ | ||
return json_encode(self::SAMPLE_DATA, JSON_THROW_ON_ERROR); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Tests\Types; | ||
|
||
use MadmagesTelegram\Types\Serializer; | ||
use MadmagesTelegram\Types\Type\Message; | ||
use Tests\BaseTestCase; | ||
|
||
class MessageTest extends BaseTestCase | ||
{ | ||
/** | ||
* @dataProvider messageDataProvider | ||
*/ | ||
public function testSerializeMessage(array $sampleMessage): void | ||
{ | ||
/** @var Message $message */ | ||
$message = Serializer::deserialize(json_encode($sampleMessage, JSON_THROW_ON_ERROR), Message::class); | ||
|
||
$messageData = self::clearNullFields($message->_getData()); | ||
|
||
self::assertSame($sampleMessage, $messageData); | ||
self::assertSame($message->getFrom()->getUsername(), 'test'); | ||
} | ||
|
||
public function messageDataProvider(): array | ||
{ | ||
return [ | ||
[ | ||
[ | ||
'message_id' => 1, | ||
'from' => | ||
[ | ||
'id' => 1, | ||
'is_bot' => true, | ||
'first_name' => 'test', | ||
'username' => 'test', | ||
], | ||
'date' => 1, | ||
'chat' => | ||
[ | ||
'id' => 1, | ||
'type' => 'private', | ||
'username' => 'test', | ||
'first_name' => 'test', | ||
], | ||
'text' => 'test', | ||
], | ||
], | ||
]; | ||
} | ||
} |