Skip to content

Commit

Permalink
Merge pull request #7 from netlogix/feat/run-tests
Browse files Browse the repository at this point in the history
feat: Add workflow for unit tests
  • Loading branch information
stephanschuler authored Oct 30, 2024
2 parents 19acc2d + 6c77002 commit fa993ac
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 11 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: unittests

on: [ push, pull_request ]

jobs:
unittests:
name: '[PHP ${{ matrix.php-version }} | DBAL ${{ matrix.dbal-version }}] Unit Tests'
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php-version: [ 8.1, 8.2, 8.3 ]
dbal-version: [ 2.13, 3.8 ]

env:
APP_ENV: true
TEST_DIR: test-dir

steps:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, xml, json, zlib, iconv, intl, pdo_sqlite
ini-values: opcache.fast_shutdown=0

- name: Checkout code
uses: actions/checkout@v4
with:
path: ${{ env.TEST_DIR }}/dist/upsert

- name: "Create composer.json"
run: |
echo '{
"name": "netlogix/upsert-test",
"description": "",
"license": "GPL-3.0-or-later",
"require": {
"netlogix/doctrine-upsert": "@dev",
"doctrine/dbal": "^${{ matrix.dbal-version }}"
},
"require-dev": {
"phpunit/phpunit": "^10.5"
},
"repositories": [
{
"type": "path",
"url": "dist/upsert"
}
]
}' > composer.json
working-directory: ${{ env.TEST_DIR }}

- name: "Install project"
run: composer install
working-directory: ${{ env.TEST_DIR }}

- name: Run tests
run: vendor/bin/phpunit -c dist/upsert/phpunit.xml.dist --testsuite="Unit"
working-directory: ${{ env.TEST_DIR }}
30 changes: 30 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<phpunit
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
timeoutForSmallTests="0">
<testsuites>
<testsuite name="Unit">
<directory>./test/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./Classes</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="Build/Artifacts/Reports/PhpUnit/Coverage"/>
<log type="coverage-clover" target="Build/Artifacts/Reports/PhpUnit/clover.xml"/>
<log type="coverage-crap4j" target="Build/Artifacts/Reports/PhpUnit/crap4j.xml"/>
<log type="junit" target="Build/Artifacts/Reports/PhpUnit/junit.xml"/>
</logging>
<php>
<ini name="date.timezone" value="Europe/Berlin"/>
<ini name="error_reporting" value="E_ALL &amp; ~E_DEPRECATED"/>
<env name="FLOW_REWRITEURLS" value="1"/>
</php>
</phpunit>
23 changes: 12 additions & 11 deletions test/UpsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\ForwardCompatibility\DriverResultStatement;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\AbstractPlatform;
Expand Down Expand Up @@ -211,20 +211,20 @@ public function Table_Name_is_used_in_Query(): void
public function Insert_works(): void
{
$connection = $this->getSQLiteConnection();
$connection->exec('CREATE TABLE foo_table(bar TEXT PRIMARY KEY, count INT);');
$connection->executeStatement('CREATE TABLE foo_table(bar TEXT PRIMARY KEY, count INT);');

Upsert::fromConnection($connection)
->forTable('foo_table')
->withIdentifier('bar', 'baz')
->withField('count', 1)
->execute();

$values = $connection->fetchAll('SELECT * FROM foo_table');
$values = $connection->fetchAllAssociative('SELECT * FROM foo_table');
self::assertCount(1, $values);
self::assertSame(
[
'bar' => 'baz',
'count' => '1'
'count' => 1
],
$values[0]
);
Expand All @@ -236,29 +236,29 @@ public function Insert_works(): void
public function Upsert_works(): void
{
$connection = $this->getSQLiteConnection();
$connection->exec('CREATE TABLE foo_table(bar TEXT PRIMARY KEY, count INT);');
$connection->exec('INSERT INTO foo_table (bar, count) VALUES ("baz", 1)');
$connection->executeStatement('CREATE TABLE foo_table(bar TEXT PRIMARY KEY, count INT);');
$connection->executeStatement('INSERT INTO foo_table (bar, count) VALUES ("baz", 1)');

Upsert::fromConnection($connection)
->forTable('foo_table')
->withIdentifier('bar', 'baz')
->withField('count', 2)
->execute();

$values = $connection->fetchAll('SELECT * FROM foo_table');
$values = $connection->fetchAllAssociative('SELECT * FROM foo_table');
self::assertCount(1, $values);
self::assertSame(
[
'bar' => 'baz',
'count' => '2'
'count' => 2
],
$values[0]
);
}

private function getSQLiteConnection(): Connection
{
if (!extension_loaded('sqlite')) {
if (!extension_loaded('sqlite3')) {
self::markTestSkipped('ext-sqlite3 is required for tests');
}

Expand Down Expand Up @@ -294,9 +294,10 @@ private function getMockConnection()
return $connection;
}

private function getMockResult(int $rowCount): DriverResultStatement
private function getMockResult(int $rowCount): Result
{
$result = $this->getMockBuilder(DriverResultStatement::class)
$result = $this->getMockBuilder(Result::class)
->disableOriginalConstructor()
->getMock();

$result
Expand Down

0 comments on commit fa993ac

Please sign in to comment.