You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Kartierung\Repository\ResolutionStrategy;
use Kartierung\Analyze\Result\ParameterFunction;
use Kartierung\Analyze\Result\RepositoryMethod;
use Kartierung\Analyze\Result\RepositoryMethodParameter;
use PDO;
use PHPUnit\Framework\Attributes\Group;
class SaveEntityTest extends SqliteBaseCase
{
protected function setUp(): void
{
parent::setUp();
$this->getPdo()->exec(
"
CREATE TABLE SimpleEntity (
stringcol VARCHAR(250),
intcol INTEGER PRIMARY KEY AUTOINCREMENT
);
INSERT INTO SimpleEntity(stringcol, intcol)
VALUES ('dings', 1),
('bumms', 2);
"
);
}
#[Group('integration')]
public function testCreatesNewEntity(): void
{
// Given
$strategy = new SaveEntity();
$method = new RepositoryMethod(
query: '',
returnType: '',
returnsNullable: true,
returnListType: '',
name: '',
parameters: [
new RepositoryMethodParameter(
name: '',
type: SimpleEntity::class,
function: ParameterFunction::STATEMENT_PARAMETER
)
],
);
$entity = new SimpleEntity(null, 'foobar');
$query = $strategy->createQuery($method, [$entity]);
// When
$result = $query->execute($this->getPdo());
// Then
$this->assertInstanceOf(SimpleEntity::class, $result);
$this->assertNotNull($result->intcol);
$this->assertNotSame($result, $entity);
$stmt = $this->getPdo()->query("SELECT COUNT(*) c FROM SimpleEntity");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$this->assertEquals(3, $row['c'], 'Three rows expected');
}
#[Group('integration')]
public function testUpdatesEntity(): void
{
// Given
$strategy = new SaveEntity();
$method = new RepositoryMethod(
query: '',
returnType: '',
returnsNullable: true,
returnListType: '',
name: '',
parameters: [
new RepositoryMethodParameter(
name: '',
type: SimpleEntity::class,
function: ParameterFunction::STATEMENT_PARAMETER
)
],
);
$entity = new SimpleEntity(1, 'foobar');
$query = $strategy->createQuery($method, [$entity]);
// When
$result = $query->execute($this->getPdo());
// Then
$this->assertInstanceOf(SimpleEntity::class, $result);
$this->assertNotSame($result, $entity);
$this->assertEquals($entity, $result);
$stmt = $this->getPdo()->query("SELECT COUNT(*) c FROM SimpleEntity");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$this->assertEquals(2, $row['c'], 'Three rows expected');
$stmt = $this->getPdo()->query('SELECT stringcol c FROM SimpleEntity WHERE intcol = 1');
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$this->assertEquals('foobar', $row['c']);
}
}