Skip to content

Commit 5018dd7

Browse files
feat: Update mutation workflow to support PostgreSQL with docker setup. (#69)
1 parent 595f12f commit 5018dd7

File tree

3 files changed

+175
-1
lines changed

3 files changed

+175
-1
lines changed

.github/workflows/mutation.yml

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,59 @@ jobs:
2121
mutation:
2222
uses: php-forge/actions/.github/workflows/infection.yml@main
2323
with:
24+
extensions: pdo, pdo_pgsql, pdo_sqlite
25+
framework-options: --test-framework-options="--group=sqlite,mutation"
26+
hook: |
27+
# Config MySQL with Docker
28+
docker run -d \
29+
--name mysql-test \
30+
-e MYSQL_ROOT_PASSWORD=root \
31+
-e MYSQL_DATABASE=yiitest \
32+
-e MYSQL_USER=test \
33+
-e MYSQL_PASSWORD=test \
34+
-p 3306:3306 \
35+
--health-cmd="mysqladmin ping -h localhost" \
36+
--health-interval=10s \
37+
--health-timeout=5s \
38+
--health-retries=5 \
39+
mysql:8.0
40+
41+
# Config PostgreSQL with Docker
42+
docker run -d \
43+
--name postgres-test \
44+
-e POSTGRES_DB=yiitest \
45+
-e POSTGRES_USER=root \
46+
-e POSTGRES_PASSWORD=root \
47+
-p 5432:5432 \
48+
--health-cmd="pg_isready -U postgres" \
49+
--health-interval=10s \
50+
--health-timeout=5s \
51+
--health-retries=3 \
52+
postgres:16
53+
54+
# Wait for MySQL to be ready
55+
echo "Waiting for MySQL to be ready..."
56+
timeout 120s bash -c 'until docker exec mysql-test mysqladmin ping -h localhost --silent; do sleep 3; done'
57+
58+
# Wait for PostgreSQL to be ready
59+
echo "Waiting for PostgreSQL to be ready..."
60+
timeout 60s bash -c 'until docker exec postgres-test pg_isready -U postgres; do sleep 2; done'
61+
62+
# Check if MySQL is running
63+
echo "Testing MySQL connection..."
64+
docker exec mysql-test mysql -u root -proot -e "SELECT VERSION();"
65+
66+
# Check if PostgreSQL is running
67+
echo "Testing PostgreSQL connection..."
68+
docker exec postgres-test psql -U root -d yiitest -c "SELECT version();"
69+
70+
# Set environment variables for MySQL and PostgreSQL
71+
echo "MYSQL_DSN=mysql:host=localhost;port=3306;dbname=yiitest" >> $GITHUB_ENV
72+
echo "MYSQL_USERNAME=root" >> $GITHUB_ENV
73+
echo "MYSQL_PASSWORD=root" >> $GITHUB_ENV
74+
echo "PGSQL_DSN=pgsql:host=localhost;port=5432;dbname=yiitest" >> $GITHUB_ENV
75+
echo "PGSQL_USERNAME=root" >> $GITHUB_ENV
76+
echo "PGSQL_PASSWORD=root" >> $GITHUB_ENV
2477
phpstan: true
25-
framework-options: --test-framework-options="--group=sqlite"
2678
secrets:
2779
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}

