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
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ jobs:
- uses: ramsey/composer-install@v3
- run: composer phpunit

code-coverage:
name: Code Coverage
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: shivammathur/setup-php@v2
with:
php-version: '8.5'
coverage: pcov
- uses: ramsey/composer-install@v3
- name: Generate coverage report
run: vendor/bin/phpunit --coverage-clover=coverage.xml
- name: Upload to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}

static-analysis:
name: Static Analysis
runs-on: ubuntu-latest
Expand All @@ -26,4 +43,5 @@ jobs:
with:
php-version: '8.5'
- uses: ramsey/composer-install@v3
- run: composer phpcs
- run: composer phpstan
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"phpcs": "vendor/bin/phpcs",
"phpstan": "vendor/bin/phpstan analyze",
"phpunit": "vendor/bin/phpunit",
"coverage": "vendor/bin/phpunit --coverage-text",
"qa": [
"@phpcs",
"@phpstan",
Expand Down
2 changes: 1 addition & 1 deletion src/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Db

protected Sql $protoSql;

public function __construct(protected PDO $connection, Sql|null $sqlPrototype = null)
public function __construct(protected readonly PDO $connection, Sql|null $sqlPrototype = null)
{
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->protoSql = $sqlPrototype ?: new Sql();
Expand Down
2 changes: 1 addition & 1 deletion src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class Mapper extends AbstractMapper implements
c\Mixable,
c\Typable
{
protected Db $db;
protected readonly Db $db;

public string $entityNamespace = '\\';

Expand Down
27 changes: 27 additions & 0 deletions tests/DbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,33 @@ public function testRawSqlWithParams(): void
$this->assertEquals(10, $line->testa);
}

public function testExecReturnsTrueOnSuccess(): void
{
$result = $this->object->insertInto('unit', ['testa' => 40, 'testb' => 'jkl'])
->values(['testa' => 40, 'testb' => 'jkl'])
->exec();
$this->assertTrue($result);
}

public function testGetConnectionReturnsPdoInstance(): void
{
$connection = $this->object->getConnection();
$this->assertInstanceOf(PDO::class, $connection);
}

public function testFetchAllWithCallback(): void
{
$all = $this->object->select('*')->from('unit')->fetchAll(
static function ($row) {
$row->extra = 'callback';

return $row;
},
);
$this->assertCount(3, $all);
$this->assertEquals('callback', $all[0]->extra);
}

protected function tearDown(): void
{
unset($this->object);
Expand Down
39 changes: 39 additions & 0 deletions tests/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,45 @@ public function testShouldNotExecuteEntityConstructorWhenDisabled(): void
);
}

public function testFetchWithStringConditionUsingColumnExpression(): void
{
$mapper = $this->mapper;
$comments = $mapper->comment(['comment.id > 0'])->fetchAll();
$this->assertCount(2, $comments);
}

public function testPersistNewEntityWithNoAutoIncrementId(): void
{
$conn = $this->createStub(PDO::class);
$conn->method('getAttribute')
->willReturn('sqlite');
$stmt = $this->createStub(PDOStatement::class);
$stmt->method('execute')
->willReturn(true);
$conn->method('prepare')
->willReturn($stmt);
$conn->method('lastInsertId')
->willReturn('0');
$conn->method('beginTransaction')
->willReturn(true);
$conn->method('commit')
->willReturn(true);
$mapper = new Mapper($conn);
$obj = new stdClass();
$obj->id = null;
$obj->name = 'test';
$mapper->foo->persist($obj);
$mapper->flush();
$this->assertNull($obj->id);
}

public function testFetchReturnsDbInstance(): void
{
$db = new Db($this->conn);
$mapper = new Mapper($db);
$this->assertInstanceOf(Db::class, $mapper->getDb());
}

private function query(string $sql): PDOStatement
{
$stmt = $this->conn->query($sql);
Expand Down
30 changes: 30 additions & 0 deletions tests/SqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,4 +507,34 @@ public function testInsertWithSelectSubquery(): void
);
$this->assertEquals(array_values($data), $this->object->getParams());
}

public function testEncloseWithStringWrapsInParentheses(): void
{
$result = Sql::enclose('SELECT 1');
$this->assertEquals('(SELECT 1) ', $result);
}

public function testEncloseWithEmptyStringReturnsEmpty(): void
{
$result = Sql::enclose('');
$this->assertEquals('', $result);
}

public function testEncloseWithSqlObjectWrapsQuery(): void
{
$sql = new Sql('SELECT 1');
$result = Sql::enclose($sql);
$this->assertInstanceOf(Sql::class, $result);
$this->assertEquals('(SELECT 1)', (string) $result);
}

public function testBuildOperationWithLeadingUnderscore(): void
{
$sql = (string) $this->object->select('*')->from('table')
->where('column > 1')->_innerSelect('f1')->from('t2')->_();
$this->assertEquals(
'SELECT * FROM table WHERE column > 1 (INNER SELECT f1 FROM t2)',
$sql,
);
}
}
6 changes: 6 additions & 0 deletions tests/Stubs/TestFetchingClassArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

class TestFetchingClassArgs
{
public int|null $testa = null;

public string|null $testb = null;

public int|null $testez = null;

public function __construct(public string|null $testd = null)
{
}
Expand Down
Loading