dj_mix_hosting_software/tests/DJTest.php
2024-05-14 17:54:06 -07:00

110 lines
4.5 KiB
PHP

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