• 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

78.57
/src/Operation/RowBased.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
 * Provides basic functionality for row based operations.
21
 *
22
 * To create a row based operation you must create two functions. The first
23
 * one, buildOperationQuery(), must return a query that will be used to create
24
 * a prepared statement. The second one, buildOperationArguments(), should
25
 * return an array containing arguments for each row.
26
 */
27
abstract class RowBased implements Operation
28
{
29
    protected const ITERATOR_TYPE_FORWARD = 0;
30
    protected const ITERATOR_TYPE_REVERSE = 1;
31

32
    /**
33
     * @var string
34
     */
35
    protected $operationName;
36

37
    protected $iteratorDirection = self::ITERATOR_TYPE_FORWARD;
38

39
    /**
40
     * @param Connection $connection
41
     * @param IDataSet $dataSet
42
     */
43
    public function execute(Connection $connection, IDataSet $dataSet): void
44
    {
45
        $databaseDataSet = $connection->createDataSet();
14✔
46

47
        $dsIterator = $this->iteratorDirection === self::ITERATOR_TYPE_REVERSE
14✔
48
            ? $dataSet->getReverseIterator()
1✔
49
            : $dataSet->getIterator();
14✔
50

51
        foreach ($dsIterator as $table) {
14✔
52
            $rowCount = $table->getRowCount();
14✔
53

54
            if ($rowCount === 0) {
14✔
55
                continue;
5✔
56
            }
57

58
            /* @var $table ITable */
59
            $databaseTableMetaData = $databaseDataSet->getTableMetaData($table->getTableMetaData()->getTableName());
13✔
60
            $query = $this->buildOperationQuery($databaseTableMetaData, $table, $connection);
13✔
61
            $disablePrimaryKeys = $this->disablePrimaryKeys($databaseTableMetaData, $table, $connection);
13✔
62

63
            if ($query === false) {
13✔
64
                if ($table->getRowCount() > 0) {
×
65
                    throw new Exception(
×
66
                        $this->operationName,
×
67
                        '',
×
68
                        [],
×
69
                        $table,
×
70
                        'Rows requested for insert, but no columns provided!'
×
71
                    );
×
72
                }
73

74
                continue;
×
75
            }
76

77
            if ($disablePrimaryKeys) {
13✔
78
                $connection->disablePrimaryKeys($databaseTableMetaData->getTableName());
11✔
79
            }
80

81
            $statement = $connection->getConnection()->prepare($query);
13✔
82

83
            for ($i = 0; $i < $rowCount; $i++) {
13✔
84
                $args = $this->buildOperationArguments($databaseTableMetaData, $table, $i);
13✔
85

86
                try {
87
                    $statement->execute($args);
13✔
88
                } catch (\Exception $e) {
1✔
89
                    throw new Exception(
1✔
90
                        $this->operationName,
1✔
91
                        $query,
1✔
92
                        $args,
1✔
93
                        $table,
1✔
94
                        $e->getMessage()
1✔
95
                    );
1✔
96
                }
97
            }
98

99
            if ($disablePrimaryKeys) {
12✔
100
                $connection->enablePrimaryKeys($databaseTableMetaData->getTableName());
11✔
101
            }
102
        }
103
    }
104

105
    /**
106
     * @param ITableMetadata $databaseTableMetaData
107
     * @param ITable $table
108
     * @param Connection $connection
109
     *
110
     * @return bool|string String containing the query or FALSE if a valid query cannot be constructed
111
     */
112
    abstract protected function buildOperationQuery(
113
        ITableMetadata $databaseTableMetaData,
114
        ITable $table,
115
        Connection $connection
116
    );
117

118
    abstract protected function buildOperationArguments(ITableMetadata $databaseTableMetaData, ITable $table, $row);
119

120
    /**
121
     * Allows an operation to disable primary keys if necessary.
122
     *
123
     * @param ITableMetadata $databaseTableMetaData
124
     * @param ITable $table
125
     * @param Connection $connection
126
     *
127
     * @return bool
128
     */
129
    protected function disablePrimaryKeys(ITableMetadata $databaseTableMetaData, ITable $table, Connection $connection): bool
130
    {
131
        return false;
3✔
132
    }
133

134
    protected function buildPreparedColumnArray($columns, Connection $connection): array
135
    {
136
        $columnArray = [];
3✔
137

138
        foreach ($columns as $columnName) {
3✔
139
            $columnArray[] = "{$connection->quoteSchemaObject($columnName)} = ?";
3✔
140
        }
141

142
        return $columnArray;
3✔
143
    }
144
}
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