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

codeigniter4 / CodeIgniter4 / 10136438005

29 Jul 2024 01:17AM UTC coverage: 84.474% (-0.004%) from 84.478%
10136438005

push

github

kenjis
Merge remote-tracking branch 'upstream/develop' into 4.6

 Conflicts:
	tests/system/Autoloader/AutoloaderTest.php
	tests/system/CommonFunctionsTest.php
	tests/system/Config/ServicesTest.php

20474 of 24237 relevant lines covered (84.47%)

189.15 hits per line

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

10.71
/system/Test/Constraints/SeeInDatabase.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Test\Constraints;
15

16
use CodeIgniter\Database\ConnectionInterface;
17
use PHPUnit\Framework\Constraint\Constraint;
18

19
class SeeInDatabase extends Constraint
20
{
21
    /**
22
     * The number of results that will show in the database
23
     * in case of failure.
24
     *
25
     * @var int
26
     */
27
    protected $show = 3;
28

29
    /**
30
     * @var ConnectionInterface
31
     */
32
    protected $db;
33

34
    /**
35
     * Data used to compare results against.
36
     *
37
     * @var array
38
     */
39
    protected $data;
40

41
    /**
42
     * SeeInDatabase constructor.
43
     */
44
    public function __construct(ConnectionInterface $db, array $data)
45
    {
46
        $this->db   = $db;
114✔
47
        $this->data = $data;
114✔
48
    }
49

50
    /**
51
     * Check if data is found in the table
52
     *
53
     * @param mixed $table
54
     */
55
    protected function matches($table): bool
56
    {
57
        return $this->db->table($table)->where($this->data)->countAllResults() > 0;
114✔
58
    }
59

60
    /**
61
     * Get the description of the failure
62
     *
63
     * @param mixed $table
64
     */
65
    protected function failureDescription($table): string
66
    {
67
        return sprintf(
×
68
            "a row in the table [%s] matches the attributes \n%s\n\n%s",
×
69
            $table,
×
70
            $this->toString(false, JSON_PRETTY_PRINT),
×
71
            $this->getAdditionalInfo($table)
×
72
        );
×
73
    }
74

75
    /**
76
     * Gets additional records similar to $data.
77
     */
78
    protected function getAdditionalInfo(string $table): string
79
    {
80
        $builder = $this->db->table($table);
×
81

82
        $similar = $builder->where(
×
83
            array_key_first($this->data),
×
84
            $this->data[array_key_first($this->data)]
×
85
        )->limit($this->show)->get()->getResultArray();
×
86

87
        if ($similar !== []) {
×
88
            $description = 'Found similar results: ' . json_encode($similar, JSON_PRETTY_PRINT);
×
89
        } else {
90
            // Does the table have any results at all?
91
            $results = $this->db->table($table)
×
92
                ->limit($this->show)
×
93
                ->get()
×
94
                ->getResultArray();
×
95

96
            if ($results !== []) {
×
97
                return 'The table is empty.';
×
98
            }
99

100
            $description = 'Found: ' . json_encode($results, JSON_PRETTY_PRINT);
×
101
        }
102

103
        $total = $this->db->table($table)->countAll();
×
104
        if ($total > $this->show) {
×
105
            $description .= sprintf(' and %s others', $total - $this->show);
×
106
        }
107

108
        return $description;
×
109
    }
110

111
    /**
112
     * Gets a string representation of the constraint
113
     *
114
     * @param int $options
115
     */
116
    public function toString(bool $exportObjects = false, $options = 0): string
117
    {
118
        return json_encode($this->data, $options);
×
119
    }
120
}
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