Skip to content
2 changes: 1 addition & 1 deletion Tests/DatabaseAwareTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DatabaseAwareTraitTest extends TestCase
*/
public function testGetSetDatabase(): void
{
$db = $this->createMock(DatabaseInterface::class);
$db = $this->createStub(DatabaseInterface::class);

$trait = new class () {
use DatabaseAwareTrait;
Expand Down
9 changes: 2 additions & 7 deletions Tests/DatabaseExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@

namespace Joomla\Database\Tests;

use Joomla\Database\DatabaseExporter;
use Joomla\Database\DatabaseImporter;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\Tests\Stubs\TestDatabaseExporter;
use Joomla\Database\Tests\Stubs\TestDatabaseImporter;
use Joomla\Test\TestHelper;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -111,12 +107,11 @@ public function testFrom($from, bool $shouldRaiseException)
}

/**
* @testdox A database drier can be set to the exporter
* @testdox A database driver can be set to the exporter
*/
public function testSetDbo()
{
/** @var DatabaseInterface|MockObject $db */
$db = $this->createMock(DatabaseInterface::class);
$db = $this->createStub(DatabaseInterface::class);

$this->assertSame($this->exporter, $this->exporter->setDbo($db), 'The exporter supports method chaining');
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/DatabaseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function testGetExporter(string $adapter, bool $shouldRaiseException, boo
$databaseDriver = null;

if ($createDb) {
$databaseDriver = $this->createMock(MysqliDriver::class);
$databaseDriver = $this->createStub(MysqliDriver::class);
}

$exporter = $this->factory->getExporter($adapter, $databaseDriver);
Expand Down Expand Up @@ -194,7 +194,7 @@ public function testGetImporter(string $adapter, bool $shouldRaiseException, boo
$databaseDriver = null;

if ($createDb) {
$databaseDriver = $this->createMock(MysqliDriver::class);
$databaseDriver = $this->createStub(MysqliDriver::class);
}

$importer = $this->factory->getImporter($adapter, $databaseDriver);
Expand Down Expand Up @@ -239,7 +239,7 @@ public function testGetIterator(string $adapter, bool $createStatement)
$statement = null;

if ($createStatement) {
$statement = $this->createMock(StatementInterface::class);
$statement = $this->createStub(StatementInterface::class);
}

$this->assertInstanceOf(
Expand Down Expand Up @@ -287,7 +287,7 @@ public function testGetQuery(string $adapter, bool $shouldRaiseException, bool $
$databaseDriver = null;

if ($createDb) {
$databaseDriver = $this->createMock(MysqliDriver::class);
$databaseDriver = $this->createStub(MysqliDriver::class);
}

$this->assertInstanceOf(
Expand Down
8 changes: 2 additions & 6 deletions Tests/DatabaseImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@

namespace Joomla\Database\Tests;

use Joomla\Database\DatabaseImporter;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\Tests\Stubs\TestDatabaseImporter;
use Joomla\Database\Tests\Stubs\TestDatabaseQuery;
use Joomla\Test\TestHelper;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -65,12 +62,11 @@ public function testAsXml()
}

/**
* @testdox A database drier can be set to the importer
* @testdox A database driver can be set to the importer
*/
public function testSetDbo()
{
/** @var DatabaseInterface|MockObject $db */
$db = $this->createMock(DatabaseInterface::class);
$db = $this->createStub(DatabaseInterface::class);

$this->assertSame($this->importer, $this->importer->setDbo($db), 'The importer supports method chaining');
}
Expand Down
7 changes: 4 additions & 3 deletions Tests/DatabaseIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Joomla\Database\FetchMode;
use Joomla\Database\StatementInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -27,7 +28,7 @@ public function testInstantiation()

$i = 0;

$statement->expects($this->any())
$statement->expects($this->once())
->method('fetch')
->willReturnCallback(
function () use ($i) {
Expand Down Expand Up @@ -229,8 +230,8 @@ public function testConstructorExceptionForNonExistingClass()
{
$this->expectException(\InvalidArgumentException::class);

/** @var StatementInterface|MockObject $statement */
$statement = $this->createMock(StatementInterface::class);
/** @var StatementInterface|Stub $statement */
$statement = $this->createStub(StatementInterface::class);

$iterator = new DatabaseIterator($statement, null, \NonExistingClass::class);
}
Expand Down
52 changes: 33 additions & 19 deletions Tests/DatabaseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Joomla\Database\ParameterType;
use Joomla\Database\Tests\Stubs\TestDatabaseQuery;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -24,14 +24,14 @@ class DatabaseQueryTest extends TestCase
/**
* Object being tested
*
* @var MockObject|DatabaseQuery
* @var DatabaseQuery
*/
private $query;

/**
* Mock database driver
*
* @var MockObject|DatabaseInterface
* @var Stub|DatabaseInterface
*/
private $db;

Expand All @@ -46,7 +46,7 @@ protected function setUp(): void
{
parent::setUp();

$this->db = $this->createMock(DatabaseInterface::class);
$this->db = $this->createStub(DatabaseInterface::class);
$this->query = new TestDatabaseQuery($this->db);
}

Expand Down Expand Up @@ -168,8 +168,7 @@ public static function dataConcatenate(): array
#[DataProvider('dataConcatenate')]
public function testConcatenate(array $values, ?string $separator, string $expected)
{
$this->db->expects($this->any())
->method('quote')
$this->db->method('quote')
->willReturnCallback(function ($text, $escape = true) {
return "'" . $text . "'";
});
Expand Down Expand Up @@ -538,19 +537,22 @@ public static function dataNullDate(): array
#[DataProvider('dataNullDate')]
public function testNullDate(bool $quoted, string $expected)
{
$this->db->expects($this->once())
$db = $this->createMock(DatabaseInterface::class);
$query = new TestDatabaseQuery($db);

$db->expects($this->once())
->method('getNullDate')
->willReturn('0000-00-00 00:00:00');

$this->db->expects($this->any())
$db->expects($this->exactly((int) $quoted))
->method('quote')
->willReturnCallback(function ($text, $escape = true) {
return "'" . $text . "'";
});

$this->assertSame(
$expected,
$this->query->nullDate($quoted)
$query->nullDate($quoted)
);
}

Expand Down Expand Up @@ -582,7 +584,9 @@ public function testIsNullDatetimeNoDates()
*/
public function testIsNullDatetimeWithDates()
{
$this->db->expects($this->any())
$db = $this->createMock(DatabaseInterface::class);

$db->expects($this->once())
->method('quote')
->willReturnCallback(function ($text, $escape = true) {
foreach ($text as $k => $v) {
Expand All @@ -592,7 +596,7 @@ public function testIsNullDatetimeWithDates()
return $text;
});

$query = new class ($this->db) extends DatabaseQuery {
$query = new class ($db) extends DatabaseQuery {
protected $nullDatetimeList = ['0000-00-00 00:00:00', '1000-01-01 00:00:00'];

public function groupConcat($expression, $separator = ',')
Expand Down Expand Up @@ -643,15 +647,18 @@ public function testOrder()
*/
public function testQuote()
{
$this->db->expects($this->any())
$db = $this->createMock(DatabaseInterface::class);
$query = new TestDatabaseQuery($db);

$db->expects($this->once())
->method('quote')
->willReturnCallback(function ($text, $escape = true) {
return "'" . $text . "'";
});

$this->assertSame(
"'foo'",
$this->query->quote('foo')
$query->quote('foo')
);
}

Expand All @@ -672,15 +679,18 @@ public function testQuoteException()
*/
public function testQuoteName()
{
$this->db->expects($this->any())
$db = $this->createMock(DatabaseInterface::class);
$query = new TestDatabaseQuery($db);

$db->expects($this->once())
->method('quoteName')
->willReturnCallback(function ($text, $escape = true) {
return "`" . $text . "`";
});

$this->assertSame(
"`foo`",
$this->query->quoteName('foo')
$query->quoteName('foo')
);
}

Expand Down Expand Up @@ -1255,13 +1265,15 @@ public function processLimit($query, $limit, $offset = 0)
*/
public function testCastingToStringInsertSet()
{
$this->db->expects($this->any())
$db = $this->createMock(DatabaseInterface::class);

$db->expects($this->once())
->method('quote')
->willReturnCallback(function ($text, $escape = true) {
return "'" . $text . "'";
});

$query = new class ($this->db) extends DatabaseQuery {
$query = new class ($db) extends DatabaseQuery {
public function groupConcat($expression, $separator = ',')
{
return '';
Expand Down Expand Up @@ -1289,13 +1301,15 @@ public function processLimit($query, $limit, $offset = 0)
*/
public function testCastingToStringInsertColumnsValues()
{
$this->db->expects($this->any())
$db = $this->createMock(DatabaseInterface::class);

$db->expects($this->once())
->method('quote')
->willReturnCallback(function ($text, $escape = true) {
return "'" . $text . "'";
});

$query = new class ($this->db) extends DatabaseQuery {
$query = new class ($db) extends DatabaseQuery {
public function groupConcat($expression, $separator = ',')
{
return '';
Expand Down
Loading
Loading