diff --git a/.github/workflows/mutation.yml b/.github/workflows/mutation.yml index 6d35498..aa68e8a 100644 --- a/.github/workflows/mutation.yml +++ b/.github/workflows/mutation.yml @@ -21,7 +21,59 @@ jobs: mutation: uses: php-forge/actions/.github/workflows/infection.yml@main with: + extensions: pdo, pdo_pgsql, pdo_sqlite + framework-options: --test-framework-options="--group=sqlite,mutation" + hook: | + # Config MySQL with Docker + docker run -d \ + --name mysql-test \ + -e MYSQL_ROOT_PASSWORD=root \ + -e MYSQL_DATABASE=yiitest \ + -e MYSQL_USER=test \ + -e MYSQL_PASSWORD=test \ + -p 3306:3306 \ + --health-cmd="mysqladmin ping -h localhost" \ + --health-interval=10s \ + --health-timeout=5s \ + --health-retries=5 \ + mysql:8.0 + + # Config PostgreSQL with Docker + docker run -d \ + --name postgres-test \ + -e POSTGRES_DB=yiitest \ + -e POSTGRES_USER=root \ + -e POSTGRES_PASSWORD=root \ + -p 5432:5432 \ + --health-cmd="pg_isready -U postgres" \ + --health-interval=10s \ + --health-timeout=5s \ + --health-retries=3 \ + postgres:16 + + # Wait for MySQL to be ready + echo "Waiting for MySQL to be ready..." + timeout 120s bash -c 'until docker exec mysql-test mysqladmin ping -h localhost --silent; do sleep 3; done' + + # Wait for PostgreSQL to be ready + echo "Waiting for PostgreSQL to be ready..." + timeout 60s bash -c 'until docker exec postgres-test pg_isready -U postgres; do sleep 2; done' + + # Check if MySQL is running + echo "Testing MySQL connection..." + docker exec mysql-test mysql -u root -proot -e "SELECT VERSION();" + + # Check if PostgreSQL is running + echo "Testing PostgreSQL connection..." + docker exec postgres-test psql -U root -d yiitest -c "SELECT version();" + + # Set environment variables for MySQL and PostgreSQL + echo "MYSQL_DSN=mysql:host=localhost;port=3306;dbname=yiitest" >> $GITHUB_ENV + echo "MYSQL_USERNAME=root" >> $GITHUB_ENV + echo "MYSQL_PASSWORD=root" >> $GITHUB_ENV + echo "PGSQL_DSN=pgsql:host=localhost;port=5432;dbname=yiitest" >> $GITHUB_ENV + echo "PGSQL_USERNAME=root" >> $GITHUB_ENV + echo "PGSQL_PASSWORD=root" >> $GITHUB_ENV phpstan: true - framework-options: --test-framework-options="--group=sqlite" secrets: STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} diff --git a/tests/mysql/MutationTest.php b/tests/mysql/MutationTest.php new file mode 100644 index 0000000..53eac97 --- /dev/null +++ b/tests/mysql/MutationTest.php @@ -0,0 +1,84 @@ +createDatabase(); + + $root = new MultipleTree(['name' => 'Root']); + + $root->makeRoot(); + + $leaf1 = new MultipleTree(['name' => 'Leaf A']); + + $leaf1->appendTo($root); + + $leaf2 = new MultipleTree(['name' => 'Leaf B']); + + $leaf2->appendTo($root); + + $initialLeaves = MultipleTree::find()->leaves()->all(); + + self::assertCount( + 2, + $initialLeaves, + "Should have exactly '2' initial leaf nodes.", + ); + + $command = $this->getDb()->createCommand(); + + $command->update('multiple_tree', ['lft' => 3, 'rgt' => 4], ['name' => 'Leaf B'])->execute(); + $command->update('multiple_tree', ['lft' => 5, 'rgt' => 6], ['name' => 'Leaf A'])->execute(); + $command->update('multiple_tree', ['lft' => 1, 'rgt' => 7], ['name' => 'Root'])->execute(); + + $leaves = MultipleTree::find()->leaves()->all(); + + /** @phpstan-var array */ + $expectedLeaves = [ + ['name' => 'Leaf B', 'lft' => 3], + ['name' => 'Leaf A', 'lft' => 5], + ]; + + self::assertCount( + 2, + $leaves, + "Should return exactly '2' leaf nodes.", + ); + + foreach ($leaves as $index => $leaf) { + self::assertInstanceOf( + MultipleTree::class, + $leaf, + "Leaf at index {$index} should be an instance of 'MultipleTree'.", + ); + + if (isset($expectedLeaves[$index])) { + self::assertEquals( + $expectedLeaves[$index]['name'], + $leaf->getAttribute('name'), + "Leaf at index {$index} should be {$expectedLeaves[$index]['name']} in correct order.", + ); + self::assertEquals( + $expectedLeaves[$index]['lft'], + $leaf->getAttribute('lft'), + "Leaf at index {$index} should have left value {$expectedLeaves[$index]['lft']}.", + ); + } + } + } +} diff --git a/tests/pgsql/MutationTest.php b/tests/pgsql/MutationTest.php new file mode 100644 index 0000000..a6ded69 --- /dev/null +++ b/tests/pgsql/MutationTest.php @@ -0,0 +1,38 @@ + 'Root', 'children' => ['Child B', 'Child C', 'Child A']], + ]; + + $updates = [ + ['name' => 'Child B', 'lft' => 4, 'rgt' => 5], + ['name' => 'Child C', 'lft' => 6, 'rgt' => 7], + ['name' => 'Child A', 'lft' => 2, 'rgt' => 3], + ['name' => 'Root', 'rgt' => 8], + ]; + + $tree = $this->createTreeStructure($treeStructure, $updates); + $nodeList = $tree->children()->all(); + + $this->assertNodesInCorrectOrder($nodeList, $expectedOrder, 'Child'); + } +}