Skip to content

Commit

Permalink
Adding phpunit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Jun 12, 2024
1 parent c9792fe commit 84fdd85
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 7 deletions.
5 changes: 5 additions & 0 deletions debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Raw cURL result: Array
Raw cURL result: Array
Raw cURL result: Array
Raw cURL result: Array
Raw cURL result: Array
60 changes: 53 additions & 7 deletions lib/telegram_webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public static function send($msg_count, $email_to, $webhook_token) {
self::delete_webhook($webhook_token);
// Get the chat ID
$chatId = self::get_chat_id($webhook_token);
// var_dump($chatId);
// print_r('chatId response: ');
// print_r($chatId);
if (!empty($chatId)) {
$text = "You have received: $msg_count unread email.s\nTo: $email_to";
$curl_handle = Hm_Functions::c_init();
Expand All @@ -35,27 +38,70 @@ public static function send($msg_count, $email_to, $webhook_token) {

}
}
}else{
Hm_Debug::add('No chat found, please check your token.');
}
}

/**
* get the chat ID using webhook_token
* @param string $webhook_token
*/
private static function get_chat_id($webhook_token) {
$curl_handle = Hm_Functions::c_init();
Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, static::PREFIX_URI.'bot'.$webhook_token.'/getUpdates');
Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$curl_result = Hm_Functions::c_exec($curl_handle);
public static function get_chat_id($webhook_token) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, static::PREFIX_URI . 'bot' . $webhook_token . '/getUpdates');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_result = curl_exec($ch);

if ($curl_result === false) {
Hm_Msgs::add('cURL Error: ' . curl_error($ch) . '<br>');
curl_close($ch);
return '';
}

curl_close($ch);
if (trim($curl_result)) {
$response_data = json_decode($curl_result, true);
if(!empty($chatId = $response_data['result'][0]['message']['chat']['id'])){
file_put_contents('./debug.log', 'Raw cURL result: ' . $response_data['result'] . "\n", FILE_APPEND);

// Log the decoded response data for debugging
if (isset($response_data['result'][0]['message']['chat']['id']) && !empty($response_data['result'][0]['message']['chat']['id'])) {
$chatId = $response_data['result'][0]['message']['chat']['id'];
return $chatId;
} else {
Hm_Msgs::add('ERRNo messages found. Please send a message to your bot first.<br>');
return '';
}
}





// $curl_handle = Hm_Functions::c_init();
// Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, static::PREFIX_URI . 'bot' . $webhook_token . '/getUpdates');
// Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
// $curl_result = Hm_Functions::c_exec($curl_handle);
// file_put_contents('./debug.log', 'Raw cURL result: ' . $curl_result . "\n", FILE_APPEND);

// if ($curl_result === false) {
// // Hm_Msgs::add('cURL Error: ' . Hm_Functions::c_error($curl_handle) . '<br>');
// // Hm_Functions::c_close($curl_handle);
// return '';
// }

// // Hm_Functions::c_close($curl_handle);

// if (trim($curl_result)) {
// $response_data = json_decode($curl_result, true);
// if (isset($response_data['result'][0]['message']['chat']['id']) && !empty($response_data['result'][0]['message']['chat']['id'])) {
// $chatId = $response_data['result'][0]['message']['chat']['id'];
// return $chatId;
// } else {
// Hm_Msgs::add('ERRNo messages found. Please send a message to your bot first.<br>');
// return '';
// }
// }
}

