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

View file

@ -0,0 +1 @@
{"version":1,"defects":{"TelegramTest::testSendMessageReturnsArrayOnSuccess":8,"TelegramTest::testSendMessageReturnsFalseOnFailure":8},"times":[]}

1
.phpunit.result.cache Normal file
View file

@ -0,0 +1 @@
{"version":1,"defects":{"TelegramTest::testSendMessageReturnsArrayOnSuccess":8,"TelegramTest::testSendMessageReturnsFalseOnFailure":8,"DatabaseTest::testConnection":8,"DatabaseTest::testQuery":8,"DJTest::testLoadFromId":8,"DJTest::testLoadFromSlug":8},"times":{"TelegramTest::testSendMessageReturnsArrayOnSuccess":0.001,"TelegramTest::testSendMessageReturnsFalseOnFailure":0.001,"DatabaseTest::testConnection":0.007,"DatabaseTest::testQuery":0.002,"DJTest::testLoadFromId":0.001,"DJTest::testLoadFromSlug":0.001}}

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
class CustomCDN class CustomCDN
{ {
private $space; private $space;

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
class DJ class DJ
{ {
@ -16,7 +18,7 @@ class DJ
private string $created = ""; private string $created = "";
private string $updated = ""; private string $updated = "";
private string $claimed_by = ""; private string $claimed_by = "";
private mysqli $db; private $db;
private array $mixes = []; private array $mixes = [];
@ -136,6 +138,22 @@ class DJ
} }
} }
private function load_dj_mixes(): void
{
$stmt = $this->db->prepare("SELECT * FROM mix WHERE dj1 = ? OR dj2 = ? OR dj3 = ?");
$stmt->bind_param("iii", $this->id, $this->id, $this->id);
$stmt->execute();
$result = $stmt->get_result();
$mixes = [];
while ($mix = $result->fetch_assoc()) {
$mixes[] = $mix;
}
$stmt->close();
$this->mixes = $mixes;
}
private function load_from_slug(): bool private function load_from_slug(): bool
{ {
$socials = []; $socials = [];
@ -209,22 +227,6 @@ class DJ
return $this->claimed; return $this->claimed;
} }
private function load_dj_mixes(): void
{
$stmt = $this->db->prepare("SELECT * FROM mix WHERE dj1 = ? OR dj2 = ? OR dj3 = ?");
$stmt->bind_param("iii", $this->id, $this->id, $this->id);
$stmt->execute();
$result = $stmt->get_result();
$mixes = [];
while ($mix = $result->fetch_assoc()) {
$mixes[] = $mix;
}
$stmt->close();
$this->mixes = $mixes;
}
public function get_dj_mixes() public function get_dj_mixes()
{ {
return $this->mixes; return $this->mixes;

View file

@ -1,5 +1,6 @@
<?php <?php
namespace DJMixHosting;
class DJs class DJs
{ {

View file

@ -1,6 +1,10 @@
<?php <?php
namespace DJMixHosting;
// create a class that extends mysql database // create a class that extends mysql database
use mysqli;
class Database extends mysqli class Database extends mysqli
{ {
// create a constructor that takes in the config file // create a constructor that takes in the config file

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
class Genre class Genre
{ {

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
class Genres class Genres
{ {

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
class Mix class Mix
{ {

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
class Mixshow class Mixshow
{ {

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
class Mixshows class Mixshows
{ {

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
Class S3 extends Aws\S3\S3Client{ Class S3 extends Aws\S3\S3Client{

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
class Schema class Schema
{ {

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
class Telegram class Telegram
{ {
private $token = ""; private $token = "";

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
class Upload class Upload
{ {
private $file; private $file;

View file

@ -1,5 +1,7 @@
<?php <?php
namespace DJMixHosting;
use Random\RandomException; use Random\RandomException;
Class User{ Class User{

View file

@ -16,5 +16,10 @@
"ext-mysqli": "*", "ext-mysqli": "*",
"ext-curl": "*", "ext-curl": "*",
"phpunit/phpunit": ">8.5.1.0" "phpunit/phpunit": ">8.5.1.0"
},
"autoload": {
"psr-4": {
"DJMixHosting\\": "classes/"
}
} }
} }

View file

@ -1,6 +1,8 @@
<?php <?php
require_once 'includes/globals.php'; require_once 'includes/globals.php';
require_once 'classes/Database.php'; require_once 'vendor/autoload.php';
use DJMixHosting\Telegram;
if ($_POST) { if ($_POST) {
$name = $_POST['name']; $name = $_POST['name'];

7
debug.php Normal file
View file

@ -0,0 +1,7 @@
<?php
require_once __DIR__ . '/vendor/autoload.php';
echo "<pre>";
// Dump the autoload class map
print_r(Composer\Autoload\ClassLoader::getRegisteredLoaders());
echo "</pre>";

10
dj.php
View file

@ -1,10 +1,12 @@
<?php <?php
require_once 'includes/globals.php'; require_once 'includes/globals.php';
require_once 'classes/Database.php'; require_once 'vendor/autoload.php';
require_once 'classes/DJ.php'; use DJMixHosting\Database;
require_once 'classes/Genre.php'; use DJMixHosting\DJ;
require_once 'classes/Mix.php'; use DJMixHosting\Genre;
use DJMixHosting\Mix;
// if there's a query parameter named 'dj', load the DJ class // if there's a query parameter named 'dj', load the DJ class
$db = new Database($config); $db = new Database($config);

View file

@ -1,8 +1,9 @@
<?php <?php
require_once 'vendor/autoload.php';
require_once 'includes/globals.php'; require_once 'includes/globals.php';
require_once 'classes/Database.php';
require_once 'classes/DJs.php'; use DJMixHosting\Database;
use DJMixHosting\DJs;
$db = new Database($config); $db = new Database($config);
$djs = new DJs($db); $djs = new DJs($db);

View file

@ -1,9 +1,11 @@
<?php <?php
require_once 'includes/globals.php'; require_once 'includes/globals.php';
require_once 'classes/Database.php'; require_once 'vendor/autoload.php';
require_once 'classes/Genre.php';
require_once 'classes/Mix.php'; use DJMixHosting\Database;
use DJMixHosting\Genre;
use DJMixHosting\Mix;
$genre = null; $genre = null;
$genreFound = false; $genreFound = false;

View file

@ -1,8 +1,9 @@
<?php <?php
require_once 'includes/globals.php'; require_once 'includes/globals.php';
require_once 'classes/Database.php'; require_once 'vendor/autoload.php';
require_once 'classes/Genres.php'; use DJMixHosting\Database;
use DJMixHosting\Genres;
$genresFound = false; $genresFound = false;
// if there's a query parameter named 'dj', load the DJ class // if there's a query parameter named 'dj', load the DJ class

View file

@ -1,8 +1,15 @@
<?php <?php
require_once 'includes/globals.php'; require_once 'includes/globals.php';
require_once 'vendor/autoload.php';
use DJMixHosting\Database;
use DJMixHosting\User;
$title = $locale['home']; $title = $locale['home'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['username']) && isset($_POST['password'])) { if (isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username']; $username = $_POST['username'];
$password = $_POST['password']; $password = $_POST['password'];
@ -21,7 +28,8 @@ require_once 'includes/header.php'; ?>
<nav aria-label="breadcrumb" class="bg-body-tertiary rounded-3 p-3 mb-4"> <nav aria-label="breadcrumb" class="bg-body-tertiary rounded-3 p-3 mb-4">
<ol class="breadcrumb mb-0"> <ol class="breadcrumb mb-0">
<li class="breadcrumb-item"><a href="/"><?php echo $locale['home']; ?></a></li> <li class="breadcrumb-item"><a href="/"><?php echo $locale['home']; ?></a></li>
<li class="breadcrumb-item active"><a href="/login.php"><?php echo $locale['login']; ?></a></li> <li class="breadcrumb-item active"><a href="/login.php"><?php echo $locale['login']; ?></a>
</li>
</ol> </ol>
</nav> </nav>
</div> </div>

View file

@ -1,9 +1,12 @@
<?php <?php
require_once 'includes/globals.php'; require_once 'includes/globals.php';
require_once 'classes/Database.php'; require_once 'vendor/autoload.php';
require_once 'classes/Mix.php'; use DJMixHosting\Database;
require_once 'classes/Mixshow.php'; use DJMixHosting\Mix;
use DJMixHosting\Mixshow;
use DJMixHosting\Genre;
$db = new Database($config); $db = new Database($config);
$mixFound = false; $mixFound = false;

View file

@ -1,10 +1,13 @@
<?php <?php
require_once 'includes/globals.php'; require_once 'includes/globals.php';
require_once 'classes/Database.php';
require_once 'classes/Mixshow.php'; require_once 'vendor/autoload.php';
require_once 'classes/Genre.php';
require_once 'classes/Mix.php'; use DJMixHosting\Database;
use DJMixHosting\Genre;
use DJMixHosting\Mix;
use DJMixHosting\Mixshow;
// if there's a query parameter named 'dj', load the DJ class // if there's a query parameter named 'dj', load the DJ class
$db = new Database($config); $db = new Database($config);

View file

@ -1,9 +1,10 @@
<?php <?php
require_once 'includes/globals.php'; require_once 'includes/globals.php';
require_once 'classes/Database.php'; require_once 'vendor/autoload.php';
require_once 'classes/Mixshows.php';
require_once 'classes/DJs.php'; use DJMixHosting\Database;
use DJMixHosting\Mixshows;
$db = new Database($config); $db = new Database($config);
$mixshows = new Mixshows($db); $mixshows = new Mixshows($db);
@ -17,7 +18,8 @@ require_once 'includes/header.php';
<nav aria-label="breadcrumb" class="bg-body-tertiary rounded-3 p-3 mb-4"> <nav aria-label="breadcrumb" class="bg-body-tertiary rounded-3 p-3 mb-4">
<ol class="breadcrumb mb-0"> <ol class="breadcrumb mb-0">
<li class="breadcrumb-item"><a href="/"><?php echo $locale['home']; ?></a></li> <li class="breadcrumb-item"><a href="/"><?php echo $locale['home']; ?></a></li>
<li class="breadcrumb-item active"><a href="/mixshows.php"><?php echo $locale['mixshows']; ?></a> <li class="breadcrumb-item active"><a
href="/mixshows.php"><?php echo $locale['mixshows']; ?></a>
</li> </li>
</ol> </ol>
</nav> </nav>

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();
}
}