Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions tests/MartinGeorgiev/Doctrine/DBAL/Types/BaseTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Tests\MartinGeorgiev\Doctrine\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use MartinGeorgiev\Doctrine\DBAL\Types\BaseType;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class BaseTypeTest extends TestCase
{
/**
* @var AbstractPlatform&MockObject
*/
private AbstractPlatform $platform;

protected function setUp(): void
{
$this->platform = $this->createMock(AbstractPlatform::class);
}

/**
* @test
*/
public function throws_exception_when_type_name_not_configured(): void
{
$type = new class extends BaseType {
protected const TYPE_NAME = '';
};

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('no meaningful value for TYPE_NAME constant');

$type->getName();
}

/**
* @test
*/
public function throws_exception_when_getting_sql_declaration_with_no_type_name(): void
{
$type = new class extends BaseType {
protected const TYPE_NAME = '';
};

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Doctrine type defined in class');

$type->getSQLDeclaration([], $this->platform);
}

/**
* @test
*/
public function returns_correct_type_name(): void
{
$type = new class extends BaseType {
protected const TYPE_NAME = 'custom_type';
};

self::assertEquals('custom_type', $type->getName());
}

/**
* @test
*/
public function gets_correct_sql_declaration(): void
{
$type = new class extends BaseType {
protected const TYPE_NAME = 'custom_type';
};

$this->platform
->expects(self::once())
->method('getDoctrineTypeMapping')
->with('custom_type')
->willReturn('CUSTOM_SQL_TYPE');

$result = $type->getSQLDeclaration([], $this->platform);
self::assertEquals('CUSTOM_SQL_TYPE', $result);
}

/**
* @test
*/
public function requires_sql_comment_hint_returns_false(): void
{
$type = new class extends BaseType {
protected const TYPE_NAME = 'custom_type';
};

// @phpstan-ignore-next-line Not all Doctrine version like this method as it's deprecated. For now, we ignore the deprecation.
self::assertFalse($type->requiresSQLCommentHint($this->platform));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;

use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\SqlWalker;
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\BaseFunction;
use PHPUnit\Framework\TestCase;

class BaseFunctionTest extends TestCase
{
/**
* @test
*/
public function get_sql_returns_formatted_function_call(): void
{
$function = new class('test_function') extends BaseFunction {
// @phpstan-ignore-next-line The type for $name is not string in older Doctrine versions
public function __construct(string $name)
{
parent::__construct($name);
$this->customizeFunction();
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('TEST(%s, %s)');
}

/**
* @param list<Node|null> $nodes
*/
public function setNodes(array $nodes): void
{
$this->nodes = $nodes;
}
};

$node1 = $this->createMock(Node::class);
$node1->expects($this->once())
->method('dispatch')
->willReturn('arg1');

$node2 = $this->createMock(Node::class);
$node2->expects($this->once())
->method('dispatch')
->willReturn('arg2');

/** @var list<Node|null> $nodes */
$nodes = [$node1, $node2];
$function->setNodes($nodes);

$sqlWalker = $this->createMock(SqlWalker::class);

$result = $function->getSql($sqlWalker);
$this->assertEquals('TEST(arg1, arg2)', $result);
}

/**
* @test
*/
public function get_sql_handles_null_nodes(): void
{
$function = new class('test_function') extends BaseFunction {
// @phpstan-ignore-next-line The type for $name is not string in older Doctrine versions
public function __construct(string $name)
{
parent::__construct($name);
$this->customizeFunction();
}

protected function customizeFunction(): void
{
$this->setFunctionPrototype('TEST(%s)');
}

/**
* @param list<Node|null> $nodes
*/
public function setNodes(array $nodes): void
{
$this->nodes = $nodes;
}
};

/** @var list<Node|null> $nodes */
$nodes = [null];
$function->setNodes($nodes);

$sqlWalker = $this->createMock(SqlWalker::class);

$result = $function->getSql($sqlWalker);
$this->assertEquals('TEST(null)', $result);
}
}