Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Testing\Constraints;
4
 
5
use Illuminate\Database\Connection;
6
use Illuminate\Database\Query\Expression;
7
use PHPUnit\Framework\Constraint\Constraint;
8
 
9
class HasInDatabase extends Constraint
10
{
11
    /**
12
     * Number of records that will be shown in the console in case of failure.
13
     *
14
     * @var int
15
     */
16
    protected $show = 3;
17
 
18
    /**
19
     * The database connection.
20
     *
21
     * @var \Illuminate\Database\Connection
22
     */
23
    protected $database;
24
 
25
    /**
26
     * The data that will be used to narrow the search in the database table.
27
     *
28
     * @var array
29
     */
30
    protected $data;
31
 
32
    /**
33
     * Create a new constraint instance.
34
     *
35
     * @param  \Illuminate\Database\Connection  $database
36
     * @param  array  $data
37
     * @return void
38
     */
39
    public function __construct(Connection $database, array $data)
40
    {
41
        $this->data = $data;
42
 
43
        $this->database = $database;
44
    }
45
 
46
    /**
47
     * Check if the data is found in the given table.
48
     *
49
     * @param  string  $table
50
     * @return bool
51
     */
52
    public function matches($table): bool
53
    {
54
        return $this->database->table($table)->where($this->data)->count() > 0;
55
    }
56
 
57
    /**
58
     * Get the description of the failure.
59
     *
60
     * @param  string  $table
61
     * @return string
62
     */
63
    public function failureDescription($table): string
64
    {
65
        return sprintf(
66
            "a row in the table [%s] matches the attributes %s.\n\n%s",
67
            $table, $this->toString(JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), $this->getAdditionalInfo($table)
68
        );
69
    }
70
 
71
    /**
72
     * Get additional info about the records found in the database table.
73
     *
74
     * @param  string  $table
75
     * @return string
76
     */
77
    protected function getAdditionalInfo($table)
78
    {
79
        $query = $this->database->table($table);
80
 
81
        $similarResults = $query->where(
82
            array_key_first($this->data),
83
            $this->data[array_key_first($this->data)]
84
        )->select(array_keys($this->data))->limit($this->show)->get();
85
 
86
        if ($similarResults->isNotEmpty()) {
87
            $description = 'Found similar results: '.json_encode($similarResults, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
88
        } else {
89
            $query = $this->database->table($table);
90
 
91
            $results = $query->select(array_keys($this->data))->limit($this->show)->get();
92
 
93
            if ($results->isEmpty()) {
94
                return 'The table is empty';
95
            }
96
 
97
            $description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
98
        }
99
 
100
        if ($query->count() > $this->show) {
101
            $description .= sprintf(' and %s others', $query->count() - $this->show);
102
        }
103
 
104
        return $description;
105
    }
106
 
107
    /**
108
     * Get a string representation of the object.
109
     *
110
     * @param  int  $options
111
     * @return string
112
     */
113
    public function toString($options = 0): string
114
    {
115
        foreach ($this->data as $key => $data) {
116
            $output[$key] = $data instanceof Expression ? (string) $data : $data;
117
        }
118
 
119
        return json_encode($output ?? [], $options);
120
    }
121
}