tests/mysql/MutationTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace yii2\extensions\nestedsets\tests\mysql;
6+
7+
use PHPUnit\Framework\Attributes\Group;
8+
use yii2\extensions\nestedsets\tests\support\model\MultipleTree;
9+
use yii2\extensions\nestedsets\tests\TestCase;
10+
11+
#[Group('mutation')]
12+
final class MutationTest extends TestCase
13+
{
14+
protected string $driverName = 'mysql';
15+
protected string|null $dsn = 'mysql:host=127.0.0.1;dbname=yiitest;charset=utf8mb4';
16+
protected string $password = 'root';
17+
protected string $username = 'root';
18+
19+
public function testLeavesMethodRequiresLeftAttributeOrderingForConsistentResults(): void
20+
{
21+
$this->createDatabase();
22+
23+
$root = new MultipleTree(['name' => 'Root']);
24+
25+
$root->makeRoot();
26+
27+
$leaf1 = new MultipleTree(['name' => 'Leaf A']);
28+
29+
$leaf1->appendTo($root);
30+
31+
$leaf2 = new MultipleTree(['name' => 'Leaf B']);
32+
33+
$leaf2->appendTo($root);
34+
35+
$initialLeaves = MultipleTree::find()->leaves()->all();
36+
37+
self::assertCount(
38+
2,
39+
$initialLeaves,
40+
"Should have exactly '2' initial leaf nodes.",
41+
);
42+
43+
$command = $this->getDb()->createCommand();
44+
45+
$command->update('multiple_tree', ['lft' => 3, 'rgt' => 4], ['name' => 'Leaf B'])->execute();
46+
$command->update('multiple_tree', ['lft' => 5, 'rgt' => 6], ['name' => 'Leaf A'])->execute();
47+
$command->update('multiple_tree', ['lft' => 1, 'rgt' => 7], ['name' => 'Root'])->execute();
48+
49+
$leaves = MultipleTree::find()->leaves()->all();
50+
51+
/** @phpstan-var array<array{name: string, lft: int}> */
52+
$expectedLeaves = [
53+
['name' => 'Leaf B', 'lft' => 3],
54+
['name' => 'Leaf A', 'lft' => 5],
55+
];
56+
57+
self::assertCount(
58+
2,
59+
$leaves,
60+
"Should return exactly '2' leaf nodes.",
61+
);
62+
63+
foreach ($leaves as $index => $leaf) {
64+
self::assertInstanceOf(
65+
MultipleTree::class,
66+
$leaf,
67+
"Leaf at index {$index} should be an instance of 'MultipleTree'.",
68+
);
69+
70+
if (isset($expectedLeaves[$index])) {
71+
self::assertEquals(
72+
$expectedLeaves[$index]['name'],
73+
$leaf->getAttribute('name'),
74+
"Leaf at index {$index} should be {$expectedLeaves[$index]['name']} in correct order.",
75+
);
76+
self::assertEquals(
77+
$expectedLeaves[$index]['lft'],
78+
$leaf->getAttribute('lft'),
79+
"Leaf at index {$index} should have left value {$expectedLeaves[$index]['lft']}.",
80+
);
81+
}
82+
}
83+
}
84+
}

tests/pgsql/MutationTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace yii2\extensions\nestedsets\tests\pgsql;
6+
7+
use PHPUnit\Framework\Attributes\Group;
8+
use yii2\extensions\nestedsets\tests\TestCase;
9+
10+
#[Group('mutation')]
11+
final class MutationTest extends TestCase
12+
{
13+
protected string $driverName = 'pgsql';
14+
protected string|null $dsn = 'pgsql:host=localhost;dbname=yiitest;port=5432;';
15+
protected string $password = 'root';
16+
protected string $username = 'root';
17+
18+
public function testChildrenMethodRequiresOrderByForCorrectTreeTraversal(): void
19+
{
20+
$expectedOrder = ['Child A', 'Child B', 'Child C'];
21+
22+
$treeStructure = [
23+
['name' => 'Root', 'children' => ['Child B', 'Child C', 'Child A']],
24+
];
25+
26+
$updates = [
27+
['name' => 'Child B', 'lft' => 4, 'rgt' => 5],
28+
['name' => 'Child C', 'lft' => 6, 'rgt' => 7],
29+
['name' => 'Child A', 'lft' => 2, 'rgt' => 3],
30+
['name' => 'Root', 'rgt' => 8],
31+
];
32+
33+
$tree = $this->createTreeStructure($treeStructure, $updates);
34+
$nodeList = $tree->children()->all();
35+
36+
$this->assertNodesInCorrectOrder($nodeList, $expectedOrder, 'Child');
37+
}
38+
}

0 commit comments

Comments
 (0)