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

tempestphp / tempest-framework / 14140550176

28 Mar 2025 10:29PM UTC coverage: 80.716% (+1.4%) from 79.334%
14140550176

push

github

web-flow
feat(support): support `$default` on array `first` and `last` methods (#1096)

11 of 12 new or added lines in 2 files covered. (91.67%)

135 existing lines in 16 files now uncovered.

10941 of 13555 relevant lines covered (80.72%)

100.32 hits per line

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

92.5
/src/Tempest/Database/src/GenericDatabase.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Database;
6

7
use BackedEnum;
8
use DateTimeInterface;
9
use PDO;
10
use PDOException;
11
use Tempest\Database\Connection\Connection;
12
use Tempest\Database\Exceptions\QueryException;
13
use Tempest\Database\Transactions\TransactionManager;
14
use Throwable;
15

16
final readonly class GenericDatabase implements Database
17
{
18
    public function __construct(
707✔
19
        private Connection $connection,
20
        private TransactionManager $transactionManager,
21
    ) {}
707✔
22

23
    public function execute(Query $query): void
705✔
24
    {
25
        $bindings = $this->resolveBindings($query);
705✔
26

27
        try {
28
            $this->connection
705✔
29
                ->prepare($query->getSql())
705✔
30
                ->execute($bindings);
705✔
31
        } catch (PDOException $pdoException) {
2✔
32
            throw new QueryException($query, $bindings, $pdoException);
2✔
33
        }
34
    }
35

36
    public function getLastInsertId(): Id
705✔
37
    {
38
        return new Id($this->connection->lastInsertId());
705✔
39
    }
40

41
    public function fetch(Query $query): array
705✔
42
    {
43
        $pdoQuery = $this->connection->prepare($query->getSql());
705✔
44

45
        $pdoQuery->execute($this->resolveBindings($query));
705✔
46

47
        return $pdoQuery->fetchAll(PDO::FETCH_NAMED);
705✔
48
    }
49

50
    public function fetchFirst(Query $query): ?array
1✔
51
    {
52
        return $this->fetch($query)[0] ?? null;
1✔
53
    }
54

55
    public function withinTransaction(callable $callback): bool
4✔
56
    {
57
        $this->transactionManager->begin();
4✔
58

59
        try {
60
            $callback();
4✔
61

62
            $this->transactionManager->commit();
2✔
63
        } catch (PDOException) {
2✔
64
            return false;
×
65
        } catch (Throwable) {
2✔
66
            $this->transactionManager->rollback();
2✔
67

68
            return false;
2✔
69
        }
70

71
        return true;
2✔
72
    }
73

74
    private function resolveBindings(Query $query): array
705✔
75
    {
76
        $bindings = [];
705✔
77

78
        foreach ($query->bindings as $key => $value) {
705✔
79
            // TODO: this should be handled by serializers (except the Query)
80
            if ($value instanceof Id) {
65✔
81
                $value = $value->id;
21✔
82
            }
83

84
            if ($value instanceof Query) {
65✔
85
                $value = $value->execute();
11✔
86
            }
87

88
            if ($value instanceof BackedEnum) {
65✔
UNCOV
89
                $value = $value->value;
×
90
            }
91

92
            if ($value instanceof DateTimeInterface) {
65✔
UNCOV
93
                $value = $value->format('Y-m-d H:i:s');
×
94
            }
95

96
            $bindings[$key] = $value;
65✔
97
        }
98

99
        return $bindings;
705✔
100
    }
101
}
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

© 2025 Coveralls, Inc