Skip to content

Commit

Permalink
Switch to dbal 3 (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: Dawid Łakomski <[email protected]>
  • Loading branch information
bronek89 and dlakomski authored Nov 14, 2022
1 parent dcdfa17 commit 09adf98
Show file tree
Hide file tree
Showing 25 changed files with 348 additions and 138 deletions.
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2
updates:
- package-ecosystem: composer
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
92 changes: 92 additions & 0 deletions .github/workflows/continous-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: "Continuous Integration"

on:
pull_request:
branches:
- 'feature/**'
push:
branches:
- 'feature/**'

jobs:
phpunit:
name: "PHPUnit tests"
runs-on: "${{ matrix.os }}"

strategy:
matrix:
os:
- "ubuntu-22.04"
php-version:
- "8.1"
- "8.2"
dependencies:
- "highest"
- "lowest"

steps:
- name: "Checkout"
uses: "actions/checkout@v3"
with:
fetch-depth: 2

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"
with:
composer-options: "--ignore-platform-req=php+"
dependency-versions: "${{ matrix.dependencies }}"

phpunit-integration:
name: "PHPUnit db integration tests"
runs-on: "ubuntu-22.04"
needs: "phpunit"

strategy:
matrix:
php-version:
- "8.1"

services:
postgres:
image: "postgres:15"
env:
POSTGRES_PASSWORD: "postgres"

options: >-
--health-cmd "pg_isready"
ports:
- "5432:5432"

mysql:
image: "mysql:8.0"

# https://stackoverflow.com/questions/60902904/how-to-pass-mysql-native-password-to-mysql-service-in-github-actions
options: >-
--health-cmd "mysqladmin ping --silent"
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes
-e MYSQL_DATABASE=dqo
--entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
ports:
- "3306:3306"

steps:
- name: "Checkout"
uses: "actions/checkout@v3"
with:
fetch-depth: 2

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"

- name: "Run PHPUnit"
run: "vendor/bin/phpunit -c ci/phpunit/integration.xml"
36 changes: 36 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: "Static Analysis"

on:
pull_request:
branches:
- 'feature/**'
push:
branches:
- 'feature/**'

jobs:
static-analysis-phpstan:
name: "Static Analysis with PHPStan"
runs-on: "ubuntu-22.04"

strategy:
matrix:
php-version:
- "8.1"

steps:
- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
tools: "cs2pr"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"

- name: "Run a static analysis with phpstan/phpstan"
run: "vendor/bin/phpstan --error-format=checkstyle | cs2pr"
35 changes: 0 additions & 35 deletions .travis.yml

This file was deleted.

5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ RUN groupadd -g ${GID} -r ${USER} && useradd -u ${UID} -rm -s /bin/zsh -g ${USER
&& mkdir -p /home/${USER}/Downloads \
&& chown -R ${USER}:${USER} /home/${USER}


RUN apt install wget && wget https://get.symfony.com/cli/installer -O - | bash
RUN mv /root/.symfony/bin/symfony /usr/local/bin/symfony
RUN apt install wget && wget https://get.symfony.com/cli/installer -O - | bash
RUN mv /root/.symfony5/bin/symfony /usr/local/bin/symfony

RUN wget https://cs.symfony.com/download/php-cs-fixer-v2.phar -O php-cs-fixer \
&& chmod a+x php-cs-fixer \
Expand Down
30 changes: 30 additions & 0 deletions ci/phpunit/integration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="../../tests/bootstrap.php"
executionOrder="depends,defects"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
verbose="true"
defaultTestSuite="unit">

<testsuites>
<testsuite name="integration">
<directory>../../tests/Usage</directory>
<exclude>../../tests/Usage/symfony</exclude>
</testsuite>
</testsuites>

<php>
<env name="POSTGRES_HOST" value="localhost" />
<env name="POSTGRES_DATABASE" value="dqo" />
<env name="POSTGRES_USER" value="postgres" />
<env name="POSTGRES_PASSWORD" value="postgres" />
<env name="MYSQL_HOST" value="127.0.0.1" />
<env name="MYSQL_DATABASE" value="dqo" />
<env name="MYSQL_USER" value="root" />
<env name="MYSQL_PASSWORD" value="" />
</php>
</phpunit>
21 changes: 11 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
"type": "library",
"license": "MIT",
"require": {
"php": ">=8.0",
"doctrine/dbal": "^2.13.9",
"php": ">=8.1",
"doctrine/dbal": "^3.3",
"gowork/values": "^0.6",
"gowork/safe": "^0.2.0",
"symfony/console": "^5.0|^6.0",
"symfony/dependency-injection": "^5.0|^6.0",
"symfony/http-kernel": "^5.0|^6.0",
"symfony/config": "^5.0|^6.0",
"symfony/process": "^5.0|^6.0",
"gowork/safe": "^0.2",
"symfony/console": "^6.0",
"symfony/dependency-injection": "^6.0",
"symfony/http-kernel": "^6.0",
"symfony/config": "^6.0",
"symfony/process": "^6.0",
"phpdocumentor/reflection-docblock": "^5.2",
"open-serializer/type": "dev-main",
"open-serializer/type": "^0.1",
"nikic/php-parser": "^4.10"
},
"require-dev": {
"phpstan/phpstan": "^1.7",
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"phpstan/phpstan-strict-rules": "^1.4"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 9
paths:
- src

includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
9 changes: 4 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.4/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="tests/bootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
verbose="true"
defaultTestSuite="unit">

<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">tests</directory>
<directory>tests</directory>
<exclude>tests/Usage</exclude>
</testsuite>
<testsuite name="integration">
<directory suffix="Test.php">tests/Usage</directory>
<directory>tests/Usage</directory>
<exclude>tests/Usage/symfony</exclude>
</testsuite>
</testsuites>
Expand Down
35 changes: 10 additions & 25 deletions src/DatabaseSelectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
use DateTimeImmutable;
use Dazet\TypeUtil\StringUtil;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Query\QueryBuilder;
use GW\Value\ArrayValue;
use GW\Value\Wrap;
use RuntimeException;
use function array_merge;
use function get_class;
use function is_array;
use function is_int;
use function is_object;
use function is_string;

final class DatabaseSelectBuilder
{
Expand Down Expand Up @@ -160,11 +157,7 @@ function (string $field): string {

public function fetchColumn(int $index = 0): false|string|null
{
$statement = (clone $this->builder)->setMaxResults(1)->execute();

if (is_int($statement) || is_string($statement)) {
throw new RuntimeException("Expected select query");
}
$statement = (clone $this->builder)->setMaxResults(1)->executeQuery();

if ($index > 0) {
$row = $statement->fetchNumeric();
Expand All @@ -189,27 +182,27 @@ public function fetchDate(int $index = 0): ?DateTimeImmutable
{
$date = $this->fetchColumn($index);

return $date ? new DateTimeImmutable($date) : null;
return is_string($date) ? new DateTimeImmutable($date) : null;
}

/**
* @return array<array<string, string|int|float|bool|null>>
*/
public function fetchAll(): array
{
/** @var ResultStatement<mixed> $statement */
$statement = (clone $this->builder)->execute();
$statement = (clone $this->builder)->executeQuery();

return $statement->fetchAll();
/** @phpstan-ignore-next-line */
return $statement->fetchAllAssociative();
}

/** @return array<string, string|int|float|bool|null>|null */
public function fetch(): ?array
{
/** @var ResultStatement<mixed> $statement */
$statement = (clone $this->builder)->execute();
$result = $statement->fetch();
$statement = (clone $this->builder)->executeQuery();
$result = $statement->fetchAssociative();

/** @phpstan-ignore-next-line */
return $result !== false ? $result : null;
}

Expand Down Expand Up @@ -262,11 +255,7 @@ public function resetOrderBy(): self
return $copy;
}

/**
* @param mixed $value
* @param string|int|null $type
*/
public function withParameter(string $key, $value, $type = null): self
public function withParameter(string $key, mixed $value, int|string $type = null): self
{
$copy = clone $this;
$copy->builder->setParameter($key, $value, $type ?? $this->paramType($value));
Expand Down Expand Up @@ -326,11 +315,7 @@ public function startOffset(): int
return $this->startOffset;
}

/**
* @param mixed $object
* @return string|int|null
*/
private function paramType($object)
private function paramType(mixed $object): int|string|null
{
if (is_array($object)) {
return Connection::PARAM_STR_ARRAY;
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function renderQueryFile(Table $table): string
$methods = [];
$uses = [];

if ($idColumn) {
if ($idColumn !== null) {
$methods[] = $factory->method('single')
->addParam($factory->param($idColumn->methodName())->setType($this->typeDef($idColumn, $this->types->type($idColumn->type()), $uses)))
->setReturnType("?{$table->name()}Row")
Expand Down
Loading

0 comments on commit 09adf98

Please sign in to comment.