• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

misantron / dbunit / 4562384438

pending completion
4562384438

push

github

Aleksandr Ivanov
Fix build status badge display

635 of 1200 relevant lines covered (52.92%)

5.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.82
/src/Operation/Replace.php
1
<?php
2

3
/*
4
 * This file is part of DbUnit.
5
 *
6
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace PHPUnit\DbUnit\Operation;
13

14
use PHPUnit\DbUnit\Database\Connection;
15
use PHPUnit\DbUnit\DataSet\IDataSet;
16
use PHPUnit\DbUnit\DataSet\ITable;
17
use PHPUnit\DbUnit\DataSet\ITableMetadata;
18

19
/**
20
 * Updates the rows in a given dataset using primary key columns.
21
 */
22
class Replace extends RowBased
23
{
24
    protected $operationName = 'REPLACE';
25

26
    /**
27
     * @param Connection $connection
28
     * @param IDataSet $dataSet
29
     */
30
    public function execute(Connection $connection, IDataSet $dataSet): void
31
    {
32
        $insertOperation = new Insert();
1✔
33
        $updateOperation = new Update();
1✔
34
        $databaseDataSet = $connection->createDataSet();
1✔
35

36
        foreach ($dataSet as $table) {
1✔
37
            /* @var $table ITable */
38
            $databaseTableMetaData = $databaseDataSet->getTableMetaData($table->getTableMetaData()->getTableName());
1✔
39

40
            $insertQuery = $insertOperation->buildOperationQuery($databaseTableMetaData, $table, $connection);
1✔
41
            $updateQuery = $updateOperation->buildOperationQuery($databaseTableMetaData, $table, $connection);
1✔
42
            $selectQuery = $this->buildOperationQuery($databaseTableMetaData, $table, $connection);
1✔
43

44
            $insertStatement = $connection->getConnection()->prepare($insertQuery);
1✔
45
            $updateStatement = $connection->getConnection()->prepare($updateQuery);
1✔
46
            $selectStatement = $connection->getConnection()->prepare($selectQuery);
1✔
47

48
            $rowCount = $table->getRowCount();
1✔
49

50
            for ($i = 0; $i < $rowCount; $i++) {
1✔
51
                $selectArgs = $this->buildOperationArguments($databaseTableMetaData, $table, $i);
1✔
52
                $query = $selectQuery;
1✔
53
                $args = $selectArgs;
1✔
54

55
                try {
56
                    $selectStatement->execute($selectArgs);
1✔
57

58
                    if ($selectStatement->fetchColumn(0) > 0) {
1✔
59
                        $updateArgs = $updateOperation->buildOperationArguments($databaseTableMetaData, $table, $i);
1✔
60
                        $query = $updateQuery;
1✔
61
                        $args = $updateArgs;
1✔
62

63
                        $updateStatement->execute($updateArgs);
1✔
64
                    } else {
65
                        $insertArgs = $insertOperation->buildOperationArguments($databaseTableMetaData, $table, $i);
1✔
66
                        $query = $insertQuery;
1✔
67
                        $args = $insertArgs;
1✔
68

69
                        $insertStatement->execute($insertArgs);
1✔
70
                    }
71
                } catch (\Throwable $e) {
×
72
                    throw new Exception(
×
73
                        $this->operationName,
×
74
                        $query,
×
75
                        $args,
×
76
                        $table,
×
77
                        $e->getMessage()
×
78
                    );
×
79
                }
80
            }
81
        }
82
    }
83

84
    protected function buildOperationQuery(
85
        ITableMetadata $databaseTableMetaData,
86
        ITable $table,
87
        Connection $connection
88
    ): string {
89
        $keys = $databaseTableMetaData->getPrimaryKeys();
1✔
90

91
        $whereStatement = 'WHERE ' . implode(' AND ', $this->buildPreparedColumnArray($keys, $connection));
1✔
92

93
        return "
1✔
94
            SELECT COUNT(*)
95
            FROM {$connection->quoteSchemaObject($table->getTableMetaData()->getTableName())}
1✔
96
            {$whereStatement}
1✔
97
        ";
1✔
98
    }
99

100
    protected function buildOperationArguments(ITableMetadata $databaseTableMetaData, ITable $table, $row): array
101
    {
102
        $args = [];
1✔
103

104
        foreach ($databaseTableMetaData->getPrimaryKeys() as $columnName) {
1✔
105
            $args[] = $table->getValue($row, $columnName);
1✔
106
        }
107

108
        return $args;
1✔
109
    }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc