Skip to content

Commit 7700dfa

Browse files
committed
tests: Add test for trait SharedDatabases
1 parent 12c9311 commit 7700dfa

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

.github/workflows/php.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ jobs:
5454
php: ['8.2', '8.3', '8.4']
5555
os: ['ubuntu-latest']
5656

57+
services:
58+
mysql:
59+
image: mariadb
60+
env:
61+
MYSQL_ROOT_PASSWORD: root
62+
MYSQL_DATABASE: icinga_unittest
63+
MYSQL_USER: icinga_unittest
64+
MYSQL_PASSWORD: icinga_unittest
65+
options: >-
66+
--health-cmd "mariadb -s -uroot -proot -e'SHOW DATABASES;' 2> /dev/null | grep icinga_unittest > test"
67+
--health-interval 10s
68+
--health-timeout 5s
69+
--health-retries 5
70+
ports:
71+
- 3306/tcp
72+
73+
pgsql:
74+
image: postgres
75+
env:
76+
POSTGRES_USER: icinga_unittest
77+
POSTGRES_PASSWORD: icinga_unittest
78+
POSTGRES_DB: icinga_unittest
79+
options: >-
80+
--health-cmd pg_isready
81+
--health-interval 10s
82+
--health-timeout 5s
83+
--health-retries 5
84+
ports:
85+
- 5432/tcp
86+
5787
steps:
5888
- name: Checkout code base
5989
uses: actions/checkout@v5
@@ -69,4 +99,15 @@ jobs:
6999
run: composer install -n --no-progress
70100

71101
- name: PHPUnit
102+
env:
103+
MYSQL_TESTDB: icinga_unittest
104+
MYSQL_TESTDB_HOST: 127.0.0.1
105+
MYSQL_TESTDB_PORT: ${{ job.services.mysql.ports['3306'] }}
106+
MYSQL_TESTDB_USER: icinga_unittest
107+
MYSQL_TESTDB_PASSWORD: icinga_unittest
108+
PGSQL_TESTDB: icinga_unittest
109+
PGSQL_TESTDB_HOST: 127.0.0.1
110+
PGSQL_TESTDB_PORT: ${{ job.services.pgsql.ports['5432'] }}
111+
PGSQL_TESTDB_USER: icinga_unittest
112+
PGSQL_TESTDB_PASSWORD: icinga_unittest
72113
run: phpunit --verbose

tests/SharedDatabasesTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace ipl\Tests\Sql;
4+
5+
use ipl\Sql\Connection;
6+
use ipl\Sql\Select;
7+
use ipl\Sql\Test\SharedDatabases;
8+
9+
/**
10+
* A test for a test component! Yay!
11+
*/
12+
class SharedDatabasesTest extends TestCase
13+
{
14+
use SharedDatabases;
15+
16+
/** @dataProvider sharedDatabases */
17+
public function testInsert(Connection $db)
18+
{
19+
// This is the first case, so the table must have been dropped and be empty
20+
$result = $db->select((new Select())->columns('name')->from('test'))->fetchAll();
21+
$this->assertEmpty($result);
22+
23+
$db->insert('test', ['name' => 'test']);
24+
$db->insert('test', ['name' => 'test2']);
25+
}
26+
27+
/**
28+
* @depends testInsert
29+
* @dataProvider sharedDatabases
30+
*/
31+
public function testSelect(Connection $db)
32+
{
33+
// The previous case inserts "name=test" but tearDown removes it
34+
$result = $db->select((new Select())->columns('name')->from('test'))->fetchAll();
35+
$this->assertCount(1, $result);
36+
$this->assertSame('test2', $result[0]['name']);
37+
}
38+
39+
/**
40+
* @depends testSelect
41+
* @dataProvider sharedDatabases
42+
*/
43+
public function testUpdate(Connection $db)
44+
{
45+
$stmt = $db->update('test', ['name' => 'test3'], ['name = ?' => 'test2']);
46+
$this->assertEquals(1, $stmt->rowCount());
47+
}
48+
49+
/**
50+
* @depends testUpdate
51+
* @dataProvider sharedDatabases
52+
*/
53+
public function testDelete(Connection $db)
54+
{
55+
$stmt = $db->delete('test', ['name = ?' => 'test3']);
56+
$this->assertEquals(1, $stmt->rowCount());
57+
}
58+
59+
protected static function setUpSchema(Connection $db, string $driver): void
60+
{
61+
$db->exec('CREATE TABLE test (name VARCHAR(255))');
62+
}
63+
64+
protected static function tearDownSchema(Connection $db, string $driver): void
65+
{
66+
$db->exec('DROP TABLE IF EXISTS test');
67+
}
68+
69+
public function tearDown(): void
70+
{
71+
$this->getConnection()->delete('test', ['name = ?' => ['test']]);
72+
}
73+
}

0 commit comments

Comments
 (0)