PHPUnit stuff.

This commit is contained in:
Cody Cook 2024-05-14 17:54:06 -07:00
commit 74f1d6e193
30 changed files with 358 additions and 89 deletions

110
tests/DJTest.php Normal file
View file

@ -0,0 +1,110 @@
<?php
require_once '../vendor/autoload.php';
use PHPUnit\Framework\TestCase;
use DJMixHosting\DJ;
use mysqli;
use mysqli_stmt;
use mysqli_result;
class DJTest extends TestCase
{
private $mockDb;
private $mockStmt;
private $mockResult;
private $dj;
protected function setUp(): void
{
$this->mockDb = $this->createMock(mysqli::class);
$this->mockStmt = $this->createMock(mysqli_stmt::class);
$this->mockResult = $this->createMock(mysqli_result::class);
}
public function testLoadFromId()
{
$djData = [
'id' => 1,
'name' => 'Test DJ',
'bio' => 'This is a test bio',
'slug' => 'test-dj',
'img' => '/djs/test-dj.png',
'email' => 'test@example.com',
'facebook_url' => 'http://facebook.com/testdj',
'instagram_url' => 'http://instagram.com/testdj',
'twitter_url' => 'http://twitter.com/testdj',
'active' => 1,
'created' => '2021-01-01 00:00:00',
'lastupdated' => '2021-01-01 00:00:00',
'claimed_by' => null,
];
// Mock the prepared statement and its behavior
$this->mockStmt->method('bind_param')->willReturn(true);
$this->mockStmt->method('execute')->willReturn(true);
$this->mockStmt->method('get_result')->willReturn($this->mockResult);
$this->mockResult->method('fetch_assoc')->willReturn($djData);
// Mock the DB connection to return the mock statement
$this->mockDb->method('prepare')->willReturn($this->mockStmt);
// Instantiate the DJ class with a valid ID and the mocked DB connection
$this->dj = new DJ(1, $this->mockDb);
// Assertions to verify that the DJ object is correctly built
$this->assertEquals(1, $this->dj->get_id());
$this->assertEquals('Test DJ', $this->dj->get_name());
$this->assertEquals('This is a test bio', $this->dj->get_bio());
$this->assertEquals('test-dj', $this->dj->get_slug());
$this->assertEquals('https://cdn.utahsdjs.com/test-dj.png', $this->dj->get_img());
$this->assertEquals('test@example.com', $this->dj->get_email());
$this->assertTrue($this->dj->get_active());
$this->assertEquals('2021-01-01 00:00:00', $this->dj->get_created());
$this->assertEquals('2021-01-01 00:00:00', $this->dj->get_updated());
$this->assertFalse($this->dj->get_claimed());
}
public function testLoadFromSlug()
{
$djData = [
'id' => 1,
'name' => 'Test DJ',
'bio' => 'This is a test bio',
'slug' => 'test-dj',
'img' => '/djs/test-dj.png',
'email' => 'test@example.com',
'facebook_url' => 'http://facebook.com/testdj',
'instagram_url' => 'http://instagram.com/testdj',
'twitter_url' => 'http://twitter.com/testdj',
'active' => 1,
'created' => '2021-01-01 00:00:00',
'lastupdated' => '2021-01-01 00:00:00',
'claimed_by' => null,
];
// Mock the prepared statement and its behavior
$this->mockStmt->method('bind_param')->willReturn(true);
$this->mockStmt->method('execute')->willReturn(true);
$this->mockStmt->method('get_result')->willReturn($this->mockResult);
$this->mockResult->method('fetch_assoc')->willReturn($djData);
// Mock the DB connection to return the mock statement
$this->mockDb->method('prepare')->willReturn($this->mockStmt);
// Instantiate the DJ class with a valid slug and the mocked DB connection
$this->dj = new DJ('test-dj', $this->mockDb);
// Assertions to verify that the DJ object is correctly built
$this->assertEquals(1, $this->dj->get_id());
$this->assertEquals('Test DJ', $this->dj->get_name());
$this->assertEquals('This is a test bio', $this->dj->get_bio());
$this->assertEquals('test-dj', $this->dj->get_slug());
$this->assertEquals('https://cdn.utahsdjs.com/test-dj.png', $this->dj->get_img());
$this->assertEquals('test@example.com', $this->dj->get_email());
$this->assertTrue($this->dj->get_active());
$this->assertEquals('2021-01-01 00:00:00', $this->dj->get_created());
$this->assertEquals('2021-01-01 00:00:00', $this->dj->get_updated());
$this->assertFalse($this->dj->get_claimed());
}
}

56
tests/DatabaseTest.php Normal file
View file

@ -0,0 +1,56 @@
<?php
require_once '../vendor/autoload.php'; // Adjust the path as needed to include Composer's autoload
use PHPUnit\Framework\TestCase;
use DJMixHosting\Database;
class DatabaseTest extends TestCase
{
private $config;
protected function setUp(): void
{
// Setup your test database configuration here
$this->config = [
'database' => [
'host' => 'localhost',
'user' => 'test_user',
'pass' => 'test_password',
'db' => 'test_database',
'port' => 3306
]
];
}
public function testConnection()
{
// Create an instance of Database
$db = new Database($this->config);
// Check if the connection is successfully established
$this->assertInstanceOf(mysqli::class, $db);
$this->assertEquals(0, $db->connect_errno, "Database connection error: " . $db->connect_error);
}
public function testQuery()
{
// Create an instance of Database
$db = new Database($this->config);
// Execute a simple query
$result = $db->query('SELECT 1 AS value');
$this->assertNotFalse($result, "Query failed: " . $db->error);
$row = $result->fetch_assoc();
$this->assertEquals(1, $row['value'], "Query did not return the correct value");
}
protected function tearDown(): void
{
// Close the database connection if it's still open
if (isset($db) && $db instanceof mysqli) {
$db->close();
}
}
}

36
tests/TelegramTest.php Normal file
View file

@ -0,0 +1,36 @@
<?php
require_once '../vendor/autoload.php';
use DJMixHosting\Telegram;
use PHPUnit\Framework\TestCase;
class TelegramTest extends TestCase
{
private $telegram;
private $token = 'YOUR_BOT_TOKEN';
private $chatId = 'YOUR_CHAT_ID';
public function testSendMessageReturnsArrayOnSuccess()
{
$expectedResult = json_encode(['ok' => true, 'result' => 'Message sent']);
$this->telegram->method('sendMessage')->willReturn($expectedResult);
$result = $this->telegram->send_message('Hello, world!');
$this->assertIsArray($result);
$this->assertEquals(['ok' => true, 'result' => 'Message sent'], $result);
}
public function testSendMessageReturnsFalseOnFailure()
{
$this->telegram->method('sendMessage')->willReturn(false);
$result = $this->telegram->send_message('Hello, world!');
$this->assertFalse($result);
}
protected function setUp(): void
{
$this->telegram = $this->getMockBuilder(Telegram::class)->setConstructorArgs([$this->token, $this->chatId])->onlyMethods(['sendMessage'])->getMock();
}
}