Cody Cook
70c8a87e15
All checks were successful
SonarQube Scan / SonarQube Trigger (push) Successful in 30s
80 lines
4.1 KiB
PHP
80 lines
4.1 KiB
PHP
<?php
|
|
|
|
require_once '../vendor/autoload.php';
|
|
|
|
use DJMixHosting\DJ;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class DJTest extends TestCase
|
|
{
|
|
private $mockDb;
|
|
private $mockStmt;
|
|
private $mockResult;
|
|
private $dj;
|
|
|
|
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->getID());
|
|
$this->assertEquals('Test DJ', $this->dj->getName());
|
|
$this->assertEquals('This is a test bio', $this->dj->getBio());
|
|
$this->assertEquals('test-dj', $this->dj->getSlug());
|
|
$this->assertEquals('https://cdn.utahsdjs.com/test-dj.png', $this->dj->getImg());
|
|
$this->assertEquals('test@example.com', $this->dj->getEmail());
|
|
$this->assertTrue($this->dj->getActive());
|
|
$this->assertEquals('2021-01-01 00:00:00', $this->dj->getCreated());
|
|
$this->assertEquals('2021-01-01 00:00:00', $this->dj->getUpdated());
|
|
$this->assertFalse($this->dj->getClaimed());
|
|
}
|
|
|
|
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->getID());
|
|
$this->assertEquals('Test DJ', $this->dj->getName());
|
|
$this->assertEquals('This is a test bio', $this->dj->getBio());
|
|
$this->assertEquals('test-dj', $this->dj->getSlug());
|
|
$this->assertEquals('https://cdn.utahsdjs.com/test-dj.png', $this->dj->getImg());
|
|
$this->assertEquals('test@example.com', $this->dj->getEmail());
|
|
$this->assertTrue($this->dj->getActive());
|
|
$this->assertEquals('2021-01-01 00:00:00', $this->dj->getCreated());
|
|
$this->assertEquals('2021-01-01 00:00:00', $this->dj->getUpdated());
|
|
$this->assertFalse($this->dj->getClaimed());
|
|
}
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->mockDb = $this->createMock(mysqli::class);
|
|
$this->mockStmt = $this->createMock(mysqli_stmt::class);
|
|
$this->mockResult = $this->createMock(mysqli_result::class);
|
|
}
|
|
}
|