/**
Expand All @@ -65,7 +111,7 @@ private static function get_chat_id($webhook_token) {
* and sometines we are gettiting not found error
* @param string $webhook_token
*/
private static function delete_webhook($webhook_token) {
public static function delete_webhook($webhook_token) {
$curl_handle = Hm_Functions::c_init();
Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, static::PREFIX_URI.'bot'.$webhook_token.'/delete_webhook');
Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/.phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"Hm_Test_Telegram_Webhook::test_send":8,"Hm_Test_Telegram_Webhook::test_get_chat_id":5,"Hm_Test_Telegram_Webhook::test_delete_webhook":5,"Hm_Test_Telegram_Webhook::test_send_with_correct_token":8,"Hm_Test_Telegram_Webhook::test_send_with_bad_token":8,"Hm_Test_Telegram_Webhook::test_get_chat_id_with_bad_token":8,"Hm_Test_Telegram_Webhook::test_get_chat_id_with_correct_token":8},"times":{"Hm_Test_Telegram_Webhook::test_get_chat_id":0.001,"Hm_Test_Telegram_Webhook::test_delete_webhook":0.002,"Hm_Test_Telegram_Webhook::test_send":0.004,"Hm_Test_Telegram_Webhook::test_send_with_bad_token":2.015,"Hm_Test_Telegram_Webhook::test_send_with_correct_token":2.014,"Hm_Test_Telegram_Webhook::test_get_chat_id_with_bad_token":2.013,"Hm_Test_Telegram_Webhook::test_get_chat_id_with_correct_token":0.885}}
1 change: 1 addition & 0 deletions tests/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

/* get the framework */
require APP_PATH.'lib/framework.php';
require APP_PATH.'lib/telegram_webhook.php';

/* get the stubs */
require APP_PATH.'tests/phpunit/stubs.php';
Expand Down
3 changes: 3 additions & 0 deletions tests/phpunit/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
<testsuite name="transform">
<file>./transform.php</file>
</testsuite>
<testsuite name="webhook">
<file>./telegram_webhook.php</file>
</testsuite>
<testsuite name="auth">
<file>./auth.php</file>
</testsuite>
Expand Down
74 changes: 74 additions & 0 deletions tests/phpunit/telegram_webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

use PHPUnit\Framework\TestCase;

/**
* tests for the Hm_Servers trait
*/
class Hm_Test_Telegram_Webhook extends TestCase {

public $telegram_webhook;
public function setUp(): void {
require 'bootstrap.php';
$this->telegram_webhook = new Hm_Telegram_Webhook();
}
/**
* @preserveGlobalState disabled
* @runInSeparateProcess
*/
// public function test_send_with_bad_token() {
// $res = $this->telegram_webhook::send(0, 'demo@cypht.org', 'testtoken');
// sleep(2);
// return $this->assertNull($res);
// }
/**
* @preserveGlobalState disabled
* @runInSeparateProcess
*/
// public function test_send_with_correct_token() {
// $res = $this->telegram_webhook::send(0, 'demo@cypht.org', '7253394590:AAHIY9VmoH3uTRFtEwmlfRPGyKoZIQMCX5A');
// sleep(2);
// return $this->assertNull($res);
// }
/**
* @preserveGlobalState disabled
* @runInSeparateProcess
*/
// public function test_get_chat_id_with_bad_token() {
// $res = $this->telegram_webhook::get_chat_id('testtoken');
// sleep(2);
// return $this->assertEmpty($res);
// }

/**
* @preserveGlobalState disabled
* @runInSeparateProcess
*/
public function test_get_chat_id_with_correct_token() {
// $res = $this->telegram_webhook::get_chat_id('6132059962:AAHrfhsq9kkg1CK3FsAFRaK4l5zfoCWu14s');
// sleep(2);
// var_dump($res);
// return $this->assertEmpty($res);

$chatId = '';
$maxAttempts = 5;
$attempt = 0;

while (empty($chatId) && $attempt < $maxAttempts) {
$chatId = Hm_Telegram_Webhook::get_chat_id('6132059962:AAHrfhsq9kkg1CK3FsAFRaK4l5zfoCWu14s');
if (empty($chatId)) {
sleep(1); // Wait for 1 second before retrying
}
$attempt++;
}
var_dump($chatId);
// $this->assertNotEmpty($chatId, 'Chat ID should not be empty');
}
/**
* @preserveGlobalState disabled
* @runInSeparateProcess
*/
// public function test_delete_webhook() {
// $this->telegram_webhook::delete_webhook('testtoken');
// }
}

0 comments on commit 84fdd85

Please sign in to comment.