Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madmages committed May 16, 2021
1 parent 75bb0b1 commit 3e26769
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea
/vendor/
/vendor/
composer.lock
.phpunit.result.cache
14 changes: 12 additions & 2 deletions composer.json
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/"
}
}
}
23 changes: 23 additions & 0 deletions phpunit.xml
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>
21 changes: 21 additions & 0 deletions tests/BaseTestCase.php
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;
}
}
39 changes: 39 additions & 0 deletions tests/Clients/ClientTest.php
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',
],
],
],
];
}
}
13 changes: 13 additions & 0 deletions tests/Clients/TestClient.php
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];
}
}
34 changes: 34 additions & 0 deletions tests/Clients/TestTypesClient.php
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);
}
}
51 changes: 51 additions & 0 deletions tests/Types/MessageTest.php
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',
],
],
];
}
}

0 comments on commit 3e26769

Please sign in to comment.